From 2509c5c8d6f650062d7bedb16ac71f81fe5e6d2b Mon Sep 17 00:00:00 2001 From: Canh Ngo Date: Wed, 23 Aug 2017 18:11:33 +0200 Subject: [PATCH] Fixed rewrite-target annotation should work with sticky-cookie Added a unit-test for this fix --- .../ingress/controller/template/template.go | 8 +-- .../controller/template/template_test.go | 49 ++++++++++++++----- 2 files changed, 41 insertions(+), 16 deletions(-) diff --git a/internal/ingress/controller/template/template.go b/internal/ingress/controller/template/template.go index b5cea55e6..190289363 100644 --- a/internal/ingress/controller/template/template.go +++ b/internal/ingress/controller/template/template.go @@ -334,14 +334,14 @@ func buildProxyPass(host string, b interface{}, loc interface{}) string { return fmt.Sprintf(` rewrite %s(.*) /$1 break; rewrite %s / break; - proxy_pass %s://%s; - %v`, path, location.Path, proto, upstreamName, abu) + %s + %v`, path, location.Path, defProxyPass, abu) } return fmt.Sprintf(` rewrite %s(.*) %s/$1 break; - proxy_pass %s://%s; - %v`, path, location.Rewrite.Target, proto, upstreamName, abu) + %s + %v`, path, location.Rewrite.Target, defProxyPass, abu) } // default proxy_pass diff --git a/internal/ingress/controller/template/template_test.go b/internal/ingress/controller/template/template_test.go index e147a2e7b..25efc7ff2 100644 --- a/internal/ingress/controller/template/template_test.go +++ b/internal/ingress/controller/template/template_test.go @@ -41,53 +41,58 @@ var ( ProxyPass string AddBaseURL bool BaseURLScheme string + Sticky bool }{ - "invalid redirect / to /": {"/", "/", "/", "proxy_pass http://upstream-name;", false, ""}, + "invalid redirect / to /": {"/", "/", "/", "proxy_pass http://upstream-name;", false, "", false}, "redirect / to /jenkins": {"/", "/jenkins", "~* /", ` rewrite /(.*) /jenkins/$1 break; proxy_pass http://upstream-name; - `, false, ""}, + `, false, "", false}, "redirect /something to /": {"/something", "/", `~* ^/something\/?(?.*)`, ` rewrite /something/(.*) /$1 break; rewrite /something / break; proxy_pass http://upstream-name; - `, false, ""}, + `, false, "", false}, "redirect /end-with-slash/ to /not-root": {"/end-with-slash/", "/not-root", "~* ^/end-with-slash/(?.*)", ` rewrite /end-with-slash/(.*) /not-root/$1 break; proxy_pass http://upstream-name; - `, false, ""}, + `, false, "", false}, "redirect /something-complex to /not-root": {"/something-complex", "/not-root", `~* ^/something-complex\/?(?.*)`, ` rewrite /something-complex/(.*) /not-root/$1 break; proxy_pass http://upstream-name; - `, false, ""}, + `, false, "", false}, "redirect / to /jenkins and rewrite": {"/", "/jenkins", "~* /", ` rewrite /(.*) /jenkins/$1 break; proxy_pass http://upstream-name; subs_filter '(<(?:H|h)(?:E|e)(?:A|a)(?:D|d)(?:[^">]|"[^"]*")*>)' '$1' ro; - `, true, ""}, + `, true, "", false}, "redirect /something to / and rewrite": {"/something", "/", `~* ^/something\/?(?.*)`, ` rewrite /something/(.*) /$1 break; rewrite /something / break; proxy_pass http://upstream-name; subs_filter '(<(?:H|h)(?:E|e)(?:A|a)(?:D|d)(?:[^">]|"[^"]*")*>)' '$1' ro; - `, true, ""}, + `, true, "", false}, "redirect /end-with-slash/ to /not-root and rewrite": {"/end-with-slash/", "/not-root", `~* ^/end-with-slash/(?.*)`, ` rewrite /end-with-slash/(.*) /not-root/$1 break; proxy_pass http://upstream-name; subs_filter '(<(?:H|h)(?:E|e)(?:A|a)(?:D|d)(?:[^">]|"[^"]*")*>)' '$1' ro; - `, true, ""}, + `, true, "", false}, "redirect /something-complex to /not-root and rewrite": {"/something-complex", "/not-root", `~* ^/something-complex\/?(?.*)`, ` rewrite /something-complex/(.*) /not-root/$1 break; proxy_pass http://upstream-name; subs_filter '(<(?:H|h)(?:E|e)(?:A|a)(?:D|d)(?:[^">]|"[^"]*")*>)' '$1' ro; - `, true, ""}, + `, true, "", false}, "redirect /something to / and rewrite with specific scheme": {"/something", "/", `~* ^/something\/?(?.*)`, ` rewrite /something/(.*) /$1 break; rewrite /something / break; proxy_pass http://upstream-name; subs_filter '(<(?:H|h)(?:E|e)(?:A|a)(?:D|d)(?:[^">]|"[^"]*")*>)' '$1' ro; - `, true, "http"}, + `, true, "http", false}, + "redirect / to /something with sticky enabled": {"/", "/something", `~* /`, ` + rewrite /(.*) /something/$1 break; + proxy_pass http://sticky-upstream-name; + `, false, "http", true}, } ) @@ -125,14 +130,34 @@ func TestBuildLocation(t *testing.T) { } func TestBuildProxyPass(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: "upstream-name", + Backend: defaultBackend, } - pp := buildProxyPass("", []*ingress.Backend{}, loc) + backends := []*ingress.Backend{} + if tc.Sticky { + backends = []*ingress.Backend{ + { + Name: defaultBackend, + SessionAffinity: ingress.SessionAffinityConfig{ + AffinityType: "cookie", + CookieSessionAffinity: ingress.CookieSessionAffinity{ + Locations: map[string][]string{ + defaultHost: {tc.Path}, + }, + }, + }, + }, + } + } + + pp := buildProxyPass(defaultHost, backends, loc) if !strings.EqualFold(tc.ProxyPass, pp) { t.Errorf("%s: expected \n'%v'\nbut returned \n'%v'", k, tc.ProxyPass, pp) }