From 4c442e99680e45297277ab73cf34276868f202da Mon Sep 17 00:00:00 2001 From: Anuj Chaudhari Date: Wed, 17 Jan 2024 15:42:48 -0800 Subject: [PATCH] Support requesting CustomPath endpoint for kubeconfig with GetKubeconfigForContext API --- config/tanzu_context.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/config/tanzu_context.go b/config/tanzu_context.go index 9e0d37c10..10ceabe9e 100644 --- a/config/tanzu_context.go +++ b/config/tanzu_context.go @@ -5,6 +5,7 @@ package config import ( "bytes" + "fmt" "io" "os" "os/exec" @@ -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) @@ -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 @@ -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 }