Skip to content

Commit 255ee7b

Browse files
Controller: Fix panic in alternative backend merging. (#11793)
Co-authored-by: joey <[email protected]>
1 parent a76ecf8 commit 255ee7b

File tree

2 files changed

+78
-2
lines changed

2 files changed

+78
-2
lines changed

internal/ingress/controller/controller.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1590,7 +1590,11 @@ func mergeAlternativeBackends(ing *ingress.Ingress, upstreams map[string]*ingres
15901590
altEqualsPri := false
15911591

15921592
for _, loc := range servers[defServerName].Locations {
1593-
priUps := upstreams[loc.Backend]
1593+
priUps, ok := upstreams[loc.Backend]
1594+
if !ok {
1595+
klog.Warningf("cannot find primary backend %s for location %s%s", loc.Backend, servers[defServerName].Hostname, loc.Path)
1596+
continue
1597+
}
15941598
altEqualsPri = altUps.Name == priUps.Name
15951599
if altEqualsPri {
15961600
klog.Warningf("alternative upstream %s in Ingress %s/%s is primary upstream in Other Ingress for location %s%s!",
@@ -1649,7 +1653,11 @@ func mergeAlternativeBackends(ing *ingress.Ingress, upstreams map[string]*ingres
16491653

16501654
// find matching paths
16511655
for _, loc := range server.Locations {
1652-
priUps := upstreams[loc.Backend]
1656+
priUps, ok := upstreams[loc.Backend]
1657+
if !ok {
1658+
klog.Warningf("cannot find primary backend %s for location %s%s", loc.Backend, server.Hostname, loc.Path)
1659+
continue
1660+
}
16531661
altEqualsPri = altUps.Name == priUps.Name
16541662
if altEqualsPri {
16551663
klog.Warningf("alternative upstream %s in Ingress %s/%s is primary upstream in Other Ingress for location %s%s!",

internal/ingress/controller/controller_test.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1292,6 +1292,74 @@ func TestMergeAlternativeBackends(t *testing.T) {
12921292
},
12931293
},
12941294
},
1295+
"alternative backend does not merge for missing upstream": {
1296+
&ingress.Ingress{
1297+
Ingress: networking.Ingress{
1298+
ObjectMeta: metav1.ObjectMeta{
1299+
Namespace: "example",
1300+
},
1301+
Spec: networking.IngressSpec{
1302+
Rules: []networking.IngressRule{
1303+
{
1304+
Host: "example.com",
1305+
IngressRuleValue: networking.IngressRuleValue{
1306+
HTTP: &networking.HTTPIngressRuleValue{
1307+
Paths: []networking.HTTPIngressPath{
1308+
{
1309+
Path: "/",
1310+
PathType: &pathTypePrefix,
1311+
Backend: networking.IngressBackend{
1312+
Service: &networking.IngressServiceBackend{
1313+
Name: "http-svc-canary",
1314+
Port: networking.ServiceBackendPort{
1315+
Number: 80,
1316+
},
1317+
},
1318+
},
1319+
},
1320+
},
1321+
},
1322+
},
1323+
},
1324+
},
1325+
},
1326+
},
1327+
},
1328+
map[string]*ingress.Backend{
1329+
"example-http-svc-canary-80": {
1330+
Name: "example-http-svc-canary-80",
1331+
NoServer: true,
1332+
TrafficShapingPolicy: ingress.TrafficShapingPolicy{
1333+
Weight: 20,
1334+
},
1335+
},
1336+
},
1337+
map[string]*ingress.Server{
1338+
"example.com": {
1339+
Hostname: "example.com",
1340+
Locations: []*ingress.Location{
1341+
{
1342+
Path: "/",
1343+
PathType: &pathTypePrefix,
1344+
Backend: "example-http-svc-80",
1345+
},
1346+
},
1347+
},
1348+
},
1349+
map[string]*ingress.Backend{},
1350+
map[string]*ingress.Server{
1351+
"example.com": {
1352+
Hostname: "example.com",
1353+
Locations: []*ingress.Location{
1354+
{
1355+
Path: "/",
1356+
PathType: &pathTypePrefix,
1357+
Backend: "example-http-svc-80",
1358+
},
1359+
},
1360+
},
1361+
},
1362+
},
12951363
}
12961364

12971365
for title, tc := range testCases {

0 commit comments

Comments
 (0)