Compare commits
No commits in common. "master" and "v1.0.1" have entirely different histories.
@ -16,5 +16,4 @@ steps:
|
|||||||
password:
|
password:
|
||||||
from_secret: docker_pwd
|
from_secret: docker_pwd
|
||||||
use_cache: true
|
use_cache: true
|
||||||
tag:
|
auto_tag: true
|
||||||
- latest
|
|
@ -2,13 +2,14 @@ package k8s
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"drone-kubernetes/utils"
|
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
appv1 "k8s.io/api/apps/v1"
|
appv1 "k8s.io/api/apps/v1"
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
|
"k8s.io/apimachinery/pkg/util/json"
|
||||||
"k8s.io/client-go/kubernetes"
|
"k8s.io/client-go/kubernetes"
|
||||||
kindv1 "k8s.io/client-go/kubernetes/typed/apps/v1"
|
kindv1 "k8s.io/client-go/kubernetes/typed/apps/v1"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -43,6 +44,12 @@ func (c *config) Client() (*client, error) {
|
|||||||
// @return err
|
// @return err
|
||||||
func (c client) Do(namespace, kind, resourceName, actionMethod, repo string) (err error) {
|
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 kindType any // 应用类型
|
||||||
var resource any // 资源详情
|
var resource any // 资源详情
|
||||||
|
|
||||||
@ -69,9 +76,12 @@ func (c client) Do(namespace, kind, resourceName, actionMethod, repo string) (er
|
|||||||
return fmt.Errorf("repo not empty")
|
return fmt.Errorf("repo not empty")
|
||||||
}
|
}
|
||||||
|
|
||||||
repo = utils.ParseImage(repo)
|
// 解析规则,判断是只单独变更tag还是镜像整个替换
|
||||||
|
if !strings.Contains(repo, ":") {
|
||||||
resource.(*appv1.Deployment).Spec.Template.Spec.Containers[0].Image = 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 {
|
||||||
|
resource.(*appv1.Deployment).Spec.Template.Spec.Containers[0].Image = repo
|
||||||
|
}
|
||||||
|
|
||||||
_, err = kindType.(kindv1.DeploymentInterface).Update(context.Background(), resource.(*appv1.Deployment), metav1.UpdateOptions{})
|
_, err = kindType.(kindv1.DeploymentInterface).Update(context.Background(), resource.(*appv1.Deployment), metav1.UpdateOptions{})
|
||||||
|
|
||||||
@ -95,6 +105,10 @@ func (c client) Do(namespace, kind, resourceName, actionMethod, repo string) (er
|
|||||||
return fmt.Errorf("get deployment error: %v", err.Error())
|
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()
|
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{})
|
_, err = kindType.(kindv1.DeploymentInterface).Update(context.Background(), resource.(*appv1.Deployment), metav1.UpdateOptions{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -103,15 +117,10 @@ func (c client) Do(namespace, kind, resourceName, actionMethod, repo string) (er
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
kindType = c.AppsV1().Deployments(namespace)
|
||||||
default:
|
default:
|
||||||
return errors.New("not support")
|
return errors.New("not support")
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println("命名空间: ", namespace)
|
|
||||||
fmt.Println("资源类型: ", kind)
|
|
||||||
fmt.Println("资源名称: ", resourceName)
|
|
||||||
fmt.Println("操作类型: ", actionMethod)
|
|
||||||
fmt.Println("镜像地址: ", repo)
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -1,59 +0,0 @@
|
|||||||
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