From 85457ab89da4361002fd52874f9e1d8e16289109 Mon Sep 17 00:00:00 2001 From: Menghan Li Date: Mon, 1 Nov 2021 15:53:08 -0700 Subject: [PATCH] [federation_target_parsing] c1 --- xds/internal/resolver/xds_resolver.go | 7 ++++++- xds/internal/testutils/fakeclient/client.go | 12 +++++------- xds/internal/xdsclient/bootstrap/template.go | 11 +++++------ xds/internal/xdsclient/bootstrap/template_test.go | 2 +- 4 files changed, 17 insertions(+), 15 deletions(-) diff --git a/xds/internal/resolver/xds_resolver.go b/xds/internal/resolver/xds_resolver.go index 5df32bfd2717..e04ce00b2d29 100644 --- a/xds/internal/resolver/xds_resolver.go +++ b/xds/internal/resolver/xds_resolver.go @@ -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) diff --git a/xds/internal/testutils/fakeclient/client.go b/xds/internal/testutils/fakeclient/client.go index 938a21176518..29b551840a67 100644 --- a/xds/internal/testutils/fakeclient/client.go +++ b/xds/internal/testutils/fakeclient/client.go @@ -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(), } } diff --git a/xds/internal/xdsclient/bootstrap/template.go b/xds/internal/xdsclient/bootstrap/template.go index 90d6db1de626..9b51fcc83972 100644 --- a/xds/internal/xdsclient/bootstrap/template.go +++ b/xds/internal/xdsclient/bootstrap/template.go @@ -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. diff --git a/xds/internal/xdsclient/bootstrap/template_test.go b/xds/internal/xdsclient/bootstrap/template_test.go index 06c8f40b7df4..1511cf0774a2 100644 --- a/xds/internal/xdsclient/bootstrap/template_test.go +++ b/xds/internal/xdsclient/bootstrap/template_test.go @@ -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 {