Skip to content

Commit

Permalink
refactor check local-cluster name
Browse files Browse the repository at this point in the history
Signed-off-by: Zhiwei Yin <[email protected]>
  • Loading branch information
zhiweiyin318 committed Aug 20, 2024
1 parent f4d98ca commit 67cfdd1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
3 changes: 2 additions & 1 deletion pkg/controller/addon/klusterlet_addon_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down
24 changes: 14 additions & 10 deletions pkg/controller/addon/klusterlet_addon_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
},
}
Expand All @@ -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{}
Expand All @@ -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",
Expand Down Expand Up @@ -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"})
Expand All @@ -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)
}
Expand All @@ -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", ""),
Expand Down Expand Up @@ -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", ""),
Expand Down

0 comments on commit 67cfdd1

Please sign in to comment.