From 24e22ec2ce7c3ddc95b492e70beb374b0f953449 Mon Sep 17 00:00:00 2001 From: Calvin Audier Date: Wed, 2 Oct 2024 11:16:03 +0200 Subject: [PATCH 1/3] Update prepareWorkflowControllerConfigMap function - Use the 2.7+ format so all config not overwritten when patch will be introduce Signed-off-by: Calvin Audier --- custom/litmus-helm-agent/pkg/litmus/client.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/custom/litmus-helm-agent/pkg/litmus/client.go b/custom/litmus-helm-agent/pkg/litmus/client.go index 089a2263..459e20a4 100644 --- a/custom/litmus-helm-agent/pkg/litmus/client.go +++ b/custom/litmus-helm-agent/pkg/litmus/client.go @@ -65,12 +65,12 @@ func prepareInfraSecret(infraConnect infrastructure.RegisterInfra, accessKey str func prepareWorkflowControllerConfigMap(clusterID string) map[string]string { configMapWorkflowController := make(map[string]string) - configMapWorkflowController["config"] = (` containerRuntimeExecutor: ` + os.Getenv("CONTAINER_RUNTIME_EXECUTOR") + ` - executor: - imagePullPolicy: IfNotPresent - instanceID: ` + clusterID) - return configMapWorkflowController + configMapWorkflowController["containerRuntimeExecutor"] = os.Getenv("CONTAINER_RUNTIME_EXECUTOR") + configMapWorkflowController["instanceID"] = clusterID + configMapWorkflowController["executor"] = "imagePullPolicy: IfNotPresent\n" + + return configMapWorkflowController } func GetProjectID(credentials types.Credentials) string { From f7f54fd3e44a4fb887e4f4b928dedf7120a5741c Mon Sep 17 00:00:00 2001 From: Calvin Audier Date: Wed, 2 Oct 2024 15:28:55 +0200 Subject: [PATCH 2/3] Transform CreateConfigmap to PatchConfigMap - Rename the function - Use kubernetes patch instead of kubernetes update Signed-off-by: Calvin Audier --- custom/litmus-helm-agent/pkg/k8s/client.go | 25 +++++++++---------- custom/litmus-helm-agent/pkg/litmus/client.go | 4 +-- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/custom/litmus-helm-agent/pkg/k8s/client.go b/custom/litmus-helm-agent/pkg/k8s/client.go index ef49a967..07739e27 100644 --- a/custom/litmus-helm-agent/pkg/k8s/client.go +++ b/custom/litmus-helm-agent/pkg/k8s/client.go @@ -7,8 +7,9 @@ import ( "os" corev1r "k8s.io/api/core/v1" - + "encoding/json" metav1r "k8s.io/apimachinery/pkg/apis/meta/v1" + types "k8s.io/apimachinery/pkg/types" "k8s.io/client-go/kubernetes" "k8s.io/client-go/rest" "k8s.io/client-go/tools/clientcmd" @@ -28,20 +29,18 @@ func ConnectKubeApi() *kubernetes.Clientset { return clientset } -func CreateConfigMap(configmapName string, configMapData map[string]string, NAMESPACE string, clientset *kubernetes.Clientset) { - configMap := corev1r.ConfigMap{ - TypeMeta: metav1r.TypeMeta{ - Kind: "ConfigMap", - APIVersion: "v1", - }, - ObjectMeta: metav1r.ObjectMeta{ - Name: configmapName, - Namespace: NAMESPACE, - }, - Data: configMapData, +func PatchConfigMap(configmapName string, configMapData map[string]string, NAMESPACE string, clientset *kubernetes.Clientset) { + jsonStr, err := json.Marshal(configMapData) + if err != nil { + fmt.Printf("❌ Error when converting " + configmapName + " data to json: " + err.Error() + "\n") + os.Exit(1) } + patchPayload := ` + { + "data": ` + string(jsonStr) + ` + }` - _, err := clientset.CoreV1().ConfigMaps(NAMESPACE).Update(context.TODO(), &configMap, metav1r.UpdateOptions{}) + _, err = clientset.CoreV1().ConfigMaps(NAMESPACE).Patch(context.TODO(), configmapName, types.StrategicMergePatchType, []byte(patchPayload), metav1r.PatchOptions{}) if err != nil { fmt.Printf("❌ Cannot update configmap " + configmapName + " : " + err.Error() + "\n") os.Exit(1) diff --git a/custom/litmus-helm-agent/pkg/litmus/client.go b/custom/litmus-helm-agent/pkg/litmus/client.go index 459e20a4..d074f2ec 100644 --- a/custom/litmus-helm-agent/pkg/litmus/client.go +++ b/custom/litmus-helm-agent/pkg/litmus/client.go @@ -148,13 +148,13 @@ func CreateInfra(credentials types.Credentials) { clientset := kubernetes.ConnectKubeApi() configMap := prepareInfraConfigMap() - kubernetes.CreateConfigMap(os.Getenv("INFRA_CONFIGMAP_NAME"), configMap, os.Getenv("NAMESPACE"), clientset) + kubernetes.PatchConfigMap(os.Getenv("INFRA_CONFIGMAP_NAME"), configMap, os.Getenv("NAMESPACE"), clientset) secret := prepareInfraSecret(connectionData.Data, accessKey) kubernetes.CreateSecret(os.Getenv("INFRA_SECRET_NAME"), secret, os.Getenv("NAMESPACE"), clientset) workflowConfigMap := prepareWorkflowControllerConfigMap(connectionData.Data.RegisterInfraDetails.InfraID) - kubernetes.CreateConfigMap(os.Getenv("WORKFLOW_CONTROLER_CONFIGMAP_NAME"), workflowConfigMap, os.Getenv("NAMESPACE"), clientset) + kubernetes.PatchConfigMap(os.Getenv("WORKFLOW_CONTROLER_CONFIGMAP_NAME"), workflowConfigMap, os.Getenv("NAMESPACE"), clientset) fmt.Printf("Infra Successfully declared, starting...\n") } else { From ff69876ec27edcb8c535156861f1b024f59ecd3a Mon Sep 17 00:00:00 2001 From: Calvin Audier Date: Wed, 2 Oct 2024 15:45:25 +0200 Subject: [PATCH 3/3] Remove executor section of configmap - It was removed to allow overwrite others values in the executor section - As this value was static it will be added back from in the default configmap value. (This mean the configuration will still be there and we can even overwrite now) Signed-off-by: Calvin Audier --- custom/litmus-helm-agent/pkg/litmus/client.go | 1 - 1 file changed, 1 deletion(-) diff --git a/custom/litmus-helm-agent/pkg/litmus/client.go b/custom/litmus-helm-agent/pkg/litmus/client.go index d074f2ec..b6368386 100644 --- a/custom/litmus-helm-agent/pkg/litmus/client.go +++ b/custom/litmus-helm-agent/pkg/litmus/client.go @@ -68,7 +68,6 @@ func prepareWorkflowControllerConfigMap(clusterID string) map[string]string { configMapWorkflowController["containerRuntimeExecutor"] = os.Getenv("CONTAINER_RUNTIME_EXECUTOR") configMapWorkflowController["instanceID"] = clusterID - configMapWorkflowController["executor"] = "imagePullPolicy: IfNotPresent\n" return configMapWorkflowController }