diff --git a/internal/ingress/controller/controller.go b/internal/ingress/controller/controller.go index 3f9aab8ee..f17f73ef3 100644 --- a/internal/ingress/controller/controller.go +++ b/internal/ingress/controller/controller.go @@ -1369,6 +1369,11 @@ func mergeAlternativeBackends(ing *ingress.Ingress, upstreams map[string]*ingres } for _, rule := range ing.Spec.Rules { + host := rule.Host + if host == "" { + host = defServerName + } + for _, path := range rule.HTTP.Paths { upsName := upstreamName(ing.Namespace, path.Backend.ServiceName, path.Backend.ServicePort) @@ -1382,11 +1387,11 @@ func mergeAlternativeBackends(ing *ingress.Ingress, upstreams map[string]*ingres merged := false altEqualsPri := false - server, ok := servers[rule.Host] + server, ok := servers[host] if !ok { klog.Errorf("cannot merge alternative backend %s into hostname %s that does not exist", altUps.Name, - rule.Host) + host) continue } diff --git a/internal/ingress/controller/controller_test.go b/internal/ingress/controller/controller_test.go index a7560c3bf..0aa522b7f 100644 --- a/internal/ingress/controller/controller_test.go +++ b/internal/ingress/controller/controller_test.go @@ -694,6 +694,88 @@ func TestMergeAlternativeBackends(t *testing.T) { }, }, }, + "non-host canary ingress use default server name as host to merge": { + &ingress.Ingress{ + Ingress: networking.Ingress{ + ObjectMeta: metav1.ObjectMeta{ + Namespace: "example", + }, + Spec: networking.IngressSpec{ + Rules: []networking.IngressRule{ + { + IngressRuleValue: networking.IngressRuleValue{ + HTTP: &networking.HTTPIngressRuleValue{ + Paths: []networking.HTTPIngressPath{ + { + Path: "/", + PathType: &pathTypePrefix, + Backend: networking.IngressBackend{ + ServiceName: "http-svc-canary", + ServicePort: intstr.IntOrString{ + IntVal: 80, + }, + }, + }, + }, + }, + }, + }, + }, + }, + }, + }, + map[string]*ingress.Backend{ + "example-http-svc-80": { + Name: "example-http-svc-80", + NoServer: false, + }, + "example-http-svc-canary-80": { + Name: "example-http-svc-canary-80", + NoServer: true, + TrafficShapingPolicy: ingress.TrafficShapingPolicy{ + Weight: 20, + }, + }, + }, + map[string]*ingress.Server{ + "_": { + Hostname: "_", + Locations: []*ingress.Location{ + { + Path: "/", + PathType: &pathTypePrefix, + Backend: "example-http-svc-80", + }, + }, + }, + }, + map[string]*ingress.Backend{ + "example-http-svc-80": { + Name: "example-http-svc-80", + NoServer: false, + AlternativeBackends: []string{"example-http-svc-canary-80"}, + }, + "example-http-svc-canary-80": { + Name: "example-http-svc-canary-80", + NoServer: true, + TrafficShapingPolicy: ingress.TrafficShapingPolicy{ + Weight: 20, + }, + }, + }, + map[string]*ingress.Server{ + "_": { + Hostname: "_", + Locations: []*ingress.Location{ + { + Path: "/", + PathType: &pathTypePrefix, + Backend: "example-http-svc-80", + }, + }, + }, + }, + }, } for title, tc := range testCases {