Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix incorrect service name being set in the nginx template #11294

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion internal/ingress/controller/template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
"strings"
text_template "text/template"

networkingv1 "k8s.io/api/networking/v1"

Check failure on line 39 in internal/ingress/controller/template/template.go

View workflow job for this annotation

GitHub Actions / lint

dupImport: package is imported 2 times under different aliases on lines 39 and 40 (gocritic)
v1 "k8s.io/api/networking/v1"

Check failure on line 40 in internal/ingress/controller/template/template.go

View workflow job for this annotation

GitHub Actions / lint

dupImport: package is imported 2 times under different aliases on lines 39 and 40 (gocritic)
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/klog/v2"

Expand Down Expand Up @@ -1060,7 +1061,7 @@
return true
}

func getIngressInformation(i, h, p interface{}) *ingressInformation {
func getIngressInformation(i, h, p, t interface{}) *ingressInformation {
ing, ok := i.(*ingress.Ingress)
if !ok {
klog.Errorf("expected an '*ingress.Ingress' type but %T was returned", i)
Expand All @@ -1079,6 +1080,11 @@
return &ingressInformation{}
}

ingressPathType, ok := t.(*v1.PathType)
if !ok {
klog.Errorf("expected a '*v1.PathType' type but %T was returned", t)
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm unsure if this is the best way to handle the type assertion.
It seems like we may get the correct type, or nothing at all (empty string I think).
Happy to change this if anyone has a suggestion.


if ing == nil {
return &ingressInformation{}
}
Expand Down Expand Up @@ -1127,6 +1133,10 @@
continue
}

if *ingressPathType != *rPath.PathType {
continue
}

if rPath.Backend.Service == nil {
continue
}
Expand Down
69 changes: 68 additions & 1 deletion internal/ingress/controller/template/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func init() {

var (
pathPrefix networking.PathType = networking.PathTypePrefix
pathExact networking.PathType = networking.PathTypeExact

// TODO: add tests for SSLPassthrough
tmplFuncTestcases = map[string]struct {
Expand Down Expand Up @@ -1157,18 +1158,21 @@ func TestGetIngressInformation(t *testing.T) {
Ingress interface{}
Host string
Path interface{}
PathType interface{}
Expected *ingressInformation
}{
"wrong ingress type": {
"wrongtype",
"host1",
"/ok",
"",
&ingressInformation{},
},
"wrong path type": {
&ingress.Ingress{},
"host1",
10,
"",
&ingressInformation{},
},
"valid ingress definition with name validIng in namespace default using a service with name a-svc port number 8080": {
Expand All @@ -1195,6 +1199,7 @@ func TestGetIngressInformation(t *testing.T) {
},
"host1",
"",
"",
&ingressInformation{
Namespace: "default",
Rule: "validIng",
Expand Down Expand Up @@ -1230,6 +1235,7 @@ func TestGetIngressInformation(t *testing.T) {
},
"host1",
"",
"",
&ingressInformation{
Namespace: "default",
Rule: "validIng",
Expand Down Expand Up @@ -1262,6 +1268,7 @@ func TestGetIngressInformation(t *testing.T) {
},
"host1",
"",
"",
&ingressInformation{
Namespace: "default",
Rule: "validIng",
Expand Down Expand Up @@ -1312,6 +1319,7 @@ func TestGetIngressInformation(t *testing.T) {
},
"foo.bar",
"/ok",
&pathPrefix,
&ingressInformation{
Namespace: "something",
Rule: "demo",
Expand Down Expand Up @@ -1362,6 +1370,7 @@ func TestGetIngressInformation(t *testing.T) {
},
"foo.bar",
"/ok",
&pathPrefix,
&ingressInformation{
Namespace: "something",
Rule: "demo",
Expand Down Expand Up @@ -1407,6 +1416,7 @@ func TestGetIngressInformation(t *testing.T) {
},
"foo.bar",
"/ok",
&pathPrefix,
&ingressInformation{
Namespace: "something",
Rule: "demo",
Expand Down Expand Up @@ -1462,6 +1472,7 @@ func TestGetIngressInformation(t *testing.T) {
},
"foo.bar",
"/oksvc",
&pathPrefix,
&ingressInformation{
Namespace: "something",
Rule: "demo",
Expand All @@ -1472,10 +1483,66 @@ func TestGetIngressInformation(t *testing.T) {
ServicePort: "b-svc-80",
},
},
"valid ingress definition with name demo in namespace something and two path / with Prefix and Exact": {
&ingress.Ingress{
Ingress: networking.Ingress{
ObjectMeta: metav1.ObjectMeta{
Name: "demo",
Namespace: "something",
Annotations: map[string]string{
"ingress.annotation": "ok",
},
},
Spec: networking.IngressSpec{
Rules: []networking.IngressRule{
{
Host: "foo.bar",
IngressRuleValue: networking.IngressRuleValue{
HTTP: &networking.HTTPIngressRuleValue{
Paths: []networking.HTTPIngressPath{
{
Path: "/",
PathType: &pathPrefix,
Backend: networking.IngressBackend{
Service: &networking.IngressServiceBackend{
Name: "a-svc",
},
},
},
{
Path: "/",
PathType: &pathExact,
Backend: networking.IngressBackend{
Service: &networking.IngressServiceBackend{
Name: "b-svc",
},
},
},
},
},
},
},
},
},
},
},
"foo.bar",
"/",
&pathExact,
&ingressInformation{
Path: "/",
Namespace: "something",
Rule: "demo",
Annotations: map[string]string{
"ingress.annotation": "ok",
},
Service: "b-svc",
},
},
}

for title, testCase := range testcases {
info := getIngressInformation(testCase.Ingress, testCase.Host, testCase.Path)
info := getIngressInformation(testCase.Ingress, testCase.Host, testCase.Path, testCase.PathType)

if !info.Equal(testCase.Expected) {
t.Fatalf("%s: expected '%v' but returned %v", title, testCase.Expected, info)
Expand Down
2 changes: 1 addition & 1 deletion rootfs/etc/nginx/template/nginx.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -1247,7 +1247,7 @@ stream {
{{ end }}

location {{ $path }} {
{{ $ing := (getIngressInformation $location.Ingress $server.Hostname $location.IngressPath) }}
{{ $ing := (getIngressInformation $location.Ingress $server.Hostname $location.IngressPath $location.PathType) }}
set $namespace {{ $ing.Namespace | quote}};
set $ingress_name {{ $ing.Rule | quote }};
set $service_name {{ $ing.Service | quote }};
Expand Down
Loading