From 7b66c59ef9671044cc3cff567013cdaa5dd1a403 Mon Sep 17 00:00:00 2001 From: zze Date: Fri, 15 Dec 2023 15:22:58 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E4=BB=A3=E7=A0=81=E6=8A=BD?= =?UTF-8?q?=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/logic/ci_pipeline/run.go | 30 ++---------------- utility/thirdclients/kubernetes/config_map.go | 31 +++++++++++++++++-- 2 files changed, 32 insertions(+), 29 deletions(-) diff --git a/internal/logic/ci_pipeline/run.go b/internal/logic/ci_pipeline/run.go index 4d80ad7..1c6a65d 100644 --- a/internal/logic/ci_pipeline/run.go +++ b/internal/logic/ci_pipeline/run.go @@ -369,17 +369,7 @@ func createCiPod(kubeClient *kubernetes.Client, namespace, name string, arrangeC } if hasKaniko { // 如果存在 kaniko 环境,检查 kaniko 环境配置的镜像仓库认证秘钥内容和 kubernetes 集群中 ConfigMap 中的配置是否一致 - configMap, err := kubeClient.GetConfigMap(namespace, consts.CI_CLIENT_CONFIG_MAP_NAME) - if err != nil && !kubernetes.IsNotFoundError(err) { - return err - } - - var ( - noConfigMap = kubernetes.IsNotFoundError(err) - configDataMap = make(map[string]string) - shouldUpdateConfigMap = false - ) - + configDataMap := make(map[string]string) for _, secretId := range dockerRegistrySecretIds.Slice() { eSecret, err := service.Secret().Get(kubeClient.Ctx, &do.Secret{Id: secretId.(int)}) if err != nil { @@ -407,24 +397,10 @@ func createCiPod(kubeClient *kubernetes.Client, namespace, name string, arrangeC itemKey := fmt.Sprintf("%s-%d", consts.CI_CLIENT_CONFIG_MAP_SECRET_KEY_PREFIX, secretId) configDataMap[fmt.Sprintf(itemKey)] = string(authConfigJson) - if !noConfigMap { // 存在 config map - if content, ok := configMap.Data[itemKey]; !ok || content != string(authConfigJson) { - shouldUpdateConfigMap = true - configMap.Data[itemKey] = string(authConfigJson) - } - } } - if noConfigMap { - if err := kubeClient.CreateConfigMap(namespace, consts.CI_CLIENT_CONFIG_MAP_NAME, configDataMap); err != nil { - return err - } - } else { - if shouldUpdateConfigMap { - if err := kubeClient.UpdateConfigMap(namespace, configMap); err != nil { - return err - } - } + if err := kubeClient.PresentConfigMapData(namespace, consts.CI_CLIENT_CONFIG_MAP_NAME, configDataMap); err != nil { + return err } volumes = append(volumes, corev1.Volume{ diff --git a/utility/thirdclients/kubernetes/config_map.go b/utility/thirdclients/kubernetes/config_map.go index 7d50479..bc0a2da 100644 --- a/utility/thirdclients/kubernetes/config_map.go +++ b/utility/thirdclients/kubernetes/config_map.go @@ -16,8 +16,7 @@ func (cli *Client) GetConfigMap(namespace, name string) (*corev1.ConfigMap, erro func (cli *Client) CreateConfigMap(namespace, name string, data map[string]string) error { configMap := &corev1.ConfigMap{ ObjectMeta: metav1.ObjectMeta{ - Name: name, - Namespace: namespace, + Name: name, }, Data: data, } @@ -35,3 +34,31 @@ func (cli *Client) UpdateConfigMap(namespace string, configMap *corev1.ConfigMap } return nil } + +// PresentConfigMapData 确保配置文件存在 +func (cli *Client) PresentConfigMapData(namespace, name string, items map[string]string) error { + configMap, err := cli.GetConfigMap(namespace, name) + if err != nil && !IsNotFoundError(err) { + return err + } + var noConfigMap = IsNotFoundError(err) + if noConfigMap { + if err := cli.CreateConfigMap(namespace, name, items); err != nil { + return err + } + } else { + shouldUpdateConfigMap := false + for itemKey, itemContent := range items { + if content, ok := configMap.Data[itemKey]; !ok || content != itemContent { + shouldUpdateConfigMap = true + configMap.Data[itemKey] = itemContent + } + } + if shouldUpdateConfigMap { + if err := cli.UpdateConfigMap(namespace, configMap); err != nil { + return err + } + } + } + return nil +}