This commit is contained in:
Alex Rowley 2017-08-20 13:48:03 +00:00 committed by GitHub
commit f9761f9562
2 changed files with 19 additions and 8 deletions

View file

@ -188,8 +188,7 @@ func buildResolvers(a interface{}) string {
return strings.Join(r, " ") return strings.Join(r, " ")
} }
// buildLocation produces the location string, if the ingress has redirects // Helper to create the correct path for location blocks
// (specified through the ingress.kubernetes.io/rewrite-to annotation)
func buildLocation(input interface{}) string { func buildLocation(input interface{}) string {
location, ok := input.(*ingress.Location) location, ok := input.(*ingress.Location)
if !ok { if !ok {
@ -197,10 +196,19 @@ func buildLocation(input interface{}) string {
} }
path := location.Path path := location.Path
if len(location.Redirect.Target) > 0 && location.Redirect.Target != path {
// Although per spec all paths should be treated as regex
// returning slash directly will result in a fall through catchall
// prefix style location block instead which will match any path
// for which a regex is not defined
if path == slash { if path == slash {
return fmt.Sprintf("~* %s", path) return slash
} }
// When rewrite is enabled via the rewrite target annotation
// (ingress.kubernetes.io/rewrite-target) this allows the baseuri to be
// appended to the path
if len(location.Redirect.Target) > 0 && location.Redirect.Target != path {
// baseuri regex will parse basename from the given location // baseuri regex will parse basename from the given location
baseuri := `(?<baseuri>.*)` baseuri := `(?<baseuri>.*)`
if !strings.HasSuffix(path, slash) { if !strings.HasSuffix(path, slash) {
@ -210,7 +218,10 @@ func buildLocation(input interface{}) string {
return fmt.Sprintf(`~* ^%s%s`, path, baseuri) return fmt.Sprintf(`~* ^%s%s`, path, baseuri)
} }
return path // Return an anchored regex location block, although not identical to the
// spec due to the type of regex used this is the closest nginx equivalent
// to the intended regex based path as defined in HTTPIngressPath
return fmt.Sprintf(`~* ^%s`, path)
} }
func buildAuthLocation(input interface{}) string { func buildAuthLocation(input interface{}) string {

View file

@ -42,7 +42,7 @@ var (
AddBaseURL bool AddBaseURL bool
}{ }{
"invalid redirect / to /": {"/", "/", "/", "proxy_pass http://upstream-name;", false}, "invalid redirect / to /": {"/", "/", "/", "proxy_pass http://upstream-name;", false},
"redirect / to /jenkins": {"/", "/jenkins", "~* /", "redirect / to /jenkins": {"/", "/jenkins", "/",
` `
rewrite /(.*) /jenkins/$1 break; rewrite /(.*) /jenkins/$1 break;
proxy_pass http://upstream-name; proxy_pass http://upstream-name;
@ -60,7 +60,7 @@ var (
rewrite /something-complex/(.*) /not-root/$1 break; rewrite /something-complex/(.*) /not-root/$1 break;
proxy_pass http://upstream-name; proxy_pass http://upstream-name;
`, false}, `, false},
"redirect / to /jenkins and rewrite": {"/", "/jenkins", "~* /", ` "redirect / to /jenkins and rewrite": {"/", "/jenkins", "/", `
rewrite /(.*) /jenkins/$1 break; rewrite /(.*) /jenkins/$1 break;
proxy_pass http://upstream-name; proxy_pass http://upstream-name;
subs_filter '<head(.*)>' '<head$1><base href="$scheme://$http_host/$baseuri">' r; subs_filter '<head(.*)>' '<head$1><base href="$scheme://$http_host/$baseuri">' r;