Skip to content

Commit 72af619

Browse files
author
Rajat Chopra
committed
support for zero weighted services in a route
1 parent c771650 commit 72af619

File tree

4 files changed

+54
-16
lines changed

4 files changed

+54
-16
lines changed

images/router/haproxy/conf/haproxy-config.template

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,8 @@ backend be_edge_http_{{$cfgIdx}}
288288
{{ end }}
289289
http-request set-header Forwarded for=%[src];host=%[req.hdr(host)];proto=%[req.hdr(X-Forwarded-Proto)]
290290
{{ range $serviceUnitName, $weight := $cfg.ServiceUnitNames }}
291-
{{ with $serviceUnit := index $.ServiceUnits $serviceUnitName }}
291+
{{ if ne $weight 0 }}
292+
{{ with $serviceUnit := index $.ServiceUnits $serviceUnitName }}
292293
{{ range $idx, $endpoint := endpointsForAlias $cfg $serviceUnit }}
293294
{{ with $healthIntv := index $cfg.Annotations "router.openshift.io/haproxy.health.check.interval" }}
294295
{{ if (matchPattern "[1-9][0-9]*(us|ms|s|m|h|d)?" $healthIntv) }}
@@ -304,7 +305,8 @@ backend be_edge_http_{{$cfgIdx}}
304305
{{ end }}
305306
{{ end }}
306307
{{ end }}
307-
{{ end }}
308+
{{ end }}
309+
{{ end }}{{/* end if weight != 0 */}}
308310
{{ end }}{{/* end iterate over services */}}
309311
{{ end }}{{/* end if tls==edge/none */}}
310312

@@ -329,8 +331,9 @@ backend be_tcp_{{$cfgIdx}}
329331
hash-type consistent
330332
timeout check 5000ms
331333
{{ range $serviceUnitName, $weight := $cfg.ServiceUnitNames }}
332-
{{ with $serviceUnit := index $.ServiceUnits $serviceUnitName }}
333-
{{ range $idx, $endpoint := endpointsForAlias $cfg $serviceUnit }}
334+
{{ if ne $weight 0 }}
335+
{{ with $serviceUnit := index $.ServiceUnits $serviceUnitName }}
336+
{{ range $idx, $endpoint := endpointsForAlias $cfg $serviceUnit }}
334337
{{ with $healthIntv := index $cfg.Annotations "router.openshift.io/haproxy.health.check.interval" }}
335338
{{ if (matchPattern "[1-9][0-9]*(us|ms|s|m|h|d)?" $healthIntv) }}
336339
server {{$endpoint.IdHash}} {{$endpoint.IP}}:{{$endpoint.Port}} check inter {{$healthIntv}} weight {{$weight}}
@@ -344,8 +347,9 @@ backend be_tcp_{{$cfgIdx}}
344347
server {{$endpoint.IdHash}} {{$endpoint.IP}}:{{$endpoint.Port}} check inter 5000ms weight {{$weight}}
345348
{{ end }}
346349
{{ end }}
347-
{{ end }}
348-
{{ end }}
350+
{{ end }}{{/* end range endpointsForAlias */}}
351+
{{ end }}{{/* end get ServiceUnit from serviceUnitName */}}
352+
{{ end }}{{/* end if weight != 0 */}}
349353
{{ end }}{{/* end iterate over services*/}}
350354
{{ end }}{{/*end tls==passthrough*/}}
351355

@@ -375,8 +379,9 @@ backend be_secure_{{$cfgIdx}}
375379
http-request set-header Forwarded for=%[src];host=%[req.hdr(host)];proto=%[req.hdr(X-Forwarded-Proto)]
376380
cookie {{$cfg.RoutingKeyName}} insert indirect nocache httponly secure
377381
{{ range $serviceUnitName, $weight := $cfg.ServiceUnitNames }}
378-
{{ with $serviceUnit := index $.ServiceUnits $serviceUnitName }}
379-
{{ range $idx, $endpoint := endpointsForAlias $cfg $serviceUnit }}
382+
{{ if ne $weight 0 }}
383+
{{ with $serviceUnit := index $.ServiceUnits $serviceUnitName }}
384+
{{ range $idx, $endpoint := endpointsForAlias $cfg $serviceUnit }}
380385
{{ with $healthIntv := index $cfg.Annotations "router.openshift.io/haproxy.health.check.interval" }}
381386
{{ if (matchPattern "[1-9][0-9]*(us|ms|s|m|h|d)?" $healthIntv) }}
382387
server {{$endpoint.IdHash}} {{$endpoint.IP}}:{{$endpoint.Port}} ssl check inter {{$healthIntv}} verify required ca-file {{ $workingDir }}/cacerts/{{$cfgIdx}}.pem cookie {{$endpoint.IdHash}} weight {{$weight}}
@@ -389,10 +394,11 @@ backend be_secure_{{$cfgIdx}}
389394
{{ else }}
390395
server {{$endpoint.IdHash}} {{$endpoint.IP}}:{{$endpoint.Port}} ssl check inter 5000ms verify required ca-file {{$workingDir}}/cacerts/{{$cfgIdx}}.pem cookie {{$endpoint.IdHash}} weight {{$weight}}
391396
{{ end }}
392-
{{ end }}
393-
{{ end }}
394-
{{ end }}
395-
{{ end }}
397+
{{ end }}{{/* end get health interval annotation */}}
398+
{{ end }}{{/* end range endpointsForAlias */}}
399+
{{ end }}{{/* end get serviceUnit from its name */}}
400+
{{ end }}{{/* end if weight != 0 */}}
401+
{{ end }}{{/* end range over serviceUnitNames */}}
396402
{{ end }}{{/* end tls==reencrypt */}}
397403
{{ end }}{{/* end loop over routes */}}
398404
{{ end }}{{/* end haproxy config template */}}

pkg/route/api/validation/validation.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ func ValidateRoute(route *routeapi.Route) field.ErrorList {
4646
if route.Spec.To.Kind != "Service" {
4747
result = append(result, field.Invalid(specPath.Child("to", "kind"), route.Spec.To.Kind, "must reference a Service"))
4848
}
49-
if route.Spec.To.Weight != nil && (*route.Spec.To.Weight < 1 || *route.Spec.To.Weight > 256) {
50-
result = append(result, field.Invalid(specPath.Child("to", "weight"), route.Spec.To.Weight, "weight must be an integer between 1 and 256"))
49+
if route.Spec.To.Weight != nil && (*route.Spec.To.Weight < 0 || *route.Spec.To.Weight > 256) {
50+
result = append(result, field.Invalid(specPath.Child("to", "weight"), route.Spec.To.Weight, "weight must be an integer between 0 and 256"))
5151
}
5252

5353
if len(route.Spec.AlternateBackends) > 3 {
@@ -60,8 +60,8 @@ func ValidateRoute(route *routeapi.Route) field.ErrorList {
6060
if svc.Kind != "Service" {
6161
result = append(result, field.Invalid(specPath.Child("alternateBackends", "kind"), svc.Kind, "must reference a Service"))
6262
}
63-
if svc.Weight != nil && (*svc.Weight < 1 || *svc.Weight > 256) {
64-
result = append(result, field.Invalid(specPath.Child("alternateBackends", "weight"), svc.Weight, "weight must be an integer between 1 and 256"))
63+
if svc.Weight != nil && (*svc.Weight < 0 || *svc.Weight > 256) {
64+
result = append(result, field.Invalid(specPath.Child("alternateBackends", "weight"), svc.Weight, "weight must be an integer between 0 and 256"))
6565
}
6666
}
6767

test/extended/router/weighted.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,15 @@ var _ = g.Describe("[networking][router] weighted openshift router", func() {
9696
if weightedRatio < 5 && weightedRatio > 0.2 {
9797
e2e.Failf("Unexpected weighted ratio for incoming traffic: %v (%d/%d)", weightedRatio, trafficEP1, trafficEP2)
9898
}
99+
100+
g.By(fmt.Sprintf("checking that zero weights are also respected by the router"))
101+
host = "zeroweighted.example.com"
102+
req, _ = requestViaReverseProxy("GET", routerURL, host)
103+
resp, err = http.DefaultClient.Do(req)
104+
o.Expect(err).NotTo(o.HaveOccurred())
105+
if resp.StatusCode != http.StatusServiceUnavailable {
106+
e2e.Failf("Expected zero weighted route to return a 503, but got %v", resp.StatusCode)
107+
}
99108
})
100109
})
101110
})

test/extended/testdata/weighted-router.yaml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,29 @@ items:
6161
ports:
6262
- targetPort: 8080
6363

64+
# a route that has multiple services but all weights are zero
65+
- apiVersion: v1
66+
kind: Route
67+
metadata:
68+
name: zeroweightedroute
69+
labels:
70+
test: router
71+
select: weighted
72+
annotations:
73+
haproxy.router.openshift.io/balance: roundrobin
74+
spec:
75+
host: zeroweighted.example.com
76+
to:
77+
name: weightedendpoints1
78+
kind: Service
79+
weight: 0
80+
alternateBackends:
81+
- name: weightedendpoints2
82+
kind: Service
83+
weight: 0
84+
ports:
85+
- targetPort: 8080
86+
6487
# two services that can be routed to
6588
- apiVersion: v1
6689
kind: Service

0 commit comments

Comments
 (0)