Skip to content

Commit 8b5b76f

Browse files
authored
Remove duplicated logic from templates (#4176)
1 parent 973317e commit 8b5b76f

File tree

4 files changed

+13
-21
lines changed

4 files changed

+13
-21
lines changed

internal/configs/version1/nginx-plus.ingress.tmpl

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -177,11 +177,7 @@ server {
177177
{{end -}}
178178

179179
{{range $location := $server.Locations}}
180-
{{ if index $.Ingress.Annotations "nginx.org/path-regex" }}
181-
location {{ makePathRegex $location.Path $.Ingress.Annotations | printf }} {
182-
{{ else }}
183-
location {{ $location.Path }} {
184-
{{ end }}
180+
location {{ makeLocationPath $location.Path $.Ingress.Annotations | printf }} {
185181
set $service "{{$location.ServiceName}}";
186182
status_zone "{{ $location.ServiceName }}";
187183
{{with $location.MinionIngress}}

internal/configs/version1/nginx.ingress.tmpl

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,7 @@ server {
102102
{{- end}}
103103

104104
{{range $location := $server.Locations}}
105-
{{ if index $.Ingress.Annotations "nginx.org/path-regex" }}
106-
location {{ makePathRegex $location.Path $.Ingress.Annotations | printf }} {
107-
{{ else }}
108-
location {{ $location.Path }} {
109-
{{ end }}
105+
location {{ makeLocationPath $location.Path $.Ingress.Annotations | printf }} {
110106
set $service "{{$location.ServiceName}}";
111107
{{with $location.MinionIngress}}
112108
# location for minion {{$location.MinionIngress.Namespace}}/{{$location.MinionIngress.Name}}

internal/configs/version1/template_helper.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ func trim(s string) string {
1414
return strings.TrimSpace(s)
1515
}
1616

17-
// makePathRegex takes a string representing a location path
17+
// makeLocationPath takes a string representing a location path
1818
// and a map representing Ingress annotations.
1919
// It returns a location path with added regular expression modifier.
2020
// See [Location Directive].
2121
//
2222
// [Location Directive]: https://nginx.org/en/docs/http/ngx_http_core_module.html#location
23-
func makePathRegex(path string, annotations map[string]string) string {
23+
func makeLocationPath(path string, annotations map[string]string) string {
2424
p, ok := annotations["nginx.org/path-regex"]
2525
if !ok {
2626
return path
@@ -38,7 +38,7 @@ func makePathRegex(path string, annotations map[string]string) string {
3838
}
3939

4040
var helperFunctions = template.FuncMap{
41-
"split": split,
42-
"trim": trim,
43-
"makePathRegex": makePathRegex,
41+
"split": split,
42+
"trim": trim,
43+
"makeLocationPath": makeLocationPath,
4444
}

internal/configs/version1/template_helper_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ func TestWithPathRegex_MatchesCaseSensitiveModifier(t *testing.T) {
1010
t.Parallel()
1111

1212
want := "~ \"^/coffee/[A-Z0-9]{3}\""
13-
got := makePathRegex("/coffee/[A-Z0-9]{3}", map[string]string{"nginx.org/path-regex": "case_sensitive"})
13+
got := makeLocationPath("/coffee/[A-Z0-9]{3}", map[string]string{"nginx.org/path-regex": "case_sensitive"})
1414
if got != want {
1515
t.Errorf("got: %s, want: %s", got, want)
1616
}
@@ -20,7 +20,7 @@ func TestWithPathRegex_MatchesCaseInsensitiveModifier(t *testing.T) {
2020
t.Parallel()
2121

2222
want := "~* \"^/coffee/[A-Z0-9]{3}\""
23-
got := makePathRegex("/coffee/[A-Z0-9]{3}", map[string]string{"nginx.org/path-regex": "case_insensitive"})
23+
got := makeLocationPath("/coffee/[A-Z0-9]{3}", map[string]string{"nginx.org/path-regex": "case_insensitive"})
2424
if got != want {
2525
t.Errorf("got: %s, want: %s", got, want)
2626
}
@@ -30,7 +30,7 @@ func TestWithPathReqex_MatchesExactModifier(t *testing.T) {
3030
t.Parallel()
3131

3232
want := "= \"/coffee\""
33-
got := makePathRegex("/coffee", map[string]string{"nginx.org/path-regex": "exact"})
33+
got := makeLocationPath("/coffee", map[string]string{"nginx.org/path-regex": "exact"})
3434
if got != want {
3535
t.Errorf("got: %s, want: %s", got, want)
3636
}
@@ -40,7 +40,7 @@ func TestWithPathReqex_DoesNotMatchModifier(t *testing.T) {
4040
t.Parallel()
4141

4242
want := "/coffee"
43-
got := makePathRegex("/coffee", map[string]string{"nginx.org/path-regex": "bogus"})
43+
got := makeLocationPath("/coffee", map[string]string{"nginx.org/path-regex": "bogus"})
4444
if got != want {
4545
t.Errorf("got: %s, want: %s", got, want)
4646
}
@@ -50,7 +50,7 @@ func TestWithPathReqex_DoesNotMatchEmptyModifier(t *testing.T) {
5050
t.Parallel()
5151

5252
want := "/coffee"
53-
got := makePathRegex("/coffee", map[string]string{"nginx.org/path-regex": ""})
53+
got := makeLocationPath("/coffee", map[string]string{"nginx.org/path-regex": ""})
5454
if got != want {
5555
t.Errorf("got: %s, want: %s", got, want)
5656
}
@@ -60,7 +60,7 @@ func TestWithPathReqex_DoesNotMatchBogusAnnotationName(t *testing.T) {
6060
t.Parallel()
6161

6262
want := "/coffee"
63-
got := makePathRegex("/coffee", map[string]string{"nginx.org/bogus-annotation": ""})
63+
got := makeLocationPath("/coffee", map[string]string{"nginx.org/bogus-annotation": ""})
6464
if got != want {
6565
t.Errorf("got: %s, want: %s", got, want)
6666
}

0 commit comments

Comments
 (0)