Skip to content

Randomize endpoints function for the router template : bz1447115 #14008

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 30, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions images/router/haproxy/conf/haproxy-config.template
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ backend be_secure:{{$cfgIdx}}
{{- range $serviceUnitName, $weight := $cfg.ServiceUnitNames }}
{{- if ne $weight 0 }}
{{- with $serviceUnit := index $.ServiceUnits $serviceUnitName }}
{{- range $idx, $endpoint := endpointsForAlias $cfg $serviceUnit }}
{{- range $idx, $endpoint := processEndpointsForAlias $cfg $serviceUnit (env "ROUTER_BACKEND_PROCESS_ENDPOINTS" "") }}
server {{$endpoint.ID}} {{$endpoint.IP}}:{{$endpoint.Port}} cookie {{$endpoint.IdHash}} weight {{$weight}}
{{- if (eq $cfg.TLSTermination "reencrypt") }} ssl
{{- if $cfg.VerifyServiceHostname }} verifyhost {{ $serviceUnit.Hostname }}
Expand Down Expand Up @@ -366,7 +366,7 @@ backend be_secure:{{$cfgIdx}}


{{- end }}{{/* end if cg.TLSTermination */}}
{{- end }}{{/* end range endpointsForAlias */}}
{{- end }}{{/* end range processEndpointsForAlias */}}
{{- end }}{{/* end get serviceUnit from its name */}}
{{- end }}{{/* end range over serviceUnitNames */}}

Expand Down Expand Up @@ -417,7 +417,7 @@ backend be_tcp:{{$cfgIdx}}
{{- range $serviceUnitName, $weight := $cfg.ServiceUnitNames }}
{{- if ne $weight 0 }}
{{- with $serviceUnit := index $.ServiceUnits $serviceUnitName }}
{{- range $idx, $endpoint := endpointsForAlias $cfg $serviceUnit }}
{{- range $idx, $endpoint := processEndpointsForAlias $cfg $serviceUnit (env "ROUTER_BACKEND_PROCESS_ENDPOINTS" "") }}
server {{$endpoint.ID}} {{$endpoint.IP}}:{{$endpoint.Port}} weight {{$weight}}
{{- if not $endpoint.NoHealthCheck }}
{{- with $healthIntv := index $cfg.Annotations "router.openshift.io/haproxy.health.check.interval" }}
Expand All @@ -430,7 +430,7 @@ backend be_tcp:{{$cfgIdx}}
{{- end }}
{{- end }}{{/* end get health interval annotation */}}
{{- end }}{{/* end else no health check */}}
{{- end }}{{/* end range endpointsForAlias */}}
{{- end }}{{/* end range processEndpointsForAlias */}}
{{- end }}{{/* end get ServiceUnit from serviceUnitName */}}
{{- end }}{{/* end if weight != 0 */}}
{{- end }}{{/* end iterate over services*/}}
Expand Down
11 changes: 6 additions & 5 deletions pkg/router/template/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,12 @@ func env(name, defaultValue string) string {
func NewTemplatePlugin(cfg TemplatePluginConfig, lookupSvc ServiceLookup) (*TemplatePlugin, error) {
templateBaseName := filepath.Base(cfg.TemplatePath)
globalFuncs := template.FuncMap{
"endpointsForAlias": endpointsForAlias, //returns the list of valid endpoints
"env": env, //tries to get an environment variable if it can't return a default
"matchPattern": matchPattern, //anchors provided regular expression and evaluates against given string
"isInteger": isInteger, //determines if a given variable is an integer
"matchValues": matchValues, //compares a given string to a list of allowed strings
"endpointsForAlias": endpointsForAlias, //returns the list of valid endpoints
"processEndpointsForAlias": processEndpointsForAlias, //returns the list of valid endpoints after processing them
"env": env, //tries to get an environment variable if it can't return a default
"matchPattern": matchPattern, //anchors provided regular expression and evaluates against given string
"isInteger": isInteger, //determines if a given variable is an integer
"matchValues": matchValues, //compares a given string to a list of allowed strings

"genSubdomainWildcardRegexp": genSubdomainWildcardRegexp, //generates a regular expression matching the subdomain for hosts (and paths) with a wildcard policy
"generateRouteRegexp": generateRouteRegexp, //generates a regular expression matching the route hosts (and paths)
Expand Down
16 changes: 16 additions & 0 deletions pkg/router/template/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"io/ioutil"
"math/rand"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -310,6 +311,21 @@ func genCertificateHostName(hostname string, wildcard bool) string {
return hostname
}

// Returns the list of endpoints for the given route's service
// action argument further processes the list e.g. shuffle
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you document what the default behavior is please?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

// The default action is in-order traversal of internal data structure that stores
// the endpoints (does not change the return order if the data structure did not mutate)
func processEndpointsForAlias(alias ServiceAliasConfig, svc ServiceUnit, action string) []Endpoint {
endpoints := endpointsForAlias(alias, svc)
if strings.ToLower(action) == "shuffle" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any particular reason for "shuffle" over "random" or something like that? Also are there docs somewhere that describe how you use ROUTER_BACKEND_PROCESS_ENDPOINTS, or is that just a normal pod definition environment variable?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a normal end var to be defined in the deployment config. No docs yet. It's likely an experiment right now.

Both random and shuffle are probably fine. I felt shuffle leaves less chance of misinterpretation.

for i := len(endpoints) - 1; i >= 0; i-- {
rIndex := rand.Intn(i + 1)
endpoints[i], endpoints[rIndex] = endpoints[rIndex], endpoints[i]
}
}
return endpoints
}

func endpointsForAlias(alias ServiceAliasConfig, svc ServiceUnit) []Endpoint {
if len(alias.PreferPort) == 0 {
return svc.EndpointTable
Expand Down