Skip to content

Commit 9e8325a

Browse files
(discoverychain) Add support to disable routes with weight 0 instead of setting it 1 explicitly
1 parent e763863 commit 9e8325a

3 files changed

Lines changed: 39 additions & 2 deletions

File tree

.changelog/23216.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:bug
2+
api-gateway: allow http-routes to set `weight: 0` to disable traffic to particular service
3+
```

agent/structs/config_entry_routes.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,19 @@ func (e *HTTPRouteConfigEntry) Normalize() error {
105105
rule.Matches[j] = normalizeHTTPMatch(match)
106106
}
107107

108+
allServicesHaveZeroWeight := true
108109
for j, service := range rule.Services {
109110
rule.Services[j] = e.normalizeHTTPService(service)
111+
if rule.Services[j].Weight != 0 {
112+
allServicesHaveZeroWeight = false
113+
}
114+
}
115+
// Preserve historical behavior for routes that omit all service weights by
116+
// normalizing them to 1.
117+
if allServicesHaveZeroWeight {
118+
for j := range rule.Services {
119+
rule.Services[j].Weight = 1
120+
}
110121
}
111122
e.Rules[i] = rule
112123
}
@@ -123,7 +134,7 @@ func (e *HTTPRouteConfigEntry) Normalize() error {
123134
func (e *HTTPRouteConfigEntry) normalizeHTTPService(service HTTPService) HTTPService {
124135
service.Merge(e.GetEnterpriseMeta())
125136
service.Normalize()
126-
if service.Weight <= 0 {
137+
if service.Weight < 0 {
127138
service.Weight = 1
128139
}
129140
return service

agent/structs/config_entry_routes_test.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ func TestHTTPRoute(t *testing.T) {
338338
}},
339339
},
340340
},
341-
"rule normalizes service weight": {
341+
"rule preserves zero service weight and normalizes negative service weight": {
342342
entry: &HTTPRouteConfigEntry{
343343
Kind: HTTPRoute,
344344
Name: "route-one",
@@ -355,6 +355,29 @@ func TestHTTPRoute(t *testing.T) {
355355
},
356356
}},
357357
},
358+
check: func(t *testing.T, entry ConfigEntry) {
359+
route := entry.(*HTTPRouteConfigEntry)
360+
require.Equal(t, 0, route.Rules[0].Services[0].Weight)
361+
require.Equal(t, 1, route.Rules[0].Services[1].Weight)
362+
},
363+
},
364+
"rule normalizes all-zero service weights to defaults": {
365+
entry: &HTTPRouteConfigEntry{
366+
Kind: HTTPRoute,
367+
Name: "route-one",
368+
Rules: []HTTPRouteRule{{
369+
Services: []HTTPService{
370+
{
371+
Name: "test",
372+
Weight: 0,
373+
},
374+
{
375+
Name: "test2",
376+
Weight: 0,
377+
},
378+
},
379+
}},
380+
},
358381
check: func(t *testing.T, entry ConfigEntry) {
359382
route := entry.(*HTTPRouteConfigEntry)
360383
require.Equal(t, 1, route.Rules[0].Services[0].Weight)

0 commit comments

Comments
 (0)