Skip to content

Commit

Permalink
Update TMC endpoint mapping rules for Tanzu context (#798)
Browse files Browse the repository at this point in the history
  • Loading branch information
anujc25 committed Aug 14, 2024
1 parent 72f0913 commit bbde4b2
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 8 deletions.
41 changes: 33 additions & 8 deletions pkg/command/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"net/url"
"os"
"reflect"
"regexp"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -668,9 +667,14 @@ func globalTanzuLogin(c *configtypes.Context, generateContextNameFunc func(orgNa

// Fetch the Tanzu Hub endpoint for the Tanzu context as a best case effort
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())
// If the TANZU_CLI_HUB_ENDPOINT is set just use the configured endpoint
if os.Getenv(constants.TPHubEndpoint) != "" {
tanzuHubEndpoint = os.Getenv(constants.TPHubEndpoint)
} else {
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)
Expand Down Expand Up @@ -2143,14 +2147,35 @@ func renderDynamicTable(slices interface{}, tableWriter component.OutputWriter,
}

func mapTanzuEndpointToTMCEndpoint(tanzuEndpoint string) string {
// Define the regular expression pattern
apiPattern := regexp.MustCompile(`https://api\.tanzu(-\w*)?\.cloud\.vmware\.com`)
// Replace "api" with "tmc" in the input URL
tmcEndpoint := apiPattern.ReplaceAllString(tanzuEndpoint, "https://tmc.tanzu$1.cloud.vmware.com")
// If the TANZU_CLI_K8S_OPS_ENDPOINT is set just return the configured endpoint
if os.Getenv(constants.TPKubernetesOpsEndpoint) != "" {
return os.Getenv(constants.TPKubernetesOpsEndpoint)
}

tmcEndpoint := ""
// Define the mapping rules
mappingRules := []struct {
tanzuEndpointPrefix string
tmcEndpointPrefix string
}{
{"https://api.tanzu", "https://tmc.tanzu"},
{"https://ucp.platform", "https://ops.platform"},
}

// Iterate through the mapping rules
for _, rule := range mappingRules {
if strings.HasPrefix(tanzuEndpoint, rule.tanzuEndpointPrefix) {
// Replace the tanzuEndpointPrefix with the tmcEndpointPrefix
tmcEndpoint = strings.Replace(tanzuEndpoint, rule.tanzuEndpointPrefix, rule.tmcEndpointPrefix, 1)
break
}
}

// Check if the transformation was successful
if tanzuEndpoint == tmcEndpoint {
return ""
}

return tmcEndpoint
}

Expand Down
12 changes: 12 additions & 0 deletions pkg/command/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1882,6 +1882,18 @@ func TestMapTanzuEndpointToTMCEndpoint(t *testing.T) {
input: "https://api.tanzu.cloud.vmware.com",
expected: "https://tmc.tanzu.cloud.vmware.com",
},
{
input: "https://ucp.platform.tanzu.broadcom.com",
expected: "https://ops.platform.tanzu.broadcom.com",
},
{
input: "https://ucp.platform-dev.tanzu.broadcom.com",
expected: "https://ops.platform-dev.tanzu.broadcom.com",
},
{
input: "https://ucp.platform-verify.tanzu.broadcom.com",
expected: "https://ops.platform-verify.tanzu.broadcom.com",
},
{
input: "https://symphony.api.tanzu.cloud.vmware.com",
expected: "",
Expand Down
8 changes: 8 additions & 0 deletions pkg/constants/env_variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,12 @@ const (

// UseTanzuCSP uses the Tanzu CSP while login/context creation
UseTanzuCSP = "TANZU_CLI_USE_TANZU_CLOUD_SERVICE_PROVIDER"

// TPKubernetesOpsEndpoint specifies kubernetes ops endpoint for the Tanzu Platform
// This will be used as part of `tanzu login`
TPKubernetesOpsEndpoint = "TANZU_CLI_K8S_OPS_ENDPOINT"

// TPHubEndpoint specifies hub endpoint for the Tanzu Platform
// This will be used as part of `tanzu login`
TPHubEndpoint = "TANZU_CLI_HUB_ENDPOINT"
)

0 comments on commit bbde4b2

Please sign in to comment.