diff --git a/pkg/v1/sdk/capabilities/discovery/tkg/capabilities.go b/pkg/v1/sdk/capabilities/discovery/tkg/capabilities.go index cb726e7252..ded9b2c33d 100644 --- a/pkg/v1/sdk/capabilities/discovery/tkg/capabilities.go +++ b/pkg/v1/sdk/capabilities/discovery/tkg/capabilities.go @@ -12,6 +12,7 @@ import ( corev1 "k8s.io/api/core/v1" "sigs.k8s.io/controller-runtime/pkg/client" + runv1alpha1 "github.com/vmware-tanzu/tanzu-framework/apis/run/v1alpha1" "github.com/vmware-tanzu/tanzu-framework/pkg/v1/sdk/capabilities/discovery" "github.com/vmware-tanzu/tanzu-framework/pkg/v1/tkg/test/tkgctl/shared" ) @@ -36,9 +37,11 @@ func (dc *DiscoveryClient) IsTKGm(ctx context.Context) (bool, error) { return dc.HasInfrastructureProvider(ctx, InfrastructureProviderVsphere) } -// IsTKGS returns true if the cluster is a TKGS cluster. +// IsTKGS returns true if the cluster is a TKGS cluster. Checks for the existence of any TKC API version. func (dc *DiscoveryClient) IsTKGS(ctx context.Context) (bool, error) { - return dc.HasTanzuKubernetesClusterV1alpha1(ctx) + query := discovery.Group("tkc", runv1alpha1.GroupVersion.Group). + WithResource("tanzukubernetesclusters") + return dc.clusterQueryClient.PreparedQuery(query)() } // IsManagementCluster returns true if the cluster is a TKG management cluster. diff --git a/pkg/v1/sdk/capabilities/discovery/tkg/capabilities_test.go b/pkg/v1/sdk/capabilities/discovery/tkg/capabilities_test.go index ce305c6e3c..6738e5397b 100644 --- a/pkg/v1/sdk/capabilities/discovery/tkg/capabilities_test.go +++ b/pkg/v1/sdk/capabilities/discovery/tkg/capabilities_test.go @@ -47,6 +47,42 @@ func TestHasNSX(t *testing.T) { } } +func TestIsTKGS(t *testing.T) { + + discoveryClientWithTKC := func() (*DiscoveryClient, error) { + return newFakeDiscoveryClient(tanzuRunAPIResourceList, Scheme, nil) + } + + discoveryClientWithoutTKC := func() (*DiscoveryClient, error) { + return newFakeDiscoveryClient([]*metav1.APIResourceList{}, Scheme, nil) + } + + testCases := []struct { + description string + discoveryClientFn func() (*DiscoveryClient, error) + errExpected bool + want bool + }{ + {"TKGS exists", discoveryClientWithTKC, false, true}, + {"TKGS does not exist", discoveryClientWithoutTKC, false, false}, + } + for _, tc := range testCases { + t.Run(tc.description, func(t *testing.T) { + dc, err := tc.discoveryClientFn() + if err != nil { + t.Error(err) + } + got, err := dc.IsTKGS(context.Background()) + if err != nil && !tc.errExpected { + t.Error(err) + } + if got != tc.want { + t.Errorf("got: %t, want %t", got, tc.want) + } + }) + } +} + var clusterInfo = ` cluster: name: tkg-cluster-wc-765 diff --git a/pkg/v1/sdk/capabilities/discovery/tkg/fake.go b/pkg/v1/sdk/capabilities/discovery/tkg/fake.go index be80d80d4b..2b646571e3 100644 --- a/pkg/v1/sdk/capabilities/discovery/tkg/fake.go +++ b/pkg/v1/sdk/capabilities/discovery/tkg/fake.go @@ -9,6 +9,8 @@ import ( ctrlfake "sigs.k8s.io/controller-runtime/pkg/client/fake" runv1alpha1 "github.com/vmware-tanzu/tanzu-framework/apis/run/v1alpha1" + runv1alpha2 "github.com/vmware-tanzu/tanzu-framework/apis/run/v1alpha2" + runv1alpha3 "github.com/vmware-tanzu/tanzu-framework/apis/run/v1alpha3" "github.com/vmware-tanzu/tanzu-framework/pkg/v1/sdk/capabilities/discovery" ) @@ -32,6 +34,34 @@ var ( Version: runv1alpha1.GroupVersion.Version, Namespaced: true, }, + { + Name: "tanzukubernetesreleases", + Kind: "TanzuKubernetesRelease", + Group: runv1alpha2.GroupVersion.Group, + Version: runv1alpha2.GroupVersion.Version, + Namespaced: true, + }, + { + Name: "tanzukubernetesclusters", + Kind: "TanzuKubernetesCluster", + Group: runv1alpha2.GroupVersion.Group, + Version: runv1alpha2.GroupVersion.Version, + Namespaced: true, + }, + { + Name: "tanzukubernetesreleases", + Kind: "TanzuKubernetesRelease", + Group: runv1alpha3.GroupVersion.Group, + Version: runv1alpha3.GroupVersion.Version, + Namespaced: true, + }, + { + Name: "tanzukubernetesclusters", + Kind: "TanzuKubernetesCluster", + Group: runv1alpha3.GroupVersion.Group, + Version: runv1alpha3.GroupVersion.Version, + Namespaced: true, + }, }, }, }