diff --git a/k8s/client.go b/k8s/client.go index e3c5a1b..6aab896 100644 --- a/k8s/client.go +++ b/k8s/client.go @@ -44,12 +44,6 @@ func (c *config) Client() (*client, error) { // @return err func (c client) Do(namespace, kind, resourceName, actionMethod, repo string) (err error) { - fmt.Println("命名空间: ", namespace) - fmt.Println("资源类型: ", kind) - fmt.Println("资源名称: ", resourceName) - fmt.Println("操作类型: ", actionMethod) - fmt.Println("镜像地址: ", repo) - var kindType any // 应用类型 var resource any // 资源详情 @@ -78,8 +72,31 @@ func (c client) Do(namespace, kind, resourceName, actionMethod, repo string) (er // 解析规则,判断是只单独变更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 } @@ -117,10 +134,15 @@ func (c client) Do(namespace, kind, resourceName, actionMethod, repo string) (er } - kindType = c.AppsV1().Deployments(namespace) default: return errors.New("not support") } + fmt.Println("命名空间: ", namespace) + fmt.Println("资源类型: ", kind) + fmt.Println("资源名称: ", resourceName) + fmt.Println("操作类型: ", actionMethod) + fmt.Println("镜像地址: ", repo) + return nil }