Skip to content

Commit

Permalink
Allow customizing the Tanzu Hub endpoint when creating tanzu context (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
anujc25 authored Jun 20, 2024
1 parent c46f410 commit 4332335
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 14 deletions.
25 changes: 17 additions & 8 deletions pkg/command/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,16 @@ import (
var (
stderrOnly, forceCSP, staging, onlyCurrent, skipTLSVerify, showAllColumns, shortCtx bool
ctxName, endpoint, apiToken, kubeConfig, kubeContext, getOutputFmt, endpointCACertPath string
tanzuHubEndpoint string

projectStr, projectIDStr, spaceStr, clustergroupStr string
contextTypeStr string
)

const (
knownGlobalHost = "cloud.vmware.com"
defaultTanzuEndpoint = "https://api.tanzu.cloud.vmware.com"
isPinnipedEndpoint = "isPinnipedEndpoint"
tanzuMissionControlEndpoint = "tanzuMissionControlEndpoint"
knownGlobalHost = "cloud.vmware.com"
defaultTanzuEndpoint = "https://api.tanzu.cloud.vmware.com"
isPinnipedEndpoint = "isPinnipedEndpoint"

contextNotExistsForContextType = "The provided context '%v' does not exist or is not active for the given context type '%v'"
noActiveContextExistsForContextType = "There is no active context for the given context type '%v'"
Expand Down Expand Up @@ -224,6 +224,8 @@ func initCreateCtxCmd() {
createCtxCmd.Flags().BoolVar(&stderrOnly, "stderr-only", false, "send all output to stderr rather than stdout")
createCtxCmd.Flags().BoolVar(&forceCSP, "force-csp", false, "force the context to use CSP auth")
createCtxCmd.Flags().BoolVar(&staging, "staging", false, "use CSP staging issuer")
createCtxCmd.Flags().StringVar(&tanzuHubEndpoint, "tanzu-hub-endpoint", "", "customize the Tanzu Hub endpoint associated with the context")

// Shell completion for this flag is the default behavior of doing file completion
createCtxCmd.Flags().StringVar(&endpointCACertPath, "endpoint-ca-certificate", "", "path to the endpoint public certificate")
createCtxCmd.Flags().BoolVar(&skipTLSVerify, "insecure-skip-tls-verify", false, "skip endpoint's TLS certificate verification")
Expand All @@ -236,6 +238,8 @@ func initCreateCtxCmd() {
utils.PanicOnErr(createCtxCmd.Flags().MarkHidden("stderr-only"))
utils.PanicOnErr(createCtxCmd.Flags().MarkHidden("force-csp"))
utils.PanicOnErr(createCtxCmd.Flags().MarkHidden("staging"))
utils.PanicOnErr(createCtxCmd.Flags().MarkHidden("tanzu-hub-endpoint"))

createCtxCmd.MarkFlagsMutuallyExclusive("endpoint", "kubecontext")
createCtxCmd.MarkFlagsMutuallyExclusive("endpoint", "kubeconfig")
createCtxCmd.MarkFlagsMutuallyExclusive("endpoint-ca-certificate", "insecure-skip-tls-verify")
Expand Down Expand Up @@ -604,7 +608,8 @@ func createContextWithTanzuEndpoint() (context *configtypes.Context, err error)
GlobalOpts: &configtypes.GlobalServer{Endpoint: sanitizedEndpoint},
ClusterOpts: &configtypes.ClusterServer{},
AdditionalMetadata: map[string]interface{}{
tanzuMissionControlEndpoint: mapTanzuEndpointToTMCEndpoint(sanitizedEndpoint),
config.TanzuMissionControlEndpointKey: mapTanzuEndpointToTMCEndpoint(sanitizedEndpoint),
config.TanzuHubEndpointKey: tanzuHubEndpoint,
},
}
return context, err
Expand Down Expand Up @@ -650,9 +655,13 @@ func globalTanzuLogin(c *configtypes.Context, generateContextNameFunc func(orgNa
}

// Fetch the Tanzu Hub endpoint for the Tanzu context as a best case effort
tanzuHubEndpoint, err := csp.GetTanzuHubEndpoint(claims.OrgID, c.GlobalOpts.Auth.AccessToken, staging)
if err != nil {
log.V(7).Infof("unable to get Tanzu Hub endpoint. Error: %v", err.Error())
if tanzuHubEndpoint == "" {
tanzuHubEndpoint, err = csp.GetTanzuHubEndpoint(claims.OrgID, c.GlobalOpts.Auth.AccessToken, staging)
if err != nil {
log.V(7).Infof("unable to get Tanzu Hub endpoint. Error: %v", err.Error())
}
} else {
log.Warningf("This tanzu context is being created with the custom Tanzu Hub endpoint: %q", tanzuHubEndpoint)
}

// update the context name using the context name generator
Expand Down
26 changes: 20 additions & 6 deletions pkg/command/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -932,12 +932,13 @@ type ContextListInfo struct {

var _ = Describe("create new context", func() {
const (
existingContext = "test-mc"
testKubeContext = "test-k8s-context"
testKubeConfigPath = "/fake/path/kubeconfig"
testContextName = "fake-context-name"
fakeTMCEndpoint = "tmc.cloud.vmware.com:443"
fakeTanzuEndpoint = "https://fake.api.tanzu.cloud.vmware.com"
existingContext = "test-mc"
testKubeContext = "test-k8s-context"
testKubeConfigPath = "/fake/path/kubeconfig"
testContextName = "fake-context-name"
fakeTMCEndpoint = "tmc.cloud.vmware.com:443"
fakeTanzuEndpoint = "https://fake.api.tanzu.cloud.vmware.com"
fakeTanzuHubEndpoint = "https://fake.api.tanzu.hub.vmware.com"
)
var (
tkgConfigFile *os.File
Expand Down Expand Up @@ -1117,6 +1118,19 @@ var _ = Describe("create new context", func() {
Expect(ctx.GlobalOpts.Endpoint).To(ContainSubstring(endpoint))
})
})
Context("with endpoint, tanzuHubEndpoint and context name provided", func() {
It("should create context with given endpoint and context name", func() {
endpoint = fakeTanzuEndpoint
tanzuHubEndpoint = fakeTanzuHubEndpoint
ctxName = testContextName
ctx, err = createNewContext()
Expect(err).To(BeNil())
Expect(ctx.Name).To(ContainSubstring("fake-context-name"))
Expect(string(ctx.ContextType)).To(ContainSubstring(contextTypeTanzu))
Expect(ctx.GlobalOpts.Endpoint).To(ContainSubstring(endpoint))
Expect(ctx.AdditionalMetadata[config.TanzuHubEndpointKey].(string)).To(ContainSubstring(tanzuHubEndpoint))
})
})
Context("context name already exists", func() {
It("should return error", func() {
endpoint = fakeTanzuEndpoint
Expand Down
3 changes: 3 additions & 0 deletions pkg/command/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,11 @@ func init() {
loginCmd.Flags().BoolVar(&staging, "staging", false, "use CSP staging issuer")
loginCmd.Flags().StringVar(&endpointCACertPath, "endpoint-ca-certificate", "", "path to the endpoint public certificate")
loginCmd.Flags().BoolVar(&skipTLSVerify, "insecure-skip-tls-verify", false, "skip endpoint's TLS certificate verification")
loginCmd.Flags().StringVar(&tanzuHubEndpoint, "tanzu-hub-endpoint", "", "customize the Tanzu Hub endpoint associated with the context")

utils.PanicOnErr(loginCmd.Flags().MarkHidden("api-token"))
utils.PanicOnErr(loginCmd.Flags().MarkHidden("staging"))
utils.PanicOnErr(loginCmd.Flags().MarkHidden("tanzu-hub-endpoint"))
loginCmd.SetUsageFunc(cli.SubCmdUsageFunc)
loginCmd.MarkFlagsMutuallyExclusive("endpoint-ca-certificate", "insecure-skip-tls-verify")

Expand Down

0 comments on commit 4332335

Please sign in to comment.