This commit is contained in:
		| @@ -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{}) | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user