Skip to content

Commit

Permalink
refactor: Update subscriber to use Kubernetes patch for ConfigMaps an… (
Browse files Browse the repository at this point in the history
#4287)

* refactor: Update subscriber to use Kubernetes patch for ConfigMaps and Secrets instead of get-update

Signed-off-by: Ripul Handoo <[email protected]>

* refactor: Update subscriber to use Kubernetes patch for ConfigMaps

Signed-off-by: Ripul Handoo <[email protected]>

* Update operations.go

Signed-off-by: Ripul Handoo <[email protected]>

---------

Signed-off-by: Ripul Handoo <[email protected]>
Signed-off-by: Ripul Handoo <[email protected]>
Co-authored-by: Saranya Jena <[email protected]>
  • Loading branch information
RipulHandoo and Saranya-jena authored Feb 12, 2024
1 parent 7b4bedb commit d23414e
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 59 deletions.
2 changes: 1 addition & 1 deletion chaoscenter/subscriber/pkg/k8s/defination.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
70 changes: 42 additions & 28 deletions chaoscenter/subscriber/pkg/k8s/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package k8s
import (
"context"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"strings"
Expand All @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion chaoscenter/subscriber/subscriber.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
70 changes: 42 additions & 28 deletions litmus-portal/cluster-agents/subscriber/pkg/k8s/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ package k8s
import (
"context"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"strings"
"sync"
"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"
Expand All @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion litmus-portal/cluster-agents/subscriber/subscriber.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit d23414e

Please sign in to comment.