Skip to content

Commit

Permalink
Update GetKubeconfigForContext API for kubernetes context (#162)
Browse files Browse the repository at this point in the history
* Update GetKubeconfigForContext to return kubeconfig for kubernetes context

* Update GetKubeconfigForContext godoc
  • Loading branch information
mpanchajanya authored Feb 23, 2024
1 parent 59c7320 commit 6362240
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 11 deletions.
23 changes: 22 additions & 1 deletion config/contexts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,27 @@ func setupForGetContext() error {
Endpoint: "test-endpoint",
},
},
{
Name: "k8s-context",
ContextType: configtypes.ContextTypeK8s,
GlobalOpts: &configtypes.GlobalServer{
Endpoint: "test-endpoint",
},
ClusterOpts: &configtypes.ClusterServer{
Endpoint: "https://api.tanzu.cloud.vmware.com:443/org/fake-org-id",
Path: "test-path",
Context: "test-context",
},
},
{
Name: "k8s-context",
ContextType: configtypes.ContextTypeK8s,
ClusterOpts: &configtypes.ClusterServer{
Endpoint: "https://api.tanzu.cloud.vmware.com:443/org/fake-org-id",
Path: "test-path",
Context: "test-context",
},
},
{
Name: "test-tanzu",
ContextType: configtypes.ContextTypeTanzu,
Expand Down Expand Up @@ -558,7 +579,7 @@ func TestGetContextsByType(t *testing.T) {
}{
{
name: "get k8s contexts",
expectedNamesOfContexts: []string{"test-mc", "test-mc-2"},
expectedNamesOfContexts: []string{"test-mc", "test-mc-2", "k8s-context"},
contextType: configtypes.ContextTypeK8s,
},
{
Expand Down
21 changes: 15 additions & 6 deletions config/tanzu_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,9 @@ func ForCustomPath(customPath string) ResourceOptions {
}
}

// GetKubeconfigForContext returns the kubeconfig for any arbitrary Tanzu resource in the Tanzu object hierarchy
// GetKubeconfigForContext returns the kubeconfig for any arbitrary kubernetes resource or Tanzu resource in the Tanzu object hierarchy
// referred by the Tanzu context
// Pre-reqs: project, space and clustergroup names should be valid
// Pre-reqs: project, space and clustergroup names should be valid for retreiving Kubeconfig of Tanzu context
//
// Notes:
//
Expand Down Expand Up @@ -169,6 +169,11 @@ func ForCustomPath(customPath string) ResourceOptions {
// ex: kubeconfig's cluster.server URL : https://endpoint/org/orgid/project/<projectName>/clustergroup/<clustergroupName>
//
// Note: Specifying `spaceName` and `clusterGroupName` both at the same time is incorrect input.
//
// Use Case 5: Get the kubeconfig pointing to Kubernetes context
// -> projectName = ""
// -> spaceName = ""
// -> clusterGroupName = ""
func GetKubeconfigForContext(contextName string, opts ...ResourceOptions) ([]byte, error) {
ctx, err := GetContext(contextName)
if err != nil {
Expand All @@ -180,10 +185,11 @@ func GetKubeconfigForContext(contextName string, opts ...ResourceOptions) ([]byt
opt(rOptions)
}

if ctx.ContextType != configtypes.ContextTypeTanzu {
return nil, errors.Errorf("context must be of type: %s", configtypes.ContextTypeTanzu)
if ctx.ContextType != configtypes.ContextTypeTanzu && ctx.ContextType != configtypes.ContextTypeK8s {
return nil, errors.Errorf("context must be of type: %s or %s", configtypes.ContextTypeTanzu, configtypes.ContextTypeK8s)
}
if rOptions.spaceName != "" && rOptions.clusterGroupName != "" {

if ctx.ContextType == configtypes.ContextTypeTanzu && rOptions.spaceName != "" && rOptions.clusterGroupName != "" {
return nil, errors.Errorf("incorrect resource options provided. Both space and clustergroup are set but only one can be set")
}

Expand All @@ -196,7 +202,10 @@ func GetKubeconfigForContext(contextName string, opts ...ResourceOptions) ([]byt
if err != nil {
return nil, errors.Wrap(err, "failed to minify the kubeconfig")
}
updateKubeconfigServerURL(kc, ctx, rOptions)

if ctx.ContextType == configtypes.ContextTypeTanzu {
updateKubeconfigServerURL(kc, ctx, rOptions)
}

kubeconfigBytes, err := yaml.Marshal(kc)
if err != nil {
Expand Down
30 changes: 26 additions & 4 deletions config/tanzu_context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,25 @@ func TestGetKubeconfigForContext(t *testing.T) {
_ = os.RemoveAll(kubeconfigFilePath.Name())
}()

// Cluster context
myTanzu := "tanzu-cli-mytanzu"

// Setup tanzu context
c, err := GetContext("test-tanzu")
assert.NoError(t, err)
c.ClusterOpts.Path = kubeconfigFilePath.Name()
c.ClusterOpts.Context = "tanzu-cli-mytanzu"
c.ClusterOpts.Context = myTanzu
err = SetContext(c, false)
assert.NoError(t, err)

// Setup kubernetes context
k8sCtx, err := GetContext("k8s-context")
assert.NoError(t, err)
k8sCtx.ClusterOpts.Path = kubeconfigFilePath.Name()
k8sCtx.ClusterOpts.Context = "k8s-context"
err = SetContext(k8sCtx, false)
assert.NoError(t, err)

// Test getting the kubeconfig for a space within a project
kubeconfigBytes, err := GetKubeconfigForContext(c.Name, ForProject("project1"), ForSpace("space1"))
assert.NoError(t, err)
Expand Down Expand Up @@ -146,11 +158,11 @@ func TestGetKubeconfigForContext(t *testing.T) {
assert.ErrorContains(t, err, "incorrect resource options provided. Both space and clustergroup are set but only one can be set")

// Test getting the kubeconfig for an arbitrary Tanzu resource for non Tanzu context
nonTanzuCtx, err := GetContext("test-mc")
tmcCtx, err := GetContext("test-tmc")
assert.NoError(t, err)
_, err = GetKubeconfigForContext(nonTanzuCtx.Name, ForProject("project2"))
_, err = GetKubeconfigForContext(tmcCtx.Name, ForProject("project2"))
assert.Error(t, err)
assert.ErrorContains(t, err, "context must be of type: tanzu")
assert.ErrorContains(t, err, "context must be of type: tanzu or kubernetes")

// Test getting the kubeconfig for a custom endpoint path
tanzuCtx, err := GetContext("test-tanzu")
Expand All @@ -161,6 +173,16 @@ func TestGetKubeconfigForContext(t *testing.T) {
assert.NoError(t, err)
cluster = kubeconfig.GetCluster(&kc, "tanzu-cli-mytanzu/current")
assert.Equal(t, cluster.Cluster.Server, tanzuCtx.GlobalOpts.Endpoint+"/custom-path")

// Test getting the kubeconfig for a kubernetes context
k8sCtx, err = GetContext("k8s-context")
assert.NoError(t, err)
kubeconfigBytes, err = GetKubeconfigForContext(k8sCtx.Name)
assert.NoError(t, err)
err = yaml.Unmarshal(kubeconfigBytes, &kc)
assert.NoError(t, err)
cluster = kubeconfig.GetCluster(&kc, "k8s-cluster")
assert.Equal(t, cluster.Cluster.Server, k8sCtx.ClusterOpts.Endpoint)
}

func TestGetTanzuContextActiveResource(t *testing.T) {
Expand Down
11 changes: 11 additions & 0 deletions fakes/config/kubeconfig-1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ clusters:
insecure-skip-tls-verify: true
server: https://api.tanzu.cloud.vmware.com:443/org/fake-org-id
name: tanzu-cli-mytanzu/current
- cluster:
insecure-skip-tls-verify: true
server: https://api.tanzu.cloud.vmware.com:443/org/fake-org-id
name: k8s-cluster
contexts:
- context:
cluster: bar-cluster
Expand All @@ -27,6 +31,10 @@ contexts:
namespace: saw-ns
user: blue-user
name: foo-context
- context:
cluster: k8s-cluster
user: k8s-user
name: k8s-context
- context:
cluster: tanzu-cli-mytanzu/current
user: tanzu-cli-mytanzu-user
Expand Down Expand Up @@ -54,3 +62,6 @@ users:
- name: testEnv2
value: testEnv2-Value
interactiveMode: Never
- name: k8s-user
user:
token: k8s-token

0 comments on commit 6362240

Please sign in to comment.