Skip to content

Commit 946dde0

Browse files
authored
xdsclient: NACK endpoint resources with zero weight (#5560)
1 parent b89f49b commit 946dde0

File tree

5 files changed

+29
-7
lines changed

5 files changed

+29
-7
lines changed

internal/testutils/xds/e2e/clientresources.go

+1
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,7 @@ func DefaultEndpoint(clusterName string, host string, ports []uint32) *v3endpoin
370370
PortSpecifier: &v3corepb.SocketAddress_PortValue{PortValue: port}},
371371
}},
372372
}},
373+
LoadBalancingWeight: &wrapperspb.UInt32Value{Value: 1},
373374
})
374375
}
375376
return &v3endpointpb.ClusterLoadAssignment{

xds/internal/testutils/protos.go

+3
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ func (clab *ClusterLoadAssignmentBuilder) AddLocality(subzone string, weight uin
118118
lbe.LoadBalancingWeight = &wrapperspb.UInt32Value{Value: opts.Weight[i]}
119119
}
120120
}
121+
if lbe.LoadBalancingWeight == nil {
122+
lbe.LoadBalancingWeight = &wrapperspb.UInt32Value{Value: 1}
123+
}
121124
lbEndPoints = append(lbEndPoints, lbe)
122125
}
123126

xds/internal/xdsclient/controller/v2_eds_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ func (s) TestEDSHandleResponse(t *testing.T) {
117117
"not-goodEDSName": {Update: xdsresource.EndpointsUpdate{
118118
Localities: []xdsresource.Locality{
119119
{
120-
Endpoints: []xdsresource.Endpoint{{Address: "addr1:314"}},
120+
Endpoints: []xdsresource.Endpoint{{Address: "addr1:314", Weight: 1}},
121121
ID: internal.LocalityID{SubZone: "locality-1"},
122122
Priority: 0,
123123
Weight: 1,
@@ -140,13 +140,13 @@ func (s) TestEDSHandleResponse(t *testing.T) {
140140
goodEDSName: {Update: xdsresource.EndpointsUpdate{
141141
Localities: []xdsresource.Locality{
142142
{
143-
Endpoints: []xdsresource.Endpoint{{Address: "addr1:314"}},
143+
Endpoints: []xdsresource.Endpoint{{Address: "addr1:314", Weight: 1}},
144144
ID: internal.LocalityID{SubZone: "locality-1"},
145145
Priority: 1,
146146
Weight: 1,
147147
},
148148
{
149-
Endpoints: []xdsresource.Endpoint{{Address: "addr2:159"}},
149+
Endpoints: []xdsresource.Endpoint{{Address: "addr2:159", Weight: 1}},
150150
ID: internal.LocalityID{SubZone: "locality-2"},
151151
Priority: 0,
152152
Weight: 1,

xds/internal/xdsclient/xdsresource/unmarshal_eds.go

+12-4
Original file line numberDiff line numberDiff line change
@@ -90,16 +90,20 @@ func parseDropPolicy(dropPolicy *v3endpointpb.ClusterLoadAssignment_Policy_DropO
9090
}
9191
}
9292

93-
func parseEndpoints(lbEndpoints []*v3endpointpb.LbEndpoint) []Endpoint {
93+
func parseEndpoints(lbEndpoints []*v3endpointpb.LbEndpoint) ([]Endpoint, error) {
9494
endpoints := make([]Endpoint, 0, len(lbEndpoints))
9595
for _, lbEndpoint := range lbEndpoints {
96+
weight := lbEndpoint.GetLoadBalancingWeight().GetValue()
97+
if weight == 0 {
98+
return nil, fmt.Errorf("EDS response contains an endpoint with zero weight: %+v", lbEndpoint)
99+
}
96100
endpoints = append(endpoints, Endpoint{
97101
HealthStatus: EndpointHealthStatus(lbEndpoint.GetHealthStatus()),
98102
Address: parseAddress(lbEndpoint.GetEndpoint().GetAddress().GetSocketAddress()),
99-
Weight: lbEndpoint.GetLoadBalancingWeight().GetValue(),
103+
Weight: weight,
100104
})
101105
}
102-
return endpoints
106+
return endpoints, nil
103107
}
104108

105109
func parseEDSRespProto(m *v3endpointpb.ClusterLoadAssignment, logger *grpclog.PrefixLogger) (EndpointsUpdate, error) {
@@ -134,9 +138,13 @@ func parseEDSRespProto(m *v3endpointpb.ClusterLoadAssignment, logger *grpclog.Pr
134138
return EndpointsUpdate{}, fmt.Errorf("duplicate locality %s with the same priority %v", lidStr, priority)
135139
}
136140
localitiesWithPriority[lidStr] = true
141+
endpoints, err := parseEndpoints(locality.GetLbEndpoints())
142+
if err != nil {
143+
return EndpointsUpdate{}, err
144+
}
137145
ret.Localities = append(ret.Localities, Locality{
138146
ID: lid,
139-
Endpoints: parseEndpoints(locality.GetLbEndpoints()),
147+
Endpoints: endpoints,
140148
Weight: locality.GetLoadBalancingWeight().GetValue(),
141149
Priority: priority,
142150
})

xds/internal/xdsclient/xdsresource/unmarshal_eds_test.go

+10
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,16 @@ func (s) TestEDSParseRespProto(t *testing.T) {
6464
want: EndpointsUpdate{},
6565
wantErr: true,
6666
},
67+
{
68+
name: "zero-endpoint-weight",
69+
m: func() *v3endpointpb.ClusterLoadAssignment {
70+
clab0 := newClaBuilder("test", nil)
71+
clab0.addLocality("locality-0", 1, 0, []string{"addr1:314"}, &addLocalityOptions{Weight: []uint32{0}})
72+
return clab0.Build()
73+
}(),
74+
want: EndpointsUpdate{},
75+
wantErr: true,
76+
},
6777
{
6878
name: "duplicate-locality-in-the-same-priority",
6979
m: func() *v3endpointpb.ClusterLoadAssignment {

0 commit comments

Comments
 (0)