This commit is contained in:
parent
d78b079290
commit
57a38b9d2b
@ -16,4 +16,5 @@ steps:
|
||||
password:
|
||||
from_secret: docker_pwd
|
||||
use_cache: true
|
||||
auto_tag: true
|
||||
tag:
|
||||
- latest
|
@ -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{})
|
||||
|
||||
|
59
utils/parse_image.go
Normal file
59
utils/parse_image.go
Normal file
@ -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
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user