diff --git a/pkg/v1/tkg/client/get_cluster.go b/pkg/v1/tkg/client/get_cluster.go index ea5d0ebedf8..77f0e331fa6 100644 --- a/pkg/v1/tkg/client/get_cluster.go +++ b/pkg/v1/tkg/client/get_cluster.go @@ -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 @@ -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) } diff --git a/pkg/v1/tkg/tkgctl/get_cluster.go b/pkg/v1/tkg/tkgctl/get_cluster.go index 2d45c086dad..9291dd82502 100644 --- a/pkg/v1/tkg/tkgctl/get_cluster.go +++ b/pkg/v1/tkg/tkgctl/get_cluster.go @@ -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 @@ -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) diff --git a/pkg/v1/tkg/tkgctl/get_cluster_test.go b/pkg/v1/tkg/tkgctl/get_cluster_test.go index 0830a1723ca..295c01aa324 100644 --- a/pkg/v1/tkg/tkgctl/get_cluster_test.go +++ b/pkg/v1/tkg/tkgctl/get_cluster_test.go @@ -14,9 +14,10 @@ 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 @@ -24,27 +25,80 @@ var _ = Describe("Unit test for get clusters", func() { 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()) }) }) + })