first commit

This commit is contained in:
李寻欢 2023-10-17 16:32:16 +08:00
commit c3d6e06235
11 changed files with 305 additions and 0 deletions

8
.idea/.gitignore vendored Normal file
View File

@ -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

9
.idea/drone-kuboard.iml Normal file
View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4">
<component name="Go" enabled="true" />
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

6
.idea/misc.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="MarkdownSettingsMigration">
<option name="stateVersion" value="1" />
</component>
</project>

8
.idea/modules.xml Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/drone-kuboard.iml" filepath="$PROJECT_DIR$/.idea/drone-kuboard.iml" />
</modules>
</component>
</project>

16
Dockerfile Normal file
View File

@ -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"]

11
go.mod Normal file
View File

@ -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
)

8
go.sum Normal file
View File

@ -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=

98
main.go Normal file
View File

@ -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.")
}

4
readme.md Normal file
View File

@ -0,0 +1,4 @@
### 手动打包镜像
```shell
docker build --push -t repo.lxh.io/lxh/drone-kuboard:latest .
```

66
restart.go Normal file
View File

@ -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
}

71
update.go Normal file
View File

@ -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
}