Skip to content

Commit

Permalink
support for zero weighted services in a route
Browse files Browse the repository at this point in the history
  • Loading branch information
Rajat Chopra authored and root committed Aug 15, 2016
1 parent b6b0bae commit ec8f26a
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 10 deletions.
22 changes: 14 additions & 8 deletions images/router/haproxy/conf/haproxy-config.template
Original file line number Diff line number Diff line change
Expand Up @@ -271,11 +271,13 @@ backend be_edge_http_{{$cfgIdx}}
{{ end }}
http-request set-header Forwarded for=%[src];host=%[req.hdr(host)];proto=%[req.hdr(X-Forwarded-Proto)]
{{ range $serviceUnitName, $weight := $cfg.ServiceUnitNames }}
{{ with $serviceUnit := index $.ServiceUnits $serviceUnitName }}
{{ if ne ($weight 0) }}
{{ with $serviceUnit := index $.ServiceUnits $serviceUnitName }}
{{ range $idx, $endpoint := endpointsForAlias $cfg $serviceUnit }}
server {{$endpoint.IdHash}} {{$endpoint.IP}}:{{$endpoint.Port}} check inter {{env "ROUTER_BACKEND_CHECK_INTERVAL" "5000ms"}} cookie {{$endpoint.IdHash}} weight {{ $weight }}
{{ end }}
{{ end }}
{{ end }}
{{ end }}{{/* end if weight != 0 */}}
{{ end }}{{/* end iterate over services */}}
{{ end }}{{/* end if tls==edge/none */}}

Expand All @@ -300,11 +302,13 @@ backend be_tcp_{{$cfgIdx}}
hash-type consistent
timeout check 5000ms
{{ range $serviceUnitName, $weight := $cfg.ServiceUnitNames }}
{{ with $serviceUnit := index $.ServiceUnits $serviceUnitName }}
{{ range $idx, $endpoint := endpointsForAlias $cfg $serviceUnit }}
{{ if ne ($weight 0) }}
{{ with $serviceUnit := index $.ServiceUnits $serviceUnitName }}
{{ range $idx, $endpoint := endpointsForAlias $cfg $serviceUnit }}
server {{$endpoint.ID}} {{$endpoint.IP}}:{{$endpoint.Port}} check inter {{env "ROUTER_BACKEND_CHECK_INTERVAL" "5000ms"}} weight {{ $weight }}
{{ end }}
{{ end }}
{{ end }}
{{ end }}{{/* end if weight != 0 */}}
{{ end }}{{/* end iterate over services*/}}
{{ end }}{{/*end tls==passthrough*/}}

Expand All @@ -329,10 +333,12 @@ backend be_secure_{{$cfgIdx}}
timeout check 5000ms
cookie {{$cfg.RoutingKeyName}} insert indirect nocache httponly secure
{{ range $serviceUnitName, $weight := $cfg.ServiceUnitNames }}
{{ with $serviceUnit := index $.ServiceUnits $serviceUnitName }}
{{ range $idx, $endpoint := endpointsForAlias $cfg $serviceUnit }}
{{ if ne ($weight 0) }}
{{ with $serviceUnit := index $.ServiceUnits $serviceUnitName }}
{{ range $idx, $endpoint := endpointsForAlias $cfg $serviceUnit }}
server {{$endpoint.IdHash}} {{$endpoint.IP}}:{{$endpoint.Port}} ssl check inter {{env "ROUTER_BACKEND_CHECK_INTERVAL" "5000ms"}} verify required ca-file {{ $workingDir }}/cacerts/{{$cfgIdx}}.pem cookie {{$endpoint.IdHash}} weight {{ $weight }}
{{ end }}
{{ end }}
{{ end }}{{/* end if weight != 0 */}}
{{ end }}
{{ end }}
{{ end }}{{/* end tls==reencrypt */}}
Expand Down
4 changes: 2 additions & 2 deletions pkg/route/api/validation/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func ValidateRoute(route *routeapi.Route) field.ErrorList {
if route.Spec.To.Kind != "Service" {
result = append(result, field.Invalid(specPath.Child("to", "kind"), route.Spec.To.Kind, "must reference a Service"))
}
if route.Spec.To.Weight != nil && (*route.Spec.To.Weight < 1 || *route.Spec.To.Weight > 256) {
if route.Spec.To.Weight != nil && (*route.Spec.To.Weight < 0 || *route.Spec.To.Weight > 256) {
result = append(result, field.Invalid(specPath.Child("to", "weight"), route.Spec.To.Weight, "weight must be an integer between 1 and 256"))
}

Expand All @@ -60,7 +60,7 @@ func ValidateRoute(route *routeapi.Route) field.ErrorList {
if svc.Kind != "Service" {
result = append(result, field.Invalid(specPath.Child("alternateBackends", "kind"), svc.Kind, "must reference a Service"))
}
if svc.Weight != nil && (*svc.Weight < 1 || *svc.Weight > 256) {
if svc.Weight != nil && (*svc.Weight < 0 || *svc.Weight > 256) {
result = append(result, field.Invalid(specPath.Child("alternateBackends", "weight"), svc.Weight, "weight must be an integer between 1 and 256"))
}
}
Expand Down
5 changes: 5 additions & 0 deletions test/extended/router/weighted.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ var _ = g.Describe("[networking][router] weighted openshift router", func() {
if weightedRatio < 5 && weightedRatio > 0.2 {
e2e.Failf("Unexpected weighted ratio for incoming traffic: %v (%d/%d)", weightedRatio, trafficEP1, trafficEP2)
}

g.By(fmt.Sprintf("checking that zero weights are also respected by the router"))
host = "zeroweighted.example.com"
err = waitForRouterOKResponse(routerURL, "weighted.example.com", 1*time.Minute)
o.Expect(err).To(o.HaveOccurred())
})
})
})
Expand Down
23 changes: 23 additions & 0 deletions test/extended/testdata/weighted-router.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,29 @@ items:
ports:
- targetPort: 8080

# a route that has multiple services but all weights are zero
- apiVersion: v1
kind: Route
metadata:
name: zeroweightedroute
labels:
test: router
select: weighted
annotations:
haproxy.router.openshift.io/balance: roundrobin
spec:
host: zeroweighted.example.com
to:
name: weightedendpoints1
kind: Service
weight: 0
alternateBackends:
- name: weightedendpoints2
kind: Service
weight: 0
ports:
- targetPort: 8080

# two services that can be routed to
- apiVersion: v1
kind: Service
Expand Down

0 comments on commit ec8f26a

Please sign in to comment.