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

Detect TKGS environment for any version of TKC API #2970

Merged
merged 1 commit into from
Jul 27, 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
7 changes: 5 additions & 2 deletions pkg/v1/sdk/capabilities/discovery/tkg/capabilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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)
rajathagasthya marked this conversation as resolved.
Show resolved Hide resolved
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.
Expand Down
36 changes: 36 additions & 0 deletions pkg/v1/sdk/capabilities/discovery/tkg/capabilities_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
rajathagasthya marked this conversation as resolved.
Show resolved Hide resolved
{"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
Expand Down
30 changes: 30 additions & 0 deletions pkg/v1/sdk/capabilities/discovery/tkg/fake.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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,
},
},
},
}
Expand Down