Skip to content

Commit 4f2950f

Browse files
committed
Fix duplicate location in NGINX config (#10090)
When creating ingresses that use the same path with a mix of Exact and Prefix path types nginx-ingress-controller can generate an NGINX config with a duplicate location: nginx: [emerg] duplicate location "/a/" in /tmp/nginx/nginx-cfg2287376726:693 nginx: configuration file /tmp/nginx/nginx-cfg2287376726 test failed The issue is that nginx-ingress-controller stops looking for duplicates once it sees the same path with a different path type.
1 parent 536c4e7 commit 4f2950f

File tree

2 files changed

+144
-1
lines changed

2 files changed

+144
-1
lines changed

Diff for: internal/ingress/controller/controller.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -795,7 +795,7 @@ func (n *NGINXController) getBackendServers(ingresses []*ingress.Ingress) ([]*in
795795
// Same paths but different types are allowed
796796
// (same type means overlap in the path definition)
797797
if !apiequality.Semantic.DeepEqual(loc.PathType, path.PathType) {
798-
break
798+
continue
799799
}
800800

801801
addLoc = false

Diff for: internal/ingress/controller/controller_test.go

+143
Original file line numberDiff line numberDiff line change
@@ -2468,6 +2468,149 @@ func TestGetBackendServers(t *testing.T) {
24682468
}
24692469
},
24702470
},
2471+
{
2472+
Ingresses: []*ingress.Ingress{
2473+
{
2474+
Ingress: networking.Ingress{
2475+
ObjectMeta: metav1.ObjectMeta{
2476+
Namespace: "example",
2477+
},
2478+
Spec: networking.IngressSpec{
2479+
Rules: []networking.IngressRule{
2480+
{
2481+
Host: "example.com",
2482+
IngressRuleValue: networking.IngressRuleValue{
2483+
HTTP: &networking.HTTPIngressRuleValue{
2484+
Paths: []networking.HTTPIngressPath{
2485+
{
2486+
Path: "/a",
2487+
PathType: &pathTypeExact,
2488+
Backend: networking.IngressBackend{
2489+
Service: &networking.IngressServiceBackend{
2490+
Name: "http-svc-1",
2491+
Port: networking.ServiceBackendPort{
2492+
Number: 80,
2493+
},
2494+
},
2495+
},
2496+
},
2497+
},
2498+
},
2499+
},
2500+
},
2501+
},
2502+
},
2503+
},
2504+
ParsedAnnotations: &annotations.Ingress{},
2505+
},
2506+
{
2507+
Ingress: networking.Ingress{
2508+
ObjectMeta: metav1.ObjectMeta{
2509+
Namespace: "example",
2510+
},
2511+
Spec: networking.IngressSpec{
2512+
Rules: []networking.IngressRule{
2513+
{
2514+
Host: "example.com",
2515+
IngressRuleValue: networking.IngressRuleValue{
2516+
HTTP: &networking.HTTPIngressRuleValue{
2517+
Paths: []networking.HTTPIngressPath{
2518+
{
2519+
Path: "/a",
2520+
PathType: &pathTypePrefix,
2521+
Backend: networking.IngressBackend{
2522+
Service: &networking.IngressServiceBackend{
2523+
Name: "http-svc-2",
2524+
Port: networking.ServiceBackendPort{
2525+
Number: 80,
2526+
},
2527+
},
2528+
},
2529+
},
2530+
},
2531+
},
2532+
},
2533+
},
2534+
},
2535+
},
2536+
},
2537+
ParsedAnnotations: &annotations.Ingress{},
2538+
},
2539+
{
2540+
Ingress: networking.Ingress{
2541+
ObjectMeta: metav1.ObjectMeta{
2542+
Namespace: "example",
2543+
},
2544+
Spec: networking.IngressSpec{
2545+
Rules: []networking.IngressRule{
2546+
{
2547+
Host: "example.com",
2548+
IngressRuleValue: networking.IngressRuleValue{
2549+
HTTP: &networking.HTTPIngressRuleValue{
2550+
Paths: []networking.HTTPIngressPath{
2551+
{
2552+
Path: "/a",
2553+
PathType: &pathTypePrefix,
2554+
Backend: networking.IngressBackend{
2555+
Service: &networking.IngressServiceBackend{
2556+
Name: "http-svc-3",
2557+
Port: networking.ServiceBackendPort{
2558+
Number: 80,
2559+
},
2560+
},
2561+
},
2562+
},
2563+
},
2564+
},
2565+
},
2566+
},
2567+
},
2568+
},
2569+
},
2570+
ParsedAnnotations: &annotations.Ingress{},
2571+
},
2572+
},
2573+
Validate: func(ingresses []*ingress.Ingress, upstreams []*ingress.Backend, servers []*ingress.Server) {
2574+
if len(servers) != 2 {
2575+
t.Errorf("servers count should be 2, got %d", len(servers))
2576+
return
2577+
}
2578+
2579+
s := servers[0]
2580+
if s.Hostname != "_" {
2581+
t.Errorf("server hostname should be '_', got '%s'", s.Hostname)
2582+
}
2583+
if len(s.Locations) != 1 {
2584+
t.Errorf("servers locations count should be 1, got %d", len(s.Locations))
2585+
return
2586+
}
2587+
if !s.Locations[0].IsDefBackend {
2588+
t.Errorf("server location 0 should be default backend")
2589+
}
2590+
if s.Locations[0].Backend != defUpstreamName {
2591+
t.Errorf("location backend should be '%s', got '%s'", defUpstreamName, s.Locations[0].Backend)
2592+
}
2593+
2594+
s = servers[1]
2595+
if s.Hostname != "example.com" {
2596+
t.Errorf("server hostname should be 'example.com', got '%s'", s.Hostname)
2597+
}
2598+
if len(s.Locations) != 3 {
2599+
t.Errorf("servers locations count should be 2, got %d", len(s.Locations))
2600+
return
2601+
}
2602+
if s.Locations[0].Backend != "example-http-svc-1-80" {
2603+
t.Errorf("location 0 backend should be 'example-http-svc-1-80', got '%s'", s.Locations[0].Backend)
2604+
}
2605+
if s.Locations[1].Backend != "example-http-svc-2-80" {
2606+
t.Errorf("location 1 backend should be 'example-http-svc-2-80', got '%s'", s.Locations[1].Backend)
2607+
}
2608+
if s.Locations[2].Backend != defUpstreamName {
2609+
t.Errorf("location 2 backend should be '%s', got '%s'", defUpstreamName, s.Locations[2].Backend)
2610+
}
2611+
},
2612+
SetConfigMap: testConfigMap,
2613+
},
24712614
}
24722615

24732616
for _, testCase := range testCases {

0 commit comments

Comments
 (0)