diff --git a/pkg/cluster/cache.go b/pkg/cluster/cache.go index 1b495671..b54a09e0 100644 --- a/pkg/cluster/cache.go +++ b/pkg/cluster/cache.go @@ -103,6 +103,24 @@ func GetCachedToolchainCluster(name string) (*CachedToolchainCluster, bool) { return clusterCache.getCachedToolchainCluster(name, true) } +// GetToolchainClustersCachedFunc a func that returns all the clusters from the cache +type GetToolchainClustersCachedFunc func(conditions ...Condition) []*CachedToolchainCluster + +// ToolchainClustersCached the func to retrieve all the clusters +var ToolchainClustersCached GetToolchainClustersCachedFunc = GetToolchainClustersCached + +// GetToolchainClustersCached returns the kube clients for all the clusters from the cache of the clusters +func GetToolchainClustersCached(conditions ...Condition) []*CachedToolchainCluster { + clusters := clusterCache.getCachedToolchainClusters(conditions...) + if len(clusters) == 0 { + if clusterCache.refreshCache != nil { + clusterCache.refreshCache() + } + clusters = clusterCache.getCachedToolchainClusters(conditions...) + } + return clusters +} + // GetHostClusterFunc a func that returns the Host cluster from the cache, // along with a bool to indicate if there was a match or not type GetHostClusterFunc func() (*CachedToolchainCluster, bool) @@ -126,24 +144,6 @@ func GetHostCluster() (*CachedToolchainCluster, bool) { return clusters[0], true } -// GetMemberClustersFunc a func that returns the member clusters from the cache -type GetMemberClustersFunc func(conditions ...Condition) []*CachedToolchainCluster - -// MemberClusters the func to retrieve the member clusters -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.getCachedToolchainClusters(conditions...) - if len(clusters) == 0 { - if clusterCache.refreshCache != nil { - clusterCache.refreshCache() - } - clusters = clusterCache.getCachedToolchainClusters(conditions...) - } - return clusters -} - // 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 18efed03..ac9066ad 100644 --- a/pkg/cluster/cache_whitebox_test.go +++ b/pkg/cluster/cache_whitebox_test.go @@ -7,7 +7,6 @@ import ( toolchainv1alpha1 "github.com/codeready-toolchain/api/api/v1alpha1" "github.com/codeready-toolchain/toolchain-common/pkg/test" "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" v1 "k8s.io/api/core/v1" ) @@ -51,37 +50,6 @@ func TestGetCluster(t *testing.T) { } } -func TestHostCluster(t *testing.T) { - // given - defer resetClusterCache() - host := newTestCachedToolchainCluster(t, "host-cluster", ready) - clusterCache.addCachedToolchainCluster(host) - - // when - returnedCachedCluster, ok := HostCluster() - - // then - assert.True(t, ok) - assert.Equal(t, host, returnedCachedCluster) -} - -func TestMemberClusters(t *testing.T) { - // given - defer resetClusterCache() - member1 := newTestCachedToolchainCluster(t, "member-cluster-1", ready) - clusterCache.addCachedToolchainCluster(member1) - member2 := newTestCachedToolchainCluster(t, "member-cluster-2", ready) - clusterCache.addCachedToolchainCluster(member2) - - // when - returnedCachedClusters := MemberClusters() - - // then - require.Len(t, returnedCachedClusters, 2) - assert.Contains(t, returnedCachedClusters, member1) - assert.Contains(t, returnedCachedClusters, member2) -} - func TestGetClusterWhenIsEmpty(t *testing.T) { // given resetClusterCache() @@ -97,17 +65,17 @@ func TestGetClusterWhenIsEmpty(t *testing.T) { } } -func TestGetClustersByType(t *testing.T) { +func TestGetClusters(t *testing.T) { - t.Run("get member clusters", func(t *testing.T) { + t.Run("get clusters", func(t *testing.T) { t.Run("not found", func(t *testing.T) { // given defer resetClusterCache() - // no members + // no clusters //when - clusters := GetMemberClusters() + clusters := GetToolchainClustersCached() //then assert.Empty(t, clusters) @@ -116,111 +84,68 @@ func TestGetClustersByType(t *testing.T) { t.Run("all clusters", func(t *testing.T) { // given defer resetClusterCache() + host := newTestCachedToolchainCluster(t, "cluster-host", ready) + clusterCache.addCachedToolchainCluster(host) member1 := newTestCachedToolchainCluster(t, "cluster-1", ready) clusterCache.addCachedToolchainCluster(member1) member2 := newTestCachedToolchainCluster(t, "cluster-2", ready) clusterCache.addCachedToolchainCluster(member2) //when - clusters := GetMemberClusters() + clusters := GetToolchainClustersCached() //then - assert.Len(t, clusters, 2) + assert.Len(t, clusters, 3) assert.Contains(t, clusters, member1) assert.Contains(t, clusters, member2) + assert.Contains(t, clusters, host) }) t.Run("found after refreshing the cache", func(t *testing.T) { // given defer resetClusterCache() - member := newTestCachedToolchainCluster(t, "member", ready) + clustertest := newTestCachedToolchainCluster(t, "clustertotest", ready) called := false clusterCache.refreshCache = func() { called = true - clusterCache.addCachedToolchainCluster(member) + clusterCache.addCachedToolchainCluster(clustertest) } //when - clusters := GetMemberClusters() + clusters := GetToolchainClustersCached() //then assert.Len(t, clusters, 1) - assert.Contains(t, clusters, member) + assert.Contains(t, clusters, clustertest) assert.True(t, called) }) - }) + t.Run("get clusters filtered by readiness and capacity", func(t *testing.T) { + defer resetClusterCache() - t.Run("get member clusters filtered by readiness and capacity", func(t *testing.T) { - defer resetClusterCache() + host := newTestCachedToolchainCluster(t, "cluster-host", ready) + clusterCache.addCachedToolchainCluster(host) + member1 := newTestCachedToolchainCluster(t, "cluster-1", ready) + clusterCache.addCachedToolchainCluster(member1) + member2 := newTestCachedToolchainCluster(t, "cluster-2", ready) + clusterCache.addCachedToolchainCluster(member2) + // noise + member3 := newTestCachedToolchainCluster(t, "cluster-3", notReady) + clusterCache.addCachedToolchainCluster(member3) - // noise - host := newTestCachedToolchainCluster(t, "cluster-host", ready) - clusterCache.addCachedToolchainCluster(host) - member1 := newTestCachedToolchainCluster(t, "cluster-1", ready) - clusterCache.addCachedToolchainCluster(member1) - member2 := newTestCachedToolchainCluster(t, "cluster-2", ready) - clusterCache.addCachedToolchainCluster(member2) - member3 := newTestCachedToolchainCluster(t, "cluster-3", notReady) - clusterCache.addCachedToolchainCluster(member3) - - t.Run("get only ready member clusters", func(t *testing.T) { //when - clusters := GetMemberClusters(Ready) + clusters := GetToolchainClustersCached(Ready) //then assert.Len(t, clusters, 3) assert.Contains(t, clusters, member1) assert.Contains(t, clusters, member2) - }) - }) - - 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() + assert.Contains(t, clusters, host) - //then - assert.False(t, ok) - }) - t.Run("found", func(t *testing.T) { - // given - defer resetClusterCache() - host := newTestCachedToolchainCluster(t, "cluster-host", ready) - clusterCache.addCachedToolchainCluster(host) - - //when - cluster, ok := GetHostCluster() - - //then - assert.True(t, ok) - assert.Equal(t, host, cluster) }) - t.Run("found after refreshing the cache", func(t *testing.T) { - // given - defer resetClusterCache() - host := newTestCachedToolchainCluster(t, "cluster-host", ready) - called := false - clusterCache.refreshCache = func() { - called = true - clusterCache.addCachedToolchainCluster(host) - } - - //when - cluster, ok := GetHostCluster() - - //then - assert.True(t, ok) - assert.Equal(t, host, cluster) - assert.True(t, called) - }) }) + } func TestGetClusterUsingDifferentKey(t *testing.T) { diff --git a/pkg/cluster/service.go b/pkg/cluster/service.go index 0d3bec3b..58097774 100644 --- a/pkg/cluster/service.go +++ b/pkg/cluster/service.go @@ -227,9 +227,9 @@ func IsReady(clusterStatus *toolchainv1alpha1.ToolchainClusterStatus) bool { return false } -func ListToolchainClusterConfigs(cl client.Client, namespace string, timeout time.Duration) ([]*Config, error) { +func ListToolchainClusterConfigs(ctx context.Context, cl client.Client, namespace string, timeout time.Duration) ([]*Config, error) { toolchainClusters := &toolchainv1alpha1.ToolchainClusterList{} - if err := cl.List(context.TODO(), toolchainClusters, client.InNamespace(namespace)); err != nil { + if err := cl.List(ctx, 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 21c6ac8c..7a7abf8d 100644 --- a/pkg/cluster/service_test.go +++ b/pkg/cluster/service_test.go @@ -81,7 +81,7 @@ func TestListToolchainClusterConfigs(t *testing.T) { t.Run("list members", func(t *testing.T) { // when - clusterConfigs, err := cluster.ListToolchainClusterConfigs(cl, m1.Namespace, time.Second) + clusterConfigs, err := cluster.ListToolchainClusterConfigs(context.TODO(), cl, m1.Namespace, time.Second) // then require.NoError(t, err) @@ -105,7 +105,7 @@ func TestListToolchainClusterConfigs(t *testing.T) { t.Run("list host", func(t *testing.T) { // when - clusterConfigs, err := cluster.ListToolchainClusterConfigs(cl, host.Namespace, time.Second) + clusterConfigs, err := cluster.ListToolchainClusterConfigs(context.TODO(), cl, host.Namespace, time.Second) // then require.NoError(t, err) @@ -124,7 +124,7 @@ func TestListToolchainClusterConfigs(t *testing.T) { cl := test.NewFakeClient(t, host, noise, secNoise) // when - clusterConfigs, err := cluster.ListToolchainClusterConfigs(cl, m1.Namespace, time.Second) + clusterConfigs, err := cluster.ListToolchainClusterConfigs(context.TODO(), cl, m1.Namespace, time.Second) // then require.NoError(t, err) @@ -139,7 +139,7 @@ func TestListToolchainClusterConfigs(t *testing.T) { } // when - clusterConfigs, err := cluster.ListToolchainClusterConfigs(cl, m1.Namespace, time.Second) + clusterConfigs, err := cluster.ListToolchainClusterConfigs(context.TODO(), cl, m1.Namespace, time.Second) // then require.Error(t, err) @@ -154,7 +154,7 @@ func TestListToolchainClusterConfigs(t *testing.T) { } // when - clusterConfigs, err := cluster.ListToolchainClusterConfigs(cl, m1.Namespace, time.Second) + clusterConfigs, err := cluster.ListToolchainClusterConfigs(context.TODO(), cl, m1.Namespace, time.Second) // then require.Error(t, err) diff --git a/pkg/status/toolchaincluster.go b/pkg/status/toolchaincluster.go index be4d3ac7..f4c7817b 100644 --- a/pkg/status/toolchaincluster.go +++ b/pkg/status/toolchaincluster.go @@ -28,6 +28,7 @@ type ToolchainClusterAttributes struct { func GetToolchainClusterConditions(logger logr.Logger, attrs ToolchainClusterAttributes) []toolchainv1alpha1.Condition { // look up cluster connection status toolchainCluster, ok := attrs.GetClusterFunc() + if !ok { return []toolchainv1alpha1.Condition{*NewComponentErrorCondition(toolchainv1alpha1.ToolchainStatusClusterConnectionNotFoundReason, ErrMsgClusterConnectionNotFound)} }