From 67cfdd11149d043c7e11b74e611e97af85c046bc Mon Sep 17 00:00:00 2001 From: Zhiwei Yin Date: Mon, 19 Aug 2024 17:07:03 +0800 Subject: [PATCH] refactor check local-cluster name Signed-off-by: Zhiwei Yin --- .../addon/klusterlet_addon_controller.go | 3 ++- .../addon/klusterlet_addon_controller_test.go | 24 +++++++++++-------- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/pkg/controller/addon/klusterlet_addon_controller.go b/pkg/controller/addon/klusterlet_addon_controller.go index 9a9bf2c5..3357f830 100644 --- a/pkg/controller/addon/klusterlet_addon_controller.go +++ b/pkg/controller/addon/klusterlet_addon_controller.go @@ -7,6 +7,7 @@ import ( "reflect" "strings" + "github.com/stolostron/cluster-lifecycle-api/helpers/localcluster" imageregistryv1alpha1 "github.com/stolostron/cluster-lifecycle-api/imageregistry/v1alpha1" agentv1 "github.com/stolostron/klusterlet-addon-controller/pkg/apis/agent/v1" "github.com/stolostron/klusterlet-addon-controller/pkg/common" @@ -224,7 +225,7 @@ func isPaused(instance *agentv1.KlusterletAddonConfig) bool { func getNodeSelector(managedCluster *managedclusterv1.ManagedCluster) (map[string]string, error) { var nodeSelector map[string]string - if managedCluster.GetName() == "local-cluster" { + if localcluster.IsClusterSelfManaged(managedCluster) { annotations := managedCluster.GetAnnotations() if nodeSelectorString, ok := annotations[annotationNodeSelector]; ok { if err := json.Unmarshal([]byte(nodeSelectorString), &nodeSelector); err != nil { diff --git a/pkg/controller/addon/klusterlet_addon_controller_test.go b/pkg/controller/addon/klusterlet_addon_controller_test.go index 6061817a..01b28cc4 100644 --- a/pkg/controller/addon/klusterlet_addon_controller_test.go +++ b/pkg/controller/addon/klusterlet_addon_controller_test.go @@ -7,6 +7,7 @@ import ( "reflect" "testing" + apiconstants "github.com/stolostron/cluster-lifecycle-api/constants" "github.com/stolostron/klusterlet-addon-controller/pkg/apis" agentv1 "github.com/stolostron/klusterlet-addon-controller/pkg/apis/agent/v1" v1 "github.com/stolostron/klusterlet-addon-controller/pkg/apis/agent/v1" @@ -165,10 +166,11 @@ func newKlusterletAddonConfigWithProxy(clusterName string) *v1.KlusterletAddonCo } } -func newManagedCluster(name string, annotations map[string]string) *mcv1.ManagedCluster { +func newManagedCluster(name string, labels, annotations map[string]string) *mcv1.ManagedCluster { return &mcv1.ManagedCluster{ ObjectMeta: metav1.ObjectMeta{ Name: name, + Labels: labels, Annotations: annotations, }, } @@ -192,7 +194,7 @@ func Test_Reconcile(t *testing.T) { { name: "cluster is created, create all addons", clusterName: "cluster1", - managedCluster: newManagedCluster("cluster1", nil), + managedCluster: newManagedCluster("cluster1", nil, nil), klusterletAddonConfig: newKlusterletAddonConfig("cluster1"), validateFunc: func(t *testing.T, kubeClient client.Client) { addonList := &v1alpha1.ManagedClusterAddOnList{} @@ -208,7 +210,7 @@ func Test_Reconcile(t *testing.T) { { name: "cluster is created in hosed mode with hosted add-on enabled", clusterName: "cluster1", - managedCluster: newManagedCluster("cluster1", map[string]string{ + managedCluster: newManagedCluster("cluster1", nil, map[string]string{ common.AnnotationKlusterletDeployMode: "Hosted", common.AnnotationKlusterletHostingClusterName: "local-cluster", common.AnnotationEnableHostedModeAddons: "true", @@ -248,7 +250,7 @@ func Test_Reconcile(t *testing.T) { { name: "no klusterletaddonconfig", clusterName: "cluster1", - managedCluster: newManagedCluster("cluster1", nil), + managedCluster: newManagedCluster("cluster1", nil, nil), validateFunc: func(t *testing.T, kubeClient client.Client) { addonList := &v1alpha1.ManagedClusterAddOnList{} err := kubeClient.List(context.TODO(), addonList, &client.ListOptions{Namespace: "cluster1"}) @@ -262,14 +264,16 @@ func Test_Reconcile(t *testing.T) { }, { name: "local-cluster with annotations", - clusterName: "local-cluster", - managedCluster: newManagedCluster("local-cluster", map[string]string{ + clusterName: "local-cluster-test", + managedCluster: newManagedCluster("local-cluster-test", map[string]string{ + apiconstants.SelfManagedClusterLabelKey: "true", + }, map[string]string{ annotationNodeSelector: `{"node":"infra"}`, }), - klusterletAddonConfig: newKlusterletAddonConfig("local-cluster"), + klusterletAddonConfig: newKlusterletAddonConfig("local-cluster-test"), validateFunc: func(t *testing.T, kubeClient client.Client) { addonList := &v1alpha1.ManagedClusterAddOnList{} - err := kubeClient.List(context.TODO(), addonList, &client.ListOptions{Namespace: "local-cluster"}) + err := kubeClient.List(context.TODO(), addonList, &client.ListOptions{Namespace: "local-cluster-test"}) if err != nil { t.Errorf("faild to list addons. %v", err) } @@ -295,7 +299,7 @@ func Test_Reconcile(t *testing.T) { { name: "cluster with proxy", clusterName: "cluster1", - managedCluster: newManagedCluster("cluster1", nil), + managedCluster: newManagedCluster("cluster1", nil, nil), klusterletAddonConfig: newKlusterletAddonConfigWithProxy("cluster1"), managedClusterAddons: []runtime.Object{ newManagedClusterAddon(v1.ApplicationAddonName, "cluster1", ""), @@ -331,7 +335,7 @@ func Test_Reconcile(t *testing.T) { { name: "upgrade remove iam addon", clusterName: "cluster1", - managedCluster: newManagedCluster("cluster1", nil), + managedCluster: newManagedCluster("cluster1", nil, nil), klusterletAddonConfig: newKlusterletAddonConfig("cluster1"), managedClusterAddons: []runtime.Object{ newManagedClusterAddon(v1.IamPolicyAddonName, "cluster1", ""),