diff --git a/internal/ingress/controller/template/template.go b/internal/ingress/controller/template/template.go index 39cfa5ddb..ff4ca1e5b 100644 --- a/internal/ingress/controller/template/template.go +++ b/internal/ingress/controller/template/template.go @@ -155,6 +155,7 @@ var ( "buildOpentracing": buildOpentracing, "proxySetHeader": proxySetHeader, "buildInfluxDB": buildInfluxDB, + "atLeastOneNeedsRewrite": atLeastOneNeedsRewrite, } ) @@ -287,9 +288,32 @@ func buildResolvers(res interface{}, disableIpv6 interface{}) string { return strings.Join(r, " ") + ";" } +func needsRewrite(location *ingress.Location) bool { + if len(location.Rewrite.Target) > 0 && location.Rewrite.Target != location.Path { + return true + } + return false +} + +// atLeastOneNeedsRewrite checks if the nginx.ingress.kubernetes.io/rewrite-target annotation is used on the '/' path +func atLeastOneNeedsRewrite(input interface{}) bool { + locations, ok := input.([]*ingress.Location) + if !ok { + glog.Errorf("expected an '[]*ingress.Location' type but %T was returned", input) + return false + } + + for _, location := range locations { + if needsRewrite(location) { + return true + } + } + return false +} + // buildLocation produces the location string, if the ingress has redirects // (specified through the nginx.ingress.kubernetes.io/rewrite-target annotation) -func buildLocation(input interface{}) string { +func buildLocation(input interface{}, rewrite bool) string { location, ok := input.(*ingress.Location) if !ok { glog.Errorf("expected an '*ingress.Location' type but %T was returned", input) @@ -297,7 +321,7 @@ func buildLocation(input interface{}) string { } path := location.Path - if len(location.Rewrite.Target) > 0 && location.Rewrite.Target != path { + if needsRewrite(location) { if path == slash { return fmt.Sprintf("~* %s", path) } @@ -310,6 +334,12 @@ func buildLocation(input interface{}) string { return fmt.Sprintf(`~* ^%s%s`, path, baseuri) } + if rewrite { + if path == slash { + return path + } + return fmt.Sprintf(`^~ %s`, path) + } return path } diff --git a/internal/ingress/controller/template/template_test.go b/internal/ingress/controller/template/template_test.go index 7068d295e..5e489d867 100644 --- a/internal/ingress/controller/template/template_test.go +++ b/internal/ingress/controller/template/template_test.go @@ -50,6 +50,7 @@ var ( XForwardedPrefix bool DynamicConfigurationEnabled bool SecureBackend bool + atLeastOneNeedsRewrite bool }{ "when secure backend enabled": { "/", @@ -61,7 +62,8 @@ var ( false, false, false, - true}, + true, + false}, "when secure backend and stickeness enabled": { "/", "/", @@ -72,7 +74,8 @@ var ( true, false, false, - true}, + true, + false}, "when secure backend and dynamic config enabled": { "/", "/", @@ -83,7 +86,8 @@ var ( false, false, true, - true}, + true, + false}, "when secure backend, stickeness and dynamic config enabled": { "/", "/", @@ -94,7 +98,8 @@ var ( true, false, true, - true}, + true, + false}, "invalid redirect / to / with dynamic config enabled": { "/", "/", @@ -105,6 +110,7 @@ var ( false, false, true, + false, false}, "invalid redirect / to /": { "/", @@ -116,6 +122,7 @@ var ( false, false, false, + false, false}, "redirect / to /jenkins": { "/", @@ -131,7 +138,8 @@ proxy_pass http://upstream-name; false, false, false, - false}, + false, + true}, "redirect /something to /": { "/something", "/", @@ -146,7 +154,8 @@ proxy_pass http://upstream-name; false, false, false, - false}, + false, + true}, "redirect /end-with-slash/ to /not-root": { "/end-with-slash/", "/not-root", @@ -161,7 +170,8 @@ proxy_pass http://upstream-name; false, false, false, - false}, + false, + true}, "redirect /something-complex to /not-root": { "/something-complex", "/not-root", @@ -176,7 +186,8 @@ proxy_pass http://upstream-name; false, false, false, - false}, + false, + true}, "redirect / to /jenkins and rewrite": { "/", "/jenkins", @@ -194,7 +205,8 @@ subs_filter '(<(?:H|h)(?:E|e)(?:A|a)(?:D|d)(?:[^">]|"[^"]*")*>)' '$1]|"[^"]*")*>)' '$1]|"[^"]*")*>)' '$1]|"[^"]*")*>)' '$1]|"[^"]*")*>)' '$1