Skip to content

Commit

Permalink
[federation_target_parsing] c1
Browse files Browse the repository at this point in the history
  • Loading branch information
menghanl committed Nov 1, 2021
1 parent cfba88f commit 85457ab
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 15 deletions.
7 changes: 6 additions & 1 deletion xds/internal/resolver/xds_resolver.go
Expand Up @@ -128,7 +128,12 @@ func (b *xdsResolverBuilder) Build(t resolver.Target, cc resolver.ClientConn, op
template = a.ClientListenerResourceNameTemplate
}
}
resourceName := bootstrap.PopulateResourceTemplate(template, strings.TrimPrefix(r.target.URL.Path, "/"))
endpoint := r.target.URL.Path
if endpoint == "" {
endpoint = r.target.URL.Opaque
}
endpoint = strings.TrimPrefix(endpoint, "/")
resourceName := bootstrap.PopulateResourceTemplate(template, endpoint)

// Register a watch on the xdsClient for the user's dial target.
cancelWatch := watchService(r.client, resourceName, r.handleServiceUpdate, r.logger)
Expand Down
12 changes: 5 additions & 7 deletions xds/internal/testutils/fakeclient/client.go
Expand Up @@ -316,12 +316,10 @@ func NewClientWithName(name string) *Client {
loadReportCh: testutils.NewChannel(),
lrsCancelCh: testutils.NewChannel(),
loadStore: load.NewStore(),
bootstrapCfg: &bootstrap.Config{
ClientDefaultListenerResourceNameTemplate: "%s",
},
rdsCbs: make(map[string]func(xdsclient.RouteConfigUpdate, error)),
cdsCbs: make(map[string]func(xdsclient.ClusterUpdate, error)),
edsCbs: make(map[string]func(xdsclient.EndpointsUpdate, error)),
Closed: grpcsync.NewEvent(),
bootstrapCfg: &bootstrap.Config{ClientDefaultListenerResourceNameTemplate: "%s"},
rdsCbs: make(map[string]func(xdsclient.RouteConfigUpdate, error)),
cdsCbs: make(map[string]func(xdsclient.ClusterUpdate, error)),
edsCbs: make(map[string]func(xdsclient.EndpointsUpdate, error)),
Closed: grpcsync.NewEvent(),
}
}
11 changes: 5 additions & 6 deletions xds/internal/xdsclient/bootstrap/template.go
Expand Up @@ -22,20 +22,19 @@ import (
"strings"
)

// PopulateResourceTemplate populates the given template using the target string
// t. "%s", if exists in the template, will be replaced with t.
// PopulateResourceTemplate populates the given template using the target
// string. "%s", if exists in the template, will be replaced with target.
//
// If the template starts with "xdstp:", the replaced string will be %-encoded.
// But note that "/" is not percent encoded.
func PopulateResourceTemplate(template string, t string) string {
func PopulateResourceTemplate(template, target string) string {
if !strings.Contains(template, "%s") {
return template
}
if strings.HasPrefix(template, "xdstp:") {
// Percent encode t.
t = percentEncode(t)
target = percentEncode(target)
}
return strings.Replace(template, "%s", t, -1)
return strings.Replace(template, "%s", target, -1)
}

// percentEncode percent encode t, except for "/". See the tests for examples.
Expand Down
2 changes: 1 addition & 1 deletion xds/internal/xdsclient/bootstrap/template_test.go
Expand Up @@ -43,7 +43,7 @@ func Test_percentEncode(t *testing.T) {
{
name: "/ should not be percent encoded",
t: "my/service/region",
want: "my/service/region", // "/"s are kepted.
want: "my/service/region", // "/"s are kept.
},
}
for _, tt := range tests {
Expand Down

0 comments on commit 85457ab

Please sign in to comment.