Skip to content

Commit

Permalink
Always define defaultBackend for every ingress
Browse files Browse the repository at this point in the history
  • Loading branch information
wpjunior committed Jan 17, 2024
1 parent 38d2199 commit 67a11e9
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 14 deletions.
12 changes: 12 additions & 0 deletions controllers/nginx_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
{
Expand Down Expand Up @@ -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"}},
},
Expand Down
25 changes: 11 additions & 14 deletions pkg/k8s/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
},
Expand All @@ -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",
Expand All @@ -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,
},
},
},
},
}
}
Expand Down
6 changes: 6 additions & 0 deletions pkg/k8s/k8s_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
{
Expand Down

0 comments on commit 67a11e9

Please sign in to comment.