Skip to content

Commit

Permalink
Support requesting CustomPath endpoint for kubeconfig with GetKubecon…
Browse files Browse the repository at this point in the history
…figForContext API (vmware-tanzu#152)

* Support requesting CustomPath endpoint for kubeconfig with GetKubeconfigForContext API

* Add KubeConfigBytes field for KubernetesDiscovery
  • Loading branch information
anujc25 authored Jan 22, 2024
1 parent 583955a commit 021d60b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
14 changes: 14 additions & 0 deletions config/tanzu_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package config

import (
"bytes"
"fmt"
"io"
"os"
"os/exec"
Expand Down Expand Up @@ -110,6 +111,8 @@ type resourceOptions struct {
spaceName string
// clusterGroupName name of the ClusterGroup
clusterGroupName string
// customPath use specified path when constructing kubeconfig
customPath string
}

type ResourceOptions func(o *resourceOptions)
Expand All @@ -129,6 +132,11 @@ func ForClusterGroup(clusterGroupName string) ResourceOptions {
o.clusterGroupName = strings.TrimSpace(clusterGroupName)
}
}
func ForCustomPath(customPath string) ResourceOptions {
return func(o *resourceOptions) {
o.customPath = customPath
}
}

// GetKubeconfigForContext returns the kubeconfig for any arbitrary Tanzu resource in the Tanzu object hierarchy
// referred by the Tanzu context
Expand Down Expand Up @@ -199,6 +207,12 @@ func GetKubeconfigForContext(contextName string, opts ...ResourceOptions) ([]byt

func prepareClusterServerURL(context *configtypes.Context, rOptions *resourceOptions) string {
serverURL := context.ClusterOpts.Endpoint

// If customPath is set, append customPath after endpoint to form endpoint URL
if rOptions.customPath != "" {
return fmt.Sprintf("%s/%s", strings.TrimRight(context.GlobalOpts.Endpoint, "/"), strings.TrimLeft(rOptions.customPath, "/"))
}

if rOptions.projectName == "" {
return serverURL
}
Expand Down
10 changes: 10 additions & 0 deletions config/tanzu_context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,16 @@ func TestGetKubeconfigForContext(t *testing.T) {
_, err = GetKubeconfigForContext(nonTanzuCtx.Name, ForProject("project2"))
assert.Error(t, err)
assert.ErrorContains(t, err, "context must be of type: tanzu")

// Test getting the kubeconfig for a custom endpoint path
tanzuCtx, err := GetContext("test-tanzu")
assert.NoError(t, err)
kubeconfigBytes, err = GetKubeconfigForContext(tanzuCtx.Name, ForCustomPath("/custom-path"))
assert.NoError(t, err)
err = yaml.Unmarshal(kubeconfigBytes, &kc)
assert.NoError(t, err)
cluster = kubeconfig.GetCluster(&kc, "tanzu-cli-mytanzu/current")
assert.Equal(t, cluster.Cluster.Server, tanzuCtx.GlobalOpts.Endpoint+"/custom-path")
}

func TestGetTanzuContextActiveResource(t *testing.T) {
Expand Down
3 changes: 3 additions & 0 deletions config/types/clientconfig_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,9 @@ type KubernetesDiscovery struct {
Path string `json:"path,omitempty" yaml:"path,omitempty"`
// The context to use (if required), defaults to current.
Context string `json:"context,omitempty" yaml:"context,omitempty"`
// KubeConfigBytes is the entire kube configuration
// Note: Either Path or KubeConfigBytes should be configured and not both
KubeConfigBytes []byte `json:"kubeConfigBytes,omitempty" yaml:"kubeConfigBytes,omitempty"`
// Version of the CLIPlugins API to query.
// E.g., v1alpha1
Version string `json:"version,omitempty" yaml:"version,omitempty"`
Expand Down

0 comments on commit 021d60b

Please sign in to comment.