Compare commits
3 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
f0bd8013fa | ||
|
57a38b9d2b | ||
|
d78b079290 |
@ -16,4 +16,5 @@ steps:
|
||||
password:
|
||||
from_secret: docker_pwd
|
||||
use_cache: true
|
||||
auto_tag: true
|
||||
tag:
|
||||
- latest
|
@ -2,14 +2,13 @@ package k8s
|
||||
|
||||
import (
|
||||
"context"
|
||||
"drone-kubernetes/utils"
|
||||
"errors"
|
||||
"fmt"
|
||||
appv1 "k8s.io/api/apps/v1"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/util/json"
|
||||
"k8s.io/client-go/kubernetes"
|
||||
kindv1 "k8s.io/client-go/kubernetes/typed/apps/v1"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
@ -44,12 +43,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 // 资源详情
|
||||
|
||||
@ -76,12 +69,9 @@ func (c client) Do(namespace, kind, resourceName, actionMethod, repo string) (er
|
||||
return fmt.Errorf("repo not empty")
|
||||
}
|
||||
|
||||
// 解析规则,判断是只单独变更tag还是镜像整个替换
|
||||
if !strings.Contains(repo, ":") {
|
||||
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 {
|
||||
repo = utils.ParseImage(repo)
|
||||
|
||||
resource.(*appv1.Deployment).Spec.Template.Spec.Containers[0].Image = repo
|
||||
}
|
||||
|
||||
_, err = kindType.(kindv1.DeploymentInterface).Update(context.Background(), resource.(*appv1.Deployment), metav1.UpdateOptions{})
|
||||
|
||||
@ -105,10 +95,6 @@ func (c client) Do(namespace, kind, resourceName, actionMethod, repo string) (er
|
||||
return fmt.Errorf("get deployment error: %v", err.Error())
|
||||
}
|
||||
|
||||
rJ, _ := json.Marshal(resource)
|
||||
|
||||
fmt.Println(string(rJ))
|
||||
|
||||
resource.(*appv1.Deployment).Spec.Template.ObjectMeta.Annotations["kubectl.kubernetes.io/restartedAt"] = time.Now().Local().String()
|
||||
_, err = kindType.(kindv1.DeploymentInterface).Update(context.Background(), resource.(*appv1.Deployment), metav1.UpdateOptions{})
|
||||
if err != nil {
|
||||
@ -117,10 +103,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
|
||||
}
|
||||
|
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…
Reference in New Issue
Block a user