Skip to content

Commit 196604e

Browse files
authored
Merge pull request #14008 from rajatchopra/randomize
Randomize endpoints function for the router template : bz1447115
2 parents 66d364b + 8e341a1 commit 196604e

File tree

3 files changed

+26
-9
lines changed

3 files changed

+26
-9
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ backend be_secure:{{$cfgIdx}}
337337
{{- range $serviceUnitName, $weight := $cfg.ServiceUnitNames }}
338338
{{- if ne $weight 0 }}
339339
{{- with $serviceUnit := index $.ServiceUnits $serviceUnitName }}
340-
{{- range $idx, $endpoint := endpointsForAlias $cfg $serviceUnit }}
340+
{{- range $idx, $endpoint := processEndpointsForAlias $cfg $serviceUnit (env "ROUTER_BACKEND_PROCESS_ENDPOINTS" "") }}
341341
server {{$endpoint.ID}} {{$endpoint.IP}}:{{$endpoint.Port}} cookie {{$endpoint.IdHash}} weight {{$weight}}
342342
{{- if (eq $cfg.TLSTermination "reencrypt") }} ssl
343343
{{- if $cfg.VerifyServiceHostname }} verifyhost {{ $serviceUnit.Hostname }}
@@ -366,7 +366,7 @@ backend be_secure:{{$cfgIdx}}
366366

367367

368368
{{- end }}{{/* end if cg.TLSTermination */}}
369-
{{- end }}{{/* end range endpointsForAlias */}}
369+
{{- end }}{{/* end range processEndpointsForAlias */}}
370370
{{- end }}{{/* end get serviceUnit from its name */}}
371371
{{- end }}{{/* end range over serviceUnitNames */}}
372372

@@ -417,7 +417,7 @@ backend be_tcp:{{$cfgIdx}}
417417
{{- range $serviceUnitName, $weight := $cfg.ServiceUnitNames }}
418418
{{- if ne $weight 0 }}
419419
{{- with $serviceUnit := index $.ServiceUnits $serviceUnitName }}
420-
{{- range $idx, $endpoint := endpointsForAlias $cfg $serviceUnit }}
420+
{{- range $idx, $endpoint := processEndpointsForAlias $cfg $serviceUnit (env "ROUTER_BACKEND_PROCESS_ENDPOINTS" "") }}
421421
server {{$endpoint.ID}} {{$endpoint.IP}}:{{$endpoint.Port}} weight {{$weight}}
422422
{{- if not $endpoint.NoHealthCheck }}
423423
{{- with $healthIntv := index $cfg.Annotations "router.openshift.io/haproxy.health.check.interval" }}
@@ -430,7 +430,7 @@ backend be_tcp:{{$cfgIdx}}
430430
{{- end }}
431431
{{- end }}{{/* end get health interval annotation */}}
432432
{{- end }}{{/* end else no health check */}}
433-
{{- end }}{{/* end range endpointsForAlias */}}
433+
{{- end }}{{/* end range processEndpointsForAlias */}}
434434
{{- end }}{{/* end get ServiceUnit from serviceUnitName */}}
435435
{{- end }}{{/* end if weight != 0 */}}
436436
{{- end }}{{/* end iterate over services*/}}

pkg/router/template/plugin.go

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

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

pkg/router/template/router.go

Lines changed: 16 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"
@@ -310,6 +311,21 @@ func genCertificateHostName(hostname string, wildcard bool) string {
310311
return hostname
311312
}
312313

314+
// Returns the list of endpoints for the given route's service
315+
// action argument further processes the list e.g. shuffle
316+
// The default action is in-order traversal of internal data structure that stores
317+
// the endpoints (does not change the return order if the data structure did not mutate)
318+
func processEndpointsForAlias(alias ServiceAliasConfig, svc ServiceUnit, action string) []Endpoint {
319+
endpoints := endpointsForAlias(alias, svc)
320+
if strings.ToLower(action) == "shuffle" {
321+
for i := len(endpoints) - 1; i >= 0; i-- {
322+
rIndex := rand.Intn(i + 1)
323+
endpoints[i], endpoints[rIndex] = endpoints[rIndex], endpoints[i]
324+
}
325+
}
326+
return endpoints
327+
}
328+
313329
func endpointsForAlias(alias ServiceAliasConfig, svc ServiceUnit) []Endpoint {
314330
if len(alias.PreferPort) == 0 {
315331
return svc.EndpointTable

0 commit comments

Comments
 (0)