From 1ba88a9d4a478f5bba96207a585dd96f28f7e359 Mon Sep 17 00:00:00 2001 From: Saranya Jena Date: Thu, 15 Feb 2024 17:11:58 +0530 Subject: [PATCH] Fixed issue infra config map and secret patch (#4440) Signed-off-by: Saranya-jena --- .../pkg/chaos_infrastructure/service.go | 2 +- chaoscenter/subscriber/pkg/k8s/operations.go | 48 +++++++------------ 2 files changed, 18 insertions(+), 32 deletions(-) diff --git a/chaoscenter/graphql/server/pkg/chaos_infrastructure/service.go b/chaoscenter/graphql/server/pkg/chaos_infrastructure/service.go index abbdf23ae08..4ed9360fa8c 100644 --- a/chaoscenter/graphql/server/pkg/chaos_infrastructure/service.go +++ b/chaoscenter/graphql/server/pkg/chaos_infrastructure/service.go @@ -1048,7 +1048,7 @@ func (in *infraService) VerifyInfra(identity model.InfraIdentity) (*dbChaosInfra return nil, err } if !(infra.AccessKey == identity.AccessKey && infra.IsRegistered) { - return nil, fmt.Errorf("ERROR: infra_ID MISMATCH") + return nil, fmt.Errorf("ERROR: accessID MISMATCH") } return &infra, nil } diff --git a/chaoscenter/subscriber/pkg/k8s/operations.go b/chaoscenter/subscriber/pkg/k8s/operations.go index 441cf62ace5..378215f425e 100644 --- a/chaoscenter/subscriber/pkg/k8s/operations.go +++ b/chaoscenter/subscriber/pkg/k8s/operations.go @@ -3,7 +3,6 @@ package k8s import ( "context" "encoding/base64" - "encoding/json" "errors" "fmt" "strings" @@ -23,7 +22,6 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/runtime/serializer/yaml" - "k8s.io/apimachinery/pkg/types" memory "k8s.io/client-go/discovery/cached" "k8s.io/client-go/dynamic" "k8s.io/client-go/kubernetes" @@ -163,57 +161,45 @@ func (k8s *k8sSubscriber) AgentRegister(accessKey string) (bool, error) { return false, err } - // Define a patch object for the ConfigMap with only "IS_CLUSTER_CONFIRMED". - newConfigMapData := map[string]interface{}{ - "data": map[string]interface{}{ - "IS_INFRA_CONFIRMED": true, - }, + getCM, err := clientset.CoreV1().ConfigMaps(InfraNamespace).Get(context.TODO(), InfraConfigName, metav1.GetOptions{}) + if err != nil { + return false, err } - - configMapPatchBytes, err := json.Marshal(newConfigMapData) - + getSecret, err := clientset.CoreV1().Secrets(InfraNamespace).Get(context.TODO(), InfraSecretName, metav1.GetOptions{}) if err != nil { return false, err } - // Patch the ConfigMap. - _, err = clientset.CoreV1().ConfigMaps(InfraNamespace).Patch( + getCM.Data["IS_INFRA_CONFIRMED"] = "true" + + // Update the ConfigMap. + _, err = clientset.CoreV1().ConfigMaps(InfraNamespace).Update( context.TODO(), - InfraConfigName, - types.StrategicMergePatchType, - configMapPatchBytes, - metav1.PatchOptions{}, + getCM, + metav1.UpdateOptions{}, ) if err != nil { return false, err } - logrus.Info("%s has been updated", InfraConfigName) - - newSecretData := map[string]string{ - "ACCESS_KEY": accessKey, - } + logrus.Infof("%s has been updated", InfraConfigName) - secretPatch := map[string]interface{}{ - "stringData": newSecretData, - } + getSecret.StringData = make(map[string]string) + getSecret.StringData["ACCESS_KEY"] = accessKey - secretPatchBytes, err := json.Marshal(secretPatch) if err != nil { return false, err } - _, err = clientset.CoreV1().Secrets(InfraNamespace).Patch( + _, err = clientset.CoreV1().Secrets(InfraNamespace).Update( context.TODO(), - InfraSecretName, - types.StrategicMergePatchType, - secretPatchBytes, - metav1.PatchOptions{}, + getSecret, + metav1.UpdateOptions{}, ) if err != nil { return false, err } - logrus.Info("%s has been updated", InfraSecretName) + logrus.Infof("%s has been updated", InfraSecretName) return true, nil }