diff --git a/internal/configs/version1/nginx-plus.ingress.tmpl b/internal/configs/version1/nginx-plus.ingress.tmpl index 54d4e980c9..a2af55811f 100644 --- a/internal/configs/version1/nginx-plus.ingress.tmpl +++ b/internal/configs/version1/nginx-plus.ingress.tmpl @@ -177,11 +177,7 @@ server { {{end -}} {{range $location := $server.Locations}} - {{ if index $.Ingress.Annotations "nginx.org/path-regex" }} - location {{ makePathRegex $location.Path $.Ingress.Annotations | printf }} { - {{ else }} - location {{ $location.Path }} { - {{ end }} + location {{ makeLocationPath $location.Path $.Ingress.Annotations | printf }} { set $service "{{$location.ServiceName}}"; status_zone "{{ $location.ServiceName }}"; {{with $location.MinionIngress}} diff --git a/internal/configs/version1/nginx.ingress.tmpl b/internal/configs/version1/nginx.ingress.tmpl index 8c544a9581..62d673e284 100644 --- a/internal/configs/version1/nginx.ingress.tmpl +++ b/internal/configs/version1/nginx.ingress.tmpl @@ -102,11 +102,7 @@ server { {{- end}} {{range $location := $server.Locations}} - {{ if index $.Ingress.Annotations "nginx.org/path-regex" }} - location {{ makePathRegex $location.Path $.Ingress.Annotations | printf }} { - {{ else }} - location {{ $location.Path }} { - {{ end }} + location {{ makeLocationPath $location.Path $.Ingress.Annotations | printf }} { set $service "{{$location.ServiceName}}"; {{with $location.MinionIngress}} # location for minion {{$location.MinionIngress.Namespace}}/{{$location.MinionIngress.Name}} diff --git a/internal/configs/version1/template_helper.go b/internal/configs/version1/template_helper.go index 4857fd155e..0938181e60 100644 --- a/internal/configs/version1/template_helper.go +++ b/internal/configs/version1/template_helper.go @@ -14,13 +14,13 @@ func trim(s string) string { return strings.TrimSpace(s) } -// makePathRegex takes a string representing a location path +// makeLocationPath takes a string representing a location path // and a map representing Ingress annotations. // It returns a location path with added regular expression modifier. // See [Location Directive]. // // [Location Directive]: https://nginx.org/en/docs/http/ngx_http_core_module.html#location -func makePathRegex(path string, annotations map[string]string) string { +func makeLocationPath(path string, annotations map[string]string) string { p, ok := annotations["nginx.org/path-regex"] if !ok { return path @@ -38,7 +38,7 @@ func makePathRegex(path string, annotations map[string]string) string { } var helperFunctions = template.FuncMap{ - "split": split, - "trim": trim, - "makePathRegex": makePathRegex, + "split": split, + "trim": trim, + "makeLocationPath": makeLocationPath, } diff --git a/internal/configs/version1/template_helper_test.go b/internal/configs/version1/template_helper_test.go index 3304f50d8b..82d8d9379f 100644 --- a/internal/configs/version1/template_helper_test.go +++ b/internal/configs/version1/template_helper_test.go @@ -10,7 +10,7 @@ func TestWithPathRegex_MatchesCaseSensitiveModifier(t *testing.T) { t.Parallel() want := "~ \"^/coffee/[A-Z0-9]{3}\"" - got := makePathRegex("/coffee/[A-Z0-9]{3}", map[string]string{"nginx.org/path-regex": "case_sensitive"}) + got := makeLocationPath("/coffee/[A-Z0-9]{3}", map[string]string{"nginx.org/path-regex": "case_sensitive"}) if got != want { t.Errorf("got: %s, want: %s", got, want) } @@ -20,7 +20,7 @@ func TestWithPathRegex_MatchesCaseInsensitiveModifier(t *testing.T) { t.Parallel() want := "~* \"^/coffee/[A-Z0-9]{3}\"" - got := makePathRegex("/coffee/[A-Z0-9]{3}", map[string]string{"nginx.org/path-regex": "case_insensitive"}) + got := makeLocationPath("/coffee/[A-Z0-9]{3}", map[string]string{"nginx.org/path-regex": "case_insensitive"}) if got != want { t.Errorf("got: %s, want: %s", got, want) } @@ -30,7 +30,7 @@ func TestWithPathReqex_MatchesExactModifier(t *testing.T) { t.Parallel() want := "= \"/coffee\"" - got := makePathRegex("/coffee", map[string]string{"nginx.org/path-regex": "exact"}) + got := makeLocationPath("/coffee", map[string]string{"nginx.org/path-regex": "exact"}) if got != want { t.Errorf("got: %s, want: %s", got, want) } @@ -40,7 +40,7 @@ func TestWithPathReqex_DoesNotMatchModifier(t *testing.T) { t.Parallel() want := "/coffee" - got := makePathRegex("/coffee", map[string]string{"nginx.org/path-regex": "bogus"}) + got := makeLocationPath("/coffee", map[string]string{"nginx.org/path-regex": "bogus"}) if got != want { t.Errorf("got: %s, want: %s", got, want) } @@ -50,7 +50,7 @@ func TestWithPathReqex_DoesNotMatchEmptyModifier(t *testing.T) { t.Parallel() want := "/coffee" - got := makePathRegex("/coffee", map[string]string{"nginx.org/path-regex": ""}) + got := makeLocationPath("/coffee", map[string]string{"nginx.org/path-regex": ""}) if got != want { t.Errorf("got: %s, want: %s", got, want) } @@ -60,7 +60,7 @@ func TestWithPathReqex_DoesNotMatchBogusAnnotationName(t *testing.T) { t.Parallel() want := "/coffee" - got := makePathRegex("/coffee", map[string]string{"nginx.org/bogus-annotation": ""}) + got := makeLocationPath("/coffee", map[string]string{"nginx.org/bogus-annotation": ""}) if got != want { t.Errorf("got: %s, want: %s", got, want) }