Skip to content

Commit 77234fc

Browse files
committed
Fix nginx ingress variables for definitions with Backend
1 parent 0b6d115 commit 77234fc

File tree

3 files changed

+87
-37
lines changed

3 files changed

+87
-37
lines changed

internal/ingress/controller/controller.go

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -567,37 +567,40 @@ func (n *NGINXController) getBackendServers(ingresses []*ingress.Ingress) ([]*in
567567

568568
addLoc := true
569569
for _, loc := range server.Locations {
570-
if loc.Path == nginxPath {
571-
// Same paths but different types are allowed
572-
// (same type means overlap in the path definition)
573-
if !apiequality.Semantic.DeepEqual(loc.PathType, path.PathType) {
574-
break
575-
}
570+
if loc.Path != nginxPath {
571+
continue
572+
}
576573

577-
addLoc = false
574+
// Same paths but different types are allowed
575+
// (same type means overlap in the path definition)
576+
if !apiequality.Semantic.DeepEqual(loc.PathType, path.PathType) {
577+
break
578+
}
578579

579-
if !loc.IsDefBackend {
580-
klog.V(3).Infof("Location %q already configured for server %q with upstream %q (Ingress %q)",
581-
loc.Path, server.Hostname, loc.Backend, ingKey)
582-
break
583-
}
580+
addLoc = false
584581

585-
klog.V(3).Infof("Replacing location %q for server %q with upstream %q to use upstream %q (Ingress %q)",
586-
loc.Path, server.Hostname, loc.Backend, ups.Name, ingKey)
582+
if !loc.IsDefBackend {
583+
klog.V(3).Infof("Location %q already configured for server %q with upstream %q (Ingress %q)",
584+
loc.Path, server.Hostname, loc.Backend, ingKey)
585+
break
586+
}
587587

588-
loc.Backend = ups.Name
589-
loc.IsDefBackend = false
590-
loc.Port = ups.Port
591-
loc.Service = ups.Service
592-
loc.Ingress = ing
588+
klog.V(3).Infof("Replacing location %q for server %q with upstream %q to use upstream %q (Ingress %q)",
589+
loc.Path, server.Hostname, loc.Backend, ups.Name, ingKey)
593590

594-
locationApplyAnnotations(loc, anns)
591+
loc.Backend = ups.Name
592+
loc.IsDefBackend = false
593+
loc.Port = ups.Port
594+
loc.Service = ups.Service
595+
loc.Ingress = ing
595596

596-
if loc.Redirect.FromToWWW {
597-
server.RedirectFromToWWW = true
598-
}
599-
break
597+
locationApplyAnnotations(loc, anns)
598+
599+
if loc.Redirect.FromToWWW {
600+
server.RedirectFromToWWW = true
600601
}
602+
603+
break
601604
}
602605

603606
// new location
@@ -1048,14 +1051,14 @@ func (n *NGINXController) createServers(data []*ingress.Ingress,
10481051

10491052
// special "catch all" case, Ingress with a backend but no rule
10501053
defLoc := servers[defServerName].Locations[0]
1054+
defLoc.Backend = backendUpstream.Name
1055+
defLoc.Service = backendUpstream.Service
1056+
defLoc.Ingress = ing
1057+
10511058
if defLoc.IsDefBackend && len(ing.Spec.Rules) == 0 {
1052-
klog.V(2).Infof("Ingress %q defines a backend but no rule. Using it to configure the catch-all server %q",
1053-
ingKey, defServerName)
1059+
klog.V(2).Infof("Ingress %q defines a backend but no rule. Using it to configure the catch-all server %q", ingKey, defServerName)
10541060

10551061
defLoc.IsDefBackend = false
1056-
defLoc.Backend = backendUpstream.Name
1057-
defLoc.Service = backendUpstream.Service
1058-
defLoc.Ingress = ing
10591062

10601063
// TODO: Redirect and rewrite can affect the catch all behavior, skip for now
10611064
originalRedirect := defLoc.Redirect
@@ -1064,8 +1067,7 @@ func (n *NGINXController) createServers(data []*ingress.Ingress,
10641067
defLoc.Redirect = originalRedirect
10651068
defLoc.Rewrite = originalRewrite
10661069
} else {
1067-
klog.V(3).Infof("Ingress %q defines both a backend and rules. Using its backend as default upstream for all its rules.",
1068-
ingKey)
1070+
klog.V(3).Infof("Ingress %q defines both a backend and rules. Using its backend as default upstream for all its rules.", ingKey)
10691071
}
10701072
}
10711073
}
@@ -1081,12 +1083,12 @@ func (n *NGINXController) createServers(data []*ingress.Ingress,
10811083
continue
10821084
}
10831085

1084-
pathTypePrefix := networking.PathTypePrefix
10851086
loc := &ingress.Location{
10861087
Path: rootLocation,
10871088
PathType: &pathTypePrefix,
10881089
IsDefBackend: true,
10891090
Backend: un,
1091+
Ingress: ing,
10901092
Service: &apiv1.Service{},
10911093
}
10921094
locationApplyAnnotations(loc, anns)

internal/ingress/controller/template/template.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -890,14 +890,21 @@ func getIngressInformation(i, h, p interface{}) *ingressInformation {
890890
}
891891

892892
for _, rPath := range rule.HTTP.Paths {
893-
if path == rPath.Path {
894-
info.Service = rPath.Backend.ServiceName
895-
if rPath.Backend.ServicePort.String() != "0" {
896-
info.ServicePort = rPath.Backend.ServicePort.String()
897-
}
893+
if path != rPath.Path {
894+
continue
895+
}
898896

897+
if info.Service != "" && rPath.Backend.ServiceName == "" {
898+
// empty rule. Only contains a Path and PathType
899899
return info
900900
}
901+
902+
info.Service = rPath.Backend.ServiceName
903+
if rPath.Backend.ServicePort.String() != "0" {
904+
info.ServicePort = rPath.Backend.ServicePort.String()
905+
}
906+
907+
return info
901908
}
902909
}
903910

test/e2e/ingress/without_host.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ import (
2222
"strings"
2323

2424
"github.com/onsi/ginkgo"
25+
networking "k8s.io/api/networking/v1beta1"
26+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
27+
"k8s.io/apimachinery/pkg/util/intstr"
2528

2629
"k8s.io/ingress-nginx/test/e2e/framework"
2730
)
@@ -49,4 +52,42 @@ var _ = framework.IngressNginxDescribe("[Ingress] definition without host", func
4952
Expect().
5053
Status(http.StatusOK)
5154
})
55+
56+
ginkgo.It("should set ingress details variables for ingresses with host without IngressRuleValue, only Backend", func() {
57+
f.NewEchoDeployment()
58+
59+
ing := &networking.Ingress{
60+
ObjectMeta: metav1.ObjectMeta{
61+
Name: "backend",
62+
Namespace: f.Namespace,
63+
},
64+
Spec: networking.IngressSpec{
65+
Backend: &networking.IngressBackend{
66+
ServiceName: framework.EchoService,
67+
ServicePort: intstr.FromInt(80),
68+
},
69+
Rules: []networking.IngressRule{
70+
{
71+
Host: "only-backend",
72+
},
73+
},
74+
},
75+
}
76+
f.EnsureIngress(ing)
77+
78+
f.WaitForNginxServer("only-backend",
79+
func(server string) bool {
80+
return strings.Contains(server, fmt.Sprintf(`set $namespace "%v";`, f.Namespace)) &&
81+
strings.Contains(server, fmt.Sprintf(`set $ingress_name "%v";`, ing.Name)) &&
82+
strings.Contains(server, fmt.Sprintf(`set $service_name "%v";`, framework.EchoService)) &&
83+
strings.Contains(server, `set $service_port "80";`) &&
84+
strings.Contains(server, `set $location_path "/";`)
85+
})
86+
87+
f.HTTPTestClient().
88+
GET("/").
89+
WithHeader("Host", "only-backend").
90+
Expect().
91+
Status(http.StatusOK)
92+
})
5293
})

0 commit comments

Comments
 (0)