Skip to content

Commit

Permalink
Update GetKubeconfigForContext API to accept Project ID instead of Pr…
Browse files Browse the repository at this point in the history
…oject Name

Signed-off-by: Prem Kumar Kalle <[email protected]>
  • Loading branch information
prkalle authored and vuil committed Mar 28, 2024
1 parent 1b2ce73 commit c2323ae
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 23 deletions.
31 changes: 16 additions & 15 deletions config/tanzu_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
// keys to Context's AdditionalMetadata map
const (
OrgIDKey = "tanzuOrgID"
OrgNameKey = "tanzuOrgName"
ProjectNameKey = "tanzuProjectName"
ProjectIDKey = "tanzuProjectID"
SpaceNameKey = "tanzuSpaceName"
Expand Down Expand Up @@ -108,8 +109,8 @@ func runCommand(commandPath string, args []string, opts *cmdOptions) (bytes.Buff

// resourceOptions specifies the resources to use for kubeconfig generation
type resourceOptions struct {
// projectName name of the Project
projectName string
// projectID UUID of the Project
projectID string
// spaceName name of the Space
spaceName string
// clusterGroupName name of the ClusterGroup
Expand All @@ -120,9 +121,9 @@ type resourceOptions struct {

type ResourceOptions func(o *resourceOptions)

func ForProject(projectName string) ResourceOptions {
func ForProject(projectID string) ResourceOptions {
return func(o *resourceOptions) {
o.projectName = strings.TrimSpace(projectName)
o.projectID = strings.TrimSpace(projectID)
}
}
func ForSpace(spaceName string) ResourceOptions {
Expand All @@ -143,38 +144,38 @@ func ForCustomPath(customPath string) ResourceOptions {

// 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 for retreiving Kubeconfig of Tanzu context
// Pre-reqs: projectID, space and clustergroup names should be valid for retrieving Kubeconfig of Tanzu context
//
// Notes:
//
// Use Case 1: Get the kubeconfig pointing to Tanzu org
// -> projectName = ""
// -> projectID = ""
// -> spaceName = ""
// -> clusterGroupName = ""
// ex: kubeconfig's cluster.server URL : https://endpoint/org/orgid
//
// Use Case 2: Get the kubeconfig pointing to Tanzu project
// -> projectName = "PROJECTNAME"
// -> projectID = "PROJECTNAME"
// -> spaceName = ""
// -> clusterGroupName = ""
// ex: kubeconfig's cluster.server URL : https://endpoint/org/orgid/project/<projectName>
// ex: kubeconfig's cluster.server URL : https://endpoint/org/orgid/project/<projectID>
//
// Use Case 3: Get the kubeconfig pointing to Tanzu space
// -> projectName = "PROJECTNAME"
// -> projectID = "PROJECTID"
// -> spaceName = "SPACENAME"
// -> clusterGroupName = ""
// ex: kubeconfig's cluster.server URL : https://endpoint/org/orgid/project/<projectName>/space/<spaceName>
// ex: kubeconfig's cluster.server URL : https://endpoint/org/orgid/project/<projectID>/space/<spaceName>
//
// Use Case 4: Get the kubeconfig pointing to Tanzu clustergroup
// -> projectName = "PROJECTNAME"
// -> projectID = "PROJECTID"
// -> spaceName = ""
// -> clusterGroupName = "CLUSTERGROUPNAME"
// ex: kubeconfig's cluster.server URL : https://endpoint/org/orgid/project/<projectName>/clustergroup/<clustergroupName>
// ex: kubeconfig's cluster.server URL : https://endpoint/org/orgid/project/<projectID>/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 = ""
// -> projectID = ""
// -> spaceName = ""
// -> clusterGroupName = ""
func GetKubeconfigForContext(contextName string, opts ...ResourceOptions) ([]byte, error) {
Expand Down Expand Up @@ -225,10 +226,10 @@ func prepareClusterServerURL(context *configtypes.Context, rOptions *resourceOpt
return fmt.Sprintf("%s/%s", strings.TrimRight(context.GlobalOpts.Endpoint, "/"), strings.TrimLeft(rOptions.customPath, "/"))
}

if rOptions.projectName == "" {
if rOptions.projectID == "" {
return serverURL
}
serverURL = serverURL + "/project/" + rOptions.projectName
serverURL = serverURL + "/project/" + rOptions.projectID

if rOptions.spaceName != "" {
return serverURL + "/space/" + rOptions.spaceName
Expand Down
16 changes: 8 additions & 8 deletions config/tanzu_context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,47 +126,47 @@ func TestGetKubeconfigForContext(t *testing.T) {
assert.NoError(t, err)

// Test getting the kubeconfig for a space within a project
kubeconfigBytes, err := GetKubeconfigForContext(c.Name, ForProject("project1"), ForSpace("space1"))
kubeconfigBytes, err := GetKubeconfigForContext(c.Name, ForProject("project1-id"), ForSpace("space1"))
assert.NoError(t, err)
c, err = GetContext("test-tanzu")
assert.NoError(t, err)
var kc kubeconfig.Config
err = yaml.Unmarshal(kubeconfigBytes, &kc)
assert.NoError(t, err)
cluster := kubeconfig.GetCluster(&kc, "tanzu-cli-mytanzu/current")
assert.Equal(t, cluster.Cluster.Server, c.ClusterOpts.Endpoint+"/project/project1/space/space1")
assert.Equal(t, cluster.Cluster.Server, c.ClusterOpts.Endpoint+"/project/project1-id/space/space1")

// Test getting the kubeconfig for a project
kubeconfigBytes, err = GetKubeconfigForContext(c.Name, ForProject("project2"))
kubeconfigBytes, err = GetKubeconfigForContext(c.Name, ForProject("project2-id"))
assert.NoError(t, err)
c, err = GetContext("test-tanzu")
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, c.ClusterOpts.Endpoint+"/project/project2")
assert.Equal(t, cluster.Cluster.Server, c.ClusterOpts.Endpoint+"/project/project2-id")

// Test getting the kubeconfig for a clustergroup within a project
kubeconfigBytes, err = GetKubeconfigForContext(c.Name, ForProject("project2"), ForClusterGroup("clustergroup1"))
kubeconfigBytes, err = GetKubeconfigForContext(c.Name, ForProject("project2-id"), ForClusterGroup("clustergroup1"))
assert.NoError(t, err)
c, err = GetContext("test-tanzu")
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, c.ClusterOpts.Endpoint+"/project/project2/clustergroup/clustergroup1")
assert.Equal(t, cluster.Cluster.Server, c.ClusterOpts.Endpoint+"/project/project2-id/clustergroup/clustergroup1")

// Test getting the kubeconfig with incorrect resource combination (request kubeconfig for space and clustergroup)
c, err = GetContext("test-tanzu")
assert.NoError(t, err)
_, err = GetKubeconfigForContext(c.Name, ForProject("project2"), ForSpace("space1"), ForClusterGroup("clustergroup1"))
_, err = GetKubeconfigForContext(c.Name, ForProject("project2-id"), ForSpace("space1"), ForClusterGroup("clustergroup1"))
assert.Error(t, err)
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
tmcCtx, err := GetContext("test-tmc")
assert.NoError(t, err)
_, err = GetKubeconfigForContext(tmcCtx.Name, ForProject("project2"))
_, err = GetKubeconfigForContext(tmcCtx.Name, ForProject("project2-id"))
assert.Error(t, err)
assert.ErrorContains(t, err, "context must be of type: tanzu or kubernetes")

Expand Down

0 comments on commit c2323ae

Please sign in to comment.