Skip to content

Commit

Permalink
Remove duplicated logic from templates (#4176)
Browse files Browse the repository at this point in the history
  • Loading branch information
jjngx authored Aug 2, 2023
1 parent 973317e commit 8b5b76f
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 21 deletions.
6 changes: 1 addition & 5 deletions internal/configs/version1/nginx-plus.ingress.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
Expand Down
6 changes: 1 addition & 5 deletions internal/configs/version1/nginx.ingress.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
Expand Down
10 changes: 5 additions & 5 deletions internal/configs/version1/template_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
}
12 changes: 6 additions & 6 deletions internal/configs/version1/template_helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand Down

0 comments on commit 8b5b76f

Please sign in to comment.