diff --git a/controllers/nginx_controller_test.go b/controllers/nginx_controller_test.go index 47656c61..3d0dae32 100644 --- a/controllers/nginx_controller_test.go +++ b/controllers/nginx_controller_test.go @@ -620,6 +620,12 @@ func TestNginxReconciler_reconcileIngress(t *testing.T) { Namespace: "default", }, Spec: networkingv1.IngressSpec{ + DefaultBackend: &networkingv1.IngressBackend{ + Service: &networkingv1.IngressServiceBackend{ + Name: "my-nginx-1-service", + Port: networkingv1.ServiceBackendPort{Name: "http"}, + }, + }, IngressClassName: func(s string) *string { return &s }("default-ingress"), Rules: []networkingv1.IngressRule{ { @@ -839,6 +845,12 @@ func TestNginxReconciler_reconcileIngress(t *testing.T) { }, }, Spec: networkingv1.IngressSpec{ + DefaultBackend: &networkingv1.IngressBackend{ + Service: &networkingv1.IngressServiceBackend{ + Name: "my-nginx-2-service", + Port: networkingv1.ServiceBackendPort{Name: "http"}, + }, + }, TLS: []networkingv1.IngressTLS{ {SecretName: "example-com-certs", Hosts: []string{"www.example.com"}}, }, diff --git a/pkg/k8s/k8s.go b/pkg/k8s/k8s.go index 6d2ac375..61a94e8f 100644 --- a/pkg/k8s/k8s.go +++ b/pkg/k8s/k8s.go @@ -326,6 +326,8 @@ func NewIngress(nginx *v1alpha1.Nginx) *networkingv1.Ingress { var rules []networkingv1.IngressRule var tls []networkingv1.IngressTLS + serviceName := fmt.Sprintf("%s-service", nginx.Name) + for _, t := range nginx.Spec.TLS { hosts := t.Hosts if len(hosts) == 0 { @@ -345,7 +347,7 @@ func NewIngress(nginx *v1alpha1.Nginx) *networkingv1.Ingress { PathType: func(pt networkingv1.PathType) *networkingv1.PathType { return &pt }(networkingv1.PathTypePrefix), Backend: networkingv1.IngressBackend{ Service: &networkingv1.IngressServiceBackend{ - Name: fmt.Sprintf("%s-service", nginx.Name), + Name: serviceName, Port: networkingv1.ServiceBackendPort{ Name: defaultHTTPPortName, }, @@ -364,18 +366,6 @@ func NewIngress(nginx *v1alpha1.Nginx) *networkingv1.Ingress { }) } - var defaultBackend *networkingv1.IngressBackend - if len(rules) == 0 { - defaultBackend = &networkingv1.IngressBackend{ - Service: &networkingv1.IngressServiceBackend{ - Name: fmt.Sprintf("%s-service", nginx.Name), - Port: networkingv1.ServiceBackendPort{ - Name: defaultHTTPPortName, - }, - }, - } - } - return &networkingv1.Ingress{ TypeMeta: metav1.TypeMeta{ APIVersion: "networking.k8s.io/v1", @@ -398,7 +388,14 @@ func NewIngress(nginx *v1alpha1.Nginx) *networkingv1.Ingress { IngressClassName: ingressClass, Rules: rules, TLS: tls, - DefaultBackend: defaultBackend, + DefaultBackend: &networkingv1.IngressBackend{ + Service: &networkingv1.IngressServiceBackend{ + Name: serviceName, + Port: networkingv1.ServiceBackendPort{ + Name: defaultHTTPPortName, + }, + }, + }, }, } } diff --git a/pkg/k8s/k8s_test.go b/pkg/k8s/k8s_test.go index 7bdaeef8..796374ad 100644 --- a/pkg/k8s/k8s_test.go +++ b/pkg/k8s/k8s_test.go @@ -1767,6 +1767,12 @@ func TestNewIngress(t *testing.T) { }, }, Spec: networkingv1.IngressSpec{ + DefaultBackend: &networkingv1.IngressBackend{ + Service: &networkingv1.IngressServiceBackend{ + Name: fmt.Sprintf("%s-service", nginx.Name), + Port: networkingv1.ServiceBackendPort{Name: "http"}, + }, + }, IngressClassName: func(s string) *string { return &s }("custom-class"), Rules: []networkingv1.IngressRule{ {