Merge pull request #2340 from Shopify/sticky-upstream-name

Fix buildupstream name to work with dynamic session affinity
This commit is contained in:
k8s-ci-robot 2018-04-12 13:09:54 -07:00 committed by GitHub
commit cd4ddb72ac
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 56 additions and 12 deletions

File diff suppressed because one or more lines are too long

View file

@ -629,8 +629,7 @@ func buildDenyVariable(a interface{}) string {
return fmt.Sprintf("$deny_%v", denyPathSlugMap[l]) return fmt.Sprintf("$deny_%v", denyPathSlugMap[l])
} }
// TODO: Needs Unit Tests func buildUpstreamName(host string, b interface{}, loc interface{}, dynamicConfigurationEnabled bool) string {
func buildUpstreamName(host string, b interface{}, loc interface{}) string {
backends, ok := b.([]*ingress.Backend) backends, ok := b.([]*ingress.Backend)
if !ok { if !ok {
@ -646,14 +645,16 @@ func buildUpstreamName(host string, b interface{}, loc interface{}) string {
upstreamName := location.Backend upstreamName := location.Backend
for _, backend := range backends { if !dynamicConfigurationEnabled {
if backend.Name == location.Backend { for _, backend := range backends {
if backend.SessionAffinity.AffinityType == "cookie" && if backend.Name == location.Backend {
isSticky(host, location, backend.SessionAffinity.CookieSessionAffinity.Locations) { if backend.SessionAffinity.AffinityType == "cookie" &&
upstreamName = fmt.Sprintf("sticky-%v", upstreamName) isSticky(host, location, backend.SessionAffinity.CookieSessionAffinity.Locations) {
} upstreamName = fmt.Sprintf("sticky-%v", upstreamName)
}
break break
}
} }
} }

View file

@ -709,3 +709,46 @@ func TestIsLocationInLocationList(t *testing.T) {
} }
} }
} }
func TestBuildUpstreamName(t *testing.T) {
defaultBackend := "upstream-name"
defaultHost := "example.com"
for k, tc := range tmplFuncTestcases {
loc := &ingress.Location{
Path: tc.Path,
Rewrite: rewrite.Config{Target: tc.Target, AddBaseURL: tc.AddBaseURL, BaseURLScheme: tc.BaseURLScheme},
Backend: defaultBackend,
XForwardedPrefix: tc.XForwardedPrefix,
}
backend := &ingress.Backend{
Name: defaultBackend,
Secure: tc.SecureBackend,
}
expected := defaultBackend
if tc.Sticky {
if !tc.DynamicConfigurationEnabled {
expected = fmt.Sprintf("sticky-" + expected)
}
backend.SessionAffinity = ingress.SessionAffinityConfig{
AffinityType: "cookie",
CookieSessionAffinity: ingress.CookieSessionAffinity{
Locations: map[string][]string{
defaultHost: {tc.Path},
},
},
}
}
backends := []*ingress.Backend{backend}
pp := buildUpstreamName(defaultHost, backends, loc, tc.DynamicConfigurationEnabled)
if !strings.EqualFold(expected, pp) {
t.Errorf("%s: expected \n'%v'\nbut returned \n'%v'", k, expected, pp)
}
}
}

View file

@ -772,7 +772,7 @@ stream {
# ngx_auth_request module overrides variables in the parent request, # ngx_auth_request module overrides variables in the parent request,
# therefore we have to explicitly set this variable again so that when the parent request # therefore we have to explicitly set this variable again so that when the parent request
# resumes it has the correct value set for this variable so that Lua can pick backend correctly # resumes it has the correct value set for this variable so that Lua can pick backend correctly
set $proxy_upstream_name "{{ buildUpstreamName $server.Hostname $all.Backends $location }}"; set $proxy_upstream_name "{{ buildUpstreamName $server.Hostname $all.Backends $location $all.DynamicConfigurationEnabled }}";
proxy_pass_request_body off; proxy_pass_request_body off;
proxy_set_header Content-Length ""; proxy_set_header Content-Length "";
@ -886,7 +886,7 @@ stream {
{{ if $all.Cfg.EnableVtsStatus }}{{ if $location.VtsFilterKey }} vhost_traffic_status_filter_by_set_key {{ $location.VtsFilterKey }};{{ end }}{{ end }} {{ if $all.Cfg.EnableVtsStatus }}{{ if $location.VtsFilterKey }} vhost_traffic_status_filter_by_set_key {{ $location.VtsFilterKey }};{{ end }}{{ end }}
set $proxy_upstream_name "{{ buildUpstreamName $server.Hostname $all.Backends $location }}"; set $proxy_upstream_name "{{ buildUpstreamName $server.Hostname $all.Backends $location $all.DynamicConfigurationEnabled }}";
{{ $ing := (getIngressInformation $location.Ingress $location.Path) }} {{ $ing := (getIngressInformation $location.Ingress $location.Path) }}
{{/* $ing.Metadata contains the Ingress metadata */}} {{/* $ing.Metadata contains the Ingress metadata */}}