Skip to content
This repository has been archived by the owner on Oct 10, 2023. It is now read-only.

Add support to list clusterclass based clusters on TKGs #2886

Merged
merged 1 commit into from
Jul 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions pkg/v1/tkg/client/get_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ import (

// ListTKGClustersOptions contains options supported by ListClusters
type ListTKGClustersOptions struct {
Namespace string
IncludeMC bool
Namespace string
IncludeMC bool
IsTKGSClusterClassFeatureActivated bool
}

// ClusterInfo defines the fields of get cluster output
Expand Down Expand Up @@ -56,7 +57,8 @@ func (c *TkgClient) ListTKGClusters(options ListTKGClustersOptions) ([]ClusterIn
if err != nil {
return nil, errors.Wrap(err, "error determining 'Tanzu Kubernetes Cluster service for vSphere' management cluster")
}
if isPacific {

if isPacific && !options.IsTKGSClusterClassFeatureActivated {
return c.GetClusterObjectsForPacific(regionalClusterClient, "", listOptions)
}

Expand Down
21 changes: 19 additions & 2 deletions pkg/v1/tkg/tkgctl/get_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@
package tkgctl

import (
"context"
"fmt"
"sort"

"github.com/pkg/errors"

"github.com/vmware-tanzu/tanzu-framework/pkg/v1/tkg/client"
"github.com/vmware-tanzu/tanzu-framework/pkg/v1/tkg/constants"
)

// ListTKGClustersOptions ptions passed while getting a list of TKG Clusters
Expand All @@ -18,9 +23,21 @@ type ListTKGClustersOptions struct {

// GetClusters returns list of cluster
func (t *tkgctl) GetClusters(options ListTKGClustersOptions) ([]client.ClusterInfo, error) {
isPacific, err := t.tkgClient.IsPacificManagementCluster()
if err != nil {
return nil, errors.Wrap(err, "unable to determine if management cluster is on vSphere with Tanzu")
}
var isTKGSClusterClassFeatureActivated bool
if isPacific {
isTKGSClusterClassFeatureActivated, err = t.featureGateHelper.FeatureActivatedInNamespace(context.Background(), constants.ClusterClassFeature, constants.TKGSClusterClassNamespace)
if err != nil {
return nil, errors.Wrap(err, fmt.Sprintf(constants.ErrorMsgFeatureGateStatus, constants.ClusterClassFeature, constants.TKGSClusterClassNamespace))
}
}
listTKGClustersOptions := client.ListTKGClustersOptions{
Namespace: options.Namespace,
IncludeMC: options.IncludeMC,
Namespace: options.Namespace,
IncludeMC: options.IncludeMC,
IsTKGSClusterClassFeatureActivated: isTKGSClusterClassFeatureActivated,
}

clusters, err := t.tkgClient.ListTKGClusters(listTKGClustersOptions)
Expand Down
70 changes: 62 additions & 8 deletions pkg/v1/tkg/tkgctl/get_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,37 +14,91 @@ import (

var _ = Describe("Unit test for get clusters", func() {
var (
ctl tkgctl
tkgClient = &fakes.Client{}
ops = ListTKGClustersOptions{
ctl tkgctl
tkgClient = &fakes.Client{}
featureGateHelper = &fakes.FakeFeatureGateHelper{}
ops = ListTKGClustersOptions{
ClusterName: "my-cluster",
}
err error
)

JustBeforeEach(func() {
ctl = tkgctl{
configDir: testingDir,
tkgClient: tkgClient,
kubeconfig: "./kube",
configDir: testingDir,
tkgClient: tkgClient,
kubeconfig: "./kube",
featureGateHelper: featureGateHelper,
}
_, err = ctl.GetClusters(ops)
})

Context("when failed to list clusters", func() {
Context("when failed to determine the management cluster is Pacific(TKGS) supervisor cluster ", func() {
BeforeEach(func() {
tkgClient.IsPacificManagementClusterReturns(false, errors.New("fake-error"))
})
It("should return an error", func() {
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("unable to determine if management cluster is on vSphere with Tanzu"))
})
})
Context("when the management cluster is not Pacific(TKGS) supervisor cluster and is able to list clusters", func() {
BeforeEach(func() {
tkgClient.IsPacificManagementClusterReturns(false, nil)
tkgClient.ListTKGClustersReturns([]client.ClusterInfo{{Name: "my-cluster", Namespace: "default"}, {Name: "my-cluster-2", Namespace: "my-system"}}, nil)
})
It("should not return an error", func() {
Expect(err).ToNot(HaveOccurred())
options := tkgClient.ListTKGClustersArgsForCall(0)
Expect(options.IsTKGSClusterClassFeatureActivated).To(BeFalse())
})
})
Context("when the management cluster is not Pacific(TKGS) supervisor cluster, but failed to list clusters", func() {
BeforeEach(func() {
tkgClient.IsPacificManagementClusterReturns(false, nil)
tkgClient.ListTKGClustersReturns(nil, errors.New("failed to list clusters"))
})
It("should return an error", func() {
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("failed to list clusters"))
options := tkgClient.ListTKGClustersArgsForCall(1)
Expect(options.IsTKGSClusterClassFeatureActivated).To(BeFalse())
})
})
Context("when the management cluster is Pacific(TKGS) supervisor cluster but failed to get the cluster class feature activation status", func() {
BeforeEach(func() {
tkgClient.IsPacificManagementClusterReturns(true, nil)
featureGateHelper.FeatureActivatedInNamespaceReturns(false, errors.New("fake-feature-gate-error"))
})
It("should return an error", func() {
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("fake-feature-gate-error"))
})
})
Context("when the management cluster is Pacific(TKGS) supervisor cluster with cluster class feature disabled and is able to list the clusters", func() {
BeforeEach(func() {
tkgClient.IsPacificManagementClusterReturns(true, nil)
featureGateHelper.FeatureActivatedInNamespaceReturns(true, nil)
tkgClient.ListTKGClustersReturns([]client.ClusterInfo{{Name: "my-cluster", Namespace: "default"}, {Name: "my-cluster-2", Namespace: "my-system"}}, nil)
})
It("should not return an error", func() {
Expect(err).ToNot(HaveOccurred())
options := tkgClient.ListTKGClustersArgsForCall(2)
Expect(options.IsTKGSClusterClassFeatureActivated).To(BeTrue())

})
})
Context("when it is able list the clusters ", func() {
Context("when the management cluster is Pacific(TKGS) supervisor cluster with cluster class feature enabled and is able to list the clusters", func() {
BeforeEach(func() {
tkgClient.IsPacificManagementClusterReturns(true, nil)
featureGateHelper.FeatureActivatedInNamespaceReturns(true, nil)
tkgClient.ListTKGClustersReturns([]client.ClusterInfo{{Name: "my-cluster", Namespace: "default"}, {Name: "my-cluster-2", Namespace: "my-system"}}, nil)
})
It("should not return an error", func() {
Expect(err).ToNot(HaveOccurred())
options := tkgClient.ListTKGClustersArgsForCall(3)
Expect(options.IsTKGSClusterClassFeatureActivated).To(BeTrue())
})
})

})