Skip to content

Commit

Permalink
Update plugin to fix some probelms amd update image version to latest
Browse files Browse the repository at this point in the history
fix version showing issue, now it uses upstream commit as plugin version
update image to 2.0.1
in login part. wait for the deployment to be ready and then getthe QR code

Signed-off-by: chenliu1993 <cl2037829916@gmail.com>
  • Loading branch information
chenliu1993 committed Aug 17, 2023
1 parent ef6ea12 commit 8b8aad2
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 32 deletions.
2 changes: 2 additions & 0 deletions krew/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bin
bilipro
26 changes: 19 additions & 7 deletions krew/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,30 @@ help: #### display help
deploy: build install #### build + install


.PHONY: fmt
fmt: #### run go fmt against code
@go fmt ./...


.PHONY: vet
vet: #### run go vet against code
@go vet ./...

.PHONY: update-modules
update-modules: tidy #### update go modules

.PHONY: tidy
tidy: #### run go mod tidy
@go mod tidy

.PHONY: build
build: #### build the plugin
@go vet ./... && \
go fmt ./... && \
echo "build on ${GOOS}/${GOARCH}" && \
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

@echo "build on ${GOOS}/${GOARCH}" && \
GOOS=${GOOS} GOARCH=${GOARCH} CGO_ENABLED=0 go build -mod readonly -ldflags "-X github.com/RayWangQvQ/BiliBiliToolPro/krew/pkg/cmd.version=${GITCOMMIT}" -o bin/$(BIN_NAME) cmd/kubectl-bilipro.go

.PHONY: install
install: #### install the plugin
@cd ${ROOT_DIR}/krew/cmd && \
@cd ${ROOT_DIR}/krew/bin && \
sudo install ./$(BIN_NAME) ${KUBECTL_DIR}/$(BIN_NAME)

.PHONY: test
Expand Down
9 changes: 6 additions & 3 deletions krew/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,18 @@ For example: the kubectl is installed under `/usr/bin`, then put the bilibilipro

### Deployment && Update

Command: `kubectl bilipro init --config config.yaml [optional]--login`
Prerequsites: please make sure you have the right permission to at least manage namespaces/deployments

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

Creates Deployment with the needed environments.

Optional Options:

- `--image=zai7lou/bilibili_tool_pro:1.0.1`
- `--image=zai7lou/bilibili_tool_pro:2.0.1`
- `--namespace=bilipro`
- `--image-pull-secret=`
- `--image-pull-secret=<docker secrets>`
- `--login` to scan QR code to login

Required Options:

Expand Down
4 changes: 2 additions & 2 deletions krew/cmd/kubectl-bilipro.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package main

import (
"fmt"
"os"

"github.com/spf13/pflag"

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

func main() {
Expand All @@ -17,7 +17,7 @@ func main() {

cmd := cmd.NewExecutor(genericclioptions.IOStreams{In: os.Stdin, Out: os.Stdout, ErrOut: os.Stderr})
if err := cmd.Execute(); err != nil {
klog.Error(helper.GenErrorMsg(helper.SERVER_ERROR, err.Error()).Error())
fmt.Println(helper.GenErrorMsg(helper.SERVER_ERROR, err.Error()).Error())
os.Exit(1)
}
}
4 changes: 2 additions & 2 deletions krew/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ require (
k8s.io/apimachinery v0.25.4
k8s.io/cli-runtime v0.25.4
k8s.io/client-go v0.25.4
k8s.io/klog/v2 v2.80.1
sigs.k8s.io/kustomize/api v0.12.1
sigs.k8s.io/kustomize/kyaml v0.13.9
sigs.k8s.io/yaml v1.3.0
Expand Down Expand Up @@ -58,10 +57,11 @@ require (
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/klog/v2 v2.80.1 // indirect
k8s.io/kube-openapi v0.0.0-20221012153701-172d655c2280 // indirect
k8s.io/utils v0.0.0-20221107191617-1a15be271d1d // 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
// replace github.com/RayWangQvQ/BiliBiliToolPro/krew => ../krew
6 changes: 3 additions & 3 deletions krew/pkg/cmd/delete.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"fmt"
"io"
"os/exec"
"strings"
Expand All @@ -9,8 +10,6 @@ import (
"sigs.k8s.io/kustomize/api/types"
"sigs.k8s.io/yaml"

"k8s.io/klog/v2"

"github.com/RayWangQvQ/BiliBiliToolPro/krew/pkg/options"
helper "github.com/RayWangQvQ/BiliBiliToolPro/krew/pkg/utils"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -42,9 +41,10 @@ func newDeleteCmd(out io.Writer, errOut io.Writer) *cobra.Command {

err := o.run(out)
if err != nil {
klog.Error(err)
fmt.Println(err)
return err
}
fmt.Println("bilibili tool is removed from your cluster")
return nil
},
}
Expand Down
5 changes: 2 additions & 3 deletions krew/pkg/cmd/get.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package cmd

import (
"fmt"
"io"
"os/exec"

"k8s.io/klog/v2"

"github.com/RayWangQvQ/BiliBiliToolPro/krew/pkg/options"
helper "github.com/RayWangQvQ/BiliBiliToolPro/krew/pkg/utils"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -37,7 +36,7 @@ func newGetCmd(out io.Writer, errOut io.Writer) *cobra.Command {

err := o.run(out)
if err != nil {
klog.Error(err)
fmt.Println(err)
return err
}
return nil
Expand Down
43 changes: 34 additions & 9 deletions krew/pkg/cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"os"
"os/exec"
"strings"
"time"

corev1 "k8s.io/api/core/v1"

Expand All @@ -17,8 +16,6 @@ import (

"sigs.k8s.io/kustomize/api/types"

"k8s.io/klog/v2"

"github.com/spf13/cobra"

"github.com/RayWangQvQ/BiliBiliToolPro/krew/pkg/options"
Expand Down Expand Up @@ -52,15 +49,15 @@ func newInitCmd(out io.Writer, errOut io.Writer) *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {
err := o.run(out)
if err != nil {
klog.Error(err)
fmt.Println(err)
return err
}
return nil
},
}

f := cmd.Flags()
f.StringVarP(&o.deployOpts.Image, "image", "i", "zai7lou/bilibili_tool_pro:1.0.1", "bilibilipro image")
f.StringVarP(&o.deployOpts.Image, "image", "i", "zai7lou/bilibili_tool_pro:2.0.1", "bilibilipro image")
f.StringVarP(&o.deployOpts.Namespace, "namespace", "n", "bilipro", "namespace scope for this request")
f.StringVar(&o.deployOpts.ImagePullSecret, "image-pull-secret", "", "image pull secret to be used for pulling bilibilipro image")
f.StringVarP(&o.deployOpts.ConfigFilePath, "config", "c", "", "the config file contanis the environment variables")
Expand Down Expand Up @@ -94,6 +91,8 @@ func (o *initCmd) run(writer io.Writer) error {
}

// TODO: All about paths are a little bit tricky should give it more thoughts

fmt.Println("Creating the kustomization file")
// if the bilibili tool is deployed under system/pre-defined namespace, ignore the namespace file
var resources []string // nolint: go-staticcheck
if o.deployOpts.Namespace == "default" || o.deployOpts.Namespace == "kube-system" || o.deployOpts.Namespace == "kube-public" {
Expand Down Expand Up @@ -211,6 +210,7 @@ func (o *initCmd) run(writer io.Writer) error {
}
}

fmt.Println("Applying the kustomization file")
// do kubectl apply
// make sure kubectl is under your PATH
cmd := exec.Command("kubectl", "apply", "-f", "-")
Expand All @@ -219,9 +219,9 @@ func (o *initCmd) run(writer io.Writer) error {
return err
}

// if there is login required
// if there is login required, exectue the login command as the last step
if o.login {

fmt.Println("please login...")
client, _, err := helper.GetK8sClient()
if err != nil {
return err
Expand All @@ -233,8 +233,33 @@ func (o *initCmd) run(writer io.Writer) error {
return err
}

// TODO: Stupid way, just sleep to wait container is ready, maybe a watch is a better option
time.Sleep(15 * time.Second)
fmt.Println("wait for the deployment to be ready")
// Wait for the deployment ready
checkCmdArgs := []string{
"rollout",
"status",
"deployment/bilibilipro",
"-n",
o.deployOpts.Namespace,
}
checkCmd := exec.Command("kubectl", checkCmdArgs...)

for {
if err := checkCmd.Start(); err != nil {
fmt.Printf("deployment is not ready yet, current status: %v\n", err)
continue
}

err := checkCmd.Wait()
if err == nil {
fmt.Printf("deployment is ready\n")
break
}
fmt.Printf("deployment is not ready yet, current status: %v\n", err)
}

fmt.Println("please scan the QR code")
// Exec the login command
args := []string{
"exec",
podName,
Expand Down
4 changes: 1 addition & 3 deletions krew/pkg/cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"fmt"
"io"

"k8s.io/klog/v2"

"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -35,7 +33,7 @@ func newVersionCmd(out io.Writer, errOut io.Writer) *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {
err := o.run()
if err != nil {
klog.Error(err)
fmt.Println(err)
return err
}
return nil
Expand Down
2 changes: 2 additions & 0 deletions krew/pkg/utils/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ func GetK8sClient() (*kubernetes.Clientset, *rest.Config, error) {
if err != nil {
return nil, nil, GenErrorMsg(SERVER_ERROR, err.Error())
}
config.QPS = float32(10.0)
config.Burst = 20

// create the clientset
clientset, err := kubernetes.NewForConfig(config)
Expand Down

0 comments on commit 8b8aad2

Please sign in to comment.