diff --git a/chaoscenter/subscriber/pkg/k8s/defination.go b/chaoscenter/subscriber/pkg/k8s/defination.go index 57708985bf3..68b2a24883f 100644 --- a/chaoscenter/subscriber/pkg/k8s/defination.go +++ b/chaoscenter/subscriber/pkg/k8s/defination.go @@ -24,7 +24,7 @@ type SubscriberK8s interface { SendKubeObjects(infraData map[string]string, kubeobjectrequest types.KubeObjRequest) error CheckComponentStatus(componentEnv string) error IsAgentConfirmed() (bool, string, error) - AgentRegister(infraData map[string]string) (bool, error) + AgentRegister(accessKey string) (bool, error) AgentOperations(infraAction types.Action) (*unstructured.Unstructured, error) AgentConfirm(infraData map[string]string) ([]byte, error) GetKubeConfig() (*rest.Config, error) diff --git a/chaoscenter/subscriber/pkg/k8s/operations.go b/chaoscenter/subscriber/pkg/k8s/operations.go index 3d25bf75315..441cf62ace5 100644 --- a/chaoscenter/subscriber/pkg/k8s/operations.go +++ b/chaoscenter/subscriber/pkg/k8s/operations.go @@ -3,6 +3,7 @@ package k8s import ( "context" "encoding/base64" + "encoding/json" "errors" "fmt" "strings" @@ -11,7 +12,7 @@ import ( "k8s.io/apimachinery/pkg/labels" - "subscriber/pkg/types" + pkgTypes "subscriber/pkg/types" yaml_converter "github.com/ghodss/yaml" "github.com/sirupsen/logrus" @@ -22,6 +23,7 @@ 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" @@ -155,51 +157,63 @@ func (k8s *k8sSubscriber) IsAgentConfirmed() (bool, string, error) { } // AgentRegister function creates litmus-portal config map in the litmus namespace -func (k8s *k8sSubscriber) AgentRegister(infraData map[string]string) (bool, error) { +func (k8s *k8sSubscriber) AgentRegister(accessKey string) (bool, error) { clientset, err := k8s.GetGenericK8sClient() if err != nil { return false, err } - newConfigMapData := map[string]string{ - "IS_INFRA_CONFIRMED": infraData["IS_INFRA_CONFIRMED"], - "SERVER_ADDR": infraData["SERVER_ADDR"], - "INFRA_SCOPE": infraData["INFRA_SCOPE"], - "COMPONENTS": infraData["COMPONENTS"], - "START_TIME": infraData["START_TIME"], - "VERSION": infraData["VERSION"], - "SKIP_SSL_VERIFY": infraData["SKIP_SSL_VERIFY"], - "CUSTOM_TLS_CERT": infraData["CUSTOM_TLS_CERT"], + // Define a patch object for the ConfigMap with only "IS_CLUSTER_CONFIRMED". + newConfigMapData := map[string]interface{}{ + "data": map[string]interface{}{ + "IS_INFRA_CONFIRMED": true, + }, } - _, err = clientset.CoreV1().ConfigMaps(InfraNamespace).Update(context.TODO(), &corev1.ConfigMap{ - Data: newConfigMapData, - ObjectMeta: metav1.ObjectMeta{ - Name: InfraConfigName, - }, - }, metav1.UpdateOptions{}) + configMapPatchBytes, err := json.Marshal(newConfigMapData) + + if err != nil { + return false, err + } + + // Patch the ConfigMap. + _, err = clientset.CoreV1().ConfigMaps(InfraNamespace).Patch( + context.TODO(), + InfraConfigName, + types.StrategicMergePatchType, + configMapPatchBytes, + metav1.PatchOptions{}, + ) if err != nil { return false, err } - logrus.Info(InfraConfigName + " has been updated") + logrus.Info("%s has been updated", InfraConfigName) newSecretData := map[string]string{ - "ACCESS_KEY": infraData["ACCESS_KEY"], - "INFRA_ID": infraData["INFRA_ID"], + "ACCESS_KEY": accessKey, } - _, err = clientset.CoreV1().Secrets(InfraNamespace).Update(context.TODO(), &corev1.Secret{ - StringData: newSecretData, - ObjectMeta: metav1.ObjectMeta{ - Name: InfraSecretName, - }, - }, metav1.UpdateOptions{}) + secretPatch := map[string]interface{}{ + "stringData": newSecretData, + } + + secretPatchBytes, err := json.Marshal(secretPatch) + if err != nil { + return false, err + } + _, err = clientset.CoreV1().Secrets(InfraNamespace).Patch( + context.TODO(), + InfraSecretName, + types.StrategicMergePatchType, + secretPatchBytes, + metav1.PatchOptions{}, + ) if err != nil { return false, err } - logrus.Info(InfraSecretName + " has been updated") + logrus.Info("%s has been updated", InfraSecretName) return true, nil } @@ -305,7 +319,7 @@ func addCustomLabels(obj *unstructured.Unstructured, customLabels map[string]str } // AgentOperations This function handles agent operations -func (k8s *k8sSubscriber) AgentOperations(infraAction types.Action) (*unstructured.Unstructured, error) { +func (k8s *k8sSubscriber) AgentOperations(infraAction pkgTypes.Action) (*unstructured.Unstructured, error) { // Converting JSON to YAML and store it in yamlStr variable yamlStr, err := yaml_converter.JSONToYAML([]byte(infraAction.K8SManifest)) if err != nil { diff --git a/chaoscenter/subscriber/subscriber.go b/chaoscenter/subscriber/subscriber.go index fe89a6dc729..08a3c42687b 100644 --- a/chaoscenter/subscriber/subscriber.go +++ b/chaoscenter/subscriber/subscriber.go @@ -121,7 +121,7 @@ func init() { infraData["ACCESS_KEY"] = infraConfirmInterface.Data.InfraConfirm.NewAccessKey infraData["IS_INFRA_CONFIRMED"] = "true" - _, err = subscriberK8s.AgentRegister(infraData) + _, err = subscriberK8s.AgentRegister(infraData["ACCESS_KEY"]) if err != nil { logrus.Fatal(err) } diff --git a/litmus-portal/cluster-agents/subscriber/pkg/k8s/operations.go b/litmus-portal/cluster-agents/subscriber/pkg/k8s/operations.go index eb9d8b8c296..6f966feb393 100644 --- a/litmus-portal/cluster-agents/subscriber/pkg/k8s/operations.go +++ b/litmus-portal/cluster-agents/subscriber/pkg/k8s/operations.go @@ -3,6 +3,7 @@ package k8s import ( "context" "encoding/base64" + "encoding/json" "errors" "fmt" "strings" @@ -10,7 +11,7 @@ import ( "time" "github.com/litmuschaos/litmus/litmus-portal/cluster-agents/subscriber/pkg/graphql" - "github.com/litmuschaos/litmus/litmus-portal/cluster-agents/subscriber/pkg/types" + atype "github.com/litmuschaos/litmus/litmus-portal/cluster-agents/subscriber/pkg/types" yaml_converter "github.com/ghodss/yaml" "github.com/sirupsen/logrus" @@ -22,6 +23,7 @@ import ( "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/labels" "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" @@ -157,51 +159,63 @@ func IsClusterConfirmed() (bool, string, error) { } // ClusterRegister function creates litmus-portal config map in the litmus namespace -func ClusterRegister(clusterData map[string]string) (bool, error) { +func ClusterRegister(accessKey string) (bool, error) { clientset, err := GetGenericK8sClient() if err != nil { return false, err } - newConfigMapData := map[string]string{ - "IS_CLUSTER_CONFIRMED": clusterData["IS_CLUSTER_CONFIRMED"], - "SERVER_ADDR": clusterData["SERVER_ADDR"], - "AGENT_SCOPE": clusterData["AGENT_SCOPE"], - "COMPONENTS": clusterData["COMPONENTS"], - "START_TIME": clusterData["START_TIME"], - "VERSION": clusterData["VERSION"], - "SKIP_SSL_VERIFY": clusterData["SKIP_SSL_VERIFY"], - "CUSTOM_TLS_CERT": clusterData["CUSTOM_TLS_CERT"], + // Define a patch object for the ConfigMap with only "IS_CLUSTER_CONFIRMED". + configMapPatch := map[string]interface{}{ + "data": map[string]interface{}{ + "IS_CLUSTER_CONFIRMED": true, + }, } - _, err = clientset.CoreV1().ConfigMaps(AgentNamespace).Update(context.TODO(), &corev1.ConfigMap{ - Data: newConfigMapData, - ObjectMeta: metav1.ObjectMeta{ - Name: AgentConfigName, - }, - }, metav1.UpdateOptions{}) + configMapPatchBytes, err := json.Marshal(configMapPatch) + + if err != nil { + return false, err + } + + // Patch the ConfigMap. + _, err = clientset.CoreV1().ConfigMaps(AgentNamespace).Patch( + context.TODO(), + AgentConfigName, + types.StrategicMergePatchType, + configMapPatchBytes, + metav1.PatchOptions{}, + ) if err != nil { return false, err } - logrus.Info(AgentConfigName + " has been updated") + logrus.Info("%s has been updated", AgentConfigName) newSecretData := map[string]string{ - "ACCESS_KEY": clusterData["ACCESS_KEY"], - "CLUSTER_ID": clusterData["CLUSTER_ID"], + "ACCESS_KEY": accessKey, } - _, err = clientset.CoreV1().Secrets(AgentNamespace).Update(context.TODO(), &corev1.Secret{ - StringData: newSecretData, - ObjectMeta: metav1.ObjectMeta{ - Name: AgentSecretName, - }, - }, metav1.UpdateOptions{}) + secretPatch := map[string]interface{}{ + "stringData": newSecretData, + } + + secretPatchBytes, err := json.Marshal(secretPatch) + if err != nil { + return false, err + } + _, err = clientset.CoreV1().Secrets(AgentNamespace).Patch( + context.TODO(), + AgentSecretName, + types.StrategicMergePatchType, + secretPatchBytes, + metav1.PatchOptions{}, + ) if err != nil { return false, err } - logrus.Info(AgentSecretName + " has been updated") + logrus.Info("%s has been updated", AgentSecretName) return true, nil } @@ -298,7 +312,7 @@ func addCustomLabels(obj *unstructured.Unstructured, customLabels map[string]str } // ClusterOperations handles cluster operations -func ClusterOperations(clusterAction types.Action) (*unstructured.Unstructured, error) { +func ClusterOperations(clusterAction atype.Action) (*unstructured.Unstructured, error) { // Converting JSON to YAML and store it in yamlStr variable yamlStr, err := yaml_converter.JSONToYAML([]byte(clusterAction.K8SManifest)) diff --git a/litmus-portal/cluster-agents/subscriber/subscriber.go b/litmus-portal/cluster-agents/subscriber/subscriber.go index f2e46f9cfb3..9c3a272ae75 100644 --- a/litmus-portal/cluster-agents/subscriber/subscriber.go +++ b/litmus-portal/cluster-agents/subscriber/subscriber.go @@ -114,7 +114,7 @@ func init() { clusterData["ACCESS_KEY"] = clusterConfirmInterface.Data.ClusterConfirm.NewAccessKey clusterData["IS_CLUSTER_CONFIRMED"] = "true" - _, err = k8s.ClusterRegister(clusterData) + _, err = k8s.ClusterRegister(clusterData["ACCESS_KEY"]) if err != nil { logrus.Fatal(err) }