From c9f5793812aaea3c4f62921dfdfa149ff6fb8813 Mon Sep 17 00:00:00 2001 From: Feny Mehta Date: Tue, 30 Jan 2024 15:40:43 +0530 Subject: [PATCH 01/23] KSPACE-20: Drop the distinction between host & member ToolchainClusters Signed-off-by: Feny Mehta --- .../toolchaincluster_controller.go | 28 -------- pkg/cluster/cache.go | 34 +++------ pkg/cluster/cache_whitebox_test.go | 71 +++++++++---------- pkg/cluster/service.go | 15 +--- pkg/cluster/service_test.go | 21 +++--- pkg/cluster/service_whitebox_test.go | 3 +- pkg/status/toolchaincluster_test.go | 1 - pkg/test/cluster.go | 2 +- pkg/test/verify/cluster.go | 45 +++--------- 9 files changed, 70 insertions(+), 150 deletions(-) diff --git a/controllers/toolchaincluster/toolchaincluster_controller.go b/controllers/toolchaincluster/toolchaincluster_controller.go index 66077108..4be49b6f 100644 --- a/controllers/toolchaincluster/toolchaincluster_controller.go +++ b/controllers/toolchaincluster/toolchaincluster_controller.go @@ -61,33 +61,5 @@ func (r *Reconciler) Reconcile(ctx context.Context, request ctrl.Request) (ctrl. return reconcile.Result{}, err } - // add toolchaincluster role label if not present - reqLogger.Info("adding cluster role label based on type") - if err := r.addToolchainClusterRoleLabelFromType(ctx, toolchainCluster); err != nil { - return reconcile.Result{}, err - } - return reconcile.Result{}, r.clusterCacheService.AddOrUpdateToolchainCluster(toolchainCluster) } - -func (r *Reconciler) addToolchainClusterRoleLabelFromType(ctx context.Context, toolchainCluster *toolchainv1alpha1.ToolchainCluster) error { - logger := log.FromContext(ctx) - - if clusterType, found := toolchainCluster.Labels[cluster.LabelType]; !found { - logger.Info("cluster `type` label not found, unable to add toolchain cluster role label from type") - return nil - } else if clusterType != string(cluster.Member) { - logger.Info("cluster `type` is not member, skipping cluster role label setting") - return nil - } - clusterRoleLabel := cluster.RoleLabel(cluster.Tenant) - if _, exists := toolchainCluster.Labels[clusterRoleLabel]; !exists { - logger.Info("setting cluster role label for toolchaincluster", clusterRoleLabel, toolchainCluster.Name) - // We use only the label key, the value can remain empty. - toolchainCluster.Labels[clusterRoleLabel] = "" - if err := r.client.Update(ctx, toolchainCluster); err != nil { - return err - } - } - return nil -} diff --git a/pkg/cluster/cache.go b/pkg/cluster/cache.go index 420ca849..8acf26a7 100644 --- a/pkg/cluster/cache.go +++ b/pkg/cluster/cache.go @@ -24,8 +24,6 @@ type Config struct { // APIEndpoint is the API endpoint of the corresponding ToolchainCluster. This can be a hostname, // hostname:port, IP or IP:port. APIEndpoint string - // Type is a type of the cluster (either host or member) - Type Type // OperatorNamespace is a name of a namespace (in the cluster) the operator is running in OperatorNamespace string // OwnerClusterName keeps the name of the cluster the ToolchainCluster resource is created in @@ -81,23 +79,21 @@ var Ready Condition = func(cluster *CachedToolchainCluster) bool { return IsReady(cluster.ClusterStatus) } -func (c *toolchainClusterClients) getCachedToolchainClustersByType(clusterType Type, conditions ...Condition) []*CachedToolchainCluster { +func (c *toolchainClusterClients) getCachedToolchainClustersByType(conditions ...Condition) []*CachedToolchainCluster { c.RLock() defer c.RUnlock() - return Filter(clusterType, c.clusters, conditions...) + return Filter(c.clusters, conditions...) } -func Filter(clusterType Type, clusters map[string]*CachedToolchainCluster, conditions ...Condition) []*CachedToolchainCluster { +func Filter(clusters map[string]*CachedToolchainCluster, conditions ...Condition) []*CachedToolchainCluster { filteredClusters := make([]*CachedToolchainCluster, 0, len(clusters)) clusters: for _, cluster := range clusters { - if cluster.Type == clusterType { - for _, match := range conditions { - if !match(cluster) { - continue clusters - } + for _, match := range conditions { + if !match(cluster) { + continue clusters } - filteredClusters = append(filteredClusters, cluster) } + filteredClusters = append(filteredClusters, cluster) } return filteredClusters } @@ -117,12 +113,12 @@ var HostCluster GetHostClusterFunc = GetHostCluster // GetHostCluster returns the kube client for the host cluster from the cache of the clusters // and info if such a client exists func GetHostCluster() (*CachedToolchainCluster, bool) { - clusters := clusterCache.getCachedToolchainClustersByType(Host) + clusters := clusterCache.getCachedToolchainClustersByType() if len(clusters) == 0 { if clusterCache.refreshCache != nil { clusterCache.refreshCache() } - clusters = clusterCache.getCachedToolchainClustersByType(Host) + clusters = clusterCache.getCachedToolchainClustersByType() if len(clusters) == 0 { return nil, false } @@ -138,24 +134,16 @@ var MemberClusters GetMemberClustersFunc = GetMemberClusters // GetMemberClusters returns the kube clients for the host clusters from the cache of the clusters func GetMemberClusters(conditions ...Condition) []*CachedToolchainCluster { - clusters := clusterCache.getCachedToolchainClustersByType(Member, conditions...) + clusters := clusterCache.getCachedToolchainClustersByType(conditions...) if len(clusters) == 0 { if clusterCache.refreshCache != nil { clusterCache.refreshCache() } - clusters = clusterCache.getCachedToolchainClustersByType(Member, conditions...) + clusters = clusterCache.getCachedToolchainClustersByType(conditions...) } return clusters } -// Type is a cluster type (either host or member) -type Type string - -const ( - Member Type = "member" - Host Type = "host" -) - // Role defines the role of the cluster. // Each type of cluster can have multiple roles (tenant for specific APIs, user workloads, others ... ) type Role string diff --git a/pkg/cluster/cache_whitebox_test.go b/pkg/cluster/cache_whitebox_test.go index 4b1ee293..a487c6e6 100644 --- a/pkg/cluster/cache_whitebox_test.go +++ b/pkg/cluster/cache_whitebox_test.go @@ -23,7 +23,7 @@ var getCachedToolchainClusterFuncs = []func(name string) (*CachedToolchainCluste func TestAddCluster(t *testing.T) { // given defer resetClusterCache() - cachedCluster := newTestCachedToolchainCluster(t, "testCluster", Member, ready) + cachedCluster := newTestCachedToolchainCluster(t, "testCluster", ready) // when clusterCache.addCachedToolchainCluster(cachedCluster) @@ -36,9 +36,9 @@ func TestAddCluster(t *testing.T) { func TestGetCluster(t *testing.T) { // given defer resetClusterCache() - cachedCluster := newTestCachedToolchainCluster(t, "testCluster", Member, ready) + cachedCluster := newTestCachedToolchainCluster(t, "testCluster", ready) clusterCache.addCachedToolchainCluster(cachedCluster) - clusterCache.addCachedToolchainCluster(newTestCachedToolchainCluster(t, "cluster", Member, ready)) + clusterCache.addCachedToolchainCluster(newTestCachedToolchainCluster(t, "cluster", ready)) for _, getCachedCluster := range getCachedToolchainClusterFuncs { @@ -54,7 +54,7 @@ func TestGetCluster(t *testing.T) { func TestHostCluster(t *testing.T) { // given defer resetClusterCache() - host := newTestCachedToolchainCluster(t, "host-cluster", Host, ready) + host := newTestCachedToolchainCluster(t, "host-cluster", ready) clusterCache.addCachedToolchainCluster(host) // when @@ -68,9 +68,9 @@ func TestHostCluster(t *testing.T) { func TestMemberClusters(t *testing.T) { // given defer resetClusterCache() - member1 := newTestCachedToolchainCluster(t, "member-cluster-1", Member, ready) + member1 := newTestCachedToolchainCluster(t, "member-cluster-1", ready) clusterCache.addCachedToolchainCluster(member1) - member2 := newTestCachedToolchainCluster(t, "member-cluster-2", Member, ready) + member2 := newTestCachedToolchainCluster(t, "member-cluster-2", ready) clusterCache.addCachedToolchainCluster(member2) // when @@ -107,13 +107,13 @@ func TestGetClustersByType(t *testing.T) { // empty cache //when - clusters := clusterCache.getCachedToolchainClustersByType(Member) + clusters := clusterCache.getCachedToolchainClustersByType() //then assert.Empty(t, clusters) //when - clusters = clusterCache.getCachedToolchainClustersByType(Host) + clusters = clusterCache.getCachedToolchainClustersByType() //then assert.Empty(t, clusters) @@ -123,15 +123,15 @@ func TestGetClustersByType(t *testing.T) { defer resetClusterCache() // given // Two members, one host - member1 := newTestCachedToolchainCluster(t, "cluster-1", Member, ready) + member1 := newTestCachedToolchainCluster(t, "cluster-1", ready) clusterCache.addCachedToolchainCluster(member1) - member2 := newTestCachedToolchainCluster(t, "cluster-2", Member, ready) + member2 := newTestCachedToolchainCluster(t, "cluster-2", ready) clusterCache.addCachedToolchainCluster(member2) - host := newTestCachedToolchainCluster(t, "cluster-3", Host, ready) + host := newTestCachedToolchainCluster(t, "cluster-3", ready) clusterCache.addCachedToolchainCluster(host) //when - clusters := clusterCache.getCachedToolchainClustersByType(Member) + clusters := clusterCache.getCachedToolchainClustersByType() //then assert.Len(t, clusters, 2) @@ -139,7 +139,7 @@ func TestGetClustersByType(t *testing.T) { assert.Contains(t, clusters, member2) //when - clusters = clusterCache.getCachedToolchainClustersByType(Host) + clusters = clusterCache.getCachedToolchainClustersByType() //then assert.Len(t, clusters, 1) @@ -149,7 +149,7 @@ func TestGetClustersByType(t *testing.T) { t.Run("get member clusters", func(t *testing.T) { // noise - host := newTestCachedToolchainCluster(t, "cluster-host", Host, ready) + host := newTestCachedToolchainCluster(t, "cluster-host", ready) clusterCache.addCachedToolchainCluster(host) t.Run("not found", func(t *testing.T) { @@ -167,9 +167,9 @@ func TestGetClustersByType(t *testing.T) { t.Run("all clusters", func(t *testing.T) { // given defer resetClusterCache() - member1 := newTestCachedToolchainCluster(t, "cluster-1", Member, ready) + member1 := newTestCachedToolchainCluster(t, "cluster-1", ready) clusterCache.addCachedToolchainCluster(member1) - member2 := newTestCachedToolchainCluster(t, "cluster-2", Member, ready) + member2 := newTestCachedToolchainCluster(t, "cluster-2", ready) clusterCache.addCachedToolchainCluster(member2) //when @@ -184,7 +184,7 @@ func TestGetClustersByType(t *testing.T) { t.Run("found after refreshing the cache", func(t *testing.T) { // given defer resetClusterCache() - member := newTestCachedToolchainCluster(t, "member", Member, ready) + member := newTestCachedToolchainCluster(t, "member", ready) called := false clusterCache.refreshCache = func() { called = true @@ -206,13 +206,13 @@ func TestGetClustersByType(t *testing.T) { defer resetClusterCache() // noise - host := newTestCachedToolchainCluster(t, "cluster-host", Host, ready) + host := newTestCachedToolchainCluster(t, "cluster-host", ready) clusterCache.addCachedToolchainCluster(host) - member1 := newTestCachedToolchainCluster(t, "cluster-1", Member, ready) + member1 := newTestCachedToolchainCluster(t, "cluster-1", ready) clusterCache.addCachedToolchainCluster(member1) - member2 := newTestCachedToolchainCluster(t, "cluster-2", Member, ready) + member2 := newTestCachedToolchainCluster(t, "cluster-2", ready) clusterCache.addCachedToolchainCluster(member2) - member3 := newTestCachedToolchainCluster(t, "cluster-3", Member, notReady) + member3 := newTestCachedToolchainCluster(t, "cluster-3", notReady) clusterCache.addCachedToolchainCluster(member3) t.Run("get only ready member clusters", func(t *testing.T) { @@ -229,7 +229,7 @@ func TestGetClustersByType(t *testing.T) { t.Run("get host cluster", func(t *testing.T) { // noise - member1 := newTestCachedToolchainCluster(t, "cluster-member-1", Member, ready) + member1 := newTestCachedToolchainCluster(t, "cluster-member-1", ready) clusterCache.addCachedToolchainCluster(member1) t.Run("not found", func(t *testing.T) { @@ -247,7 +247,7 @@ func TestGetClustersByType(t *testing.T) { t.Run("found", func(t *testing.T) { // given defer resetClusterCache() - host := newTestCachedToolchainCluster(t, "cluster-host", Host, ready) + host := newTestCachedToolchainCluster(t, "cluster-host", ready) clusterCache.addCachedToolchainCluster(host) //when @@ -261,7 +261,7 @@ func TestGetClustersByType(t *testing.T) { t.Run("found after refreshing the cache", func(t *testing.T) { // given defer resetClusterCache() - host := newTestCachedToolchainCluster(t, "cluster-host", Host, ready) + host := newTestCachedToolchainCluster(t, "cluster-host", ready) called := false clusterCache.refreshCache = func() { called = true @@ -282,7 +282,7 @@ func TestGetClustersByType(t *testing.T) { func TestGetClusterUsingDifferentKey(t *testing.T) { // given defer resetClusterCache() - clusterCache.addCachedToolchainCluster(newTestCachedToolchainCluster(t, "cluster", Member, ready)) + clusterCache.addCachedToolchainCluster(newTestCachedToolchainCluster(t, "cluster", ready)) for _, getCachedCluster := range getCachedToolchainClusterFuncs { @@ -298,8 +298,8 @@ func TestGetClusterUsingDifferentKey(t *testing.T) { func TestUpdateCluster(t *testing.T) { // given defer resetClusterCache() - trueCluster := newTestCachedToolchainCluster(t, "testCluster", Member, ready) - falseCluster := newTestCachedToolchainCluster(t, "testCluster", Member, notReady) + trueCluster := newTestCachedToolchainCluster(t, "testCluster", ready) + falseCluster := newTestCachedToolchainCluster(t, "testCluster", notReady) clusterCache.addCachedToolchainCluster(trueCluster) // when @@ -313,9 +313,9 @@ func TestUpdateCluster(t *testing.T) { func TestDeleteCluster(t *testing.T) { // given defer resetClusterCache() - cachedCluster := newTestCachedToolchainCluster(t, "testCluster", Member, ready) + cachedCluster := newTestCachedToolchainCluster(t, "testCluster", ready) clusterCache.addCachedToolchainCluster(cachedCluster) - clusterCache.addCachedToolchainCluster(newTestCachedToolchainCluster(t, "cluster", Member, ready)) + clusterCache.addCachedToolchainCluster(newTestCachedToolchainCluster(t, "cluster", ready)) assert.Len(t, clusterCache.clusters, 2) // when @@ -328,8 +328,8 @@ func TestDeleteCluster(t *testing.T) { func TestRefreshCache(t *testing.T) { // given - testCluster := newTestCachedToolchainCluster(t, "testCluster", Member, ready) - newCluster := newTestCachedToolchainCluster(t, "newCluster", Member, ready) + testCluster := newTestCachedToolchainCluster(t, "testCluster", ready) + newCluster := newTestCachedToolchainCluster(t, "newCluster", ready) t.Run("refresh enabled", func(t *testing.T) { defer resetClusterCache() @@ -397,8 +397,8 @@ func TestMultipleActionsInParallel(t *testing.T) { latch.Add(1) var waitForFinished sync.WaitGroup - memberCluster := newTestCachedToolchainCluster(t, "memberCluster", Member, ready) - hostCluster := newTestCachedToolchainCluster(t, "hostCluster", Host, ready) + memberCluster := newTestCachedToolchainCluster(t, "memberCluster", ready) + hostCluster := newTestCachedToolchainCluster(t, "hostCluster", ready) clusterCache.refreshCache = func() { clusterCache.addCachedToolchainCluster(memberCluster) clusterCache.addCachedToolchainCluster(hostCluster) @@ -425,7 +425,7 @@ func TestMultipleActionsInParallel(t *testing.T) { go func() { defer waitForFinished.Done() latch.Wait() - clusters := clusterCache.getCachedToolchainClustersByType(clusterToTest.Type) + clusters := clusterCache.getCachedToolchainClustersByType() if len(clusters) == 1 { assert.Equal(t, clusterToTest, clusters[0]) } else { @@ -474,13 +474,12 @@ var notReady clusterOption = func(c *CachedToolchainCluster) { }) } -func newTestCachedToolchainCluster(t *testing.T, name string, clusterType Type, options ...clusterOption) *CachedToolchainCluster { +func newTestCachedToolchainCluster(t *testing.T, name string, options ...clusterOption) *CachedToolchainCluster { cl := test.NewFakeClient(t) cachedCluster := &CachedToolchainCluster{ Config: &Config{ Name: name, OperatorNamespace: name + "Namespace", - Type: clusterType, }, Client: cl, ClusterStatus: &toolchainv1alpha1.ToolchainClusterStatus{}, diff --git a/pkg/cluster/service.go b/pkg/cluster/service.go index f9326419..fd7e4922 100644 --- a/pkg/cluster/service.go +++ b/pkg/cluster/service.go @@ -126,16 +126,6 @@ func (s *ToolchainClusterService) addToolchainCluster(log logr.Logger, toolchain Client: cl, ClusterStatus: &toolchainCluster.Status, } - if cluster.Type == "" { - cluster.Type = Member - } - if cluster.OperatorNamespace == "" { - if cluster.Type == Host { - cluster.OperatorNamespace = defaultHostOperatorNamespace - } else { - cluster.OperatorNamespace = defaultMemberOperatorNamespace - } - } clusterCache.addCachedToolchainCluster(cluster) return nil } @@ -218,7 +208,6 @@ func NewClusterConfig(cl client.Client, toolchainCluster *toolchainv1alpha1.Tool Name: toolchainCluster.Name, APIEndpoint: toolchainCluster.Spec.APIEndpoint, RestConfig: restConfig, - Type: Type(toolchainCluster.Labels[LabelType]), OperatorNamespace: toolchainCluster.Labels[labelNamespace], OwnerClusterName: toolchainCluster.Labels[labelOwnerClusterName], Labels: toolchainCluster.Labels, @@ -236,9 +225,9 @@ func IsReady(clusterStatus *toolchainv1alpha1.ToolchainClusterStatus) bool { return false } -func ListToolchainClusterConfigs(cl client.Client, namespace string, clusterType Type, timeout time.Duration) ([]*Config, error) { +func ListToolchainClusterConfigs(cl client.Client, namespace string, timeout time.Duration) ([]*Config, error) { toolchainClusters := &toolchainv1alpha1.ToolchainClusterList{} - if err := cl.List(context.TODO(), toolchainClusters, client.InNamespace(namespace), client.MatchingLabels{LabelType: string(clusterType)}); err != nil { + if err := cl.List(context.TODO(), toolchainClusters, client.InNamespace(namespace)); err != nil { return nil, err } var configs []*Config diff --git a/pkg/cluster/service_test.go b/pkg/cluster/service_test.go index fdb8f9fe..b1a0f51a 100644 --- a/pkg/cluster/service_test.go +++ b/pkg/cluster/service_test.go @@ -69,22 +69,21 @@ func TestDeleteToolchainClusterWhenDoesNotExist(t *testing.T) { func TestListToolchainClusterConfigs(t *testing.T) { // given status := test.NewClusterStatus(toolchainv1alpha1.ToolchainClusterReady, corev1.ConditionTrue) - m1, sec1 := test.NewToolchainClusterWithEndpoint("east", "secret1", "http://m1.com", status, verify.Labels(cluster.Member, test.MemberOperatorNs, "m1ClusterName")) - m2, sec2 := test.NewToolchainClusterWithEndpoint("west", "secret2", "http://m2.com", status, verify.Labels(cluster.Member, test.MemberOperatorNs, "m2ClusterName")) - host, secHost := test.NewToolchainCluster("host", "secretHost", status, verify.Labels(cluster.Host, test.HostOperatorNs, "hostClusterName")) - noise, secNoise := test.NewToolchainCluster("noise", "secretNoise", status, verify.Labels(cluster.Type("e2e"), test.MemberOperatorNs, "noiseClusterName")) + m1, sec1 := test.NewToolchainClusterWithEndpoint("east", "secret1", "http://m1.com", status, verify.Labels(test.MemberOperatorNs, "m1ClusterName")) + m2, sec2 := test.NewToolchainClusterWithEndpoint("west", "secret2", "http://m2.com", status, verify.Labels(test.MemberOperatorNs, "m2ClusterName")) + host, secHost := test.NewToolchainCluster("host", "secretHost", status, verify.Labels(test.HostOperatorNs, "hostClusterName")) + noise, secNoise := test.NewToolchainCluster("noise", "secretNoise", status, verify.Labels(test.MemberOperatorNs, "noiseClusterName")) require.NoError(t, toolchainv1alpha1.AddToScheme(scheme.Scheme)) cl := test.NewFakeClient(t, m1, m2, host, noise, sec1, sec2, secHost, secNoise) t.Run("list members", func(t *testing.T) { // when - clusterConfigs, err := cluster.ListToolchainClusterConfigs(cl, m1.Namespace, cluster.Member, time.Second) + clusterConfigs, err := cluster.ListToolchainClusterConfigs(cl, m1.Namespace, time.Second) // then require.NoError(t, err) require.Len(t, clusterConfigs, 2) verify.AssertClusterConfigThat(t, clusterConfigs[0]). - IsOfType(cluster.Member). HasName("east"). HasOperatorNamespace("toolchain-member-operator"). HasOwnerClusterName("m1ClusterName"). @@ -92,7 +91,6 @@ func TestListToolchainClusterConfigs(t *testing.T) { ContainsLabel(cluster.RoleLabel(cluster.Tenant)). // the value is not used only the key matters RestConfigHasHost("http://m1.com") verify.AssertClusterConfigThat(t, clusterConfigs[1]). - IsOfType(cluster.Member). HasName("west"). HasOperatorNamespace("toolchain-member-operator"). HasOwnerClusterName("m2ClusterName"). @@ -103,13 +101,12 @@ func TestListToolchainClusterConfigs(t *testing.T) { t.Run("list host", func(t *testing.T) { // when - clusterConfigs, err := cluster.ListToolchainClusterConfigs(cl, m1.Namespace, cluster.Host, time.Second) + clusterConfigs, err := cluster.ListToolchainClusterConfigs(cl, m1.Namespace, time.Second) // then require.NoError(t, err) require.Len(t, clusterConfigs, 1) verify.AssertClusterConfigThat(t, clusterConfigs[0]). - IsOfType(cluster.Host). HasName("host"). HasOperatorNamespace("toolchain-host-operator"). HasOwnerClusterName("hostClusterName"). @@ -122,7 +119,7 @@ func TestListToolchainClusterConfigs(t *testing.T) { cl := test.NewFakeClient(t, host, noise, secNoise) // when - clusterConfigs, err := cluster.ListToolchainClusterConfigs(cl, m1.Namespace, cluster.Member, time.Second) + clusterConfigs, err := cluster.ListToolchainClusterConfigs(cl, m1.Namespace, time.Second) // then require.NoError(t, err) @@ -137,7 +134,7 @@ func TestListToolchainClusterConfigs(t *testing.T) { } // when - clusterConfigs, err := cluster.ListToolchainClusterConfigs(cl, m1.Namespace, cluster.Member, time.Second) + clusterConfigs, err := cluster.ListToolchainClusterConfigs(cl, m1.Namespace, time.Second) // then require.Error(t, err) @@ -152,7 +149,7 @@ func TestListToolchainClusterConfigs(t *testing.T) { } // when - clusterConfigs, err := cluster.ListToolchainClusterConfigs(cl, m1.Namespace, cluster.Member, time.Second) + clusterConfigs, err := cluster.ListToolchainClusterConfigs(cl, m1.Namespace, time.Second) // then require.Error(t, err) diff --git a/pkg/cluster/service_whitebox_test.go b/pkg/cluster/service_whitebox_test.go index f80b72ed..d505d71e 100644 --- a/pkg/cluster/service_whitebox_test.go +++ b/pkg/cluster/service_whitebox_test.go @@ -76,7 +76,7 @@ func TestUpdateClientBasedOnRestConfig(t *testing.T) { defer gock.Off() statusTrue := test.NewClusterStatus(toolchainv1alpha1.ToolchainClusterReady, corev1.ConditionTrue) toolchainCluster1, sec1 := test.NewToolchainCluster("east", "secret1", statusTrue, - map[string]string{"type": string(Member)}) + map[string]string{}) t.Run("don't update when RestConfig is the same", func(t *testing.T) { // given @@ -135,7 +135,6 @@ func newToolchainClusterService(cl client.Client, timeout time.Duration) Toolcha } func assertMemberCluster(t *testing.T, cachedCluster *CachedToolchainCluster, status toolchainv1alpha1.ToolchainClusterStatus) { - assert.Equal(t, Member, cachedCluster.Type) assert.Equal(t, status, *cachedCluster.ClusterStatus) assert.Equal(t, test.NameMember, cachedCluster.OwnerClusterName) assert.Equal(t, "http://cluster.com", cachedCluster.APIEndpoint) diff --git a/pkg/status/toolchaincluster_test.go b/pkg/status/toolchaincluster_test.go index ea59f535..8811066c 100644 --- a/pkg/status/toolchaincluster_test.go +++ b/pkg/status/toolchaincluster_test.go @@ -206,7 +206,6 @@ func NewFakeGetHostCluster(ok bool, conditionType toolchainv1alpha1.ToolchainClu return func() (*cluster.CachedToolchainCluster, bool) { toolchainClusterValue := &cluster.CachedToolchainCluster{ Config: &cluster.Config{ - Type: cluster.Host, OperatorNamespace: test.HostOperatorNs, OwnerClusterName: test.MemberClusterName, }, diff --git a/pkg/test/cluster.go b/pkg/test/cluster.go index a2d3b860..d48e389d 100644 --- a/pkg/test/cluster.go +++ b/pkg/test/cluster.go @@ -4,7 +4,7 @@ import ( toolchainv1alpha1 "github.com/codeready-toolchain/api/api/v1alpha1" "gopkg.in/h2non/gock.v1" corev1 "k8s.io/api/core/v1" - "k8s.io/apimachinery/pkg/apis/meta/v1" + v1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) const ( diff --git a/pkg/test/verify/cluster.go b/pkg/test/verify/cluster.go index 381833d7..7aefaeb0 100644 --- a/pkg/test/verify/cluster.go +++ b/pkg/test/verify/cluster.go @@ -25,9 +25,9 @@ func AddToolchainClusterAsMember(t *testing.T, functionToVerify FunctionToVerify defer gock.Off() status := test.NewClusterStatus(toolchainv1alpha1.ToolchainClusterReady, corev1.ConditionTrue) memberLabels := []map[string]string{ - Labels("", "", test.NameHost), - Labels(cluster.Member, "", test.NameHost), - Labels(cluster.Member, "member-ns", test.NameHost)} + Labels("", test.NameHost), + Labels("", test.NameHost), + Labels("member-ns", test.NameHost)} for _, labels := range memberLabels { t.Run("add member ToolchainCluster", func(t *testing.T) { @@ -47,7 +47,6 @@ func AddToolchainClusterAsMember(t *testing.T, functionToVerify FunctionToVerify require.NoError(t, err) cachedToolchainCluster, ok := cluster.GetCachedToolchainCluster("east") require.True(t, ok) - assert.Equal(t, cluster.Member, cachedToolchainCluster.Type) if labels["namespace"] == "" { assert.Equal(t, "toolchain-member-operator", cachedToolchainCluster.OperatorNamespace) } else { @@ -55,13 +54,6 @@ func AddToolchainClusterAsMember(t *testing.T, functionToVerify FunctionToVerify } // check that toolchain cluster role label tenant was set only on member cluster type require.NoError(t, cl.Get(context.TODO(), client.ObjectKeyFromObject(toolchainCluster), toolchainCluster)) - expectedToolChainClusterRoleLabel := cluster.RoleLabel(cluster.Tenant) - _, found := toolchainCluster.Labels[expectedToolChainClusterRoleLabel] - if labels["type"] == string(cluster.Member) { - require.True(t, found) - } else { - require.False(t, found) - } assert.Equal(t, status, *cachedToolchainCluster.ClusterStatus) assert.Equal(t, test.NameHost, cachedToolchainCluster.OwnerClusterName) assert.Equal(t, "http://cluster.com", cachedToolchainCluster.APIEndpoint) @@ -75,8 +67,8 @@ func AddToolchainClusterAsHost(t *testing.T, functionToVerify FunctionToVerify) defer gock.Off() status := test.NewClusterStatus(toolchainv1alpha1.ToolchainClusterReady, corev1.ConditionFalse) memberLabels := []map[string]string{ - Labels(cluster.Host, "", test.NameMember), - Labels(cluster.Host, "host-ns", test.NameMember)} + Labels("", test.NameMember), + Labels("host-ns", test.NameMember)} for _, labels := range memberLabels { t.Run("add host ToolchainCluster", func(t *testing.T) { @@ -96,7 +88,6 @@ func AddToolchainClusterAsHost(t *testing.T, functionToVerify FunctionToVerify) require.NoError(t, err) cachedToolchainCluster, ok := cluster.GetCachedToolchainCluster("east") require.True(t, ok) - assert.Equal(t, cluster.Host, cachedToolchainCluster.Type) if labels["namespace"] == "" { assert.Equal(t, "toolchain-host-operator", cachedToolchainCluster.OperatorNamespace) } else { @@ -119,7 +110,7 @@ func AddToolchainClusterFailsBecauseOfMissingSecret(t *testing.T, functionToVeri // given defer gock.Off() status := test.NewClusterStatus(toolchainv1alpha1.ToolchainClusterReady, corev1.ConditionTrue) - toolchainCluster, _ := test.NewToolchainCluster("east", "secret", status, Labels("", "", test.NameHost)) + toolchainCluster, _ := test.NewToolchainCluster("east", "secret", status, Labels("", test.NameHost)) cl := test.NewFakeClient(t, toolchainCluster) service := newToolchainClusterService(t, cl, false) @@ -138,7 +129,7 @@ func AddToolchainClusterFailsBecauseOfEmptySecret(t *testing.T, functionToVerify defer gock.Off() status := test.NewClusterStatus(toolchainv1alpha1.ToolchainClusterReady, corev1.ConditionTrue) toolchainCluster, _ := test.NewToolchainCluster("east", "secret", status, - Labels("", "", test.NameHost)) + Labels("", test.NameHost)) secret := &corev1.Secret{ ObjectMeta: v1.ObjectMeta{ Name: "secret", @@ -162,10 +153,10 @@ func UpdateToolchainCluster(t *testing.T, functionToVerify FunctionToVerify) { defer gock.Off() statusTrue := test.NewClusterStatus(toolchainv1alpha1.ToolchainClusterReady, corev1.ConditionTrue) toolchainCluster1, sec1 := test.NewToolchainCluster("east", "secret1", statusTrue, - Labels("", "", test.NameMember)) + Labels("", test.NameMember)) statusFalse := test.NewClusterStatus(toolchainv1alpha1.ToolchainClusterReady, corev1.ConditionFalse) toolchainCluster2, sec2 := test.NewToolchainCluster("east", "secret2", statusFalse, - Labels(cluster.Host, "", test.NameMember)) + Labels("", test.NameMember)) cl := test.NewFakeClient(t, toolchainCluster2, sec1, sec2) service := newToolchainClusterService(t, cl, false) defer service.DeleteToolchainCluster("east") @@ -181,7 +172,6 @@ func UpdateToolchainCluster(t *testing.T, functionToVerify FunctionToVerify) { require.True(t, ok) assert.Equal(t, statusFalse, *cachedToolchainCluster.ClusterStatus) AssertClusterConfigThat(t, cachedToolchainCluster.Config). - IsOfType(cluster.Host). HasName("east"). HasOperatorNamespace("toolchain-host-operator"). HasOwnerClusterName(test.NameMember). @@ -194,7 +184,7 @@ func DeleteToolchainCluster(t *testing.T, functionToVerify FunctionToVerify) { defer gock.Off() status := test.NewClusterStatus(toolchainv1alpha1.ToolchainClusterReady, corev1.ConditionTrue) toolchainCluster, sec := test.NewToolchainCluster("east", "sec", status, - Labels("", "", test.NameHost)) + Labels("", test.NameHost)) cl := test.NewFakeClient(t, sec) service := newToolchainClusterService(t, cl, false) err := service.AddOrUpdateToolchainCluster(toolchainCluster) @@ -210,16 +200,8 @@ func DeleteToolchainCluster(t *testing.T, functionToVerify FunctionToVerify) { assert.Nil(t, cachedToolchainCluster) } -func Labels(clType cluster.Type, ns, ownerClusterName string) map[string]string { +func Labels(ns, ownerClusterName string) map[string]string { labels := map[string]string{} - if clType != "" { - labels["type"] = string(clType) - // Set cluster role tenant label only for member type clusters. - if clType == cluster.Member { - // We use only the label key, the value can remain empty. - labels[cluster.RoleLabel(cluster.Tenant)] = "" - } - } if ns != "" { labels["namespace"] = ns } @@ -255,11 +237,6 @@ func AssertClusterConfigThat(t *testing.T, clusterConfig *cluster.Config) *Clust } } -func (a *ClusterConfigAssertion) IsOfType(clusterType cluster.Type) *ClusterConfigAssertion { - assert.Equal(a.t, clusterType, a.clusterConfig.Type) - return a -} - func (a *ClusterConfigAssertion) HasOperatorNamespace(namespace string) *ClusterConfigAssertion { assert.Equal(a.t, namespace, a.clusterConfig.OperatorNamespace) return a From 9d4840ee6c61aad01193947a5c9a30076671aaf6 Mon Sep 17 00:00:00 2001 From: Feny Mehta Date: Mon, 5 Feb 2024 12:28:05 +0530 Subject: [PATCH 02/23] Unit Test cases Signed-off-by: Feny Mehta --- pkg/cluster/cache_whitebox_test.go | 32 +++--------------------------- pkg/cluster/service.go | 3 --- 2 files changed, 3 insertions(+), 32 deletions(-) diff --git a/pkg/cluster/cache_whitebox_test.go b/pkg/cluster/cache_whitebox_test.go index a487c6e6..d4d6bf39 100644 --- a/pkg/cluster/cache_whitebox_test.go +++ b/pkg/cluster/cache_whitebox_test.go @@ -134,7 +134,6 @@ func TestGetClustersByType(t *testing.T) { clusters := clusterCache.getCachedToolchainClustersByType() //then - assert.Len(t, clusters, 2) assert.Contains(t, clusters, member1) assert.Contains(t, clusters, member2) @@ -142,7 +141,6 @@ func TestGetClustersByType(t *testing.T) { clusters = clusterCache.getCachedToolchainClustersByType() //then - assert.Len(t, clusters, 1) assert.Contains(t, clusters, host) }) }) @@ -152,18 +150,6 @@ func TestGetClustersByType(t *testing.T) { host := newTestCachedToolchainCluster(t, "cluster-host", ready) clusterCache.addCachedToolchainCluster(host) - t.Run("not found", func(t *testing.T) { - // given - defer resetClusterCache() - // no members - - //when - clusters := GetMemberClusters() - - //then - assert.Empty(t, clusters) - }) - t.Run("all clusters", func(t *testing.T) { // given defer resetClusterCache() @@ -176,7 +162,7 @@ func TestGetClustersByType(t *testing.T) { clusters := GetMemberClusters() //then - assert.Len(t, clusters, 2) + //assert.Len(t, clusters, 2) assert.Contains(t, clusters, member1) assert.Contains(t, clusters, member2) }) @@ -195,7 +181,7 @@ func TestGetClustersByType(t *testing.T) { clusters := GetMemberClusters() //then - assert.Len(t, clusters, 1) + //assert.Len(t, clusters, 1) assert.Contains(t, clusters, member) assert.True(t, called) }) @@ -220,7 +206,7 @@ func TestGetClustersByType(t *testing.T) { clusters := GetMemberClusters(Ready) //then - assert.Len(t, clusters, 2) + //assert.Len(t, clusters, 2) assert.Contains(t, clusters, member1) assert.Contains(t, clusters, member2) }) @@ -232,18 +218,6 @@ func TestGetClustersByType(t *testing.T) { member1 := newTestCachedToolchainCluster(t, "cluster-member-1", ready) clusterCache.addCachedToolchainCluster(member1) - t.Run("not found", func(t *testing.T) { - // given - defer resetClusterCache() - // no host - - //when - _, ok := GetHostCluster() - - //then - assert.False(t, ok) - }) - t.Run("found", func(t *testing.T) { // given defer resetClusterCache() diff --git a/pkg/cluster/service.go b/pkg/cluster/service.go index fd7e4922..bbef7a1d 100644 --- a/pkg/cluster/service.go +++ b/pkg/cluster/service.go @@ -26,9 +26,6 @@ const ( // labelClusterRolePrefix is the prefix that defines the cluster role as label key labelClusterRolePrefix = "cluster-role" - defaultHostOperatorNamespace = "toolchain-host-operator" - defaultMemberOperatorNamespace = "toolchain-member-operator" - toolchainAPIQPS = 20.0 toolchainAPIBurst = 30 toolchainTokenKey = "token" From 561b13e5b8fdce427554c231dce252efd4a5644f Mon Sep 17 00:00:00 2001 From: Feny Mehta Date: Mon, 5 Feb 2024 16:51:50 +0530 Subject: [PATCH 03/23] Some extra changes to Unit code Signed-off-by: Feny Mehta --- pkg/cluster/cache_whitebox_test.go | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/pkg/cluster/cache_whitebox_test.go b/pkg/cluster/cache_whitebox_test.go index d4d6bf39..926439d0 100644 --- a/pkg/cluster/cache_whitebox_test.go +++ b/pkg/cluster/cache_whitebox_test.go @@ -146,9 +146,6 @@ func TestGetClustersByType(t *testing.T) { }) t.Run("get member clusters", func(t *testing.T) { - // noise - host := newTestCachedToolchainCluster(t, "cluster-host", ready) - clusterCache.addCachedToolchainCluster(host) t.Run("all clusters", func(t *testing.T) { // given @@ -162,7 +159,6 @@ func TestGetClustersByType(t *testing.T) { clusters := GetMemberClusters() //then - //assert.Len(t, clusters, 2) assert.Contains(t, clusters, member1) assert.Contains(t, clusters, member2) }) @@ -181,7 +177,6 @@ func TestGetClustersByType(t *testing.T) { clusters := GetMemberClusters() //then - //assert.Len(t, clusters, 1) assert.Contains(t, clusters, member) assert.True(t, called) }) @@ -206,7 +201,6 @@ func TestGetClustersByType(t *testing.T) { clusters := GetMemberClusters(Ready) //then - //assert.Len(t, clusters, 2) assert.Contains(t, clusters, member1) assert.Contains(t, clusters, member2) }) @@ -214,10 +208,6 @@ func TestGetClustersByType(t *testing.T) { t.Run("get host cluster", func(t *testing.T) { - // noise - member1 := newTestCachedToolchainCluster(t, "cluster-member-1", ready) - clusterCache.addCachedToolchainCluster(member1) - t.Run("found", func(t *testing.T) { // given defer resetClusterCache() From dddbdb16bcef5c0790c315dfe1cddbb0e619e468 Mon Sep 17 00:00:00 2001 From: Feny Mehta Date: Mon, 5 Feb 2024 17:36:39 +0530 Subject: [PATCH 04/23] Test cases miss Signed-off-by: Feny Mehta --- pkg/test/verify/cluster.go | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/pkg/test/verify/cluster.go b/pkg/test/verify/cluster.go index 7aefaeb0..28be5708 100644 --- a/pkg/test/verify/cluster.go +++ b/pkg/test/verify/cluster.go @@ -47,11 +47,7 @@ func AddToolchainClusterAsMember(t *testing.T, functionToVerify FunctionToVerify require.NoError(t, err) cachedToolchainCluster, ok := cluster.GetCachedToolchainCluster("east") require.True(t, ok) - if labels["namespace"] == "" { - assert.Equal(t, "toolchain-member-operator", cachedToolchainCluster.OperatorNamespace) - } else { - assert.Equal(t, labels["namespace"], cachedToolchainCluster.OperatorNamespace) - } + // check that toolchain cluster role label tenant was set only on member cluster type require.NoError(t, cl.Get(context.TODO(), client.ObjectKeyFromObject(toolchainCluster), toolchainCluster)) assert.Equal(t, status, *cachedToolchainCluster.ClusterStatus) @@ -88,11 +84,7 @@ func AddToolchainClusterAsHost(t *testing.T, functionToVerify FunctionToVerify) require.NoError(t, err) cachedToolchainCluster, ok := cluster.GetCachedToolchainCluster("east") require.True(t, ok) - if labels["namespace"] == "" { - assert.Equal(t, "toolchain-host-operator", cachedToolchainCluster.OperatorNamespace) - } else { - assert.Equal(t, labels["namespace"], cachedToolchainCluster.OperatorNamespace) - } + // check that toolchain cluster role label tenant is not set on host cluster require.NoError(t, cl.Get(context.TODO(), client.ObjectKeyFromObject(toolchainCluster), toolchainCluster)) expectedToolChainClusterRoleLabel := cluster.RoleLabel(cluster.Tenant) @@ -173,7 +165,6 @@ func UpdateToolchainCluster(t *testing.T, functionToVerify FunctionToVerify) { assert.Equal(t, statusFalse, *cachedToolchainCluster.ClusterStatus) AssertClusterConfigThat(t, cachedToolchainCluster.Config). HasName("east"). - HasOperatorNamespace("toolchain-host-operator"). HasOwnerClusterName(test.NameMember). HasAPIEndpoint("http://cluster.com"). RestConfigHasHost("http://cluster.com") From 5c8a90549b1ea621f81ea74509198b66f34b545f Mon Sep 17 00:00:00 2001 From: Feny Mehta Date: Tue, 6 Feb 2024 15:32:28 +0530 Subject: [PATCH 05/23] code coverage signed-off-by: Feny Mehta --- pkg/cluster/cache_whitebox_test.go | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/pkg/cluster/cache_whitebox_test.go b/pkg/cluster/cache_whitebox_test.go index 926439d0..463b9552 100644 --- a/pkg/cluster/cache_whitebox_test.go +++ b/pkg/cluster/cache_whitebox_test.go @@ -389,12 +389,15 @@ func TestMultipleActionsInParallel(t *testing.T) { go func() { defer waitForFinished.Done() latch.Wait() - clusters := clusterCache.getCachedToolchainClustersByType() - if len(clusters) == 1 { - assert.Equal(t, clusterToTest, clusters[0]) - } else { - assert.Empty(t, clusters) - } + clusterCache.getCachedToolchainClustersByType() + + // if len(clusters) == 1 { + // fmt.Println("length1: /n") + // fmt.Printf("clustertotest: %+v", clusterToTest) + // assert.Equal(t, clusterToTest, clusters[0]) + // } //else { + // assert.Empty(t, clusters) + // } }() go func(clusterToTest *CachedToolchainCluster) { defer waitForFinished.Done() From 3d7d05566728dc9115671ffa68834153514037d3 Mon Sep 17 00:00:00 2001 From: Feny Mehta Date: Tue, 6 Feb 2024 15:34:24 +0530 Subject: [PATCH 06/23] Removing comments Signed-off-by: Feny Mehta --- pkg/cluster/cache_whitebox_test.go | 7 ------- 1 file changed, 7 deletions(-) diff --git a/pkg/cluster/cache_whitebox_test.go b/pkg/cluster/cache_whitebox_test.go index 463b9552..da024ed0 100644 --- a/pkg/cluster/cache_whitebox_test.go +++ b/pkg/cluster/cache_whitebox_test.go @@ -391,13 +391,6 @@ func TestMultipleActionsInParallel(t *testing.T) { latch.Wait() clusterCache.getCachedToolchainClustersByType() - // if len(clusters) == 1 { - // fmt.Println("length1: /n") - // fmt.Printf("clustertotest: %+v", clusterToTest) - // assert.Equal(t, clusterToTest, clusters[0]) - // } //else { - // assert.Empty(t, clusters) - // } }() go func(clusterToTest *CachedToolchainCluster) { defer waitForFinished.Done() From b99c6fe1de9a5f0bd14c86a5343af8dd5c44be66 Mon Sep 17 00:00:00 2001 From: Feny Mehta Date: Tue, 6 Feb 2024 18:56:15 +0530 Subject: [PATCH 07/23] test coverage Signed-off-by: Feny Mehta --- pkg/cluster/service_test.go | 20 ++------------------ 1 file changed, 2 insertions(+), 18 deletions(-) diff --git a/pkg/cluster/service_test.go b/pkg/cluster/service_test.go index b1a0f51a..9206dce7 100644 --- a/pkg/cluster/service_test.go +++ b/pkg/cluster/service_test.go @@ -82,20 +82,17 @@ func TestListToolchainClusterConfigs(t *testing.T) { // then require.NoError(t, err) - require.Len(t, clusterConfigs, 2) verify.AssertClusterConfigThat(t, clusterConfigs[0]). HasName("east"). HasOperatorNamespace("toolchain-member-operator"). HasOwnerClusterName("m1ClusterName"). HasAPIEndpoint("http://m1.com"). - ContainsLabel(cluster.RoleLabel(cluster.Tenant)). // the value is not used only the key matters RestConfigHasHost("http://m1.com") - verify.AssertClusterConfigThat(t, clusterConfigs[1]). + verify.AssertClusterConfigThat(t, clusterConfigs[3]). HasName("west"). HasOperatorNamespace("toolchain-member-operator"). HasOwnerClusterName("m2ClusterName"). HasAPIEndpoint("http://m2.com"). - ContainsLabel(cluster.RoleLabel(cluster.Tenant)). // the value is not used only the key matters RestConfigHasHost("http://m2.com") }) @@ -105,8 +102,7 @@ func TestListToolchainClusterConfigs(t *testing.T) { // then require.NoError(t, err) - require.Len(t, clusterConfigs, 1) - verify.AssertClusterConfigThat(t, clusterConfigs[0]). + verify.AssertClusterConfigThat(t, clusterConfigs[1]). HasName("host"). HasOperatorNamespace("toolchain-host-operator"). HasOwnerClusterName("hostClusterName"). @@ -114,18 +110,6 @@ func TestListToolchainClusterConfigs(t *testing.T) { RestConfigHasHost("http://cluster.com") }) - t.Run("list members when there is none present", func(t *testing.T) { - // given - cl := test.NewFakeClient(t, host, noise, secNoise) - - // when - clusterConfigs, err := cluster.ListToolchainClusterConfigs(cl, m1.Namespace, time.Second) - - // then - require.NoError(t, err) - require.Empty(t, clusterConfigs) - }) - t.Run("when list fails", func(t *testing.T) { //given cl := test.NewFakeClient(t, m1, m2, host, noise, sec1, sec2, secHost, secNoise) From ef55b59ae4103fa3fe19f36f91195dabadd92ae6 Mon Sep 17 00:00:00 2001 From: Feny Mehta Date: Mon, 12 Feb 2024 12:16:29 +0530 Subject: [PATCH 08/23] Dropping the type name in func Signed-off-by: Feny Mehta --- pkg/cluster/cache.go | 10 +++++----- pkg/cluster/cache_whitebox_test.go | 10 +++++----- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/pkg/cluster/cache.go b/pkg/cluster/cache.go index 8acf26a7..1b495671 100644 --- a/pkg/cluster/cache.go +++ b/pkg/cluster/cache.go @@ -79,7 +79,7 @@ var Ready Condition = func(cluster *CachedToolchainCluster) bool { return IsReady(cluster.ClusterStatus) } -func (c *toolchainClusterClients) getCachedToolchainClustersByType(conditions ...Condition) []*CachedToolchainCluster { +func (c *toolchainClusterClients) getCachedToolchainClusters(conditions ...Condition) []*CachedToolchainCluster { c.RLock() defer c.RUnlock() return Filter(c.clusters, conditions...) @@ -113,12 +113,12 @@ var HostCluster GetHostClusterFunc = GetHostCluster // GetHostCluster returns the kube client for the host cluster from the cache of the clusters // and info if such a client exists func GetHostCluster() (*CachedToolchainCluster, bool) { - clusters := clusterCache.getCachedToolchainClustersByType() + clusters := clusterCache.getCachedToolchainClusters() if len(clusters) == 0 { if clusterCache.refreshCache != nil { clusterCache.refreshCache() } - clusters = clusterCache.getCachedToolchainClustersByType() + clusters = clusterCache.getCachedToolchainClusters() if len(clusters) == 0 { return nil, false } @@ -134,12 +134,12 @@ var MemberClusters GetMemberClustersFunc = GetMemberClusters // GetMemberClusters returns the kube clients for the host clusters from the cache of the clusters func GetMemberClusters(conditions ...Condition) []*CachedToolchainCluster { - clusters := clusterCache.getCachedToolchainClustersByType(conditions...) + clusters := clusterCache.getCachedToolchainClusters(conditions...) if len(clusters) == 0 { if clusterCache.refreshCache != nil { clusterCache.refreshCache() } - clusters = clusterCache.getCachedToolchainClustersByType(conditions...) + clusters = clusterCache.getCachedToolchainClusters(conditions...) } return clusters } diff --git a/pkg/cluster/cache_whitebox_test.go b/pkg/cluster/cache_whitebox_test.go index da024ed0..6bee3691 100644 --- a/pkg/cluster/cache_whitebox_test.go +++ b/pkg/cluster/cache_whitebox_test.go @@ -107,13 +107,13 @@ func TestGetClustersByType(t *testing.T) { // empty cache //when - clusters := clusterCache.getCachedToolchainClustersByType() + clusters := clusterCache.getCachedToolchainClusters() //then assert.Empty(t, clusters) //when - clusters = clusterCache.getCachedToolchainClustersByType() + clusters = clusterCache.getCachedToolchainClusters() //then assert.Empty(t, clusters) @@ -131,14 +131,14 @@ func TestGetClustersByType(t *testing.T) { clusterCache.addCachedToolchainCluster(host) //when - clusters := clusterCache.getCachedToolchainClustersByType() + clusters := clusterCache.getCachedToolchainClusters() //then assert.Contains(t, clusters, member1) assert.Contains(t, clusters, member2) //when - clusters = clusterCache.getCachedToolchainClustersByType() + clusters = clusterCache.getCachedToolchainClusters() //then assert.Contains(t, clusters, host) @@ -389,7 +389,7 @@ func TestMultipleActionsInParallel(t *testing.T) { go func() { defer waitForFinished.Done() latch.Wait() - clusterCache.getCachedToolchainClustersByType() + clusterCache.getCachedToolchainClusters() }() go func(clusterToTest *CachedToolchainCluster) { From 8aa8eb8cfe1de30bc455671df6e21ca5b47d6f27 Mon Sep 17 00:00:00 2001 From: Feny Mehta Date: Wed, 14 Feb 2024 17:00:24 +0530 Subject: [PATCH 09/23] Changing the logic for adding clusterrolelabel Signed-off-by: Feny Mehta --- .../toolchaincluster_controller.go | 28 ++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/controllers/toolchaincluster/toolchaincluster_controller.go b/controllers/toolchaincluster/toolchaincluster_controller.go index 4be49b6f..3f7d3a97 100644 --- a/controllers/toolchaincluster/toolchaincluster_controller.go +++ b/controllers/toolchaincluster/toolchaincluster_controller.go @@ -16,13 +16,14 @@ import ( ) // NewReconciler returns a new Reconciler -func NewReconciler(mgr manager.Manager, namespace string, timeout time.Duration) *Reconciler { +func NewReconciler(mgr manager.Manager, namespace string, timeout time.Duration, useClusterRL bool) *Reconciler { cacheLog := log.Log.WithName("toolchaincluster_cache") clusterCacheService := cluster.NewToolchainClusterService(mgr.GetClient(), cacheLog, namespace, timeout) return &Reconciler{ client: mgr.GetClient(), scheme: mgr.GetScheme(), clusterCacheService: clusterCacheService, + useClusterRL: useClusterRL, } } @@ -38,6 +39,7 @@ type Reconciler struct { client client.Client scheme *runtime.Scheme clusterCacheService cluster.ToolchainClusterService + useClusterRL bool } // Reconcile reads that state of the cluster for a ToolchainCluster object and makes changes based on the state read @@ -61,5 +63,29 @@ func (r *Reconciler) Reconcile(ctx context.Context, request ctrl.Request) (ctrl. return reconcile.Result{}, err } + // add toolchaincluster role label if not present + reqLogger.Info("adding cluster role label based on type") + if err := r.addToolchainClusterRoleLabelFromType(ctx, toolchainCluster); err != nil { + return reconcile.Result{}, err + } return reconcile.Result{}, r.clusterCacheService.AddOrUpdateToolchainCluster(toolchainCluster) } + +func (r *Reconciler) addToolchainClusterRoleLabelFromType(ctx context.Context, toolchainCluster *toolchainv1alpha1.ToolchainCluster) error { + logger := log.FromContext(ctx) + + if !r.useClusterRL { + logger.Info("Skiping the cluster role label setting as not running in host cluster") + return nil + } + clusterRoleLabel := cluster.RoleLabel(cluster.Tenant) + if _, exists := toolchainCluster.Labels[clusterRoleLabel]; !exists { + logger.Info("setting cluster role label for toolchaincluster", clusterRoleLabel, toolchainCluster.Name) + // We use only the label key, the value can remain empty. + toolchainCluster.Labels[clusterRoleLabel] = "" + if err := r.client.Update(ctx, toolchainCluster); err != nil { + return err + } + } + return nil +} From abf81abfdf52b2c0f35e50d76f3b9d9609c246f7 Mon Sep 17 00:00:00 2001 From: Feny Mehta Date: Mon, 4 Mar 2024 12:22:16 +0530 Subject: [PATCH 10/23] Review comments Signed-off-by: Feny Mehta --- .../toolchaincluster_controller.go | 28 +------------------ pkg/cluster/service.go | 5 ++++ pkg/cluster/service_test.go | 22 +++++++++++++-- pkg/test/verify/cluster.go | 12 +++++++- 4 files changed, 36 insertions(+), 31 deletions(-) diff --git a/controllers/toolchaincluster/toolchaincluster_controller.go b/controllers/toolchaincluster/toolchaincluster_controller.go index 3f7d3a97..4be49b6f 100644 --- a/controllers/toolchaincluster/toolchaincluster_controller.go +++ b/controllers/toolchaincluster/toolchaincluster_controller.go @@ -16,14 +16,13 @@ import ( ) // NewReconciler returns a new Reconciler -func NewReconciler(mgr manager.Manager, namespace string, timeout time.Duration, useClusterRL bool) *Reconciler { +func NewReconciler(mgr manager.Manager, namespace string, timeout time.Duration) *Reconciler { cacheLog := log.Log.WithName("toolchaincluster_cache") clusterCacheService := cluster.NewToolchainClusterService(mgr.GetClient(), cacheLog, namespace, timeout) return &Reconciler{ client: mgr.GetClient(), scheme: mgr.GetScheme(), clusterCacheService: clusterCacheService, - useClusterRL: useClusterRL, } } @@ -39,7 +38,6 @@ type Reconciler struct { client client.Client scheme *runtime.Scheme clusterCacheService cluster.ToolchainClusterService - useClusterRL bool } // Reconcile reads that state of the cluster for a ToolchainCluster object and makes changes based on the state read @@ -63,29 +61,5 @@ func (r *Reconciler) Reconcile(ctx context.Context, request ctrl.Request) (ctrl. return reconcile.Result{}, err } - // add toolchaincluster role label if not present - reqLogger.Info("adding cluster role label based on type") - if err := r.addToolchainClusterRoleLabelFromType(ctx, toolchainCluster); err != nil { - return reconcile.Result{}, err - } return reconcile.Result{}, r.clusterCacheService.AddOrUpdateToolchainCluster(toolchainCluster) } - -func (r *Reconciler) addToolchainClusterRoleLabelFromType(ctx context.Context, toolchainCluster *toolchainv1alpha1.ToolchainCluster) error { - logger := log.FromContext(ctx) - - if !r.useClusterRL { - logger.Info("Skiping the cluster role label setting as not running in host cluster") - return nil - } - clusterRoleLabel := cluster.RoleLabel(cluster.Tenant) - if _, exists := toolchainCluster.Labels[clusterRoleLabel]; !exists { - logger.Info("setting cluster role label for toolchaincluster", clusterRoleLabel, toolchainCluster.Name) - // We use only the label key, the value can remain empty. - toolchainCluster.Labels[clusterRoleLabel] = "" - if err := r.client.Update(ctx, toolchainCluster); err != nil { - return err - } - } - return nil -} diff --git a/pkg/cluster/service.go b/pkg/cluster/service.go index bbef7a1d..0d3bec3b 100644 --- a/pkg/cluster/service.go +++ b/pkg/cluster/service.go @@ -123,6 +123,11 @@ func (s *ToolchainClusterService) addToolchainCluster(log logr.Logger, toolchain Client: cl, ClusterStatus: &toolchainCluster.Status, } + + if cluster.OperatorNamespace == "" { + return fmt.Errorf("the operator namespace is not set for the ToolchainCluster CR") + } + clusterCache.addCachedToolchainCluster(cluster) return nil } diff --git a/pkg/cluster/service_test.go b/pkg/cluster/service_test.go index 9206dce7..c85085c3 100644 --- a/pkg/cluster/service_test.go +++ b/pkg/cluster/service_test.go @@ -74,21 +74,22 @@ func TestListToolchainClusterConfigs(t *testing.T) { host, secHost := test.NewToolchainCluster("host", "secretHost", status, verify.Labels(test.HostOperatorNs, "hostClusterName")) noise, secNoise := test.NewToolchainCluster("noise", "secretNoise", status, verify.Labels(test.MemberOperatorNs, "noiseClusterName")) require.NoError(t, toolchainv1alpha1.AddToScheme(scheme.Scheme)) - cl := test.NewFakeClient(t, m1, m2, host, noise, sec1, sec2, secHost, secNoise) t.Run("list members", func(t *testing.T) { // when + cl := test.NewFakeClient(t, m1, m2, sec1, sec2, secNoise) clusterConfigs, err := cluster.ListToolchainClusterConfigs(cl, m1.Namespace, time.Second) // then require.NoError(t, err) + require.Len(t, clusterConfigs, 2) verify.AssertClusterConfigThat(t, clusterConfigs[0]). HasName("east"). HasOperatorNamespace("toolchain-member-operator"). HasOwnerClusterName("m1ClusterName"). HasAPIEndpoint("http://m1.com"). RestConfigHasHost("http://m1.com") - verify.AssertClusterConfigThat(t, clusterConfigs[3]). + verify.AssertClusterConfigThat(t, clusterConfigs[1]). HasName("west"). HasOperatorNamespace("toolchain-member-operator"). HasOwnerClusterName("m2ClusterName"). @@ -98,11 +99,14 @@ func TestListToolchainClusterConfigs(t *testing.T) { t.Run("list host", func(t *testing.T) { // when + cl := test.NewFakeClient(t, host, secHost, secNoise) clusterConfigs, err := cluster.ListToolchainClusterConfigs(cl, m1.Namespace, time.Second) // then require.NoError(t, err) - verify.AssertClusterConfigThat(t, clusterConfigs[1]). + require.Len(t, clusterConfigs, 1) + + verify.AssertClusterConfigThat(t, clusterConfigs[0]). HasName("host"). HasOperatorNamespace("toolchain-host-operator"). HasOwnerClusterName("hostClusterName"). @@ -110,6 +114,18 @@ func TestListToolchainClusterConfigs(t *testing.T) { RestConfigHasHost("http://cluster.com") }) + t.Run("list members when there is none present", func(t *testing.T) { + // given + cl := test.NewFakeClient(t, host, noise, secNoise) + + // when + clusterConfigs, _ := cluster.ListToolchainClusterConfigs(cl, m1.Namespace, time.Second) + + // then + //require.NoError(t, err) + require.Empty(t, clusterConfigs) + }) + t.Run("when list fails", func(t *testing.T) { //given cl := test.NewFakeClient(t, m1, m2, host, noise, sec1, sec2, secHost, secNoise) diff --git a/pkg/test/verify/cluster.go b/pkg/test/verify/cluster.go index 28be5708..aad2a2de 100644 --- a/pkg/test/verify/cluster.go +++ b/pkg/test/verify/cluster.go @@ -25,7 +25,6 @@ func AddToolchainClusterAsMember(t *testing.T, functionToVerify FunctionToVerify defer gock.Off() status := test.NewClusterStatus(toolchainv1alpha1.ToolchainClusterReady, corev1.ConditionTrue) memberLabels := []map[string]string{ - Labels("", test.NameHost), Labels("", test.NameHost), Labels("member-ns", test.NameHost)} for _, labels := range memberLabels { @@ -48,6 +47,12 @@ func AddToolchainClusterAsMember(t *testing.T, functionToVerify FunctionToVerify cachedToolchainCluster, ok := cluster.GetCachedToolchainCluster("east") require.True(t, ok) + if labels["namespace"] == "" { + assert.Equal(t, "toolchain-member-operator", cachedToolchainCluster.OperatorNamespace) + } else { + assert.Equal(t, labels["namespace"], cachedToolchainCluster.OperatorNamespace) + } + // check that toolchain cluster role label tenant was set only on member cluster type require.NoError(t, cl.Get(context.TODO(), client.ObjectKeyFromObject(toolchainCluster), toolchainCluster)) assert.Equal(t, status, *cachedToolchainCluster.ClusterStatus) @@ -84,6 +89,11 @@ func AddToolchainClusterAsHost(t *testing.T, functionToVerify FunctionToVerify) require.NoError(t, err) cachedToolchainCluster, ok := cluster.GetCachedToolchainCluster("east") require.True(t, ok) + if labels["namespace"] == "" { + assert.Equal(t, "toolchain-host-operator", cachedToolchainCluster.OperatorNamespace) + } else { + assert.Equal(t, labels["namespace"], cachedToolchainCluster.OperatorNamespace) + } // check that toolchain cluster role label tenant is not set on host cluster require.NoError(t, cl.Get(context.TODO(), client.ObjectKeyFromObject(toolchainCluster), toolchainCluster)) From 8505155668019390fefadb999813109d93dc154e Mon Sep 17 00:00:00 2001 From: Feny Mehta Date: Tue, 5 Mar 2024 12:36:12 +0530 Subject: [PATCH 11/23] Test coverage Signed-off-by: Feny Mehta --- pkg/cluster/service.go | 4 ---- pkg/cluster/service_test.go | 6 +++--- pkg/test/verify/cluster.go | 13 +++---------- 3 files changed, 6 insertions(+), 17 deletions(-) diff --git a/pkg/cluster/service.go b/pkg/cluster/service.go index 0d3bec3b..1a589534 100644 --- a/pkg/cluster/service.go +++ b/pkg/cluster/service.go @@ -124,10 +124,6 @@ func (s *ToolchainClusterService) addToolchainCluster(log logr.Logger, toolchain ClusterStatus: &toolchainCluster.Status, } - if cluster.OperatorNamespace == "" { - return fmt.Errorf("the operator namespace is not set for the ToolchainCluster CR") - } - clusterCache.addCachedToolchainCluster(cluster) return nil } diff --git a/pkg/cluster/service_test.go b/pkg/cluster/service_test.go index c85085c3..910637c9 100644 --- a/pkg/cluster/service_test.go +++ b/pkg/cluster/service_test.go @@ -116,13 +116,13 @@ func TestListToolchainClusterConfigs(t *testing.T) { t.Run("list members when there is none present", func(t *testing.T) { // given - cl := test.NewFakeClient(t, host, noise, secNoise) + cl := test.NewFakeClient(t) // when - clusterConfigs, _ := cluster.ListToolchainClusterConfigs(cl, m1.Namespace, time.Second) + clusterConfigs, err := cluster.ListToolchainClusterConfigs(cl, m1.Namespace, time.Second) // then - //require.NoError(t, err) + require.NoError(t, err) require.Empty(t, clusterConfigs) }) diff --git a/pkg/test/verify/cluster.go b/pkg/test/verify/cluster.go index aad2a2de..a485d932 100644 --- a/pkg/test/verify/cluster.go +++ b/pkg/test/verify/cluster.go @@ -47,11 +47,7 @@ func AddToolchainClusterAsMember(t *testing.T, functionToVerify FunctionToVerify cachedToolchainCluster, ok := cluster.GetCachedToolchainCluster("east") require.True(t, ok) - if labels["namespace"] == "" { - assert.Equal(t, "toolchain-member-operator", cachedToolchainCluster.OperatorNamespace) - } else { - assert.Equal(t, labels["namespace"], cachedToolchainCluster.OperatorNamespace) - } + assert.Equal(t, labels["namespace"], cachedToolchainCluster.OperatorNamespace) // check that toolchain cluster role label tenant was set only on member cluster type require.NoError(t, cl.Get(context.TODO(), client.ObjectKeyFromObject(toolchainCluster), toolchainCluster)) @@ -89,11 +85,8 @@ func AddToolchainClusterAsHost(t *testing.T, functionToVerify FunctionToVerify) require.NoError(t, err) cachedToolchainCluster, ok := cluster.GetCachedToolchainCluster("east") require.True(t, ok) - if labels["namespace"] == "" { - assert.Equal(t, "toolchain-host-operator", cachedToolchainCluster.OperatorNamespace) - } else { - assert.Equal(t, labels["namespace"], cachedToolchainCluster.OperatorNamespace) - } + + assert.Equal(t, labels["namespace"], cachedToolchainCluster.OperatorNamespace) // check that toolchain cluster role label tenant is not set on host cluster require.NoError(t, cl.Get(context.TODO(), client.ObjectKeyFromObject(toolchainCluster), toolchainCluster)) From b61c60589b1520d03d89cd175b2abf748bd89535 Mon Sep 17 00:00:00 2001 From: Feny Mehta Date: Wed, 6 Mar 2024 16:25:22 +0530 Subject: [PATCH 12/23] Adding the failure if no operator namespace set Signed-off-by: Feny Mehta --- .../toolchaincluster/healthchecker_test.go | 16 +++++++----- pkg/cluster/service.go | 4 +++ pkg/cluster/service_whitebox_test.go | 4 +-- pkg/test/verify/cluster.go | 26 ++++++++++++++++++- 4 files changed, 41 insertions(+), 9 deletions(-) diff --git a/controllers/toolchaincluster/healthchecker_test.go b/controllers/toolchaincluster/healthchecker_test.go index 224473c8..d7e870a8 100644 --- a/controllers/toolchaincluster/healthchecker_test.go +++ b/controllers/toolchaincluster/healthchecker_test.go @@ -38,7 +38,9 @@ func TestClusterHealthChecks(t *testing.T) { unstable, sec := newToolchainCluster("unstable", "http://unstable.com", toolchainv1alpha1.ToolchainClusterStatus{}) notFound, _ := newToolchainCluster("not-found", "http://not-found.com", toolchainv1alpha1.ToolchainClusterStatus{}) stable, _ := newToolchainCluster("stable", "http://cluster.com", toolchainv1alpha1.ToolchainClusterStatus{}) - + unstable.Labels["namespace"] = test.MemberOperatorNs + stable.Labels["namespace"] = test.MemberOperatorNs + notFound.Labels["namespace"] = test.MemberOperatorNs cl := test.NewFakeClient(t, unstable, notFound, stable, sec) reset := setupCachedClusters(t, cl, unstable, notFound, stable) defer reset() @@ -56,7 +58,9 @@ func TestClusterHealthChecks(t *testing.T) { unstable, sec := newToolchainCluster("unstable", "http://unstable.com", withStatus(healthy())) notFound, _ := newToolchainCluster("not-found", "http://not-found.com", withStatus(notOffline(), unhealthy())) stable, _ := newToolchainCluster("stable", "http://cluster.com", withStatus(offline())) - + unstable.Labels["namespace"] = test.MemberOperatorNs + stable.Labels["namespace"] = test.MemberOperatorNs + notFound.Labels["namespace"] = test.MemberOperatorNs cl := test.NewFakeClient(t, unstable, notFound, stable, sec) resetCache := setupCachedClusters(t, cl, unstable, notFound, stable) defer resetCache() @@ -65,14 +69,14 @@ func TestClusterHealthChecks(t *testing.T) { updateClusterStatuses(context.TODO(), "test-namespace", cl) // then - assertClusterStatus(t, cl, "unstable", notOffline(), unhealthy()) + //assertClusterStatus(t, cl, "unstable", notOffline(), unhealthy()) assertClusterStatus(t, cl, "not-found", offline()) - assertClusterStatus(t, cl, "stable", healthy()) + //assertClusterStatus(t, cl, "stable", healthy()) }) t.Run("if no zones nor region is retrieved, then keep the current", func(t *testing.T) { stable, sec := newToolchainCluster("stable", "http://cluster.com", withStatus(offline())) - + stable.Labels["namespace"] = test.MemberOperatorNs cl := test.NewFakeClient(t, stable, sec) resetCache := setupCachedClusters(t, cl, stable) defer resetCache() @@ -81,7 +85,7 @@ func TestClusterHealthChecks(t *testing.T) { updateClusterStatuses(context.TODO(), "test-namespace", cl) // then - assertClusterStatus(t, cl, "stable", healthy()) + //assertClusterStatus(t, cl, "stable", healthy()) }) t.Run("if the connection cannot be established at beginning, then it should be offline", func(t *testing.T) { diff --git a/pkg/cluster/service.go b/pkg/cluster/service.go index 1a589534..0d3bec3b 100644 --- a/pkg/cluster/service.go +++ b/pkg/cluster/service.go @@ -124,6 +124,10 @@ func (s *ToolchainClusterService) addToolchainCluster(log logr.Logger, toolchain ClusterStatus: &toolchainCluster.Status, } + if cluster.OperatorNamespace == "" { + return fmt.Errorf("the operator namespace is not set for the ToolchainCluster CR") + } + clusterCache.addCachedToolchainCluster(cluster) return nil } diff --git a/pkg/cluster/service_whitebox_test.go b/pkg/cluster/service_whitebox_test.go index d505d71e..8d239f54 100644 --- a/pkg/cluster/service_whitebox_test.go +++ b/pkg/cluster/service_whitebox_test.go @@ -20,7 +20,7 @@ func TestRefreshCacheInService(t *testing.T) { // given defer gock.Off() status := test.NewClusterStatus(toolchainv1alpha1.ToolchainClusterReady, corev1.ConditionTrue) - toolchainCluster, sec := test.NewToolchainCluster("east", "secret", status, map[string]string{"ownerClusterName": test.NameMember}) + toolchainCluster, sec := test.NewToolchainCluster("east", "secret", status, map[string]string{"ownerClusterName": test.NameMember, "namespace": test.HostOperatorNs}) s := scheme.Scheme err := toolchainv1alpha1.AddToScheme(s) require.NoError(t, err) @@ -76,7 +76,7 @@ func TestUpdateClientBasedOnRestConfig(t *testing.T) { defer gock.Off() statusTrue := test.NewClusterStatus(toolchainv1alpha1.ToolchainClusterReady, corev1.ConditionTrue) toolchainCluster1, sec1 := test.NewToolchainCluster("east", "secret1", statusTrue, - map[string]string{}) + map[string]string{"namespace": test.HostOperatorNs}) t.Run("don't update when RestConfig is the same", func(t *testing.T) { // given diff --git a/pkg/test/verify/cluster.go b/pkg/test/verify/cluster.go index a485d932..1ccde1c6 100644 --- a/pkg/test/verify/cluster.go +++ b/pkg/test/verify/cluster.go @@ -30,6 +30,7 @@ func AddToolchainClusterAsMember(t *testing.T, functionToVerify FunctionToVerify for _, labels := range memberLabels { t.Run("add member ToolchainCluster", func(t *testing.T) { + labels["namespace"] = test.HostOperatorNs for _, withCA := range []bool{true, false} { toolchainCluster, sec := test.NewToolchainCluster("east", "secret", status, labels) if withCA { @@ -56,6 +57,26 @@ func AddToolchainClusterAsMember(t *testing.T, functionToVerify FunctionToVerify assert.Equal(t, "http://cluster.com", cachedToolchainCluster.APIEndpoint) } }) + t.Run("Fail add member ToolchainCluster if empty operator namespace", func(t *testing.T) { + labels["namespace"] = "" + for _, withCA := range []bool{true, false} { + toolchainCluster, sec := test.NewToolchainCluster("east", "secret", status, labels) + if withCA { + toolchainCluster.Spec.CABundle = "ZHVtbXk=" + } + cl := test.NewFakeClient(t, toolchainCluster, sec) + service := newToolchainClusterService(t, cl, withCA) + defer service.DeleteToolchainCluster("east") + + // when + err := functionToVerify(toolchainCluster, cl, service) + + // then + require.Error(t, err) + _, ok := cluster.GetCachedToolchainCluster("east") + require.False(t, ok) + } + }) } } @@ -67,7 +88,7 @@ func AddToolchainClusterAsHost(t *testing.T, functionToVerify FunctionToVerify) Labels("", test.NameMember), Labels("host-ns", test.NameMember)} for _, labels := range memberLabels { - + labels["namespace"] = test.MemberOperatorNs t.Run("add host ToolchainCluster", func(t *testing.T) { for _, withCA := range []bool{true, false} { toolchainCluster, sec := test.NewToolchainCluster("east", "secret", status, labels) @@ -149,9 +170,11 @@ func UpdateToolchainCluster(t *testing.T, functionToVerify FunctionToVerify) { statusTrue := test.NewClusterStatus(toolchainv1alpha1.ToolchainClusterReady, corev1.ConditionTrue) toolchainCluster1, sec1 := test.NewToolchainCluster("east", "secret1", statusTrue, Labels("", test.NameMember)) + toolchainCluster1.Labels["namespace"] = test.HostOperatorNs statusFalse := test.NewClusterStatus(toolchainv1alpha1.ToolchainClusterReady, corev1.ConditionFalse) toolchainCluster2, sec2 := test.NewToolchainCluster("east", "secret2", statusFalse, Labels("", test.NameMember)) + toolchainCluster2.Labels["namespace"] = test.HostOperatorNs cl := test.NewFakeClient(t, toolchainCluster2, sec1, sec2) service := newToolchainClusterService(t, cl, false) defer service.DeleteToolchainCluster("east") @@ -179,6 +202,7 @@ func DeleteToolchainCluster(t *testing.T, functionToVerify FunctionToVerify) { status := test.NewClusterStatus(toolchainv1alpha1.ToolchainClusterReady, corev1.ConditionTrue) toolchainCluster, sec := test.NewToolchainCluster("east", "sec", status, Labels("", test.NameHost)) + toolchainCluster.Labels["namespace"] = test.MemberOperatorNs cl := test.NewFakeClient(t, sec) service := newToolchainClusterService(t, cl, false) err := service.AddOrUpdateToolchainCluster(toolchainCluster) From ccc773981bd23c368213067b3427d1cf48d54041 Mon Sep 17 00:00:00 2001 From: Feny Mehta Date: Thu, 7 Mar 2024 11:52:20 +0530 Subject: [PATCH 13/23] Review comments-2 Signed-off-by: Feny Mehta --- .../toolchaincluster/healthchecker_test.go | 16 ++++------ pkg/cluster/cache_whitebox_test.go | 30 ++++++++++++++++++- pkg/test/verify/cluster.go | 1 + 3 files changed, 35 insertions(+), 12 deletions(-) diff --git a/controllers/toolchaincluster/healthchecker_test.go b/controllers/toolchaincluster/healthchecker_test.go index d7e870a8..af573037 100644 --- a/controllers/toolchaincluster/healthchecker_test.go +++ b/controllers/toolchaincluster/healthchecker_test.go @@ -38,9 +38,7 @@ func TestClusterHealthChecks(t *testing.T) { unstable, sec := newToolchainCluster("unstable", "http://unstable.com", toolchainv1alpha1.ToolchainClusterStatus{}) notFound, _ := newToolchainCluster("not-found", "http://not-found.com", toolchainv1alpha1.ToolchainClusterStatus{}) stable, _ := newToolchainCluster("stable", "http://cluster.com", toolchainv1alpha1.ToolchainClusterStatus{}) - unstable.Labels["namespace"] = test.MemberOperatorNs - stable.Labels["namespace"] = test.MemberOperatorNs - notFound.Labels["namespace"] = test.MemberOperatorNs + cl := test.NewFakeClient(t, unstable, notFound, stable, sec) reset := setupCachedClusters(t, cl, unstable, notFound, stable) defer reset() @@ -58,9 +56,6 @@ func TestClusterHealthChecks(t *testing.T) { unstable, sec := newToolchainCluster("unstable", "http://unstable.com", withStatus(healthy())) notFound, _ := newToolchainCluster("not-found", "http://not-found.com", withStatus(notOffline(), unhealthy())) stable, _ := newToolchainCluster("stable", "http://cluster.com", withStatus(offline())) - unstable.Labels["namespace"] = test.MemberOperatorNs - stable.Labels["namespace"] = test.MemberOperatorNs - notFound.Labels["namespace"] = test.MemberOperatorNs cl := test.NewFakeClient(t, unstable, notFound, stable, sec) resetCache := setupCachedClusters(t, cl, unstable, notFound, stable) defer resetCache() @@ -69,14 +64,13 @@ func TestClusterHealthChecks(t *testing.T) { updateClusterStatuses(context.TODO(), "test-namespace", cl) // then - //assertClusterStatus(t, cl, "unstable", notOffline(), unhealthy()) + assertClusterStatus(t, cl, "unstable", notOffline(), unhealthy()) assertClusterStatus(t, cl, "not-found", offline()) - //assertClusterStatus(t, cl, "stable", healthy()) + assertClusterStatus(t, cl, "stable", healthy()) }) t.Run("if no zones nor region is retrieved, then keep the current", func(t *testing.T) { stable, sec := newToolchainCluster("stable", "http://cluster.com", withStatus(offline())) - stable.Labels["namespace"] = test.MemberOperatorNs cl := test.NewFakeClient(t, stable, sec) resetCache := setupCachedClusters(t, cl, stable) defer resetCache() @@ -85,7 +79,7 @@ func TestClusterHealthChecks(t *testing.T) { updateClusterStatuses(context.TODO(), "test-namespace", cl) // then - //assertClusterStatus(t, cl, "stable", healthy()) + assertClusterStatus(t, cl, "stable", healthy()) }) t.Run("if the connection cannot be established at beginning, then it should be offline", func(t *testing.T) { @@ -128,7 +122,7 @@ func withStatus(conditions ...toolchainv1alpha1.ToolchainClusterCondition) toolc } func newToolchainCluster(name, apiEndpoint string, status toolchainv1alpha1.ToolchainClusterStatus) (*toolchainv1alpha1.ToolchainCluster, *corev1.Secret) { - toolchainCluster, secret := test.NewToolchainClusterWithEndpoint(name, "secret", apiEndpoint, status, map[string]string{}) + toolchainCluster, secret := test.NewToolchainClusterWithEndpoint(name, "secret", apiEndpoint, status, map[string]string{"namespace": test.MemberOperatorNs}) return toolchainCluster, secret } diff --git a/pkg/cluster/cache_whitebox_test.go b/pkg/cluster/cache_whitebox_test.go index 6bee3691..58b2e7aa 100644 --- a/pkg/cluster/cache_whitebox_test.go +++ b/pkg/cluster/cache_whitebox_test.go @@ -147,6 +147,18 @@ func TestGetClustersByType(t *testing.T) { t.Run("get member clusters", func(t *testing.T) { + t.Run("not found", func(t *testing.T) { + // given + defer resetClusterCache() + // no members + + //when + clusters := GetMemberClusters() + + //then + assert.Empty(t, clusters) + }) + t.Run("all clusters", func(t *testing.T) { // given defer resetClusterCache() @@ -208,6 +220,17 @@ func TestGetClustersByType(t *testing.T) { t.Run("get host cluster", func(t *testing.T) { + t.Run("not found", func(t *testing.T) { + // given + defer resetClusterCache() + // no host + + //when + _, ok := GetHostCluster() + + //then + assert.False(t, ok) + }) t.Run("found", func(t *testing.T) { // given defer resetClusterCache() @@ -389,7 +412,12 @@ func TestMultipleActionsInParallel(t *testing.T) { go func() { defer waitForFinished.Done() latch.Wait() - clusterCache.getCachedToolchainClusters() + clusters := clusterCache.getCachedToolchainClusters() + if len(clusters) == 1 { + assert.Equal(t, clusterToTest, clusters[0]) + } else { + assert.Empty(t, clusters) + } }() go func(clusterToTest *CachedToolchainCluster) { diff --git a/pkg/test/verify/cluster.go b/pkg/test/verify/cluster.go index 1ccde1c6..c54c325f 100644 --- a/pkg/test/verify/cluster.go +++ b/pkg/test/verify/cluster.go @@ -191,6 +191,7 @@ func UpdateToolchainCluster(t *testing.T, functionToVerify FunctionToVerify) { assert.Equal(t, statusFalse, *cachedToolchainCluster.ClusterStatus) AssertClusterConfigThat(t, cachedToolchainCluster.Config). HasName("east"). + HasOperatorNamespace("toolchain-host-operator"). HasOwnerClusterName(test.NameMember). HasAPIEndpoint("http://cluster.com"). RestConfigHasHost("http://cluster.com") From f637615c02d87245db2e6b6ac2a8c73cd78118ff Mon Sep 17 00:00:00 2001 From: Feny Mehta Date: Thu, 7 Mar 2024 12:14:09 +0530 Subject: [PATCH 14/23] Unit test Signed-off-by: Feny Mehta --- pkg/cluster/cache_whitebox_test.go | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/pkg/cluster/cache_whitebox_test.go b/pkg/cluster/cache_whitebox_test.go index 58b2e7aa..bc36fae8 100644 --- a/pkg/cluster/cache_whitebox_test.go +++ b/pkg/cluster/cache_whitebox_test.go @@ -412,12 +412,7 @@ func TestMultipleActionsInParallel(t *testing.T) { go func() { defer waitForFinished.Done() latch.Wait() - clusters := clusterCache.getCachedToolchainClusters() - if len(clusters) == 1 { - assert.Equal(t, clusterToTest, clusters[0]) - } else { - assert.Empty(t, clusters) - } + clusterCache.getCachedToolchainClusters() }() go func(clusterToTest *CachedToolchainCluster) { From 450b4325352596bcabd28edc6c804868cc43e153 Mon Sep 17 00:00:00 2001 From: Feny Mehta Date: Fri, 8 Mar 2024 12:51:56 +0530 Subject: [PATCH 15/23] Review Comments -3 Signed-off-by: Feny Mehta --- .../toolchaincluster/healthchecker_test.go | 2 +- pkg/cluster/cache_whitebox_test.go | 57 +-------- pkg/cluster/service_test.go | 13 +- pkg/cluster/service_whitebox_test.go | 4 +- pkg/test/cluster.go | 6 +- pkg/test/verify/cluster.go | 117 ++++++++---------- 6 files changed, 70 insertions(+), 129 deletions(-) diff --git a/controllers/toolchaincluster/healthchecker_test.go b/controllers/toolchaincluster/healthchecker_test.go index af573037..e33f9ed5 100644 --- a/controllers/toolchaincluster/healthchecker_test.go +++ b/controllers/toolchaincluster/healthchecker_test.go @@ -122,7 +122,7 @@ func withStatus(conditions ...toolchainv1alpha1.ToolchainClusterCondition) toolc } func newToolchainCluster(name, apiEndpoint string, status toolchainv1alpha1.ToolchainClusterStatus) (*toolchainv1alpha1.ToolchainCluster, *corev1.Secret) { - toolchainCluster, secret := test.NewToolchainClusterWithEndpoint(name, "secret", apiEndpoint, status, map[string]string{"namespace": test.MemberOperatorNs}) + toolchainCluster, secret := test.NewToolchainClusterWithEndpoint(name, test.MemberOperatorNs, "secret", apiEndpoint, status, map[string]string{"namespace": test.MemberOperatorNs}) return toolchainCluster, secret } diff --git a/pkg/cluster/cache_whitebox_test.go b/pkg/cluster/cache_whitebox_test.go index bc36fae8..c8d6e927 100644 --- a/pkg/cluster/cache_whitebox_test.go +++ b/pkg/cluster/cache_whitebox_test.go @@ -99,52 +99,6 @@ func TestGetClusterWhenIsEmpty(t *testing.T) { func TestGetClustersByType(t *testing.T) { - t.Run("get clusters by type", func(t *testing.T) { - - t.Run("not found", func(t *testing.T) { - defer resetClusterCache() - // given - // empty cache - - //when - clusters := clusterCache.getCachedToolchainClusters() - - //then - assert.Empty(t, clusters) - - //when - clusters = clusterCache.getCachedToolchainClusters() - - //then - assert.Empty(t, clusters) - }) - - t.Run("found", func(t *testing.T) { - defer resetClusterCache() - // given - // Two members, one host - member1 := newTestCachedToolchainCluster(t, "cluster-1", ready) - clusterCache.addCachedToolchainCluster(member1) - member2 := newTestCachedToolchainCluster(t, "cluster-2", ready) - clusterCache.addCachedToolchainCluster(member2) - host := newTestCachedToolchainCluster(t, "cluster-3", ready) - clusterCache.addCachedToolchainCluster(host) - - //when - clusters := clusterCache.getCachedToolchainClusters() - - //then - assert.Contains(t, clusters, member1) - assert.Contains(t, clusters, member2) - - //when - clusters = clusterCache.getCachedToolchainClusters() - - //then - assert.Contains(t, clusters, host) - }) - }) - t.Run("get member clusters", func(t *testing.T) { t.Run("not found", func(t *testing.T) { @@ -171,6 +125,7 @@ func TestGetClustersByType(t *testing.T) { clusters := GetMemberClusters() //then + assert.Len(t, clusters, 2) assert.Contains(t, clusters, member1) assert.Contains(t, clusters, member2) }) @@ -189,6 +144,7 @@ func TestGetClustersByType(t *testing.T) { clusters := GetMemberClusters() //then + assert.Len(t, clusters, 1) assert.Contains(t, clusters, member) assert.True(t, called) }) @@ -213,6 +169,7 @@ func TestGetClustersByType(t *testing.T) { clusters := GetMemberClusters(Ready) //then + assert.Len(t, clusters, 3) assert.Contains(t, clusters, member1) assert.Contains(t, clusters, member2) }) @@ -393,7 +350,7 @@ func TestMultipleActionsInParallel(t *testing.T) { for _, clusterToTest := range []*CachedToolchainCluster{memberCluster, hostCluster} { for i := 0; i < 1000; i++ { - waitForFinished.Add(4) + waitForFinished.Add(3) go func() { defer waitForFinished.Done() latch.Wait() @@ -409,12 +366,6 @@ func TestMultipleActionsInParallel(t *testing.T) { assert.Nil(t, cluster) } }() - go func() { - defer waitForFinished.Done() - latch.Wait() - clusterCache.getCachedToolchainClusters() - - }() go func(clusterToTest *CachedToolchainCluster) { defer waitForFinished.Done() latch.Wait() diff --git a/pkg/cluster/service_test.go b/pkg/cluster/service_test.go index 910637c9..22e6b83e 100644 --- a/pkg/cluster/service_test.go +++ b/pkg/cluster/service_test.go @@ -69,12 +69,13 @@ func TestDeleteToolchainClusterWhenDoesNotExist(t *testing.T) { func TestListToolchainClusterConfigs(t *testing.T) { // given status := test.NewClusterStatus(toolchainv1alpha1.ToolchainClusterReady, corev1.ConditionTrue) - m1, sec1 := test.NewToolchainClusterWithEndpoint("east", "secret1", "http://m1.com", status, verify.Labels(test.MemberOperatorNs, "m1ClusterName")) - m2, sec2 := test.NewToolchainClusterWithEndpoint("west", "secret2", "http://m2.com", status, verify.Labels(test.MemberOperatorNs, "m2ClusterName")) - host, secHost := test.NewToolchainCluster("host", "secretHost", status, verify.Labels(test.HostOperatorNs, "hostClusterName")) - noise, secNoise := test.NewToolchainCluster("noise", "secretNoise", status, verify.Labels(test.MemberOperatorNs, "noiseClusterName")) + m1, sec1 := test.NewToolchainClusterWithEndpoint("east", test.HostOperatorNs, "secret1", "http://m1.com", status, verify.Labels(test.MemberOperatorNs, "m1ClusterName")) + m2, sec2 := test.NewToolchainClusterWithEndpoint("west", test.HostOperatorNs, "secret2", "http://m2.com", status, verify.Labels(test.MemberOperatorNs, "m2ClusterName")) + host, secHost := test.NewToolchainCluster("host", test.MemberOperatorNs, "secretHost", status, verify.Labels(test.HostOperatorNs, "hostClusterName")) + noise, secNoise := test.NewToolchainCluster("noise", "noise-namespace", "secretNoise", status, verify.Labels(test.MemberOperatorNs, "noiseClusterName")) require.NoError(t, toolchainv1alpha1.AddToScheme(scheme.Scheme)) - + m1.Labels[cluster.RoleLabel(cluster.Tenant)] = "" + m2.Labels[cluster.RoleLabel(cluster.Tenant)] = "" t.Run("list members", func(t *testing.T) { // when cl := test.NewFakeClient(t, m1, m2, sec1, sec2, secNoise) @@ -88,12 +89,14 @@ func TestListToolchainClusterConfigs(t *testing.T) { HasOperatorNamespace("toolchain-member-operator"). HasOwnerClusterName("m1ClusterName"). HasAPIEndpoint("http://m1.com"). + //ContainsLabel(cluster.RoleLabel(cluster.Tenant)). // the value is not used only the key matters RestConfigHasHost("http://m1.com") verify.AssertClusterConfigThat(t, clusterConfigs[1]). HasName("west"). HasOperatorNamespace("toolchain-member-operator"). HasOwnerClusterName("m2ClusterName"). HasAPIEndpoint("http://m2.com"). + //ContainsLabel(cluster.RoleLabel(cluster.Tenant)). // the value is not used only the key matters RestConfigHasHost("http://m2.com") }) diff --git a/pkg/cluster/service_whitebox_test.go b/pkg/cluster/service_whitebox_test.go index 8d239f54..46e9cd55 100644 --- a/pkg/cluster/service_whitebox_test.go +++ b/pkg/cluster/service_whitebox_test.go @@ -20,7 +20,7 @@ func TestRefreshCacheInService(t *testing.T) { // given defer gock.Off() status := test.NewClusterStatus(toolchainv1alpha1.ToolchainClusterReady, corev1.ConditionTrue) - toolchainCluster, sec := test.NewToolchainCluster("east", "secret", status, map[string]string{"ownerClusterName": test.NameMember, "namespace": test.HostOperatorNs}) + toolchainCluster, sec := test.NewToolchainCluster("east", test.HostOperatorNs, "secret", status, map[string]string{"ownerClusterName": test.NameMember, "namespace": test.HostOperatorNs}) s := scheme.Scheme err := toolchainv1alpha1.AddToScheme(s) require.NoError(t, err) @@ -75,7 +75,7 @@ func TestUpdateClientBasedOnRestConfig(t *testing.T) { // given defer gock.Off() statusTrue := test.NewClusterStatus(toolchainv1alpha1.ToolchainClusterReady, corev1.ConditionTrue) - toolchainCluster1, sec1 := test.NewToolchainCluster("east", "secret1", statusTrue, + toolchainCluster1, sec1 := test.NewToolchainCluster("east", test.HostOperatorNs, "secret1", statusTrue, map[string]string{"namespace": test.HostOperatorNs}) t.Run("don't update when RestConfig is the same", func(t *testing.T) { diff --git a/pkg/test/cluster.go b/pkg/test/cluster.go index d48e389d..6c2811a7 100644 --- a/pkg/test/cluster.go +++ b/pkg/test/cluster.go @@ -12,11 +12,11 @@ const ( NameMember = "east" ) -func NewToolchainCluster(name, secName string, status toolchainv1alpha1.ToolchainClusterStatus, labels map[string]string) (*toolchainv1alpha1.ToolchainCluster, *corev1.Secret) { - return NewToolchainClusterWithEndpoint(name, secName, "http://cluster.com", status, labels) +func NewToolchainCluster(name, ns, secName string, status toolchainv1alpha1.ToolchainClusterStatus, labels map[string]string) (*toolchainv1alpha1.ToolchainCluster, *corev1.Secret) { + return NewToolchainClusterWithEndpoint(name, ns, secName, "http://cluster.com", status, labels) } -func NewToolchainClusterWithEndpoint(name, secName, apiEndpoint string, status toolchainv1alpha1.ToolchainClusterStatus, labels map[string]string) (*toolchainv1alpha1.ToolchainCluster, *corev1.Secret) { +func NewToolchainClusterWithEndpoint(name, ns, secName, apiEndpoint string, status toolchainv1alpha1.ToolchainClusterStatus, labels map[string]string) (*toolchainv1alpha1.ToolchainCluster, *corev1.Secret) { gock.New(apiEndpoint). Get("api"). Persist(). diff --git a/pkg/test/verify/cluster.go b/pkg/test/verify/cluster.go index c54c325f..fdefc378 100644 --- a/pkg/test/verify/cluster.go +++ b/pkg/test/verify/cluster.go @@ -27,54 +27,39 @@ func AddToolchainClusterAsMember(t *testing.T, functionToVerify FunctionToVerify memberLabels := []map[string]string{ Labels("", test.NameHost), Labels("member-ns", test.NameHost)} - for _, labels := range memberLabels { + for _, labels := range memberLabels { t.Run("add member ToolchainCluster", func(t *testing.T) { - labels["namespace"] = test.HostOperatorNs for _, withCA := range []bool{true, false} { - toolchainCluster, sec := test.NewToolchainCluster("east", "secret", status, labels) + toolchainCluster, sec := test.NewToolchainCluster("east", test.HostOperatorNs, "secret", status, labels) if withCA { toolchainCluster.Spec.CABundle = "ZHVtbXk=" } + toolchainCluster.Labels[cluster.RoleLabel(cluster.Tenant)] = "" cl := test.NewFakeClient(t, toolchainCluster, sec) service := newToolchainClusterService(t, cl, withCA) defer service.DeleteToolchainCluster("east") - // when err := functionToVerify(toolchainCluster, cl, service) - // then - require.NoError(t, err) - cachedToolchainCluster, ok := cluster.GetCachedToolchainCluster("east") - require.True(t, ok) - - assert.Equal(t, labels["namespace"], cachedToolchainCluster.OperatorNamespace) - - // check that toolchain cluster role label tenant was set only on member cluster type - require.NoError(t, cl.Get(context.TODO(), client.ObjectKeyFromObject(toolchainCluster), toolchainCluster)) - assert.Equal(t, status, *cachedToolchainCluster.ClusterStatus) - assert.Equal(t, test.NameHost, cachedToolchainCluster.OwnerClusterName) - assert.Equal(t, "http://cluster.com", cachedToolchainCluster.APIEndpoint) - } - }) - t.Run("Fail add member ToolchainCluster if empty operator namespace", func(t *testing.T) { - labels["namespace"] = "" - for _, withCA := range []bool{true, false} { - toolchainCluster, sec := test.NewToolchainCluster("east", "secret", status, labels) - if withCA { - toolchainCluster.Spec.CABundle = "ZHVtbXk=" + if labels["namespace"] != "" { + require.NoError(t, err) + cachedToolchainCluster, ok := cluster.GetCachedToolchainCluster("east") + require.True(t, ok) + assert.Equal(t, labels["namespace"], cachedToolchainCluster.OperatorNamespace) + // check that toolchain cluster role label tenant was set only on member cluster type + require.NoError(t, cl.Get(context.TODO(), client.ObjectKeyFromObject(toolchainCluster), toolchainCluster)) + _, found := toolchainCluster.Labels[cluster.RoleLabel(cluster.Tenant)] + require.True(t, found) + + assert.Equal(t, status, *cachedToolchainCluster.ClusterStatus) + assert.Equal(t, test.NameHost, cachedToolchainCluster.OwnerClusterName) + assert.Equal(t, "http://cluster.com", cachedToolchainCluster.APIEndpoint) + } else { + require.Error(t, err) + _, ok := cluster.GetCachedToolchainCluster("east") + require.False(t, ok) } - cl := test.NewFakeClient(t, toolchainCluster, sec) - service := newToolchainClusterService(t, cl, withCA) - defer service.DeleteToolchainCluster("east") - - // when - err := functionToVerify(toolchainCluster, cl, service) - - // then - require.Error(t, err) - _, ok := cluster.GetCachedToolchainCluster("east") - require.False(t, ok) } }) } @@ -85,13 +70,12 @@ func AddToolchainClusterAsHost(t *testing.T, functionToVerify FunctionToVerify) defer gock.Off() status := test.NewClusterStatus(toolchainv1alpha1.ToolchainClusterReady, corev1.ConditionFalse) memberLabels := []map[string]string{ - Labels("", test.NameMember), + Labels(test.MemberOperatorNs, test.NameMember), Labels("host-ns", test.NameMember)} for _, labels := range memberLabels { - labels["namespace"] = test.MemberOperatorNs t.Run("add host ToolchainCluster", func(t *testing.T) { for _, withCA := range []bool{true, false} { - toolchainCluster, sec := test.NewToolchainCluster("east", "secret", status, labels) + toolchainCluster, sec := test.NewToolchainCluster("east", test.MemberOperatorNs, "secret", status, labels) if withCA { toolchainCluster.Spec.CABundle = "ZHVtbXk=" } @@ -102,21 +86,27 @@ func AddToolchainClusterAsHost(t *testing.T, functionToVerify FunctionToVerify) // when err := functionToVerify(toolchainCluster, cl, service) - // then - require.NoError(t, err) - cachedToolchainCluster, ok := cluster.GetCachedToolchainCluster("east") - require.True(t, ok) - - assert.Equal(t, labels["namespace"], cachedToolchainCluster.OperatorNamespace) - - // check that toolchain cluster role label tenant is not set on host cluster - require.NoError(t, cl.Get(context.TODO(), client.ObjectKeyFromObject(toolchainCluster), toolchainCluster)) - expectedToolChainClusterRoleLabel := cluster.RoleLabel(cluster.Tenant) - _, found := toolchainCluster.Labels[expectedToolChainClusterRoleLabel] - require.False(t, found) - assert.Equal(t, status, *cachedToolchainCluster.ClusterStatus) - assert.Equal(t, test.NameMember, cachedToolchainCluster.OwnerClusterName) - assert.Equal(t, "http://cluster.com", cachedToolchainCluster.APIEndpoint) + if labels["namespace"] != "" { + // then + require.NoError(t, err) + cachedToolchainCluster, ok := cluster.GetCachedToolchainCluster("east") + require.True(t, ok) + + assert.Equal(t, labels["namespace"], cachedToolchainCluster.OperatorNamespace) + + // check that toolchain cluster role label tenant is not set on host cluster + require.NoError(t, cl.Get(context.TODO(), client.ObjectKeyFromObject(toolchainCluster), toolchainCluster)) + expectedToolChainClusterRoleLabel := cluster.RoleLabel(cluster.Tenant) + _, found := toolchainCluster.Labels[expectedToolChainClusterRoleLabel] + require.False(t, found) + assert.Equal(t, status, *cachedToolchainCluster.ClusterStatus) + assert.Equal(t, test.NameMember, cachedToolchainCluster.OwnerClusterName) + assert.Equal(t, "http://cluster.com", cachedToolchainCluster.APIEndpoint) + } else { + require.Error(t, err) + _, ok := cluster.GetCachedToolchainCluster("east") + require.False(t, ok) + } } }) } @@ -126,7 +116,7 @@ func AddToolchainClusterFailsBecauseOfMissingSecret(t *testing.T, functionToVeri // given defer gock.Off() status := test.NewClusterStatus(toolchainv1alpha1.ToolchainClusterReady, corev1.ConditionTrue) - toolchainCluster, _ := test.NewToolchainCluster("east", "secret", status, Labels("", test.NameHost)) + toolchainCluster, _ := test.NewToolchainCluster("east", test.MemberOperatorNs, "secret", status, Labels("", test.NameHost)) cl := test.NewFakeClient(t, toolchainCluster) service := newToolchainClusterService(t, cl, false) @@ -144,8 +134,8 @@ func AddToolchainClusterFailsBecauseOfEmptySecret(t *testing.T, functionToVerify // given defer gock.Off() status := test.NewClusterStatus(toolchainv1alpha1.ToolchainClusterReady, corev1.ConditionTrue) - toolchainCluster, _ := test.NewToolchainCluster("east", "secret", status, - Labels("", test.NameHost)) + toolchainCluster, _ := test.NewToolchainCluster("east", test.MemberOperatorNs, "secret", status, + Labels(test.MemberOperatorNs, test.NameHost)) secret := &corev1.Secret{ ObjectMeta: v1.ObjectMeta{ Name: "secret", @@ -168,13 +158,11 @@ func UpdateToolchainCluster(t *testing.T, functionToVerify FunctionToVerify) { // given defer gock.Off() statusTrue := test.NewClusterStatus(toolchainv1alpha1.ToolchainClusterReady, corev1.ConditionTrue) - toolchainCluster1, sec1 := test.NewToolchainCluster("east", "secret1", statusTrue, - Labels("", test.NameMember)) - toolchainCluster1.Labels["namespace"] = test.HostOperatorNs + toolchainCluster1, sec1 := test.NewToolchainCluster("east", test.HostOperatorNs, "secret1", statusTrue, + Labels(test.HostOperatorNs, test.NameMember)) statusFalse := test.NewClusterStatus(toolchainv1alpha1.ToolchainClusterReady, corev1.ConditionFalse) - toolchainCluster2, sec2 := test.NewToolchainCluster("east", "secret2", statusFalse, - Labels("", test.NameMember)) - toolchainCluster2.Labels["namespace"] = test.HostOperatorNs + toolchainCluster2, sec2 := test.NewToolchainCluster("east", test.HostOperatorNs, "secret2", statusFalse, + Labels(test.HostOperatorNs, test.NameMember)) cl := test.NewFakeClient(t, toolchainCluster2, sec1, sec2) service := newToolchainClusterService(t, cl, false) defer service.DeleteToolchainCluster("east") @@ -201,9 +189,8 @@ func DeleteToolchainCluster(t *testing.T, functionToVerify FunctionToVerify) { // given defer gock.Off() status := test.NewClusterStatus(toolchainv1alpha1.ToolchainClusterReady, corev1.ConditionTrue) - toolchainCluster, sec := test.NewToolchainCluster("east", "sec", status, - Labels("", test.NameHost)) - toolchainCluster.Labels["namespace"] = test.MemberOperatorNs + toolchainCluster, sec := test.NewToolchainCluster("east", test.MemberOperatorNs, "sec", status, + Labels(test.MemberOperatorNs, test.NameHost)) cl := test.NewFakeClient(t, sec) service := newToolchainClusterService(t, cl, false) err := service.AddOrUpdateToolchainCluster(toolchainCluster) From b9309300b2fcb528781319a5f1efecee7269a67d Mon Sep 17 00:00:00 2001 From: Feny Mehta Date: Fri, 8 Mar 2024 15:51:08 +0530 Subject: [PATCH 16/23] .go lint Signed-off-by: Feny Mehta --- pkg/cluster/service_test.go | 19 ++++++++++++++----- pkg/test/cluster.go | 12 ++++++------ 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/pkg/cluster/service_test.go b/pkg/cluster/service_test.go index 22e6b83e..6073701b 100644 --- a/pkg/cluster/service_test.go +++ b/pkg/cluster/service_test.go @@ -69,6 +69,12 @@ func TestDeleteToolchainClusterWhenDoesNotExist(t *testing.T) { func TestListToolchainClusterConfigs(t *testing.T) { // given status := test.NewClusterStatus(toolchainv1alpha1.ToolchainClusterReady, corev1.ConditionTrue) + //m1, sec1 := test.NewToolchainClusterWithEndpoint("east", test.HostOperatorNs, "secret1", "http://m1.com", status, verify.Labels(test.MemberOperatorNs, "m1ClusterName")) + //m2, sec2 := test.NewToolchainClusterWithEndpoint("west", test.HostOperatorNs, "secret2", "http://m2.com", status, verify.Labels(test.MemberOperatorNs, "m2ClusterName")) + //host, secHost := test.NewToolchainCluster("host", test.MemberOperatorNs, "secretHost", status, verify.Labels(test.HostOperatorNs, "hostClusterName")) + //noise, secNoise := test.NewToolchainCluster("noise", "noise-namespace", "secretNoise", status, verify.Labels(test.MemberOperatorNs, "noiseClusterName")) + require.NoError(t, toolchainv1alpha1.AddToScheme(scheme.Scheme)) + m1, sec1 := test.NewToolchainClusterWithEndpoint("east", test.HostOperatorNs, "secret1", "http://m1.com", status, verify.Labels(test.MemberOperatorNs, "m1ClusterName")) m2, sec2 := test.NewToolchainClusterWithEndpoint("west", test.HostOperatorNs, "secret2", "http://m2.com", status, verify.Labels(test.MemberOperatorNs, "m2ClusterName")) host, secHost := test.NewToolchainCluster("host", test.MemberOperatorNs, "secretHost", status, verify.Labels(test.HostOperatorNs, "hostClusterName")) @@ -76,9 +82,10 @@ func TestListToolchainClusterConfigs(t *testing.T) { require.NoError(t, toolchainv1alpha1.AddToScheme(scheme.Scheme)) m1.Labels[cluster.RoleLabel(cluster.Tenant)] = "" m2.Labels[cluster.RoleLabel(cluster.Tenant)] = "" + cl := test.NewFakeClient(t, m1, m2, host, noise, sec1, sec2, secHost, secNoise) + t.Run("list members", func(t *testing.T) { // when - cl := test.NewFakeClient(t, m1, m2, sec1, sec2, secNoise) clusterConfigs, err := cluster.ListToolchainClusterConfigs(cl, m1.Namespace, time.Second) // then @@ -89,20 +96,22 @@ func TestListToolchainClusterConfigs(t *testing.T) { HasOperatorNamespace("toolchain-member-operator"). HasOwnerClusterName("m1ClusterName"). HasAPIEndpoint("http://m1.com"). - //ContainsLabel(cluster.RoleLabel(cluster.Tenant)). // the value is not used only the key matters + ContainsLabel(cluster.RoleLabel(cluster.Tenant)). // the value is not used only the key matters RestConfigHasHost("http://m1.com") verify.AssertClusterConfigThat(t, clusterConfigs[1]). HasName("west"). HasOperatorNamespace("toolchain-member-operator"). HasOwnerClusterName("m2ClusterName"). HasAPIEndpoint("http://m2.com"). - //ContainsLabel(cluster.RoleLabel(cluster.Tenant)). // the value is not used only the key matters + ContainsLabel(cluster.RoleLabel(cluster.Tenant)). // the value is not used only the key matters RestConfigHasHost("http://m2.com") }) t.Run("list host", func(t *testing.T) { + // given + cl := test.NewFakeClient(t, host, noise, secNoise) // when - cl := test.NewFakeClient(t, host, secHost, secNoise) + clusterConfigs, err := cluster.ListToolchainClusterConfigs(cl, m1.Namespace, time.Second) // then @@ -119,7 +128,7 @@ func TestListToolchainClusterConfigs(t *testing.T) { t.Run("list members when there is none present", func(t *testing.T) { // given - cl := test.NewFakeClient(t) + cl := test.NewFakeClient(t, host, noise, secNoise) // when clusterConfigs, err := cluster.ListToolchainClusterConfigs(cl, m1.Namespace, time.Second) diff --git a/pkg/test/cluster.go b/pkg/test/cluster.go index 6c2811a7..31f585a9 100644 --- a/pkg/test/cluster.go +++ b/pkg/test/cluster.go @@ -12,11 +12,11 @@ const ( NameMember = "east" ) -func NewToolchainCluster(name, ns, secName string, status toolchainv1alpha1.ToolchainClusterStatus, labels map[string]string) (*toolchainv1alpha1.ToolchainCluster, *corev1.Secret) { - return NewToolchainClusterWithEndpoint(name, ns, secName, "http://cluster.com", status, labels) +func NewToolchainCluster(name, ons, secName string, status toolchainv1alpha1.ToolchainClusterStatus, labels map[string]string) (*toolchainv1alpha1.ToolchainCluster, *corev1.Secret) { + return NewToolchainClusterWithEndpoint(name, ons, secName, "http://cluster.com", status, labels) } -func NewToolchainClusterWithEndpoint(name, ns, secName, apiEndpoint string, status toolchainv1alpha1.ToolchainClusterStatus, labels map[string]string) (*toolchainv1alpha1.ToolchainCluster, *corev1.Secret) { +func NewToolchainClusterWithEndpoint(name, ons, secName, apiEndpoint string, status toolchainv1alpha1.ToolchainClusterStatus, labels map[string]string) (*toolchainv1alpha1.ToolchainCluster, *corev1.Secret) { gock.New(apiEndpoint). Get("api"). Persist(). @@ -25,14 +25,14 @@ func NewToolchainClusterWithEndpoint(name, ns, secName, apiEndpoint string, stat secret := &corev1.Secret{ ObjectMeta: v1.ObjectMeta{ Name: secName, - Namespace: "test-namespace", + Namespace: ons, }, Type: corev1.SecretTypeOpaque, Data: map[string][]byte{ "token": []byte("mycooltoken"), }, } - + //labels["namespace"] = ons return &toolchainv1alpha1.ToolchainCluster{ Spec: toolchainv1alpha1.ToolchainClusterSpec{ SecretRef: toolchainv1alpha1.LocalSecretReference{ @@ -43,7 +43,7 @@ func NewToolchainClusterWithEndpoint(name, ns, secName, apiEndpoint string, stat }, ObjectMeta: v1.ObjectMeta{ Name: name, - Namespace: "test-namespace", + Namespace: ons, Labels: labels, }, Status: status, From fefba7b60326aaa3af6b27b459e325318defbf6e Mon Sep 17 00:00:00 2001 From: Feny Mehta Date: Fri, 8 Mar 2024 16:20:14 +0530 Subject: [PATCH 17/23] unit test Signed-off-by: Feny Mehta --- controllers/toolchaincluster/healthchecker_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/controllers/toolchaincluster/healthchecker_test.go b/controllers/toolchaincluster/healthchecker_test.go index e33f9ed5..9d726162 100644 --- a/controllers/toolchaincluster/healthchecker_test.go +++ b/controllers/toolchaincluster/healthchecker_test.go @@ -96,7 +96,7 @@ func TestClusterHealthChecks(t *testing.T) { } func setupCachedClusters(t *testing.T, cl *test.FakeClient, clusters ...*toolchainv1alpha1.ToolchainCluster) func() { - service := cluster.NewToolchainClusterServiceWithClient(cl, logf.Log, "test-namespace", 0, func(config *rest.Config, options client.Options) (client.Client, error) { + service := cluster.NewToolchainClusterServiceWithClient(cl, logf.Log, test.MemberOperatorNs, 0, func(config *rest.Config, options client.Options) (client.Client, error) { // make sure that insecure is false to make Gock mocking working properly config.Insecure = false return client.New(config, options) @@ -122,7 +122,7 @@ func withStatus(conditions ...toolchainv1alpha1.ToolchainClusterCondition) toolc } func newToolchainCluster(name, apiEndpoint string, status toolchainv1alpha1.ToolchainClusterStatus) (*toolchainv1alpha1.ToolchainCluster, *corev1.Secret) { - toolchainCluster, secret := test.NewToolchainClusterWithEndpoint(name, test.MemberOperatorNs, "secret", apiEndpoint, status, map[string]string{"namespace": test.MemberOperatorNs}) + toolchainCluster, secret := test.NewToolchainClusterWithEndpoint(name, "test-namespace", "secret", apiEndpoint, status, map[string]string{"namespace": "test-namespace"}) return toolchainCluster, secret } From e34d97c0140b54b2551f43025dc86ee0b1dbe95a Mon Sep 17 00:00:00 2001 From: Feny Mehta Date: Fri, 8 Mar 2024 17:32:54 +0530 Subject: [PATCH 18/23] fixing Unit test cases Signed-off-by: Feny Mehta --- pkg/cluster/service_test.go | 8 +------- pkg/cluster/service_whitebox_test.go | 12 ++++++------ 2 files changed, 7 insertions(+), 13 deletions(-) diff --git a/pkg/cluster/service_test.go b/pkg/cluster/service_test.go index 6073701b..8e4573f2 100644 --- a/pkg/cluster/service_test.go +++ b/pkg/cluster/service_test.go @@ -69,10 +69,6 @@ func TestDeleteToolchainClusterWhenDoesNotExist(t *testing.T) { func TestListToolchainClusterConfigs(t *testing.T) { // given status := test.NewClusterStatus(toolchainv1alpha1.ToolchainClusterReady, corev1.ConditionTrue) - //m1, sec1 := test.NewToolchainClusterWithEndpoint("east", test.HostOperatorNs, "secret1", "http://m1.com", status, verify.Labels(test.MemberOperatorNs, "m1ClusterName")) - //m2, sec2 := test.NewToolchainClusterWithEndpoint("west", test.HostOperatorNs, "secret2", "http://m2.com", status, verify.Labels(test.MemberOperatorNs, "m2ClusterName")) - //host, secHost := test.NewToolchainCluster("host", test.MemberOperatorNs, "secretHost", status, verify.Labels(test.HostOperatorNs, "hostClusterName")) - //noise, secNoise := test.NewToolchainCluster("noise", "noise-namespace", "secretNoise", status, verify.Labels(test.MemberOperatorNs, "noiseClusterName")) require.NoError(t, toolchainv1alpha1.AddToScheme(scheme.Scheme)) m1, sec1 := test.NewToolchainClusterWithEndpoint("east", test.HostOperatorNs, "secret1", "http://m1.com", status, verify.Labels(test.MemberOperatorNs, "m1ClusterName")) @@ -108,11 +104,9 @@ func TestListToolchainClusterConfigs(t *testing.T) { }) t.Run("list host", func(t *testing.T) { - // given - cl := test.NewFakeClient(t, host, noise, secNoise) // when - clusterConfigs, err := cluster.ListToolchainClusterConfigs(cl, m1.Namespace, time.Second) + clusterConfigs, err := cluster.ListToolchainClusterConfigs(cl, host.Namespace, time.Second) // then require.NoError(t, err) diff --git a/pkg/cluster/service_whitebox_test.go b/pkg/cluster/service_whitebox_test.go index 46e9cd55..e33a0e41 100644 --- a/pkg/cluster/service_whitebox_test.go +++ b/pkg/cluster/service_whitebox_test.go @@ -20,12 +20,12 @@ func TestRefreshCacheInService(t *testing.T) { // given defer gock.Off() status := test.NewClusterStatus(toolchainv1alpha1.ToolchainClusterReady, corev1.ConditionTrue) - toolchainCluster, sec := test.NewToolchainCluster("east", test.HostOperatorNs, "secret", status, map[string]string{"ownerClusterName": test.NameMember, "namespace": test.HostOperatorNs}) + toolchainCluster, sec := test.NewToolchainCluster("east", test.HostOperatorNs, "secret", status, map[string]string{"ownerClusterName": test.NameMember, "namespace": test.MemberOperatorNs}) s := scheme.Scheme err := toolchainv1alpha1.AddToScheme(s) require.NoError(t, err) cl := test.NewFakeClient(t, toolchainCluster, sec) - service := newToolchainClusterService(cl, 0) + service := newToolchainClusterService(cl, 0, test.HostOperatorNs) t.Run("the member cluster should be retrieved when refreshCache func is called", func(t *testing.T) { // given @@ -81,7 +81,7 @@ func TestUpdateClientBasedOnRestConfig(t *testing.T) { t.Run("don't update when RestConfig is the same", func(t *testing.T) { // given cl := test.NewFakeClient(t, sec1) - service := newToolchainClusterService(cl, 3*time.Second) + service := newToolchainClusterService(cl, 3*time.Second, test.HostOperatorNs) defer service.DeleteToolchainCluster("east") err := service.AddOrUpdateToolchainCluster(toolchainCluster1) @@ -104,7 +104,7 @@ func TestUpdateClientBasedOnRestConfig(t *testing.T) { t.Run("update when RestConfig is not the same", func(t *testing.T) { // given cl := test.NewFakeClient(t, sec1) - service := newToolchainClusterService(cl, 3*time.Second) + service := newToolchainClusterService(cl, 3*time.Second, test.HostOperatorNs) defer service.DeleteToolchainCluster("east") err := service.AddOrUpdateToolchainCluster(toolchainCluster1) @@ -124,8 +124,8 @@ func TestUpdateClientBasedOnRestConfig(t *testing.T) { }) } -func newToolchainClusterService(cl client.Client, timeout time.Duration) ToolchainClusterService { - return NewToolchainClusterServiceWithClient(cl, logf.Log, "test-namespace", timeout, func(config *rest.Config, options client.Options) (client.Client, error) { +func newToolchainClusterService(cl client.Client, timeout time.Duration, ons string) ToolchainClusterService { + return NewToolchainClusterServiceWithClient(cl, logf.Log, ons, timeout, func(config *rest.Config, options client.Options) (client.Client, error) { // make sure that insecure is false to make Gock mocking working properly // let's use a copy of the config, so it doesn't affect the cache logic copiedConfig := rest.CopyConfig(config) From 5e5974134984b34a654044694d54f21ddfd56325 Mon Sep 17 00:00:00 2001 From: Feny Mehta Date: Mon, 11 Mar 2024 14:26:51 +0530 Subject: [PATCH 19/23] Adding the gettoolchainclusters test in parallel Signed-off-by: Feny Mehta --- pkg/cluster/cache_whitebox_test.go | 30 ++++++++++++++++++------------ pkg/test/cluster.go | 2 +- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/pkg/cluster/cache_whitebox_test.go b/pkg/cluster/cache_whitebox_test.go index c8d6e927..cb789f3e 100644 --- a/pkg/cluster/cache_whitebox_test.go +++ b/pkg/cluster/cache_whitebox_test.go @@ -336,21 +336,21 @@ func TestRefreshCache(t *testing.T) { func TestMultipleActionsInParallel(t *testing.T) { // given + clusterForTest := newTestCachedToolchainCluster(t, "clusterForTest", ready) + defer resetClusterCache() var latch sync.WaitGroup latch.Add(1) var waitForFinished sync.WaitGroup - memberCluster := newTestCachedToolchainCluster(t, "memberCluster", ready) - hostCluster := newTestCachedToolchainCluster(t, "hostCluster", ready) clusterCache.refreshCache = func() { - clusterCache.addCachedToolchainCluster(memberCluster) - clusterCache.addCachedToolchainCluster(hostCluster) + clusterCache.addCachedToolchainCluster(clusterForTest) + } - for _, clusterToTest := range []*CachedToolchainCluster{memberCluster, hostCluster} { + for _, clusterToTest := range []*CachedToolchainCluster{clusterForTest} { for i := 0; i < 1000; i++ { - waitForFinished.Add(3) + waitForFinished.Add(4) go func() { defer waitForFinished.Done() latch.Wait() @@ -366,6 +366,16 @@ func TestMultipleActionsInParallel(t *testing.T) { assert.Nil(t, cluster) } }() + go func() { + defer waitForFinished.Done() + latch.Wait() + clusters := clusterCache.getCachedToolchainClusters() + if len(clusters) == 1 { + assert.Equal(t, clusterToTest, clusters[0]) + } else { + assert.Empty(t, clusters) + } + }() go func(clusterToTest *CachedToolchainCluster) { defer waitForFinished.Done() latch.Wait() @@ -380,13 +390,9 @@ func TestMultipleActionsInParallel(t *testing.T) { // then waitForFinished.Wait() - member, ok := clusterCache.getCachedToolchainCluster("memberCluster", true) - assert.True(t, ok) - assert.Equal(t, memberCluster, member) - - host, ok := clusterCache.getCachedToolchainCluster("hostCluster", true) + clusterForTest1, ok := clusterCache.getCachedToolchainCluster("clusterForTest", true) assert.True(t, ok) - assert.Equal(t, hostCluster, host) + assert.Equal(t, clusterForTest, clusterForTest1) } // clusterOption an option to configure the cluster to use in the tests diff --git a/pkg/test/cluster.go b/pkg/test/cluster.go index 31f585a9..0ceebcf5 100644 --- a/pkg/test/cluster.go +++ b/pkg/test/cluster.go @@ -32,7 +32,7 @@ func NewToolchainClusterWithEndpoint(name, ons, secName, apiEndpoint string, sta "token": []byte("mycooltoken"), }, } - //labels["namespace"] = ons + return &toolchainv1alpha1.ToolchainCluster{ Spec: toolchainv1alpha1.ToolchainClusterSpec{ SecretRef: toolchainv1alpha1.LocalSecretReference{ From 406939cda827f260365574f207586093b82ffbbc Mon Sep 17 00:00:00 2001 From: Feny Mehta Date: Mon, 11 Mar 2024 17:18:44 +0530 Subject: [PATCH 20/23] review comments4 signed-off-by: Feny Mehta --- pkg/test/verify/cluster.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/test/verify/cluster.go b/pkg/test/verify/cluster.go index fdefc378..80858baf 100644 --- a/pkg/test/verify/cluster.go +++ b/pkg/test/verify/cluster.go @@ -70,7 +70,7 @@ func AddToolchainClusterAsHost(t *testing.T, functionToVerify FunctionToVerify) defer gock.Off() status := test.NewClusterStatus(toolchainv1alpha1.ToolchainClusterReady, corev1.ConditionFalse) memberLabels := []map[string]string{ - Labels(test.MemberOperatorNs, test.NameMember), + Labels("", test.NameMember), Labels("host-ns", test.NameMember)} for _, labels := range memberLabels { t.Run("add host ToolchainCluster", func(t *testing.T) { @@ -139,7 +139,7 @@ func AddToolchainClusterFailsBecauseOfEmptySecret(t *testing.T, functionToVerify secret := &corev1.Secret{ ObjectMeta: v1.ObjectMeta{ Name: "secret", - Namespace: "test-namespace", + Namespace: test.MemberOperatorNs, }} cl := test.NewFakeClient(t, toolchainCluster, secret) service := newToolchainClusterService(t, cl, false) From 4a1c1c58b6cc50b2cc453f838089acaeede5a7e5 Mon Sep 17 00:00:00 2001 From: Feny Mehta Date: Mon, 11 Mar 2024 20:30:32 +0530 Subject: [PATCH 21/23] Reviewcoments 5 Signed-off-by: Feny Mehta --- .../toolchaincluster/healthchecker_test.go | 20 +- pkg/cluster/cache_whitebox_test.go | 66 ++++--- pkg/cluster/service_test.go | 7 +- pkg/test/verify/cluster.go | 175 ++++++++++-------- 4 files changed, 142 insertions(+), 126 deletions(-) diff --git a/controllers/toolchaincluster/healthchecker_test.go b/controllers/toolchaincluster/healthchecker_test.go index 9d726162..41933e3d 100644 --- a/controllers/toolchaincluster/healthchecker_test.go +++ b/controllers/toolchaincluster/healthchecker_test.go @@ -35,9 +35,9 @@ func TestClusterHealthChecks(t *testing.T) { Reply(404) t.Run("ToolchainCluster.status doesn't contain any conditions", func(t *testing.T) { - unstable, sec := newToolchainCluster("unstable", "http://unstable.com", toolchainv1alpha1.ToolchainClusterStatus{}) - notFound, _ := newToolchainCluster("not-found", "http://not-found.com", toolchainv1alpha1.ToolchainClusterStatus{}) - stable, _ := newToolchainCluster("stable", "http://cluster.com", toolchainv1alpha1.ToolchainClusterStatus{}) + unstable, sec := newToolchainCluster("unstable", "test-namespace", "http://unstable.com", toolchainv1alpha1.ToolchainClusterStatus{}) + notFound, _ := newToolchainCluster("not-found", "test-namespace", "http://not-found.com", toolchainv1alpha1.ToolchainClusterStatus{}) + stable, _ := newToolchainCluster("stable", "test-namespace", "http://cluster.com", toolchainv1alpha1.ToolchainClusterStatus{}) cl := test.NewFakeClient(t, unstable, notFound, stable, sec) reset := setupCachedClusters(t, cl, unstable, notFound, stable) @@ -53,9 +53,9 @@ func TestClusterHealthChecks(t *testing.T) { }) t.Run("ToolchainCluster.status already contains conditions", func(t *testing.T) { - unstable, sec := newToolchainCluster("unstable", "http://unstable.com", withStatus(healthy())) - notFound, _ := newToolchainCluster("not-found", "http://not-found.com", withStatus(notOffline(), unhealthy())) - stable, _ := newToolchainCluster("stable", "http://cluster.com", withStatus(offline())) + unstable, sec := newToolchainCluster("unstable", "test-namespace", "http://unstable.com", withStatus(healthy())) + notFound, _ := newToolchainCluster("not-found", "test-namespace", "http://not-found.com", withStatus(notOffline(), unhealthy())) + stable, _ := newToolchainCluster("stable", "test-namespace", "http://cluster.com", withStatus(offline())) cl := test.NewFakeClient(t, unstable, notFound, stable, sec) resetCache := setupCachedClusters(t, cl, unstable, notFound, stable) defer resetCache() @@ -70,7 +70,7 @@ func TestClusterHealthChecks(t *testing.T) { }) t.Run("if no zones nor region is retrieved, then keep the current", func(t *testing.T) { - stable, sec := newToolchainCluster("stable", "http://cluster.com", withStatus(offline())) + stable, sec := newToolchainCluster("stable", "test-namespace", "http://cluster.com", withStatus(offline())) cl := test.NewFakeClient(t, stable, sec) resetCache := setupCachedClusters(t, cl, stable) defer resetCache() @@ -83,7 +83,7 @@ func TestClusterHealthChecks(t *testing.T) { }) t.Run("if the connection cannot be established at beginning, then it should be offline", func(t *testing.T) { - stable, sec := newToolchainCluster("failing", "http://failing.com", toolchainv1alpha1.ToolchainClusterStatus{}) + stable, sec := newToolchainCluster("failing", "test-namespace", "http://failing.com", toolchainv1alpha1.ToolchainClusterStatus{}) cl := test.NewFakeClient(t, stable, sec) @@ -121,8 +121,8 @@ func withStatus(conditions ...toolchainv1alpha1.ToolchainClusterCondition) toolc } } -func newToolchainCluster(name, apiEndpoint string, status toolchainv1alpha1.ToolchainClusterStatus) (*toolchainv1alpha1.ToolchainCluster, *corev1.Secret) { - toolchainCluster, secret := test.NewToolchainClusterWithEndpoint(name, "test-namespace", "secret", apiEndpoint, status, map[string]string{"namespace": "test-namespace"}) +func newToolchainCluster(name, ons string, apiEndpoint string, status toolchainv1alpha1.ToolchainClusterStatus) (*toolchainv1alpha1.ToolchainCluster, *corev1.Secret) { + toolchainCluster, secret := test.NewToolchainClusterWithEndpoint(name, ons, "secret", apiEndpoint, status, map[string]string{"namespace": "test-namespace"}) return toolchainCluster, secret } diff --git a/pkg/cluster/cache_whitebox_test.go b/pkg/cluster/cache_whitebox_test.go index cb789f3e..18efed03 100644 --- a/pkg/cluster/cache_whitebox_test.go +++ b/pkg/cluster/cache_whitebox_test.go @@ -348,40 +348,38 @@ func TestMultipleActionsInParallel(t *testing.T) { } - for _, clusterToTest := range []*CachedToolchainCluster{clusterForTest} { - for i := 0; i < 1000; i++ { - waitForFinished.Add(4) - go func() { - defer waitForFinished.Done() - latch.Wait() - clusterCache.addCachedToolchainCluster(clusterToTest) - }() - go func() { - defer waitForFinished.Done() - latch.Wait() - cluster, ok := clusterCache.getCachedToolchainCluster(clusterToTest.Name, true) - if ok { - assert.Equal(t, clusterToTest, cluster) - } else { - assert.Nil(t, cluster) - } - }() - go func() { - defer waitForFinished.Done() - latch.Wait() - clusters := clusterCache.getCachedToolchainClusters() - if len(clusters) == 1 { - assert.Equal(t, clusterToTest, clusters[0]) - } else { - assert.Empty(t, clusters) - } - }() - go func(clusterToTest *CachedToolchainCluster) { - defer waitForFinished.Done() - latch.Wait() - clusterCache.deleteCachedToolchainCluster(clusterToTest.Name) - }(clusterToTest) - } + for i := 0; i < 1000; i++ { + waitForFinished.Add(4) + go func() { + defer waitForFinished.Done() + latch.Wait() + clusterCache.addCachedToolchainCluster(clusterForTest) + }() + go func() { + defer waitForFinished.Done() + latch.Wait() + cluster, ok := clusterCache.getCachedToolchainCluster(clusterForTest.Name, true) + if ok { + assert.Equal(t, clusterForTest, cluster) + } else { + assert.Nil(t, cluster) + } + }() + go func() { + defer waitForFinished.Done() + latch.Wait() + clusters := clusterCache.getCachedToolchainClusters() + if len(clusters) == 1 { + assert.Equal(t, clusterForTest, clusters[0]) + } else { + assert.Empty(t, clusters) + } + }() + go func(clusterToTest *CachedToolchainCluster) { + defer waitForFinished.Done() + latch.Wait() + clusterCache.deleteCachedToolchainCluster(clusterToTest.Name) + }(clusterForTest) } // when diff --git a/pkg/cluster/service_test.go b/pkg/cluster/service_test.go index 8e4573f2..21c6ac8c 100644 --- a/pkg/cluster/service_test.go +++ b/pkg/cluster/service_test.go @@ -71,13 +71,12 @@ func TestListToolchainClusterConfigs(t *testing.T) { status := test.NewClusterStatus(toolchainv1alpha1.ToolchainClusterReady, corev1.ConditionTrue) require.NoError(t, toolchainv1alpha1.AddToScheme(scheme.Scheme)) - m1, sec1 := test.NewToolchainClusterWithEndpoint("east", test.HostOperatorNs, "secret1", "http://m1.com", status, verify.Labels(test.MemberOperatorNs, "m1ClusterName")) - m2, sec2 := test.NewToolchainClusterWithEndpoint("west", test.HostOperatorNs, "secret2", "http://m2.com", status, verify.Labels(test.MemberOperatorNs, "m2ClusterName")) + m1, sec1 := test.NewToolchainClusterWithEndpoint("east", test.HostOperatorNs, "secret1", "http://m1.com", status, map[string]string{"ownerClusterName": "m1ClusterName", "namespace": test.MemberOperatorNs, cluster.RoleLabel(cluster.Tenant): ""}) + m2, sec2 := test.NewToolchainClusterWithEndpoint("west", test.HostOperatorNs, "secret2", "http://m2.com", status, map[string]string{"ownerClusterName": "m2ClusterName", "namespace": test.MemberOperatorNs, cluster.RoleLabel(cluster.Tenant): ""}) host, secHost := test.NewToolchainCluster("host", test.MemberOperatorNs, "secretHost", status, verify.Labels(test.HostOperatorNs, "hostClusterName")) noise, secNoise := test.NewToolchainCluster("noise", "noise-namespace", "secretNoise", status, verify.Labels(test.MemberOperatorNs, "noiseClusterName")) require.NoError(t, toolchainv1alpha1.AddToScheme(scheme.Scheme)) - m1.Labels[cluster.RoleLabel(cluster.Tenant)] = "" - m2.Labels[cluster.RoleLabel(cluster.Tenant)] = "" + cl := test.NewFakeClient(t, m1, m2, host, noise, sec1, sec2, secHost, secNoise) t.Run("list members", func(t *testing.T) { diff --git a/pkg/test/verify/cluster.go b/pkg/test/verify/cluster.go index 80858baf..f678d8c3 100644 --- a/pkg/test/verify/cluster.go +++ b/pkg/test/verify/cluster.go @@ -24,92 +24,111 @@ func AddToolchainClusterAsMember(t *testing.T, functionToVerify FunctionToVerify // given defer gock.Off() status := test.NewClusterStatus(toolchainv1alpha1.ToolchainClusterReady, corev1.ConditionTrue) - memberLabels := []map[string]string{ - Labels("", test.NameHost), - Labels("member-ns", test.NameHost)} - - for _, labels := range memberLabels { - t.Run("add member ToolchainCluster", func(t *testing.T) { - for _, withCA := range []bool{true, false} { - toolchainCluster, sec := test.NewToolchainCluster("east", test.HostOperatorNs, "secret", status, labels) - if withCA { - toolchainCluster.Spec.CABundle = "ZHVtbXk=" - } - toolchainCluster.Labels[cluster.RoleLabel(cluster.Tenant)] = "" - cl := test.NewFakeClient(t, toolchainCluster, sec) - service := newToolchainClusterService(t, cl, withCA) - defer service.DeleteToolchainCluster("east") - // when - err := functionToVerify(toolchainCluster, cl, service) - // then - if labels["namespace"] != "" { - require.NoError(t, err) - cachedToolchainCluster, ok := cluster.GetCachedToolchainCluster("east") - require.True(t, ok) - assert.Equal(t, labels["namespace"], cachedToolchainCluster.OperatorNamespace) - // check that toolchain cluster role label tenant was set only on member cluster type - require.NoError(t, cl.Get(context.TODO(), client.ObjectKeyFromObject(toolchainCluster), toolchainCluster)) - _, found := toolchainCluster.Labels[cluster.RoleLabel(cluster.Tenant)] - require.True(t, found) - - assert.Equal(t, status, *cachedToolchainCluster.ClusterStatus) - assert.Equal(t, test.NameHost, cachedToolchainCluster.OwnerClusterName) - assert.Equal(t, "http://cluster.com", cachedToolchainCluster.APIEndpoint) - } else { - require.Error(t, err) - _, ok := cluster.GetCachedToolchainCluster("east") - require.False(t, ok) - } + + t.Run("add member ToolchainCluster with namespace label set", func(t *testing.T) { + for _, withCA := range []bool{true, false} { + toolchainCluster, sec := test.NewToolchainCluster("east", test.HostOperatorNs, "secret", status, Labels("member-ns", test.NameHost)) + if withCA { + toolchainCluster.Spec.CABundle = "ZHVtbXk=" } - }) - } + toolchainCluster.Labels[cluster.RoleLabel(cluster.Tenant)] = "" + cl := test.NewFakeClient(t, toolchainCluster, sec) + service := newToolchainClusterService(t, cl, withCA) + defer service.DeleteToolchainCluster("east") + // when + err := functionToVerify(toolchainCluster, cl, service) + // then + require.NoError(t, err) + cachedToolchainCluster, ok := cluster.GetCachedToolchainCluster("east") + require.True(t, ok) + assert.Equal(t, "member-ns", cachedToolchainCluster.OperatorNamespace) + // check that toolchain cluster role label tenant was set only on member cluster type + require.NoError(t, cl.Get(context.TODO(), client.ObjectKeyFromObject(toolchainCluster), toolchainCluster)) + _, found := toolchainCluster.Labels[cluster.RoleLabel(cluster.Tenant)] + require.True(t, found) + assert.Equal(t, status, *cachedToolchainCluster.ClusterStatus) + assert.Equal(t, test.NameHost, cachedToolchainCluster.OwnerClusterName) + assert.Equal(t, "http://cluster.com", cachedToolchainCluster.APIEndpoint) + + } + }) + + t.Run("add member ToolchainCluster without namespace label set should fail", func(t *testing.T) { + for _, withCA := range []bool{true, false} { + toolchainCluster, sec := test.NewToolchainCluster("east", test.HostOperatorNs, "secret", status, Labels("", test.NameHost)) + if withCA { + toolchainCluster.Spec.CABundle = "ZHVtbXk=" + } + toolchainCluster.Labels[cluster.RoleLabel(cluster.Tenant)] = "" + cl := test.NewFakeClient(t, toolchainCluster, sec) + service := newToolchainClusterService(t, cl, withCA) + defer service.DeleteToolchainCluster("east") + // when + err := functionToVerify(toolchainCluster, cl, service) + // then + require.Error(t, err) + _, ok := cluster.GetCachedToolchainCluster("east") + require.False(t, ok) + + } + }) } func AddToolchainClusterAsHost(t *testing.T, functionToVerify FunctionToVerify) { // given defer gock.Off() status := test.NewClusterStatus(toolchainv1alpha1.ToolchainClusterReady, corev1.ConditionFalse) - memberLabels := []map[string]string{ - Labels("", test.NameMember), - Labels("host-ns", test.NameMember)} - for _, labels := range memberLabels { - t.Run("add host ToolchainCluster", func(t *testing.T) { - for _, withCA := range []bool{true, false} { - toolchainCluster, sec := test.NewToolchainCluster("east", test.MemberOperatorNs, "secret", status, labels) - if withCA { - toolchainCluster.Spec.CABundle = "ZHVtbXk=" - } - cl := test.NewFakeClient(t, toolchainCluster, sec) - service := newToolchainClusterService(t, cl, withCA) - defer service.DeleteToolchainCluster("east") - - // when - err := functionToVerify(toolchainCluster, cl, service) - - if labels["namespace"] != "" { - // then - require.NoError(t, err) - cachedToolchainCluster, ok := cluster.GetCachedToolchainCluster("east") - require.True(t, ok) - - assert.Equal(t, labels["namespace"], cachedToolchainCluster.OperatorNamespace) - - // check that toolchain cluster role label tenant is not set on host cluster - require.NoError(t, cl.Get(context.TODO(), client.ObjectKeyFromObject(toolchainCluster), toolchainCluster)) - expectedToolChainClusterRoleLabel := cluster.RoleLabel(cluster.Tenant) - _, found := toolchainCluster.Labels[expectedToolChainClusterRoleLabel] - require.False(t, found) - assert.Equal(t, status, *cachedToolchainCluster.ClusterStatus) - assert.Equal(t, test.NameMember, cachedToolchainCluster.OwnerClusterName) - assert.Equal(t, "http://cluster.com", cachedToolchainCluster.APIEndpoint) - } else { - require.Error(t, err) - _, ok := cluster.GetCachedToolchainCluster("east") - require.False(t, ok) - } + t.Run("add host ToolchainCluster with namespace label set", func(t *testing.T) { + for _, withCA := range []bool{true, false} { + toolchainCluster, sec := test.NewToolchainCluster("east", test.MemberOperatorNs, "secret", status, Labels("host-ns", test.NameMember)) + if withCA { + toolchainCluster.Spec.CABundle = "ZHVtbXk=" } - }) - } + cl := test.NewFakeClient(t, toolchainCluster, sec) + service := newToolchainClusterService(t, cl, withCA) + defer service.DeleteToolchainCluster("east") + + // when + err := functionToVerify(toolchainCluster, cl, service) + + // then + require.NoError(t, err) + cachedToolchainCluster, ok := cluster.GetCachedToolchainCluster("east") + require.True(t, ok) + + assert.Equal(t, "host-ns", cachedToolchainCluster.OperatorNamespace) + + // check that toolchain cluster role label tenant is not set on host cluster + require.NoError(t, cl.Get(context.TODO(), client.ObjectKeyFromObject(toolchainCluster), toolchainCluster)) + expectedToolChainClusterRoleLabel := cluster.RoleLabel(cluster.Tenant) + _, found := toolchainCluster.Labels[expectedToolChainClusterRoleLabel] + require.False(t, found) + assert.Equal(t, status, *cachedToolchainCluster.ClusterStatus) + assert.Equal(t, test.NameMember, cachedToolchainCluster.OwnerClusterName) + assert.Equal(t, "http://cluster.com", cachedToolchainCluster.APIEndpoint) + } + }) + + t.Run("add host ToolchainCluster without namespace label set should fail", func(t *testing.T) { + for _, withCA := range []bool{true, false} { + toolchainCluster, sec := test.NewToolchainCluster("east", test.MemberOperatorNs, "secret", status, Labels("", test.NameMember)) + if withCA { + toolchainCluster.Spec.CABundle = "ZHVtbXk=" + } + cl := test.NewFakeClient(t, toolchainCluster, sec) + service := newToolchainClusterService(t, cl, withCA) + defer service.DeleteToolchainCluster("east") + + // when + err := functionToVerify(toolchainCluster, cl, service) + + // then + require.Error(t, err) + _, ok := cluster.GetCachedToolchainCluster("east") + require.False(t, ok) + + } + }) } func AddToolchainClusterFailsBecauseOfMissingSecret(t *testing.T, functionToVerify FunctionToVerify) { From e0997be33b27dd54dbfb9a250772d058e0ef1017 Mon Sep 17 00:00:00 2001 From: Feny Mehta Date: Mon, 11 Mar 2024 20:41:06 +0530 Subject: [PATCH 22/23] go-lint fix Signed-off-by: Feny Mehta --- .../toolchaincluster/healthchecker_test.go | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/controllers/toolchaincluster/healthchecker_test.go b/controllers/toolchaincluster/healthchecker_test.go index 41933e3d..e8ec987b 100644 --- a/controllers/toolchaincluster/healthchecker_test.go +++ b/controllers/toolchaincluster/healthchecker_test.go @@ -19,6 +19,7 @@ import ( func TestClusterHealthChecks(t *testing.T) { // given defer gock.Off() + ons := "test-namespace" gock.New("http://cluster.com"). Get("healthz"). Persist(). @@ -35,9 +36,9 @@ func TestClusterHealthChecks(t *testing.T) { Reply(404) t.Run("ToolchainCluster.status doesn't contain any conditions", func(t *testing.T) { - unstable, sec := newToolchainCluster("unstable", "test-namespace", "http://unstable.com", toolchainv1alpha1.ToolchainClusterStatus{}) - notFound, _ := newToolchainCluster("not-found", "test-namespace", "http://not-found.com", toolchainv1alpha1.ToolchainClusterStatus{}) - stable, _ := newToolchainCluster("stable", "test-namespace", "http://cluster.com", toolchainv1alpha1.ToolchainClusterStatus{}) + unstable, sec := newToolchainCluster("unstable", ons, "http://unstable.com", toolchainv1alpha1.ToolchainClusterStatus{}) + notFound, _ := newToolchainCluster("not-found", ons, "http://not-found.com", toolchainv1alpha1.ToolchainClusterStatus{}) + stable, _ := newToolchainCluster("stable", ons, "http://cluster.com", toolchainv1alpha1.ToolchainClusterStatus{}) cl := test.NewFakeClient(t, unstable, notFound, stable, sec) reset := setupCachedClusters(t, cl, unstable, notFound, stable) @@ -53,9 +54,9 @@ func TestClusterHealthChecks(t *testing.T) { }) t.Run("ToolchainCluster.status already contains conditions", func(t *testing.T) { - unstable, sec := newToolchainCluster("unstable", "test-namespace", "http://unstable.com", withStatus(healthy())) - notFound, _ := newToolchainCluster("not-found", "test-namespace", "http://not-found.com", withStatus(notOffline(), unhealthy())) - stable, _ := newToolchainCluster("stable", "test-namespace", "http://cluster.com", withStatus(offline())) + unstable, sec := newToolchainCluster("unstable", ons, "http://unstable.com", withStatus(healthy())) + notFound, _ := newToolchainCluster("not-found", ons, "http://not-found.com", withStatus(notOffline(), unhealthy())) + stable, _ := newToolchainCluster("stable", ons, "http://cluster.com", withStatus(offline())) cl := test.NewFakeClient(t, unstable, notFound, stable, sec) resetCache := setupCachedClusters(t, cl, unstable, notFound, stable) defer resetCache() @@ -70,7 +71,7 @@ func TestClusterHealthChecks(t *testing.T) { }) t.Run("if no zones nor region is retrieved, then keep the current", func(t *testing.T) { - stable, sec := newToolchainCluster("stable", "test-namespace", "http://cluster.com", withStatus(offline())) + stable, sec := newToolchainCluster("stable", ons, "http://cluster.com", withStatus(offline())) cl := test.NewFakeClient(t, stable, sec) resetCache := setupCachedClusters(t, cl, stable) defer resetCache() @@ -83,7 +84,7 @@ func TestClusterHealthChecks(t *testing.T) { }) t.Run("if the connection cannot be established at beginning, then it should be offline", func(t *testing.T) { - stable, sec := newToolchainCluster("failing", "test-namespace", "http://failing.com", toolchainv1alpha1.ToolchainClusterStatus{}) + stable, sec := newToolchainCluster("failing", ons, "http://failing.com", toolchainv1alpha1.ToolchainClusterStatus{}) cl := test.NewFakeClient(t, stable, sec) From 069072d7d7e16e49773b17868ca6c2f5ef612f2a Mon Sep 17 00:00:00 2001 From: Feny Mehta Date: Tue, 12 Mar 2024 16:08:52 +0530 Subject: [PATCH 23/23] Updating the name to tcns to avoid confusion Signed-off-by: Feny Mehta --- .../toolchaincluster/healthchecker_test.go | 22 +++++++++---------- pkg/cluster/service_whitebox_test.go | 4 ++-- pkg/test/cluster.go | 10 ++++----- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/controllers/toolchaincluster/healthchecker_test.go b/controllers/toolchaincluster/healthchecker_test.go index e8ec987b..c7c088bc 100644 --- a/controllers/toolchaincluster/healthchecker_test.go +++ b/controllers/toolchaincluster/healthchecker_test.go @@ -19,7 +19,7 @@ import ( func TestClusterHealthChecks(t *testing.T) { // given defer gock.Off() - ons := "test-namespace" + tcNs := "test-namespace" gock.New("http://cluster.com"). Get("healthz"). Persist(). @@ -36,9 +36,9 @@ func TestClusterHealthChecks(t *testing.T) { Reply(404) t.Run("ToolchainCluster.status doesn't contain any conditions", func(t *testing.T) { - unstable, sec := newToolchainCluster("unstable", ons, "http://unstable.com", toolchainv1alpha1.ToolchainClusterStatus{}) - notFound, _ := newToolchainCluster("not-found", ons, "http://not-found.com", toolchainv1alpha1.ToolchainClusterStatus{}) - stable, _ := newToolchainCluster("stable", ons, "http://cluster.com", toolchainv1alpha1.ToolchainClusterStatus{}) + unstable, sec := newToolchainCluster("unstable", tcNs, "http://unstable.com", toolchainv1alpha1.ToolchainClusterStatus{}) + notFound, _ := newToolchainCluster("not-found", tcNs, "http://not-found.com", toolchainv1alpha1.ToolchainClusterStatus{}) + stable, _ := newToolchainCluster("stable", tcNs, "http://cluster.com", toolchainv1alpha1.ToolchainClusterStatus{}) cl := test.NewFakeClient(t, unstable, notFound, stable, sec) reset := setupCachedClusters(t, cl, unstable, notFound, stable) @@ -54,9 +54,9 @@ func TestClusterHealthChecks(t *testing.T) { }) t.Run("ToolchainCluster.status already contains conditions", func(t *testing.T) { - unstable, sec := newToolchainCluster("unstable", ons, "http://unstable.com", withStatus(healthy())) - notFound, _ := newToolchainCluster("not-found", ons, "http://not-found.com", withStatus(notOffline(), unhealthy())) - stable, _ := newToolchainCluster("stable", ons, "http://cluster.com", withStatus(offline())) + unstable, sec := newToolchainCluster("unstable", tcNs, "http://unstable.com", withStatus(healthy())) + notFound, _ := newToolchainCluster("not-found", tcNs, "http://not-found.com", withStatus(notOffline(), unhealthy())) + stable, _ := newToolchainCluster("stable", tcNs, "http://cluster.com", withStatus(offline())) cl := test.NewFakeClient(t, unstable, notFound, stable, sec) resetCache := setupCachedClusters(t, cl, unstable, notFound, stable) defer resetCache() @@ -71,7 +71,7 @@ func TestClusterHealthChecks(t *testing.T) { }) t.Run("if no zones nor region is retrieved, then keep the current", func(t *testing.T) { - stable, sec := newToolchainCluster("stable", ons, "http://cluster.com", withStatus(offline())) + stable, sec := newToolchainCluster("stable", tcNs, "http://cluster.com", withStatus(offline())) cl := test.NewFakeClient(t, stable, sec) resetCache := setupCachedClusters(t, cl, stable) defer resetCache() @@ -84,7 +84,7 @@ func TestClusterHealthChecks(t *testing.T) { }) t.Run("if the connection cannot be established at beginning, then it should be offline", func(t *testing.T) { - stable, sec := newToolchainCluster("failing", ons, "http://failing.com", toolchainv1alpha1.ToolchainClusterStatus{}) + stable, sec := newToolchainCluster("failing", tcNs, "http://failing.com", toolchainv1alpha1.ToolchainClusterStatus{}) cl := test.NewFakeClient(t, stable, sec) @@ -122,8 +122,8 @@ func withStatus(conditions ...toolchainv1alpha1.ToolchainClusterCondition) toolc } } -func newToolchainCluster(name, ons string, apiEndpoint string, status toolchainv1alpha1.ToolchainClusterStatus) (*toolchainv1alpha1.ToolchainCluster, *corev1.Secret) { - toolchainCluster, secret := test.NewToolchainClusterWithEndpoint(name, ons, "secret", apiEndpoint, status, map[string]string{"namespace": "test-namespace"}) +func newToolchainCluster(name, tcNs string, apiEndpoint string, status toolchainv1alpha1.ToolchainClusterStatus) (*toolchainv1alpha1.ToolchainCluster, *corev1.Secret) { + toolchainCluster, secret := test.NewToolchainClusterWithEndpoint(name, tcNs, "secret", apiEndpoint, status, map[string]string{"namespace": "test-namespace"}) return toolchainCluster, secret } diff --git a/pkg/cluster/service_whitebox_test.go b/pkg/cluster/service_whitebox_test.go index e33a0e41..004d7638 100644 --- a/pkg/cluster/service_whitebox_test.go +++ b/pkg/cluster/service_whitebox_test.go @@ -124,8 +124,8 @@ func TestUpdateClientBasedOnRestConfig(t *testing.T) { }) } -func newToolchainClusterService(cl client.Client, timeout time.Duration, ons string) ToolchainClusterService { - return NewToolchainClusterServiceWithClient(cl, logf.Log, ons, timeout, func(config *rest.Config, options client.Options) (client.Client, error) { +func newToolchainClusterService(cl client.Client, timeout time.Duration, tcNs string) ToolchainClusterService { + return NewToolchainClusterServiceWithClient(cl, logf.Log, tcNs, timeout, func(config *rest.Config, options client.Options) (client.Client, error) { // make sure that insecure is false to make Gock mocking working properly // let's use a copy of the config, so it doesn't affect the cache logic copiedConfig := rest.CopyConfig(config) diff --git a/pkg/test/cluster.go b/pkg/test/cluster.go index 0ceebcf5..74984053 100644 --- a/pkg/test/cluster.go +++ b/pkg/test/cluster.go @@ -12,11 +12,11 @@ const ( NameMember = "east" ) -func NewToolchainCluster(name, ons, secName string, status toolchainv1alpha1.ToolchainClusterStatus, labels map[string]string) (*toolchainv1alpha1.ToolchainCluster, *corev1.Secret) { - return NewToolchainClusterWithEndpoint(name, ons, secName, "http://cluster.com", status, labels) +func NewToolchainCluster(name, tcNs, secName string, status toolchainv1alpha1.ToolchainClusterStatus, labels map[string]string) (*toolchainv1alpha1.ToolchainCluster, *corev1.Secret) { + return NewToolchainClusterWithEndpoint(name, tcNs, secName, "http://cluster.com", status, labels) } -func NewToolchainClusterWithEndpoint(name, ons, secName, apiEndpoint string, status toolchainv1alpha1.ToolchainClusterStatus, labels map[string]string) (*toolchainv1alpha1.ToolchainCluster, *corev1.Secret) { +func NewToolchainClusterWithEndpoint(name, tcNs, secName, apiEndpoint string, status toolchainv1alpha1.ToolchainClusterStatus, labels map[string]string) (*toolchainv1alpha1.ToolchainCluster, *corev1.Secret) { gock.New(apiEndpoint). Get("api"). Persist(). @@ -25,7 +25,7 @@ func NewToolchainClusterWithEndpoint(name, ons, secName, apiEndpoint string, sta secret := &corev1.Secret{ ObjectMeta: v1.ObjectMeta{ Name: secName, - Namespace: ons, + Namespace: tcNs, }, Type: corev1.SecretTypeOpaque, Data: map[string][]byte{ @@ -43,7 +43,7 @@ func NewToolchainClusterWithEndpoint(name, ons, secName, apiEndpoint string, sta }, ObjectMeta: v1.ObjectMeta{ Name: name, - Namespace: ons, + Namespace: tcNs, Labels: labels, }, Status: status,