Skip to content

Commit e0f32fd

Browse files
author
Rajat Chopra
committed
Randomize endpoints function for the router template : bz1447115
1 parent 67a30d2 commit e0f32fd

File tree

3 files changed

+23
-10
lines changed

3 files changed

+23
-10
lines changed

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ backend be_edge_http:{{$cfgIdx}}
318318
{{- range $serviceUnitName, $weight := $cfg.ServiceUnitNames }}
319319
{{- if ne $weight 0 }}
320320
{{- with $serviceUnit := index $.ServiceUnits $serviceUnitName }}
321-
{{- range $idx, $endpoint := endpointsForAlias $cfg $serviceUnit }}
321+
{{- range $idx, $endpoint := randomEndpointsForAlias $cfg $serviceUnit true }}
322322
{{- if $endpoint.NoHealthCheck }}
323323
server {{$endpoint.ID}} {{$endpoint.IP}}:{{$endpoint.Port}} cookie {{$endpoint.IdHash}} weight {{$weight}}
324324
{{- else }}
@@ -387,7 +387,7 @@ backend be_tcp:{{$cfgIdx}}
387387
{{- range $serviceUnitName, $weight := $cfg.ServiceUnitNames }}
388388
{{- if ne $weight 0 }}
389389
{{- with $serviceUnit := index $.ServiceUnits $serviceUnitName }}
390-
{{- range $idx, $endpoint := endpointsForAlias $cfg $serviceUnit }}
390+
{{- range $idx, $endpoint := randomEndpointsForAlias $cfg $serviceUnit true }}
391391
{{- if $endpoint.NoHealthCheck }}
392392
server {{$endpoint.ID}} {{$endpoint.IP}}:{{$endpoint.Port}} weight {{$weight}}
393393
{{- else }}
@@ -405,7 +405,7 @@ backend be_tcp:{{$cfgIdx}}
405405
{{- end }}
406406
{{- end }}
407407
{{- end }}
408-
{{- end }}{{/* end range endpointsForAlias */}}
408+
{{- end }}{{/* end range randomEndpointsForAlias */}}
409409
{{- end }}{{/* end get ServiceUnit from serviceUnitName */}}
410410
{{- end }}{{/* end if weight != 0 */}}
411411
{{- end }}{{/* end iterate over services*/}}
@@ -472,7 +472,7 @@ backend be_secure:{{$cfgIdx}}
472472
{{- range $serviceUnitName, $weight := $cfg.ServiceUnitNames }}
473473
{{- if ne $weight 0 }}
474474
{{- with $serviceUnit := index $.ServiceUnits $serviceUnitName }}
475-
{{- range $idx, $endpoint := endpointsForAlias $cfg $serviceUnit }}
475+
{{- range $idx, $endpoint := randomEndpointsForAlias $cfg $serviceUnit true }}
476476
{{- if $endpoint.NoHealthCheck }}
477477
server {{$endpoint.ID}} {{$endpoint.IP}}:{{$endpoint.Port}} ssl verify required ca-file {{ $workingDir }}/cacerts/{{$cfgIdx}}.pem cookie {{$endpoint.IdHash}} weight {{$weight}}
478478
{{- else }}
@@ -490,7 +490,7 @@ backend be_secure:{{$cfgIdx}}
490490
{{- end }}
491491
{{- end }}{{/* end get health interval annotation */}}
492492
{{- end }}{{/* end else no health check */}}
493-
{{- end }}{{/* end range endpointsForAlias */}}
493+
{{- end }}{{/* end range randomEndpointsForAlias */}}
494494
{{- end }}{{/* end get serviceUnit from its name */}}
495495
{{- end }}{{/* end if weight != 0 */}}
496496
{{- end }}{{/* end range over serviceUnitNames */}}

pkg/router/template/plugin.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,12 @@ func env(name, defaultValue string) string {
100100
func NewTemplatePlugin(cfg TemplatePluginConfig, lookupSvc ServiceLookup) (*TemplatePlugin, error) {
101101
templateBaseName := filepath.Base(cfg.TemplatePath)
102102
globalFuncs := template.FuncMap{
103-
"endpointsForAlias": endpointsForAlias, //returns the list of valid endpoints
104-
"env": env, //tries to get an environment variable if it can't return a default
105-
"matchPattern": matchPattern, //anchors provided regular expression and evaluates against given string
106-
"isInteger": isInteger, //determines if a given variable is an integer
107-
"matchValues": matchValues, //compares a given string to a list of allowed strings
103+
"endpointsForAlias": endpointsForAlias, //returns the list of valid endpoints
104+
"randomEndpointsForAlias": randomEndpointsForAlias, //returns the list of valid endpoints
105+
"env": env, //tries to get an environment variable if it can't return a default
106+
"matchPattern": matchPattern, //anchors provided regular expression and evaluates against given string
107+
"isInteger": isInteger, //determines if a given variable is an integer
108+
"matchValues": matchValues, //compares a given string to a list of allowed strings
108109

109110
"genSubdomainWildcardRegexp": genSubdomainWildcardRegexp, //generates a regular expression matching the subdomain for hosts (and paths) with a wildcard policy
110111
"generateRouteRegexp": generateRouteRegexp, //generates a regular expression matching the route hosts (and paths)

pkg/router/template/router.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/json"
66
"fmt"
77
"io/ioutil"
8+
"math/rand"
89
"os"
910
"os/exec"
1011
"path/filepath"
@@ -301,6 +302,17 @@ func genCertificateHostName(hostname string, wildcard bool) string {
301302
return hostname
302303
}
303304

305+
func randomEndpointsForAlias(alias ServiceAliasConfig, svc ServiceUnit, random bool) []Endpoint {
306+
endpoints := endpointsForAlias(alias, svc)
307+
if random {
308+
for i := range endpoints {
309+
rIndex := rand.Intn(i + 1)
310+
endpoints[i], endpoints[rIndex] = endpoints[rIndex], endpoints[i]
311+
}
312+
}
313+
return endpoints
314+
}
315+
304316
func endpointsForAlias(alias ServiceAliasConfig, svc ServiceUnit) []Endpoint {
305317
if len(alias.PreferPort) == 0 {
306318
return svc.EndpointTable

0 commit comments

Comments
 (0)