From 4332335c836a27d07f414d46a602730216706e4f Mon Sep 17 00:00:00 2001 From: Anuj Chaudhari Date: Thu, 20 Jun 2024 11:09:18 -0700 Subject: [PATCH] Allow customizing the Tanzu Hub endpoint when creating tanzu context (#782) --- pkg/command/context.go | 25 +++++++++++++++++-------- pkg/command/context_test.go | 26 ++++++++++++++++++++------ pkg/command/login.go | 3 +++ 3 files changed, 40 insertions(+), 14 deletions(-) diff --git a/pkg/command/context.go b/pkg/command/context.go index efe12429f..8d3c9d46d 100644 --- a/pkg/command/context.go +++ b/pkg/command/context.go @@ -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'" @@ -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") @@ -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") @@ -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 @@ -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 diff --git a/pkg/command/context_test.go b/pkg/command/context_test.go index 11484d416..9ec44b853 100644 --- a/pkg/command/context_test.go +++ b/pkg/command/context_test.go @@ -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 @@ -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 diff --git a/pkg/command/login.go b/pkg/command/login.go index d84d43778..70bf6c663 100644 --- a/pkg/command/login.go +++ b/pkg/command/login.go @@ -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")