Skip to content

Commit 233c67e

Browse files
committed
Path-Regex annotation on master and minions are set independently (#4223)
1 parent 893ab2f commit 233c67e

File tree

3 files changed

+122
-9
lines changed

3 files changed

+122
-9
lines changed

internal/configs/version1/template_helper.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,15 @@ func trim(s string) string {
2626
func makeLocationPath(loc *Location, ingressAnnotations map[string]string) string {
2727
if loc.MinionIngress != nil {
2828
// Case when annotation 'path-regex' set on Location's Minion.
29-
_, isMinion := loc.MinionIngress.Annotations["nginx.org/mergeable-ingress-type"]
29+
ingressType, isMergeable := loc.MinionIngress.Annotations["nginx.org/mergeable-ingress-type"]
3030
regexType, hasRegex := loc.MinionIngress.Annotations["nginx.org/path-regex"]
3131

32-
if isMinion && hasRegex {
32+
if isMergeable && ingressType == "minion" && hasRegex {
3333
return makePathWithRegex(loc.Path, regexType)
3434
}
35+
if isMergeable && ingressType == "minion" && !hasRegex {
36+
return loc.Path
37+
}
3538
}
3639

3740
// Case when annotation 'path-regex' set on Ingress (including Master).

internal/configs/version1/template_helper_test.go

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,6 @@ func TestMakeLocationPath_ForIngressWithPathRegexSetOnMaster(t *testing.T) {
148148
MinionIngress: &Ingress{
149149
Name: "cafe-ingress-coffee-minion",
150150
Namespace: "default",
151-
Annotations: map[string]string{
152-
"nginx.org/mergeable-ingress-type": "minion",
153-
},
154151
},
155152
},
156153
map[string]string{
@@ -191,10 +188,10 @@ func TestMakeLocationPath_SetOnMinionTakesPrecedenceOverMaster(t *testing.T) {
191188
}
192189
}
193190

194-
func TestMakeLocationPath_PathRegexSetOnMaster(t *testing.T) {
191+
func TestMakeLocationPath_PathRegexSetOnMasterDoesNotModifyMinionWithoutPathRegexAnnotation(t *testing.T) {
195192
t.Parallel()
196193

197-
want := "= \"/coffee\""
194+
want := "/coffee"
198195
got := makeLocationPath(
199196
&Location{
200197
Path: "/coffee",
@@ -217,6 +214,24 @@ func TestMakeLocationPath_PathRegexSetOnMaster(t *testing.T) {
217214
}
218215
}
219216

217+
func TestMakeLocationPath_ForIngress(t *testing.T) {
218+
t.Parallel()
219+
220+
want := "~ \"^/coffee\""
221+
got := makeLocationPath(
222+
&Location{
223+
Path: "/coffee",
224+
},
225+
map[string]string{
226+
"nginx.org/path-regex": "case_sensitive",
227+
},
228+
)
229+
230+
if got != want {
231+
t.Errorf("got %q, want %q", got, want)
232+
}
233+
}
234+
220235
func TestSplitInputString(t *testing.T) {
221236
t.Parallel()
222237

internal/configs/version1/template_test.go

Lines changed: 97 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,27 @@ func TestExecuteTemplate_ForMergeableIngressForNGINXPlus(t *testing.T) {
152152
}
153153
}
154154

155+
func TestExecuteTemplate_ForMergeableIngressForNGINXPlusWithMasterPathRegex(t *testing.T) {
156+
t.Parallel()
157+
158+
tmpl := newNGINXPlusIngressTmpl(t)
159+
buf := &bytes.Buffer{}
160+
161+
err := tmpl.Execute(buf, ingressCfgMasterMinionNGINXPlusMasterMinions)
162+
t.Log(buf.String())
163+
if err != nil {
164+
t.Fatal(err)
165+
}
166+
want := "location /coffee {"
167+
if !strings.Contains(buf.String(), want) {
168+
t.Errorf("want %q in generated config", want)
169+
}
170+
want = "location /tea {"
171+
if !strings.Contains(buf.String(), want) {
172+
t.Errorf("want %q in generated config", want)
173+
}
174+
}
175+
155176
func TestExecuteTemplate_ForMergeableIngressWithOneMinionWithPathRegexAnnotation(t *testing.T) {
156177
t.Parallel()
157178

@@ -210,11 +231,11 @@ func TestExecuteTemplate_ForMergeableIngressForNGINXPlusWithPathRegexAnnotationO
210231
t.Fatal(err)
211232
}
212233

213-
want := "location ~ \"^/coffee\" {"
234+
want := "location /coffee {"
214235
if !strings.Contains(buf.String(), want) {
215236
t.Errorf("want %q in generated config", want)
216237
}
217-
want = "location ~ \"^/tea\" {"
238+
want = "location /tea {"
218239
if !strings.Contains(buf.String(), want) {
219240
t.Errorf("want %q in generated config", want)
220241
}
@@ -915,6 +936,80 @@ var (
915936
},
916937
}
917938

939+
// ingressCfgMasterMinionNGINXPlusMasterMinions holds data to test the following scenario:
940+
//
941+
// Ingress Master - Minion
942+
// - Master: with `path-regex` annotation
943+
// - Minion 1 (cafe-ingress-coffee-minion): without `path-regex` annotation
944+
// - Minion 2 (cafe-ingress-tea-minion): without `path-regex` annotation
945+
ingressCfgMasterMinionNGINXPlusMasterMinions = IngressNginxConfig{
946+
Upstreams: []Upstream{
947+
coffeeUpstreamNginxPlus,
948+
teaUpstreamNGINXPlus,
949+
},
950+
Servers: []Server{
951+
{
952+
Name: "cafe.example.com",
953+
ServerTokens: "on",
954+
Locations: []Location{
955+
{
956+
Path: "/coffee",
957+
ServiceName: "coffee-svc",
958+
Upstream: coffeeUpstreamNginxPlus,
959+
ProxyConnectTimeout: "60s",
960+
ProxyReadTimeout: "60s",
961+
ProxySendTimeout: "60s",
962+
ClientMaxBodySize: "1m",
963+
ProxyBuffering: true,
964+
MinionIngress: &Ingress{
965+
Name: "cafe-ingress-coffee-minion",
966+
Namespace: "default",
967+
Annotations: map[string]string{
968+
"nginx.org/mergeable-ingress-type": "minion",
969+
},
970+
},
971+
ProxySSLName: "coffee-svc.default.svc",
972+
},
973+
{
974+
Path: "/tea",
975+
ServiceName: "tea-svc",
976+
Upstream: teaUpstreamNGINXPlus,
977+
ProxyConnectTimeout: "60s",
978+
ProxyReadTimeout: "60s",
979+
ProxySendTimeout: "60s",
980+
ClientMaxBodySize: "1m",
981+
ProxyBuffering: true,
982+
MinionIngress: &Ingress{
983+
Name: "cafe-ingress-tea-minion",
984+
Namespace: "default",
985+
Annotations: map[string]string{
986+
"nginx.org/mergeable-ingress-type": "minion",
987+
},
988+
},
989+
ProxySSLName: "tea-svc.default.svc",
990+
},
991+
},
992+
SSL: true,
993+
SSLCertificate: "/etc/nginx/secrets/default-cafe-secret",
994+
SSLCertificateKey: "/etc/nginx/secrets/default-cafe-secret",
995+
StatusZone: "cafe.example.com",
996+
HSTSMaxAge: 2592000,
997+
Ports: []int{80},
998+
SSLPorts: []int{443},
999+
SSLRedirect: true,
1000+
HealthChecks: make(map[string]HealthCheck),
1001+
},
1002+
},
1003+
Ingress: Ingress{
1004+
Name: "cafe-ingress-master",
1005+
Namespace: "default",
1006+
Annotations: map[string]string{
1007+
"nginx.org/mergeable-ingress-type": "master",
1008+
"nginx.org/path-regex": "case_sensitive",
1009+
},
1010+
},
1011+
}
1012+
9181013
// ingressCfgMasterMinionNGINXPlusMinionWithPathRegexAnnotation holds data to test the following scenario:
9191014
//
9201015
// Ingress Master - Minion

0 commit comments

Comments
 (0)