From dea5883df999d103d70da5a49e98cc53ad3a9ded Mon Sep 17 00:00:00 2001 From: morvencao Date: Thu, 21 Mar 2024 01:12:02 +0000 Subject: [PATCH] address comments. Signed-off-by: morvencao --- pkg/operator/helpers/helpers.go | 43 +------------------ .../clustermanager_controller.go | 9 +++- 2 files changed, 10 insertions(+), 42 deletions(-) diff --git a/pkg/operator/helpers/helpers.go b/pkg/operator/helpers/helpers.go index 8eab11340..b7acb71f3 100644 --- a/pkg/operator/helpers/helpers.go +++ b/pkg/operator/helpers/helpers.go @@ -725,45 +725,6 @@ func GetHubKubeconfig(ctx context.Context, } } -// SyncWorkConfigSecret is used to sync the secret of work config from operator namespace to the cluster manager namespace. -// For both Default and Hosted mode, the original secret of work config should be in the operator namespace. -func SyncWorkConfigSecret(ctx context.Context, - operatorClient kubernetes.Interface, - clusterManagerNamespace string, -) error { - // get the namespace of the operator - operatorNamespace, err := getOperatorNamespace() - if err != nil { - return err - } - - // get secret of work config - workDriverSecret, err := operatorClient.CoreV1().Secrets(operatorNamespace).Get(ctx, WorkDriverConfig, metav1.GetOptions{}) - if err != nil { - return err - } - - // copy the secret of work config to new secret in the cluster manager namespace - newSecret := &corev1.Secret{ - ObjectMeta: metav1.ObjectMeta{ - Name: workDriverSecret.Name, - Namespace: clusterManagerNamespace, - }, - Data: workDriverSecret.Data, - } - // create or update secret of work config in the cluster manager namespace - _, err = operatorClient.CoreV1().Secrets(clusterManagerNamespace).Create(ctx, newSecret, metav1.CreateOptions{}) - if err != nil { - if errors.IsAlreadyExists(err) { - _, err = operatorClient.CoreV1().Secrets(clusterManagerNamespace).Update(ctx, newSecret, metav1.UpdateOptions{}) - return err - } - return err - } - - return nil -} - // RemoveWorkConfigSecret is used to remove the secret of work config from the cluster manager namespace. func RemoveWorkConfigSecret(ctx context.Context, operatorClient kubernetes.Interface, @@ -783,8 +744,8 @@ func RemoveWorkConfigSecret(ctx context.Context, return err } -// getOperatorNamespace is used to get the namespace where the operator is running. -func getOperatorNamespace() (string, error) { +// GetOperatorNamespace is used to get the namespace where the operator is running. +func GetOperatorNamespace() (string, error) { podNamespace, exists := os.LookupEnv("POD_NAMESPACE") if exists { return podNamespace, nil diff --git a/pkg/operator/operators/clustermanager/controllers/clustermanagercontroller/clustermanager_controller.go b/pkg/operator/operators/clustermanager/controllers/clustermanagercontroller/clustermanager_controller.go index 0b9163a86..b3582c6b9 100644 --- a/pkg/operator/operators/clustermanager/controllers/clustermanagercontroller/clustermanager_controller.go +++ b/pkg/operator/operators/clustermanager/controllers/clustermanagercontroller/clustermanager_controller.go @@ -218,7 +218,14 @@ func (n *clusterManagerController) sync(ctx context.Context, controllerContext f // If the work driver is not kube, we need to sync the work driver config secret if workDriver != operatorapiv1.WorkDriverTypeKube { - if err := helpers.SyncWorkConfigSecret(ctx, n.operatorKubeClient, clusterManagerNamespace); err != nil { + operatorNamespace, err := helpers.GetOperatorNamespace() + if err != nil { + return err + } + _, _, err = resourceapply.SyncSecret(ctx, n.operatorKubeClient.CoreV1(), n.recorder, + operatorNamespace, helpers.WorkDriverConfig, clusterManagerNamespace, helpers.WorkDriverConfig, + []metav1.OwnerReference{}) + if err != nil { return err } } else {