Skip to content

Commit a0be99a

Browse files
authored
xds: use BuildOptions.Target.endpoint string instead of deprecated cc.Target (#2807)
1 parent 4748a04 commit a0be99a

File tree

8 files changed

+58
-27
lines changed

8 files changed

+58
-27
lines changed

balancer/xds/internal/const.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
*
3+
* Copyright 2019 gRPC authors.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*/
18+
19+
package internal
20+
21+
var (
22+
// GrpcHostname is the metadata key for specifying the grpc service name when sending xDS requests
23+
// from grpc to the traffic director.
24+
GrpcHostname = "TRAFFICDIRECTOR_GRPC_HOSTNAME"
25+
)

balancer/xds/lrs/lrs.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"github.com/golang/protobuf/ptypes"
2626
structpb "github.com/golang/protobuf/ptypes/struct"
2727
"google.golang.org/grpc"
28+
"google.golang.org/grpc/balancer/xds/internal"
2829
basepb "google.golang.org/grpc/balancer/xds/internal/proto/envoy/api/v2/core/base"
2930
loadreportpb "google.golang.org/grpc/balancer/xds/internal/proto/envoy/api/v2/endpoint/load_report"
3031
lrspb "google.golang.org/grpc/balancer/xds/internal/proto/envoy/service/load_stats/v2/lrs"
@@ -50,16 +51,14 @@ type lrsStore struct {
5051
drops sync.Map // map[string]*uint64
5152
}
5253

53-
const grpcHostname = "com.googleapis.trafficdirector.grpc_hostname"
54-
5554
// NewStore creates a store for load reports.
5655
func NewStore(serviceName string) Store {
5756
return &lrsStore{
5857
serviceName: serviceName,
5958
node: &basepb.Node{
6059
Metadata: &structpb.Struct{
6160
Fields: map[string]*structpb.Value{
62-
grpcHostname: {
61+
internal.GrpcHostname: {
6362
Kind: &structpb.Value_StringValue{StringValue: serviceName},
6463
},
6564
},

balancer/xds/lrs/lrs_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
structpb "github.com/golang/protobuf/ptypes/struct"
3333
"github.com/google/go-cmp/cmp"
3434
"google.golang.org/grpc"
35+
"google.golang.org/grpc/balancer/xds/internal"
3536
basepb "google.golang.org/grpc/balancer/xds/internal/proto/envoy/api/v2/core/base"
3637
loadreportpb "google.golang.org/grpc/balancer/xds/internal/proto/envoy/api/v2/endpoint/load_report"
3738
lrspb "google.golang.org/grpc/balancer/xds/internal/proto/envoy/service/load_stats/v2/lrs"
@@ -144,7 +145,7 @@ func (lrss *lrsServer) StreamLoadStats(stream lrspb.LoadReportingService_StreamL
144145
Node: &basepb.Node{
145146
Metadata: &structpb.Struct{
146147
Fields: map[string]*structpb.Value{
147-
grpcHostname: {
148+
internal.GrpcHostname: {
148149
Kind: &structpb.Value_StringValue{StringValue: testService},
149150
},
150151
},

balancer/xds/xds.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ import (
3636
cdspb "google.golang.org/grpc/balancer/xds/internal/proto/envoy/api/v2/cds"
3737
edspb "google.golang.org/grpc/balancer/xds/internal/proto/envoy/api/v2/eds"
3838
"google.golang.org/grpc/balancer/xds/lrs"
39-
4039
"google.golang.org/grpc/connectivity"
4140
"google.golang.org/grpc/grpclog"
4241
"google.golang.org/grpc/resolver"
@@ -79,7 +78,7 @@ func (b *xdsBalancerBuilder) Build(cc balancer.ClientConn, opts balancer.BuildOp
7978
grpcUpdate: make(chan interface{}),
8079
xdsClientUpdate: make(chan interface{}),
8180
timer: createDrainedTimer(), // initialized a timer that won't fire without reset
82-
loadStore: lrs.NewStore(cc.Target()),
81+
loadStore: lrs.NewStore(opts.Target.Endpoint),
8382
}
8483
x.cc = &xdsClientConn{
8584
updateState: x.connStateMgr.updateState,
@@ -187,7 +186,7 @@ func (x *xdsBalancer) startNewXDSClient(u *xdsConfig) {
187186
prevClient.close()
188187
}
189188
}
190-
x.client = newXDSClient(u.BalancerName, x.cc.Target(), u.ChildPolicy == nil, x.buildOpts, x.loadStore, newADS, loseContact, exitCleanup)
189+
x.client = newXDSClient(u.BalancerName, u.ChildPolicy == nil, x.buildOpts, x.loadStore, newADS, loseContact, exitCleanup)
191190
go x.client.run()
192191
}
193192

@@ -451,8 +450,8 @@ func (x *xdsBalancer) newADSResponse(ctx context.Context, resp proto.Message) er
451450
var update interface{}
452451
switch u := resp.(type) {
453452
case *cdspb.Cluster:
454-
if u.GetName() != x.cc.Target() {
455-
return fmt.Errorf("unmatched service name, got %s, want %s", u.GetName(), x.cc.Target())
453+
if u.GetName() != x.buildOpts.Target.Endpoint {
454+
return fmt.Errorf("unmatched service name, got %s, want %s", u.GetName(), x.buildOpts.Target.Endpoint)
456455
}
457456
if u.GetType() != cdspb.Cluster_EDS {
458457
return fmt.Errorf("unexpected service discovery type, got %v, want %v", u.GetType(), cdspb.Cluster_EDS)

balancer/xds/xds_client.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
structpb "github.com/golang/protobuf/ptypes/struct"
3131
"google.golang.org/grpc"
3232
"google.golang.org/grpc/balancer"
33+
"google.golang.org/grpc/balancer/xds/internal"
3334
cdspb "google.golang.org/grpc/balancer/xds/internal/proto/envoy/api/v2/cds"
3435
basepb "google.golang.org/grpc/balancer/xds/internal/proto/envoy/api/v2/core/base"
3536
discoverypb "google.golang.org/grpc/balancer/xds/internal/proto/envoy/api/v2/discovery"
@@ -42,7 +43,6 @@ import (
4243
)
4344

4445
const (
45-
grpcHostname = "com.googleapis.trafficdirector.grpc_hostname"
4646
cdsType = "type.googleapis.com/envoy.api.v2.Cluster"
4747
edsType = "type.googleapis.com/envoy.api.v2.ClusterLoadAssignment"
4848
endpointRequired = "endpoints_required"
@@ -135,7 +135,7 @@ func (c *client) newCDSRequest() *discoverypb.DiscoveryRequest {
135135
Node: &basepb.Node{
136136
Metadata: &structpb.Struct{
137137
Fields: map[string]*structpb.Value{
138-
grpcHostname: {
138+
internal.GrpcHostname: {
139139
Kind: &structpb.Value_StringValue{StringValue: c.serviceName},
140140
},
141141
},
@@ -209,18 +209,21 @@ func (c *client) adsCallAttempt() (firstRespReceived bool) {
209209
if c.enableCDS {
210210
if err := st.Send(c.newCDSRequest()); err != nil {
211211
// current stream is broken, start a new one.
212+
grpclog.Infof("xds: ads RPC failed due to err: %v, when sending the CDS request ", err)
212213
return
213214
}
214215
}
215216
if err := st.Send(c.newEDSRequest()); err != nil {
216217
// current stream is broken, start a new one.
218+
grpclog.Infof("xds: ads RPC failed due to err: %v, when sending the EDS request", err)
217219
return
218220
}
219221
expectCDS := c.enableCDS
220222
for {
221223
resp, err := st.Recv()
222224
if err != nil {
223225
// current stream is broken, start a new one.
226+
grpclog.Infof("xds: ads RPC failed due to err: %v, when receiving the response", err)
224227
return
225228
}
226229
firstRespReceived = true
@@ -264,10 +267,11 @@ func (c *client) adsCallAttempt() (firstRespReceived bool) {
264267
})
265268
}
266269
}
267-
func newXDSClient(balancerName string, serviceName string, enableCDS bool, opts balancer.BuildOptions, loadStore lrs.Store, newADS func(context.Context, proto.Message) error, loseContact func(ctx context.Context), exitCleanup func()) *client {
270+
271+
func newXDSClient(balancerName string, enableCDS bool, opts balancer.BuildOptions, loadStore lrs.Store, newADS func(context.Context, proto.Message) error, loseContact func(ctx context.Context), exitCleanup func()) *client {
268272
c := &client{
269273
balancerName: balancerName,
270-
serviceName: serviceName,
274+
serviceName: opts.Target.Endpoint,
271275
enableCDS: enableCDS,
272276
opts: opts,
273277
newADS: newADS,

balancer/xds/xds_client_test.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import (
3535
wrpb "github.com/golang/protobuf/ptypes/wrappers"
3636
"google.golang.org/grpc"
3737
"google.golang.org/grpc/balancer"
38+
"google.golang.org/grpc/balancer/xds/internal"
3839
cdspb "google.golang.org/grpc/balancer/xds/internal/proto/envoy/api/v2/cds"
3940
addresspb "google.golang.org/grpc/balancer/xds/internal/proto/envoy/api/v2/core/address"
4041
basepb "google.golang.org/grpc/balancer/xds/internal/proto/envoy/api/v2/core/base"
@@ -44,6 +45,7 @@ import (
4445
adspb "google.golang.org/grpc/balancer/xds/internal/proto/envoy/service/discovery/v2/ads"
4546
lrspb "google.golang.org/grpc/balancer/xds/internal/proto/envoy/service/load_stats/v2/lrs"
4647
"google.golang.org/grpc/codes"
48+
"google.golang.org/grpc/resolver"
4749
"google.golang.org/grpc/status"
4850
)
4951

@@ -53,7 +55,7 @@ var (
5355
Node: &basepb.Node{
5456
Metadata: &structpb.Struct{
5557
Fields: map[string]*structpb.Value{
56-
grpcHostname: {
58+
internal.GrpcHostname: {
5759
Kind: &structpb.Value_StringValue{StringValue: testServiceName},
5860
},
5961
},
@@ -318,7 +320,7 @@ func testXdsClientResponseHandling(t *testing.T, test *testConfig) {
318320
adsChan <- i
319321
return nil
320322
}
321-
client := newXDSClient(addr, testServiceName, test.doCDS, balancer.BuildOptions{}, nil, newADS, func(context.Context) {}, func() {})
323+
client := newXDSClient(addr, test.doCDS, balancer.BuildOptions{Target: resolver.Target{Endpoint: testServiceName}}, nil, newADS, func(context.Context) {}, func() {})
322324
defer client.close()
323325
go client.run()
324326

@@ -386,7 +388,7 @@ func testXdsClientLoseContactRemoteClose(t *testing.T, test *testConfig) {
386388
loseContactFunc := func(context.Context) {
387389
contactChan <- &loseContact{}
388390
}
389-
client := newXDSClient(addr, testServiceName, test.doCDS, balancer.BuildOptions{}, nil, newADS, loseContactFunc, func() {})
391+
client := newXDSClient(addr, test.doCDS, balancer.BuildOptions{Target: resolver.Target{Endpoint: testServiceName}}, nil, newADS, loseContactFunc, func() {})
390392
defer client.close()
391393
go client.run()
392394

@@ -420,7 +422,7 @@ func testXdsClientLoseContactADSRelatedErrorOccur(t *testing.T, test *testConfig
420422
loseContactFunc := func(context.Context) {
421423
contactChan <- &loseContact{}
422424
}
423-
client := newXDSClient(addr, testServiceName, test.doCDS, balancer.BuildOptions{}, nil, newADS, loseContactFunc, func() {})
425+
client := newXDSClient(addr, test.doCDS, balancer.BuildOptions{Target: resolver.Target{Endpoint: testServiceName}}, nil, newADS, loseContactFunc, func() {})
424426
defer client.close()
425427
go client.run()
426428

@@ -454,7 +456,7 @@ func (s) TestXdsClientExponentialRetry(t *testing.T) {
454456
loseContactFunc := func(context.Context) {
455457
contactChan <- &loseContact{}
456458
}
457-
client := newXDSClient(addr, testServiceName, cfg.doCDS, balancer.BuildOptions{}, nil, newADS, loseContactFunc, func() {})
459+
client := newXDSClient(addr, cfg.doCDS, balancer.BuildOptions{Target: resolver.Target{Endpoint: testServiceName}}, nil, newADS, loseContactFunc, func() {})
458460
defer client.close()
459461
go client.run()
460462

balancer/xds/xds_lrs_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
structpb "github.com/golang/protobuf/ptypes/struct"
3232
"github.com/google/go-cmp/cmp"
3333
"google.golang.org/grpc/balancer"
34+
"google.golang.org/grpc/balancer/xds/internal"
3435
basepb "google.golang.org/grpc/balancer/xds/internal/proto/envoy/api/v2/core/base"
3536
lrspb "google.golang.org/grpc/balancer/xds/internal/proto/envoy/service/load_stats/v2/lrs"
3637
"google.golang.org/grpc/codes"
@@ -54,7 +55,7 @@ func (lrss *lrsServer) StreamLoadStats(stream lrspb.LoadReportingService_StreamL
5455
Node: &basepb.Node{
5556
Metadata: &structpb.Struct{
5657
Fields: map[string]*structpb.Value{
57-
grpcHostname: {
58+
internal.GrpcHostname: {
5859
Kind: &structpb.Value_StringValue{StringValue: testServiceName},
5960
},
6061
},
@@ -97,7 +98,7 @@ func (s) TestXdsLoadReporting(t *testing.T) {
9798

9899
builder := balancer.Get("xds")
99100
cc := newTestClientConn()
100-
lb, ok := builder.Build(cc, balancer.BuildOptions{}).(*xdsBalancer)
101+
lb, ok := builder.Build(cc, balancer.BuildOptions{Target: resolver.Target{Endpoint: testServiceName}}).(*xdsBalancer)
101102
if !ok {
102103
t.Fatalf("unable to type assert to *xdsBalancer")
103104
}
@@ -152,6 +153,6 @@ func (s) TestXdsLoadReporting(t *testing.T) {
152153
lrss.mu.Lock()
153154
defer lrss.mu.Unlock()
154155
if !cmp.Equal(lrss.drops, drops) {
155-
t.Errorf("different: %v", cmp.Diff(lrss.drops, drops))
156+
t.Errorf("different: %v %v %v", lrss.drops, drops, cmp.Diff(lrss.drops, drops))
156157
}
157158
}

balancer/xds/xds_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ func (s) TestXdsBalanceHandleResolvedAddrs(t *testing.T) {
276276

277277
builder := balancer.Get("xds")
278278
cc := newTestClientConn()
279-
lb, ok := builder.Build(cc, balancer.BuildOptions{}).(*xdsBalancer)
279+
lb, ok := builder.Build(cc, balancer.BuildOptions{Target: resolver.Target{Endpoint: testServiceName}}).(*xdsBalancer)
280280
if !ok {
281281
t.Fatalf("unable to type assert to *xdsBalancer")
282282
}
@@ -310,7 +310,7 @@ func (s) TestXdsBalanceHandleBalancerConfigBalancerNameUpdate(t *testing.T) {
310310

311311
builder := balancer.Get("xds")
312312
cc := newTestClientConn()
313-
lb, ok := builder.Build(cc, balancer.BuildOptions{}).(*xdsBalancer)
313+
lb, ok := builder.Build(cc, balancer.BuildOptions{Target: resolver.Target{Endpoint: testServiceName}}).(*xdsBalancer)
314314
if !ok {
315315
t.Fatalf("unable to type assert to *xdsBalancer")
316316
}
@@ -385,7 +385,7 @@ func (s) TestXdsBalanceHandleBalancerConfigChildPolicyUpdate(t *testing.T) {
385385

386386
builder := balancer.Get("xds")
387387
cc := newTestClientConn()
388-
lb, ok := builder.Build(cc, balancer.BuildOptions{}).(*xdsBalancer)
388+
lb, ok := builder.Build(cc, balancer.BuildOptions{Target: resolver.Target{Endpoint: testServiceName}}).(*xdsBalancer)
389389
if !ok {
390390
t.Fatalf("unable to type assert to *xdsBalancer")
391391
}
@@ -471,7 +471,7 @@ func (s) TestXdsBalanceHandleBalancerConfigFallbackUpdate(t *testing.T) {
471471

472472
builder := balancer.Get("xds")
473473
cc := newTestClientConn()
474-
lb, ok := builder.Build(cc, balancer.BuildOptions{}).(*xdsBalancer)
474+
lb, ok := builder.Build(cc, balancer.BuildOptions{Target: resolver.Target{Endpoint: testServiceName}}).(*xdsBalancer)
475475
if !ok {
476476
t.Fatalf("unable to type assert to *xdsBalancer")
477477
}
@@ -546,7 +546,7 @@ func (s) TestXdsBalancerHandlerSubConnStateChange(t *testing.T) {
546546

547547
builder := balancer.Get("xds")
548548
cc := newTestClientConn()
549-
lb, ok := builder.Build(cc, balancer.BuildOptions{}).(*xdsBalancer)
549+
lb, ok := builder.Build(cc, balancer.BuildOptions{Target: resolver.Target{Endpoint: testServiceName}}).(*xdsBalancer)
550550
if !ok {
551551
t.Fatalf("unable to type assert to *xdsBalancer")
552552
}
@@ -626,7 +626,7 @@ func (s) TestXdsBalancerFallbackSignalFromEdsBalancer(t *testing.T) {
626626

627627
builder := balancer.Get("xds")
628628
cc := newTestClientConn()
629-
lb, ok := builder.Build(cc, balancer.BuildOptions{}).(*xdsBalancer)
629+
lb, ok := builder.Build(cc, balancer.BuildOptions{Target: resolver.Target{Endpoint: testServiceName}}).(*xdsBalancer)
630630
if !ok {
631631
t.Fatalf("unable to type assert to *xdsBalancer")
632632
}

0 commit comments

Comments
 (0)