From 57a38b9d2b7f147b509c4e3b4e265db55f566cdc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B0=A2=E4=BD=9C=E6=AC=A2?= Date: Fri, 3 Nov 2023 15:57:34 +0800 Subject: [PATCH] =?UTF-8?q?:zap:=E9=87=8D=E6=96=B0=E8=A7=A3=E6=9E=90image?= =?UTF-8?q?=E5=9C=B0=E5=9D=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .drone.yml | 3 ++- k8s/client.go | 32 ++---------------------- utils/parse_image.go | 59 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 31 deletions(-) create mode 100644 utils/parse_image.go diff --git a/.drone.yml b/.drone.yml index 216c822..f469436 100644 --- a/.drone.yml +++ b/.drone.yml @@ -16,4 +16,5 @@ steps: password: from_secret: docker_pwd use_cache: true - auto_tag: true \ No newline at end of file + tag: + - latest \ No newline at end of file diff --git a/k8s/client.go b/k8s/client.go index 6aab896..214d537 100644 --- a/k8s/client.go +++ b/k8s/client.go @@ -2,6 +2,7 @@ package k8s import ( "context" + "drone-kubernetes/utils" "errors" "fmt" appv1 "k8s.io/api/apps/v1" @@ -9,7 +10,6 @@ import ( "k8s.io/apimachinery/pkg/util/json" "k8s.io/client-go/kubernetes" kindv1 "k8s.io/client-go/kubernetes/typed/apps/v1" - "strings" "time" ) @@ -70,35 +70,7 @@ func (c client) Do(namespace, kind, resourceName, actionMethod, repo string) (er return fmt.Errorf("repo not empty") } - // 解析规则,判断是只单独变更tag还是镜像整个替换 - if !strings.Contains(repo, ":") { - - // 判断是镜像地址还是tag标签 - if strings.Contains(repo, "/") { - // 镜像地址,默认tag为 latest - repo = fmt.Sprintf("%s:latest", repo) - } else { - // 只是tag标签,判断tag标签是否带有v开头的字符 - if strings.HasPrefix(repo, "v") { - repo = strings.ReplaceAll(repo, "v", "") - } - } - - resource.(*appv1.Deployment).Spec.Template.Spec.Containers[0].Image = fmt.Sprintf("%s:%s", strings.Split(resource.(*appv1.Deployment).Spec.Template.Spec.Containers[0].Image, ":")[0], repo) - } else { - - // 获取tag - image := strings.Split(repo, ":")[0] - tag := strings.Split(repo, ":")[1] - - if strings.Contains(tag, "v") { - tag = strings.ReplaceAll(tag, "v", "") - } - - repo = fmt.Sprintf("%s:%s", image, tag) - - resource.(*appv1.Deployment).Spec.Template.Spec.Containers[0].Image = repo - } + resource.(*appv1.Deployment).Spec.Template.Spec.Containers[0].Image = utils.ParseImage(repo) _, err = kindType.(kindv1.DeploymentInterface).Update(context.Background(), resource.(*appv1.Deployment), metav1.UpdateOptions{}) diff --git a/utils/parse_image.go b/utils/parse_image.go new file mode 100644 index 0000000..df809f3 --- /dev/null +++ b/utils/parse_image.go @@ -0,0 +1,59 @@ +package utils + +import ( + "fmt" + "os" + "strings" +) + +// ParseImage +// @description: 解析镜像地址 +// @param repo +// @return string +func ParseImage(repo string) string { + if repo == "" { + return "" + } + + // 判断镜像地址是否包含tag + if strings.Contains(repo, ":") { + // 整个镜像地址 + image := strings.Split(repo, ":")[0] + tag := strings.Split(repo, ":")[1] + + if strings.Contains(tag, "v") { + tag = GetRealImageTag() + } + + repo = fmt.Sprintf("%s:%s", image, tag) + + } else { + // 不包含,判断是tag还是镜像地址 + if strings.Contains(repo, "/") { + // 镜像地址,默认tag为latest + repo = fmt.Sprintf("%s:latest", repo) + } else { + // tag,只是tag,判断是否带有v + if strings.Contains(repo, "v") { + repo = GetRealImageTag() + } + + } + + } + + return repo + +} + +// GetRealImageTag +// @description: 获取真实的镜像tag +// @return repo +func GetRealImageTag() (tag string) { + // 这才是真是的最前面的那一块 + realImageShort := os.Getenv("DRONE_SEMVER_SHORT") + imageAlpha := os.Getenv("DRONE_SEMVER_PRERELEASE") + + tag = fmt.Sprintf("%s-%s", realImageShort, imageAlpha) + return +}