Skip to content

Commit

Permalink
Add krew (#341)
Browse files Browse the repository at this point in the history
Add krew support for bilibili tool
Signed-off-by: chenliu1993 <[email protected]>
  • Loading branch information
chenliu1993 authored Dec 8, 2022
1 parent 9062965 commit 876fbf5
Show file tree
Hide file tree
Showing 21 changed files with 1,459 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -358,3 +358,10 @@ tencentScf/.env

# vs code
.vscode

# krew
krew/bilipro
krew/cmd/kubectl-bilipro
krew/kustomization.yaml
bilipro
kustomization.yaml
34 changes: 34 additions & 0 deletions krew/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
.DEFAULT_GOAL:=help
SHELL := /usr/bin/env bash

ROOT_DIR := $(shell git rev-parse --show-toplevel)
GOOS ?= $(shell go env GOOS)
GOARCH ?= $(shell go env GOARCH)
BIN_NAME ?= kubectl-bilipro
KUBECTL_DIR ?= $(shell which kubectl | awk -F 'kubectl' '{printf "%s\n", $$1 }')



GITCOMMIT ?= `git rev-parse HEAD`

help: #### display help
@awk 'BEGIN {FS = ":.*## "; printf "\nTargets:\n"} /^[a-zA-Z_-]+:.*?#### / { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)
@awk 'BEGIN {FS = ":.* ## "; printf "\n \033[1;32mBuild targets\033[36m\033[0m\n \033[0;37mTargets for building and/or installing CLI plugins on the system.\n Append \"ENVS=<os-arch>\" to the end of these targets to limit the binaries built.\n e.g.: make build-all-tanzu-cli-plugins ENVS=linux-amd64 \n List available at https://github.com/golang/go/blob/master/src/go/build/syslist.go\033[36m\033[0m\n\n"} /^[a-zA-Z_-]+:.*? ## / { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)
##### GLOBAL

.PHONY: deploy
deploy: build install #### build + install


.PHONY: build
build: #### build the plugin
@go vet ./... && \
go fmt ./... && \
cd ${ROOT_DIR}/krew/cmd && \
GOOS=${GOOS} GOARCH=${GOARCH} CGO_ENABLED=0 go build -ldflags "-X github.com/RayWangQvQ/BiliBiliToolPro/krew/pkg/cmd.Version=${GITCOMMIT}" -o $(BIN_NAME) kubectl-bilipro.go


.PHONY: install
install: #### install the plugin
@cd ${ROOT_DIR}/krew/cmd && \
sudo install ./$(BIN_NAME) ${KUBECTL_DIR}/$(BIN_NAME)
68 changes: 68 additions & 0 deletions krew/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# BiliBiliPro Kubectl Plugin

## Prerequisites

- Kubernetes >= v1.23.0.
- go >= v1.18
- kubectl installed on your local machine, configured to an existing healthy Kubernetes cluster.
- [krew](https://krew.sigs.k8s.io/docs/user-guide/setup/install/) plugin installed

## Install Plugin

Command: `cd ./krew && make deploy`
The binary will be generated in cmd/ install it alonside the kubectl binary.

For example: the kubectl is installed under `/usr/bin`, then put the bilibilipro plugin under `/usr/bin` too.

## Plugin Commands

### Deployment && Update

Command: `kubectl bilipro init --config config.yaml`

Creates Deployment with the needed environments.


Optional Options:

- `--image=zai7lou/bilibili_tool_pro:0.2.1`
- `--namespace=bilipro`
- `--image-pull-secret=`

Required Options:

- `--config=<config.yaml>`

The content of <config.yaml> is a yaml array, please refer to the example config yaml under the krew directory.

For example
````yaml
- name: Ray_BiliBiliCookies__2
value: "cookie"
# DailyTrigger - required
- name: Ray_DailyTaskConfig__Cron
value: "11 11 * * *"
````


Suggestions: Deploy this workload in namespace other than default or kube-* namespace, because the delete logic should be improved


### Deletion

Command: `kubectl bilipro delete [options]`

Deletes Deployment.
v
Optional Options:

- `--namespace=<deploy-namespace>`
- `--name=<deploy-name>`

### Version

Command: `kubectl bilipro version`

Output the plugin version.


20 changes: 20 additions & 0 deletions krew/cmd/kubectl-bilipro.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package main

import (
"os"

"github.com/spf13/pflag"

"github.com/RayWangQvQ/BiliBiliToolPro/krew/pkg/cmd"
"k8s.io/cli-runtime/pkg/genericclioptions"
)

func main() {
flags := pflag.NewFlagSet("kubectl-bilipro", pflag.ExitOnError)
pflag.CommandLine = flags

cmd := cmd.NewExecutor(genericclioptions.IOStreams{In: os.Stdin, Out: os.Stdout, ErrOut: os.Stderr})
if err := cmd.Execute(); err != nil {
os.Exit(1)
}
}
5 changes: 5 additions & 0 deletions krew/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
- name: Ray_BiliBiliCookies__2
value: "cookie"
# DailyTrigger - required
- name: Ray_DailyTaskConfig__Cron
value: "11 11 * * *"
68 changes: 68 additions & 0 deletions krew/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
module github.com/RayWangQvQ/BiliBiliToolPro/krew

go 1.18

require (
github.com/spf13/cobra v1.4.0
github.com/spf13/pflag v1.0.5
k8s.io/api v0.25.4
k8s.io/cli-runtime v0.25.4
k8s.io/klog/v2 v2.70.1
sigs.k8s.io/kustomize/api v0.12.1
sigs.k8s.io/kustomize/kyaml v0.13.9
sigs.k8s.io/yaml v1.2.0
)

require (
github.com/PuerkitoBio/purell v1.1.1 // indirect
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emicklei/go-restful/v3 v3.8.0 // indirect
github.com/evanphx/json-patch v4.12.0+incompatible // indirect
github.com/go-errors/errors v1.0.1 // indirect
github.com/go-logr/logr v1.2.3 // indirect
github.com/go-openapi/jsonpointer v0.19.5 // indirect
github.com/go-openapi/jsonreference v0.19.5 // indirect
github.com/go-openapi/swag v0.19.14 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/btree v1.0.1 // indirect
github.com/google/gnostic v0.5.7-v3refs // indirect
github.com/google/gofuzz v1.1.0 // indirect
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
github.com/google/uuid v1.1.2 // indirect
github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7 // indirect
github.com/imdario/mergo v0.3.6 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de // indirect
github.com/mailru/easyjson v0.7.6 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/peterbourgon/diskv v2.0.1+incompatible // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/xlab/treeprint v1.1.0 // indirect
go.starlark.net v0.0.0-20200306205701-8dd3e2ee1dd5 // indirect
golang.org/x/net v0.0.0-20220722155237-a158d28d115b // indirect
golang.org/x/oauth2 v0.0.0-20211104180415-d3ed0bb246c8 // indirect
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f // indirect
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/time v0.0.0-20220210224613-90d013bbcef8 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.28.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/apimachinery v0.25.4 // indirect
k8s.io/client-go v0.25.4 // indirect
k8s.io/kube-openapi v0.0.0-20220803162953-67bda5d908f1 // indirect
k8s.io/utils v0.0.0-20220728103510-ee6ede2d64ed // indirect
sigs.k8s.io/json v0.0.0-20220713155537-f223a00ba0e2 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
)

replace github.com/RayWangQvQ/BiliBiliToolPro/krew => ../krew
Loading

0 comments on commit 876fbf5

Please sign in to comment.