Skip to content

Commit 87df35c

Browse files
authored
Experimental: Get the Tanzu Hub Endpoint based on the Central Config Metadata (#764)
* Update Test Central Config to include Hub metadata * Get the Tanzu Hub Endpoint based on the Central Config Metadata
1 parent 7793dbd commit 87df35c

4 files changed

Lines changed: 80 additions & 5 deletions

File tree

2.23 KB
Binary file not shown.

hack/central-repo/upload-plugins.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,12 @@ cli.core.tanzu_application_platform_scopes:
233233
- scope: tap:viewer
234234
- scope: tap:admin
235235
- scope: tap:member
236+
cli.core.tanzu_hub_metadata:
237+
cspProductIdentifier: "TANZU-SAAS"
238+
cspDisplayName: "Tanzu Platform"
239+
endpointProduction: https://www.production.fake.vmware.com/hub
240+
endpointStaging: https://www.staging.fake.vmware.com/hub
241+
useCentralConfig: false
236242
cli.core.some-string: "the meaning of life, the universe, and everything"
237243
cli.core.some-int: 42
238244
cli.core.some-bool: true

pkg/auth/csp/tanzu.go

Lines changed: 73 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ import (
2828
"golang.org/x/oauth2"
2929
"golang.org/x/term"
3030

31+
"github.com/vmware-tanzu/tanzu-cli/pkg/centralconfig"
32+
cliconfig "github.com/vmware-tanzu/tanzu-cli/pkg/config"
33+
"github.com/vmware-tanzu/tanzu-plugin-runtime/config"
3134
"github.com/vmware-tanzu/tanzu-plugin-runtime/log"
3235
)
3336

@@ -37,6 +40,10 @@ const (
3740
tanzuCLIClientID = "tanzu-cli-client-id"
3841
defaultListenAddress = "127.0.0.1:0"
3942
defaultCallbackPath = "/callback"
43+
44+
centralConfigTanzuHubMetadata = "cli.core.tanzu_hub_metadata"
45+
defaultCSPDisplayName = "Tanzu Platform"
46+
defaultCSPProductIdentifier = "TANZU-SAAS"
4047
)
4148

4249
// stdin returns the file descriptor for stdin as an int.
@@ -47,6 +54,15 @@ type orgInfo struct {
4754
Name string `json:"displayName"`
4855
}
4956

57+
// tanzuHubMetadata to parse the hub metadata from central config
58+
type tanzuHubMetadata struct {
59+
CSPProductIdentifier string `json:"cspProductIdentifier" yaml:"cspProductIdentifier"`
60+
CSPDisplayName string `json:"cspDisplayName" yaml:"cspDisplayName"`
61+
EndpointProduction string `json:"endpointProduction" yaml:"endpointProduction"`
62+
EndpointStaging string `json:"endpointStaging" yaml:"endpointStaging"`
63+
UseCentralConfig bool `json:"useCentralConfig" yaml:"useCentralConfig"`
64+
}
65+
5066
type cspLoginHandler struct {
5167
tokenExchange context.Context
5268
tokenExchangeComplete context.CancelFunc
@@ -429,8 +445,44 @@ func GetOrgNameFromOrgID(orgID, accessToken, issuer string) (string, error) {
429445
return org.Name, nil
430446
}
431447

432-
// GetTanzuHubEndpointForTAP retrieves Tanzu Hub Endpoint For TAP SaaS through the CSP API
433-
func GetTanzuHubEndpointForTAP(orgID, accessToken string, useStagingIssuer bool) (string, error) {
448+
// GetTanzuHubEndpoint retrieves Tanzu Hub Endpoint through the CSP API or through Central Config as fallback
449+
func GetTanzuHubEndpoint(orgID, accessToken string, useStagingIssuer bool) (string, error) {
450+
var errCSP error
451+
var endpoint string
452+
453+
hubMetadata := getTanzuHubMetadataFromCentralConfig()
454+
if hubMetadata.CSPDisplayName == "" {
455+
hubMetadata.CSPDisplayName = defaultCSPDisplayName
456+
}
457+
if hubMetadata.CSPProductIdentifier == "" {
458+
hubMetadata.CSPProductIdentifier = defaultCSPProductIdentifier
459+
}
460+
461+
// If feature-flag to get endpoint from central config is not configured
462+
// try to use CSP api to get the endpoint
463+
if !hubMetadata.UseCentralConfig {
464+
endpoint, errCSP = getTanzuHubEndpointFromCSP(hubMetadata, orgID, accessToken, useStagingIssuer)
465+
}
466+
467+
// If the endpoint is empty or we got error while getting endpoint from CSP
468+
// try to get the endpoint from central configuration with hubMetadata
469+
if endpoint == "" || errCSP != nil {
470+
endpoint = hubMetadata.EndpointProduction
471+
if useStagingIssuer {
472+
endpoint = hubMetadata.EndpointStaging
473+
}
474+
}
475+
476+
// If endpoint is still empty return error
477+
if endpoint == "" {
478+
return "", errCSP
479+
}
480+
481+
return endpoint, nil
482+
}
483+
484+
// getTanzuHubEndpointFromCSP retrieves Tanzu Hub Endpoint through the CSP API
485+
func getTanzuHubEndpointFromCSP(metadata tanzuHubMetadata, orgID, accessToken string, useStagingIssuer bool) (string, error) {
434486
// CSPServiceURLs stores the CSP service URL information
435487
type CSPServiceURLs struct {
436488
ServiceHome string `json:"serviceHome"`
@@ -477,12 +529,29 @@ func GetTanzuHubEndpointForTAP(orgID, accessToken string, useStagingIssuer bool)
477529
}
478530

479531
for _, s := range services.ServicesList {
480-
if s.ProductIdentifier == "TANZU-SAAS" && strings.Contains(s.DisplayName, "Tanzu Application Platform") { // TODO: Can this be improved to use some unique id?
532+
if s.ProductIdentifier == metadata.CSPProductIdentifier && strings.Contains(s.DisplayName, metadata.CSPDisplayName) {
481533
// Remove `www.` if present from the endpoint. Because when invoking directly through API it does not work
482534
tanzuHubEndpoint := strings.Replace(s.ServiceUrls.ServiceHome, "www.", "", 1)
483535
return tanzuHubEndpoint, nil
484536
}
485537
}
486538

487-
return "", errors.New("could not find 'Tanzu Application Platform' service associated with the specified organization")
539+
return "", errors.Errorf("could not find '%s' service associated with the specified organization", metadata.CSPDisplayName)
540+
}
541+
542+
// getTanzuHubMetadataFromCentralConfig gets the tanzu hub metadata from central config as best effort
543+
// If cannot get the data from central config it will set few default CSP config before returning the object
544+
func getTanzuHubMetadataFromCentralConfig() tanzuHubMetadata {
545+
hubMetadata := tanzuHubMetadata{}
546+
547+
// We will get the central configuration from the default discovery source
548+
discoverySource, err := config.GetCLIDiscoverySource(cliconfig.DefaultStandaloneDiscoveryName)
549+
if err != nil {
550+
return hubMetadata
551+
}
552+
centralConfigReader := centralconfig.NewCentralConfigReader(discoverySource)
553+
554+
// Get the tanzu hub metadata
555+
_ = centralConfigReader.GetCentralConfigEntry(centralConfigTanzuHubMetadata, &hubMetadata)
556+
return hubMetadata
488557
}

pkg/command/context.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -654,7 +654,7 @@ func globalTanzuLogin(c *configtypes.Context, generateContextNameFunc func(orgNa
654654
}
655655

656656
// Fetch the Tanzu Hub endpoint for the Tanzu context as a best case effort
657-
tanzuHubEndpoint, err := csp.GetTanzuHubEndpointForTAP(claims.OrgID, c.GlobalOpts.Auth.AccessToken, staging)
657+
tanzuHubEndpoint, err := csp.GetTanzuHubEndpoint(claims.OrgID, c.GlobalOpts.Auth.AccessToken, staging)
658658
if err != nil {
659659
log.V(7).Infof("unable to get Tanzu Hub endpoint. Error: %v", err.Error())
660660
}

0 commit comments

Comments
 (0)