Skip to content

Commit 686d7b6

Browse files
committed
Changed the router to default to roundrobin when weights are used
If the route has weights set for the services it is associated with, then we will set the default load balance policy to RoundRobin if no policy is set with an annotation or as a global default with an environment variable. Without this change the user would need to both set the weights, and then set an annotation to change the default balancing algorithm... which people almost always forgot to do. For bug 1416869 (https://bugzilla.redhat.com/show_bug.cgi?id=1416869)
1 parent ed1d332 commit 686d7b6

File tree

4 files changed

+28
-6
lines changed

4 files changed

+28
-6
lines changed

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ backend be_edge_http_{{$cfgIdx}}
255255
{{ if (matchPattern "roundrobin|leastconn|source" (env "ROUTER_LOAD_BALANCE_ALGORITHM" "")) }}
256256
balance {{ env "ROUTER_LOAD_BALANCE_ALGORITHM" "leastconn"}}
257257
{{ else }}
258-
balance leastconn
258+
balance {{ if cfg.HasWeights }}roundrobin{{ else }}leastconn{{ end }}
259259
{{ end }}
260260
{{ end }}
261261
{{ with $value := index $cfg.Annotations "haproxy.router.openshift.io/timeout"}}
@@ -337,7 +337,11 @@ backend be_tcp_{{$cfgIdx}}
337337
balance {{ $balanceAlgo }}
338338
{{ end }}
339339
{{ else }}
340-
balance {{ env "ROUTER_TCP_BALANCE_SCHEME" "source" }}
340+
{{ if (matchPattern "roundrobin|leastconn|source" (env "ROUTER_TCP_BALANCE_SCHEME" "")) }}
341+
balance {{ env "ROUTER_TCP_BALANCE_SCHEME" "source"}}
342+
{{ else }}
343+
balance {{ if cfg.HasWeights }}roundrobin{{ else }}source{{ end }}
344+
{{ end }}
341345
{{ end }}
342346
{{ with $value := index $cfg.Annotations "haproxy.router.openshift.io/timeout"}}
343347
{{if (matchPattern "[1-9][0-9]*(us|ms|s|m|h|d)?" $value) }}
@@ -403,7 +407,7 @@ backend be_secure_{{$cfgIdx}}
403407
{{ if (matchPattern "roundrobin|leastconn|source" (env "ROUTER_LOAD_BALANCE_ALGORITHM" "")) }}
404408
balance {{ env "ROUTER_LOAD_BALANCE_ALGORITHM" "leastconn"}}
405409
{{ else }}
406-
balance leastconn
410+
balance {{ if cfg.HasWeights }}roundrobin{{ else }}leastconn{{ end }}
407411
{{ end }}
408412
{{ end }}
409413
{{ with $value := index $cfg.Annotations "haproxy.router.openshift.io/timeout"}}

pkg/router/template/router.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -598,14 +598,17 @@ func (r *templateRouter) createServiceAliasConfig(route *routeapi.Route, routeKe
598598
// The router config trumps what the route asks for/wants.
599599
wildcard := r.allowWildcardRoutes && wantsWildcardSupport
600600

601+
serviceUnits, hasWeights := getServiceUnits(route)
602+
601603
config := ServiceAliasConfig{
602604
Name: route.Name,
603605
Namespace: route.Namespace,
604606
Host: route.Spec.Host,
605607
Path: route.Spec.Path,
606608
IsWildcard: wildcard,
607609
Annotations: route.Annotations,
608-
ServiceUnitNames: getServiceUnits(route),
610+
ServiceUnitNames: serviceUnits,
611+
HasWeights: hasWeights,
609612
}
610613

611614
if route.Spec.Port != nil {
@@ -841,23 +844,26 @@ func generateDestCertKey(config *ServiceAliasConfig) string {
841844
// getServiceUnits returns a map of service keys to their weights.
842845
// Weight suggests the % of traffic that a given service will receive
843846
// compared to other services pointed to by the route.
844-
func getServiceUnits(route *routeapi.Route) map[string]int32 {
847+
func getServiceUnits(route *routeapi.Route) (map[string]int32, bool) {
845848
serviceUnits := make(map[string]int32)
849+
hasWeights := false
846850
key := fmt.Sprintf("%s/%s", route.Namespace, route.Spec.To.Name)
847851
if route.Spec.To.Weight == nil {
848852
serviceUnits[key] = 0
849853
} else {
850854
serviceUnits[key] = *route.Spec.To.Weight
855+
hasWeights = true
851856
}
852857
for _, svc := range route.Spec.AlternateBackends {
853858
key = fmt.Sprintf("%s/%s", route.Namespace, svc.Name)
854859
if svc.Weight == nil {
855860
serviceUnits[key] = 0
856861
} else {
857862
serviceUnits[key] = *svc.Weight
863+
hasWeights = true
858864
}
859865
}
860-
return serviceUnits
866+
return serviceUnits, hasWeights
861867
}
862868

863869
// configsAreEqual determines whether the given service alias configs can be considered equal.

pkg/router/template/router_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,11 @@ func TestCreateServiceAliasConfig(t *testing.T) {
326326
config.PreferPort != route.Spec.Port.TargetPort.String() || !reflect.DeepEqual(expectedSUs, config.ServiceUnitNames) {
327327
t.Errorf("Route %v did not match service alias config %v", route, config)
328328
}
329+
330+
// Check that we are detecting weights are set
331+
if !config.HasWeights {
332+
t.Errorf("Route %v did not generate a service alias config with HasWeights set %v", route, config)
333+
}
329334
}
330335

331336
// TestAddRoute validates that adding a route creates a service alias config and associated service units
@@ -373,6 +378,9 @@ func TestAddRoute(t *testing.T) {
373378
} else if config.Host != route.Spec.Host {
374379
// This test is not validating createServiceAliasConfig, so superficial validation should be good enough.
375380
t.Errorf("Route %v did not match service alias config %v", route, config)
381+
} else if config.HasWeights {
382+
// Check that we don't set HasWeights when there are no weights
383+
t.Errorf("Route %v set HasWeights even though there are no weights defined, config %v", route, config)
376384
}
377385
}
378386

pkg/router/template/types.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ type ServiceAliasConfig struct {
5252
// ServiceUnitNames is a collection of services that support this route, keyed by service name
5353
// and valued on the weight attached to it with respect to other entries in the map
5454
ServiceUnitNames map[string]int32
55+
56+
// HasWeights is a boolean that signals whether or not there are any weights set in the services
57+
// that support this route.
58+
HasWeights bool
5559
}
5660

5761
type ServiceAliasConfigStatus string

0 commit comments

Comments
 (0)