From c3d6e0623568efb92af9c463bae1761c9577d93f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=AF=BB=E6=AC=A2?= Date: Tue, 17 Oct 2023 16:32:16 +0800 Subject: [PATCH] first commit --- .idea/.gitignore | 8 ++++ .idea/drone-kuboard.iml | 9 ++++ .idea/misc.xml | 6 +++ .idea/modules.xml | 8 ++++ Dockerfile | 16 +++++++ go.mod | 11 +++++ go.sum | 8 ++++ main.go | 98 +++++++++++++++++++++++++++++++++++++++++ readme.md | 4 ++ restart.go | 66 +++++++++++++++++++++++++++ update.go | 71 +++++++++++++++++++++++++++++ 11 files changed, 305 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/drone-kuboard.iml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 Dockerfile create mode 100644 go.mod create mode 100644 go.sum create mode 100644 main.go create mode 100644 readme.md create mode 100644 restart.go create mode 100644 update.go diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/drone-kuboard.iml b/.idea/drone-kuboard.iml new file mode 100644 index 0000000..5e764c4 --- /dev/null +++ b/.idea/drone-kuboard.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..3ce3588 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..11be372 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..93d7849 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,16 @@ +FROM golang:alpine as builder +WORKDIR /builder +COPY . . + +ENV GO111MODULE=on +ENV GOPROXY=https://goproxy.cn,direct + +RUN go version +RUN go mod download && go build -o drone-kuboard +RUN ls -lh && chmod +x ./drone-kuboard + +FROM repo.lxh.io/alpine:3.16.0 as runner +LABEL org.opencontainers.image.authors="lxh@cxh.cn" + +COPY --from=builder /builder/drone-kuboard /bin +ENTRYPOINT ["/bin/drone-kuboard"] \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..6df42a9 --- /dev/null +++ b/go.mod @@ -0,0 +1,11 @@ +module drone-kuboard + +go 1.21 + +require github.com/urfave/cli/v2 v2.25.7 + +require ( + github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect + github.com/russross/blackfriday/v2 v2.1.0 // indirect + github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..54cdbc1 --- /dev/null +++ b/go.sum @@ -0,0 +1,8 @@ +github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w= +github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= +github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk= +github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/urfave/cli/v2 v2.25.7 h1:VAzn5oq403l5pHjc4OhD54+XGO9cdKVL/7lDjF+iKUs= +github.com/urfave/cli/v2 v2.25.7/go.mod h1:8qnjx1vcq5s2/wpsqoZFndg2CE5tNFyrTvS6SinrnYQ= +github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU= +github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8= diff --git a/main.go b/main.go new file mode 100644 index 0000000..dd10a93 --- /dev/null +++ b/main.go @@ -0,0 +1,98 @@ +package main + +import ( + "github.com/urfave/cli/v2" + "log" + "os" + "strings" +) + +type payload struct { + Kind string `json:"kind"` + Name string `json:"name"` + Namespace string `json:"namespace"` + Images map[string]string `json:"images,omitempty"` +} + +type errorResponse struct { + Reason string +} + +func main() { + // 默认是重启 + action := restart + // 判断是啥操作 + if actionKey := os.Getenv("PLUGIN_ACTION"); actionKey != "" { + switch strings.ToLower(actionKey) { + case "restart": + action = restart + case "update": + action = update + } + } + + app := &cli.App{ + Name: "drone-kuboard", + Usage: "Update Kuboard Workloads Image Tag", + Authors: []*cli.Author{ + &cli.Author{ + Name: "lxh", + Email: "lxh@cxh.cn", + }, + }, + Action: action, + Flags: []cli.Flag{ + &cli.StringFlag{ + Name: "cluster", + Usage: "cluster name", + EnvVars: []string{"PLUGIN_CLUSTER"}, + }, + &cli.StringFlag{ + Name: "kind", + Usage: "workload type", + EnvVars: []string{"PLUGIN_KIND"}, + }, + &cli.StringFlag{ + Name: "name", + Usage: "workload name", + EnvVars: []string{"PLUGIN_NAME"}, + }, + &cli.StringFlag{ + Name: "namespace", + Usage: "workload namespace", + EnvVars: []string{"PLUGIN_NAMESPACE"}, + }, + &cli.StringFlag{ + Name: "image", + Usage: "image uri", + EnvVars: []string{"PLUGIN_IMAGE"}, + }, + &cli.StringFlag{ + Name: "tag", + Usage: "image tag", + EnvVars: []string{"PLUGIN_TAG"}, + }, + &cli.StringFlag{ + Name: "kuboard_uri", + Usage: "kuboard uri", + EnvVars: []string{"PLUGIN_KUBOARD_URI"}, + }, + &cli.StringFlag{ + Name: "kuboard_username", + Usage: "kuboard username", + EnvVars: []string{"PLUGIN_KUBOARD_USERNAME"}, + }, + &cli.StringFlag{ + Name: "kuboard_key", + Usage: "kuboard access key", + EnvVars: []string{"PLUGIN_KUBOARD_KEY"}, + }, + }, + } + + if err := app.Run(os.Args); err != nil { + log.Fatal(err) + } + + log.Println("The image version was successfully updated.") +} diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..019231f --- /dev/null +++ b/readme.md @@ -0,0 +1,4 @@ +### 手动打包镜像 +```shell +docker build --push -t repo.lxh.io/lxh/drone-kuboard:latest . +``` \ No newline at end of file diff --git a/restart.go b/restart.go new file mode 100644 index 0000000..374aab1 --- /dev/null +++ b/restart.go @@ -0,0 +1,66 @@ +package main + +import ( + "bytes" + "encoding/json" + "errors" + "github.com/urfave/cli/v2" + "io" + "net/http" + "strings" +) + +func restart(ctx *cli.Context) error { + cluster := ctx.Value("cluster").(string) + uri := strings.Trim(ctx.Value("kuboard_uri").(string), "/") + uri = uri + "/kuboard-api/cluster/" + cluster + "/kind/CICDApi/admin/resource/restartWorkload" + + data, err := json.Marshal(payload{ + Name: ctx.Value("name").(string), + Namespace: ctx.Value("namespace").(string), + Kind: ctx.Value("kind").(string), + }) + + if err != nil { + return err + } + + request, err := http.NewRequest("PUT", uri, bytes.NewBuffer(data)) + + if err != nil { + return err + } + + request.Header.Add("Content-Type", "application/json") + request.AddCookie(&http.Cookie{ + Name: "KuboardUsername", + Value: ctx.Value("kuboard_username").(string), + }) + request.AddCookie(&http.Cookie{ + Name: "KuboardAccessKey", + Value: ctx.Value("kuboard_key").(string), + }) + + client := &http.Client{} + response, err := client.Do(request) + + if err != nil { + return err + } + + defer response.Body.Close() + + if response.StatusCode != 200 { + body, _ := io.ReadAll(response.Body) + errResp := &errorResponse{} + err := json.Unmarshal(body, errResp) + + if err != nil { + return errors.New(response.Status) + } else { + return errors.New(errResp.Reason) + } + } + + return err +} diff --git a/update.go b/update.go new file mode 100644 index 0000000..22b60ae --- /dev/null +++ b/update.go @@ -0,0 +1,71 @@ +package main + +import ( + "bytes" + "encoding/json" + "errors" + "github.com/urfave/cli/v2" + "io" + "net/http" + "strings" +) + +func update(ctx *cli.Context) error { + cluster := ctx.Value("cluster").(string) + uri := strings.Trim(ctx.Value("kuboard_uri").(string), "/") + uri = uri + "/kuboard-api/cluster/" + cluster + "/kind/CICDApi/admin/resource/updateImageTag" + image := strings.Trim(ctx.Value("image").(string), "/") + tag := ctx.Value("tag").(string) + + data, err := json.Marshal(payload{ + Name: ctx.Value("name").(string), + Namespace: ctx.Value("namespace").(string), + Kind: ctx.Value("kind").(string), + Images: map[string]string{ + image: image + ":" + tag, + }, + }) + + if err != nil { + return err + } + + request, err := http.NewRequest("PUT", uri, bytes.NewBuffer(data)) + + if err != nil { + return err + } + + request.Header.Add("Content-Type", "application/json") + request.AddCookie(&http.Cookie{ + Name: "KuboardUsername", + Value: ctx.Value("kuboard_username").(string), + }) + request.AddCookie(&http.Cookie{ + Name: "KuboardAccessKey", + Value: ctx.Value("kuboard_key").(string), + }) + + client := &http.Client{} + response, err := client.Do(request) + + if err != nil { + return err + } + + defer response.Body.Close() + + if response.StatusCode != 200 { + body, _ := io.ReadAll(response.Body) + errResp := &errorResponse{} + err := json.Unmarshal(body, errResp) + + if err != nil { + return errors.New(response.Status) + } else { + return errors.New(errResp.Reason) + } + } + + return err +}