Controller: Fix panic in alternative backend merging. (#11794)

Co-authored-by: joey <zchengjoey@gmail.com>
This commit is contained in:
k8s-infra-cherrypick-robot 2024-08-13 03:00:56 -07:00 committed by GitHub
parent 5fec9baa79
commit c419a2ca27
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 78 additions and 2 deletions

View file

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

View file

@ -1292,6 +1292,74 @@ func TestMergeAlternativeBackends(t *testing.T) {
},
},
},
"alternative backend does not merge for missing upstream": {
&ingress.Ingress{
Ingress: networking.Ingress{
ObjectMeta: metav1.ObjectMeta{
Namespace: "example",
},
Spec: networking.IngressSpec{
Rules: []networking.IngressRule{
{
Host: "example.com",
IngressRuleValue: networking.IngressRuleValue{
HTTP: &networking.HTTPIngressRuleValue{
Paths: []networking.HTTPIngressPath{
{
Path: "/",
PathType: &pathTypePrefix,
Backend: networking.IngressBackend{
Service: &networking.IngressServiceBackend{
Name: "http-svc-canary",
Port: networking.ServiceBackendPort{
Number: 80,
},
},
},
},
},
},
},
},
},
},
},
},
map[string]*ingress.Backend{
"example-http-svc-canary-80": {
Name: "example-http-svc-canary-80",
NoServer: true,
TrafficShapingPolicy: ingress.TrafficShapingPolicy{
Weight: 20,
},
},
},
map[string]*ingress.Server{
"example.com": {
Hostname: "example.com",
Locations: []*ingress.Location{
{
Path: "/",
PathType: &pathTypePrefix,
Backend: "example-http-svc-80",
},
},
},
},
map[string]*ingress.Backend{},
map[string]*ingress.Server{
"example.com": {
Hostname: "example.com",
Locations: []*ingress.Location{
{
Path: "/",
PathType: &pathTypePrefix,
Backend: "example-http-svc-80",
},
},
},
},
},
}
for title, tc := range testCases {