diff --git a/pkg/cluster/cache.go b/pkg/cluster/cache.go index e7fde1e9..b54a09e0 100644 --- a/pkg/cluster/cache.go +++ b/pkg/cluster/cache.go @@ -103,14 +103,14 @@ func GetCachedToolchainCluster(name string) (*CachedToolchainCluster, bool) { return clusterCache.getCachedToolchainCluster(name, true) } -// GetClustersFunc a func that returns all the clusters from the cache -type GetClustersFunc func(conditions ...Condition) []*CachedToolchainCluster +// GetToolchainClustersCachedFunc a func that returns all the clusters from the cache +type GetToolchainClustersCachedFunc func(conditions ...Condition) []*CachedToolchainCluster -// Clusters the func to retrieve all the clusters -var Clusters GetClustersFunc = GetClusters +// ToolchainClustersCached the func to retrieve all the clusters +var ToolchainClustersCached GetToolchainClustersCachedFunc = GetToolchainClustersCached -// GetClusters returns the kube clients for all the clusters from the cache of the clusters -func GetClusters(conditions ...Condition) []*CachedToolchainCluster { +// 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 { @@ -144,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 28dda373..ac9066ad 100644 --- a/pkg/cluster/cache_whitebox_test.go +++ b/pkg/cluster/cache_whitebox_test.go @@ -75,7 +75,7 @@ func TestGetClusters(t *testing.T) { // no clusters //when - clusters := GetClusters() + clusters := GetToolchainClustersCached() //then assert.Empty(t, clusters) @@ -92,7 +92,7 @@ func TestGetClusters(t *testing.T) { clusterCache.addCachedToolchainCluster(member2) //when - clusters := GetClusters() + clusters := GetToolchainClustersCached() //then assert.Len(t, clusters, 3) @@ -112,7 +112,7 @@ func TestGetClusters(t *testing.T) { } //when - clusters := GetClusters() + clusters := GetToolchainClustersCached() //then assert.Len(t, clusters, 1) @@ -134,7 +134,7 @@ func TestGetClusters(t *testing.T) { clusterCache.addCachedToolchainCluster(member3) //when - clusters := GetClusters(Ready) + clusters := GetToolchainClustersCached(Ready) //then assert.Len(t, clusters, 3) diff --git a/pkg/status/toolchaincluster.go b/pkg/status/toolchaincluster.go index 062be6f9..f4c7817b 100644 --- a/pkg/status/toolchaincluster.go +++ b/pkg/status/toolchaincluster.go @@ -19,20 +19,19 @@ const ( // ToolchainClusterAttributes required attributes for obtaining ToolchainCluster status type ToolchainClusterAttributes struct { - GetClustersFunc cluster.GetClustersFunc - Period time.Duration - Timeout time.Duration + GetClusterFunc func() (*cluster.CachedToolchainCluster, bool) + Period time.Duration + Timeout time.Duration } // GetToolchainClusterConditions uses the provided ToolchainCluster attributes to determine status conditions func GetToolchainClusterConditions(logger logr.Logger, attrs ToolchainClusterAttributes) []toolchainv1alpha1.Condition { // look up cluster connection status - toolchainClusters := attrs.GetClustersFunc() + toolchainCluster, ok := attrs.GetClusterFunc() - if len(toolchainClusters) != 1 { + if !ok { return []toolchainv1alpha1.Condition{*NewComponentErrorCondition(toolchainv1alpha1.ToolchainStatusClusterConnectionNotFoundReason, ErrMsgClusterConnectionNotFound)} } - toolchainCluster := toolchainClusters[0] // check conditions of cluster connection if !cluster.IsReady(toolchainCluster.ClusterStatus) { diff --git a/pkg/status/toolchaincluster_test.go b/pkg/status/toolchaincluster_test.go index 5749d769..8811066c 100644 --- a/pkg/status/toolchaincluster_test.go +++ b/pkg/status/toolchaincluster_test.go @@ -27,9 +27,9 @@ func TestGetToolchainClusterConditions(t *testing.T) { t.Run("condition ready", func(t *testing.T) { // given readyAttrs := ToolchainClusterAttributes{ - GetClustersFunc: newGetHostClusterReady(), - Period: 10 * time.Second, - Timeout: 3 * time.Second, + GetClusterFunc: newGetHostClusterReady(), + Period: 10 * time.Second, + Timeout: 3 * time.Second, } expected := toolchainv1alpha1.Condition{ Type: toolchainv1alpha1.ConditionReady, @@ -51,9 +51,9 @@ func TestGetToolchainClusterConditions(t *testing.T) { // given msg := "the cluster connection was not found" readyAttrs := ToolchainClusterAttributes{ - GetClustersFunc: newGetHostClusterNotOk(), - Period: 10 * time.Second, - Timeout: 3 * time.Second, + GetClusterFunc: newGetHostClusterNotOk(), + Period: 10 * time.Second, + Timeout: 3 * time.Second, } expected := toolchainv1alpha1.Condition{ Type: toolchainv1alpha1.ConditionReady, @@ -75,9 +75,9 @@ func TestGetToolchainClusterConditions(t *testing.T) { t.Run("condition cluster ok but not ready", func(t *testing.T) { // given readyAttrs := ToolchainClusterAttributes{ - GetClustersFunc: newGetHostClusterOkButNotReady(fakeToolchainClusterMsg), - Period: 10 * time.Second, - Timeout: 3 * time.Second, + GetClusterFunc: newGetHostClusterOkButNotReady(fakeToolchainClusterMsg), + Period: 10 * time.Second, + Timeout: 3 * time.Second, } expected := toolchainv1alpha1.Condition{ Type: toolchainv1alpha1.ConditionReady, @@ -100,9 +100,9 @@ func TestGetToolchainClusterConditions(t *testing.T) { // given msg := "the cluster connection is not ready" readyAttrs := ToolchainClusterAttributes{ - GetClustersFunc: newGetHostClusterOkButNotReady(""), - Period: 10 * time.Second, - Timeout: 3 * time.Second, + GetClusterFunc: newGetHostClusterOkButNotReady(""), + Period: 10 * time.Second, + Timeout: 3 * time.Second, } expected := toolchainv1alpha1.Condition{ Type: toolchainv1alpha1.ConditionReady, @@ -125,9 +125,9 @@ func TestGetToolchainClusterConditions(t *testing.T) { // given msg := "the cluster connection is not ready" readyAttrs := ToolchainClusterAttributes{ - GetClustersFunc: newGetHostClusterOkWithClusterOfflineCondition(), - Period: 10 * time.Second, - Timeout: 3 * time.Second, + GetClusterFunc: newGetHostClusterOkWithClusterOfflineCondition(), + Period: 10 * time.Second, + Timeout: 3 * time.Second, } expected := toolchainv1alpha1.Condition{ Type: toolchainv1alpha1.ConditionReady, @@ -150,9 +150,9 @@ func TestGetToolchainClusterConditions(t *testing.T) { // given msg := "exceeded the maximum duration since the last probe: 13s" readyAttrs := ToolchainClusterAttributes{ - GetClustersFunc: newGetHostClusterLastProbeTimeExceeded(), - Period: 10 * time.Second, - Timeout: 3 * time.Second, + GetClusterFunc: newGetHostClusterLastProbeTimeExceeded(), + Period: 10 * time.Second, + Timeout: 3 * time.Second, } expected := toolchainv1alpha1.Condition{ Type: toolchainv1alpha1.ConditionReady, @@ -173,23 +173,23 @@ func TestGetToolchainClusterConditions(t *testing.T) { }) } -func newGetHostClusterReady() cluster.GetClustersFunc { +func newGetHostClusterReady() cluster.GetHostClusterFunc { return NewFakeGetHostCluster(true, toolchainv1alpha1.ToolchainClusterReady, corev1.ConditionTrue, metav1.Now(), fakeToolchainClusterReason, "") } -func newGetHostClusterNotOk() cluster.GetClustersFunc { +func newGetHostClusterNotOk() cluster.GetHostClusterFunc { return NewFakeGetHostCluster(false, toolchainv1alpha1.ToolchainClusterReady, corev1.ConditionFalse, metav1.Now(), fakeToolchainClusterReason, fakeToolchainClusterMsg) } -func newGetHostClusterOkButNotReady(message string) cluster.GetClustersFunc { +func newGetHostClusterOkButNotReady(message string) cluster.GetHostClusterFunc { return NewFakeGetHostCluster(true, toolchainv1alpha1.ToolchainClusterReady, corev1.ConditionFalse, metav1.Now(), fakeToolchainClusterReason, message) } -func newGetHostClusterOkWithClusterOfflineCondition() cluster.GetClustersFunc { +func newGetHostClusterOkWithClusterOfflineCondition() cluster.GetHostClusterFunc { return NewFakeGetHostCluster(true, toolchainv1alpha1.ToolchainClusterOffline, corev1.ConditionFalse, metav1.Now(), fakeToolchainClusterReason, fakeToolchainClusterMsg) } -func newGetHostClusterLastProbeTimeExceeded() cluster.GetClustersFunc { +func newGetHostClusterLastProbeTimeExceeded() cluster.GetHostClusterFunc { tenMinsAgo := metav1.Now().Add(time.Duration(-10) * time.Minute) return NewFakeGetHostCluster(true, toolchainv1alpha1.ToolchainClusterReady, corev1.ConditionTrue, metav1.NewTime(tenMinsAgo), fakeToolchainClusterReason, fakeToolchainClusterMsg) } @@ -197,14 +197,13 @@ func newGetHostClusterLastProbeTimeExceeded() cluster.GetClustersFunc { // NewGetHostCluster returns cluster.GetHostClusterFunc function. The cluster.CachedToolchainCluster // that is returned by the function then contains the given client and the given status and lastProbeTime. // If ok == false, then the function returns nil for the cluster. -func NewFakeGetHostCluster(ok bool, conditionType toolchainv1alpha1.ToolchainClusterConditionType, status corev1.ConditionStatus, lastProbeTime metav1.Time, reason, message string) cluster.GetClustersFunc { +func NewFakeGetHostCluster(ok bool, conditionType toolchainv1alpha1.ToolchainClusterConditionType, status corev1.ConditionStatus, lastProbeTime metav1.Time, reason, message string) cluster.GetHostClusterFunc { if !ok { - return func(conditions ...cluster.Condition) []*cluster.CachedToolchainCluster { - return nil + return func() (*cluster.CachedToolchainCluster, bool) { + return nil, false } } - - return func(conditions ...cluster.Condition) []*cluster.CachedToolchainCluster { + return func() (*cluster.CachedToolchainCluster, bool) { toolchainClusterValue := &cluster.CachedToolchainCluster{ Config: &cluster.Config{ OperatorNamespace: test.HostOperatorNs, @@ -223,6 +222,6 @@ func NewFakeGetHostCluster(ok bool, conditionType toolchainv1alpha1.ToolchainClu toolchainClusterValue.ClusterStatus.Conditions[0].Message = message } - return []*cluster.CachedToolchainCluster{toolchainClusterValue} + return toolchainClusterValue, true } }