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

Commit

Permalink
Add support for "tanzu cluster get" to show tree view of cluster for …
Browse files Browse the repository at this point in the history
…TKGS

- It would be best effort to show the treeview of the cluster object for TKGS provider
 because the older TKGS versions supports different CAPI version causing the clusterctl
 describe library unable to query/list the clsuter objects. So if clusterctl library
 successfully query the objects the treeview would be shown.
Signed-off-by: Prem Kumar Kalle <pkalle@vmware.com>

Signed-off-by: Prem Kumar Kalle <pkalle@vmware.com>
  • Loading branch information
prkalle authored and anujc25 committed Aug 8, 2022
1 parent 5003fae commit 6358333
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 21 deletions.
20 changes: 7 additions & 13 deletions cmd/cli/plugin/cluster/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"strings"
"time"

"github.com/aunum/log"
"github.com/fatih/color"
"github.com/gosuri/uitable"
"github.com/spf13/cobra"
Expand All @@ -25,6 +24,7 @@ import (
"github.com/vmware-tanzu/tanzu-framework/pkg/v1/cli"
"github.com/vmware-tanzu/tanzu-framework/pkg/v1/cli/component"
"github.com/vmware-tanzu/tanzu-framework/pkg/v1/config"
"github.com/vmware-tanzu/tanzu-framework/pkg/v1/tkg/log"
"github.com/vmware-tanzu/tanzu-framework/pkg/v1/tkg/tkgctl"
)

Expand Down Expand Up @@ -144,20 +144,14 @@ func getCluster(server *v1alpha1.Server, clusterName string) error {

t.Render()

isPacific, err := tkgctlClient.IsPacificRegionalCluster()
if err != nil {
return errors.New("error determining 'Tanzu Kubernetes Cluster service for vSphere' management cluster")
}

if isPacific {
return nil
if results.Objs != nil && results.Cluster != nil {
log.Infof("\n\nDetails:\n\n")
treeView(results.Objs, results.Cluster)
} else {
// printing the below at log level 1, so that if users want to know why the tree view is not available(for TKGS) it could provide insights
log.V(1).Infof("\n\n Warning! Unable to get cluster ObjectTree/cluster objects, so detailed(tree) view of cluster resources is not available!!\n\n")
}

// TODO: Can be removed when TKGS and TKGm converge to the same CAPI version.
// https://github.com/vmware-tanzu/tanzu-framework/issues/1063
log.Infof("\n\nDetails:\n\n")
treeView(results.Objs, results.Cluster)

// If it is a Management Cluster, output the providers
if results.InstalledProviders != nil {
log.Infof("\n\nProviders:\n\n")
Expand Down
13 changes: 7 additions & 6 deletions pkg/v1/tkg/tkgctl/describe_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (

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

// DescribeTKGClustersOptions options that can be passed while requesting to describe a cluster
Expand Down Expand Up @@ -103,14 +104,14 @@ func (t *tkgctl) DescribeCluster(options DescribeTKGClustersOptions) (DescribeCl
}
}

// TODO: Can be removed when TKGS and TKGm converge to the same CAPI version.
// https://github.com/vmware-tanzu/tanzu-framework/issues/1063
if isPacific {
return results, nil
}

objs, cluster, installedProviders, err := t.tkgClient.DescribeCluster(DescribeTKGClustersOptions)
if err != nil {
// If it is pacific(TKGS), it would be the best effort to return the objectTree and cluster, so if there is an error
// fetching these objects, return empty objects without error.
if isPacific {
log.V(5).Infof("Failed to get cluster ObjectTree/cluster objects(so detailed(tree) view of cluster resources may be affected), reason: %v", err)
return results, nil
}
return results, err
}
results.Objs = objs
Expand Down
17 changes: 15 additions & 2 deletions pkg/v1/tkg/tkgctl/describe_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ var _ = Describe("Unit test for describe cluster", func() {
ClusterName: "my-cluster",
Namespace: "",
}
err error
err error
result DescribeClusterResult
)

JustBeforeEach(func() {
Expand All @@ -31,7 +32,7 @@ var _ = Describe("Unit test for describe cluster", func() {
kubeconfig: "./kube",
featureGateHelper: featureGateHelper,
}
_, err = ctl.DescribeCluster(ops)
result, err = ctl.DescribeCluster(ops)
})

Context("when failed to determine the management cluster is Pacific(TKGS) supervisor cluster ", func() {
Expand Down Expand Up @@ -100,4 +101,16 @@ var _ = Describe("Unit test for describe cluster", func() {
Expect(options.IsTKGSClusterClassFeatureActivated).To(BeTrue())
})
})
Context("when the management cluster is Pacific(TKGS) supervisor cluster and when tkgClient failed to describe the cluster", func() {
BeforeEach(func() {
tkgClient.IsPacificManagementClusterReturns(true, nil)
tkgClient.ListTKGClustersReturns([]client.ClusterInfo{{Name: "my-cluster", Roles: []string{"<none>"}}}, nil)
tkgClient.DescribeClusterReturns(nil, nil, nil, errors.New("failed to describe cluster"))
})
It("should not return an error but ObjectTree and cluster objects should be nil", func() {
Expect(err).ToNot(HaveOccurred())
Expect(result.Objs).To(BeNil())
Expect(result.Cluster).To(BeNil())
})
})
})

0 comments on commit 6358333

Please # to comment.