Skip to content

Commit 108fbac

Browse files
cpanatorikatz
authored andcommitted
Handle named (non-numeric) ports correctly (kubernetes#7311)
Signed-off-by: Carlos Panato <[email protected]>
1 parent b1d5b47 commit 108fbac

File tree

4 files changed

+200
-1
lines changed

4 files changed

+200
-1
lines changed

cmd/plugin/commands/ingresses/ingresses.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ func serviceToNameAndPort(svc *networking.IngressServiceBackend) (string, intstr
227227
return svcName, intstr.FromInt(int(svc.Port.Number))
228228
}
229229
if svc.Port.Name != "" {
230-
return svcName, intstr.FromString(svc.Port.String())
230+
return svcName, intstr.FromString(svc.Port.Name)
231231
}
232232
}
233233
return "", intstr.IntOrString{}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
Copyright 2021 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package ingresses
18+
19+
import (
20+
"testing"
21+
22+
networking "k8s.io/api/networking/v1"
23+
"k8s.io/apimachinery/pkg/util/intstr"
24+
)
25+
26+
func TestGetIngressInformation(t *testing.T) {
27+
28+
testcases := map[string]struct {
29+
ServiceBackend *networking.IngressServiceBackend
30+
wantName string
31+
wantPort intstr.IntOrString
32+
}{
33+
"empty ingressServiceBackend": {
34+
ServiceBackend: &networking.IngressServiceBackend{},
35+
wantName: "",
36+
wantPort: intstr.IntOrString{},
37+
},
38+
"ingressServiceBackend with port 8080": {
39+
ServiceBackend: &networking.IngressServiceBackend{
40+
Name: "test",
41+
Port: networking.ServiceBackendPort{
42+
Number: 8080,
43+
},
44+
},
45+
wantName: "test",
46+
wantPort: intstr.IntOrString{
47+
Type: intstr.Int,
48+
IntVal: 8080,
49+
},
50+
},
51+
"ingressServiceBackend with port name a-svc": {
52+
ServiceBackend: &networking.IngressServiceBackend{
53+
Name: "test",
54+
Port: networking.ServiceBackendPort{
55+
Name: "a-svc",
56+
},
57+
},
58+
wantName: "test",
59+
wantPort: intstr.IntOrString{
60+
Type: intstr.String,
61+
StrVal: "a-svc",
62+
},
63+
},
64+
}
65+
66+
for title, testCase := range testcases {
67+
gotName, gotPort := serviceToNameAndPort(testCase.ServiceBackend)
68+
if gotName != testCase.wantName {
69+
t.Fatalf("%s: expected '%v' but returned %v", title, testCase.wantName, gotName)
70+
}
71+
if gotPort != testCase.wantPort {
72+
t.Fatalf("%s: expected '%v' but returned %v", title, testCase.wantPort, gotPort)
73+
}
74+
}
75+
}

internal/ingress/controller/template/template.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -990,6 +990,8 @@ func getIngressInformation(i, h, p interface{}) *ingressInformation {
990990
info.Service = ing.Spec.DefaultBackend.Service.Name
991991
if ing.Spec.DefaultBackend.Service.Port.Number > 0 {
992992
info.ServicePort = strconv.Itoa(int(ing.Spec.DefaultBackend.Service.Port.Number))
993+
} else {
994+
info.ServicePort = ing.Spec.DefaultBackend.Service.Port.Name
993995
}
994996
}
995997

@@ -1024,6 +1026,8 @@ func getIngressInformation(i, h, p interface{}) *ingressInformation {
10241026
info.Service = rPath.Backend.Service.Name
10251027
if rPath.Backend.Service.Port.Number > 0 {
10261028
info.ServicePort = strconv.Itoa(int(rPath.Backend.Service.Port.Number))
1029+
} else {
1030+
info.ServicePort = rPath.Backend.Service.Port.Name
10271031
}
10281032

10291033
return info

internal/ingress/controller/template/template_test.go

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,6 +1002,76 @@ func TestGetIngressInformation(t *testing.T) {
10021002
10,
10031003
&ingressInformation{},
10041004
},
1005+
"valid ingress definition with name validIng in namespace default using a service with name a-svc port number 8080": {
1006+
&ingress.Ingress{
1007+
Ingress: networking.Ingress{
1008+
ObjectMeta: metav1.ObjectMeta{
1009+
Name: "validIng",
1010+
Namespace: apiv1.NamespaceDefault,
1011+
Annotations: map[string]string{
1012+
"ingress.annotation": "ok",
1013+
},
1014+
},
1015+
Spec: networking.IngressSpec{
1016+
DefaultBackend: &networking.IngressBackend{
1017+
Service: &networking.IngressServiceBackend{
1018+
Name: "a-svc",
1019+
Port: networking.ServiceBackendPort{
1020+
Number: 8080,
1021+
},
1022+
},
1023+
},
1024+
},
1025+
},
1026+
},
1027+
"host1",
1028+
"",
1029+
&ingressInformation{
1030+
Namespace: "default",
1031+
Rule: "validIng",
1032+
Path: "/",
1033+
Annotations: map[string]string{
1034+
"ingress.annotation": "ok",
1035+
},
1036+
Service: "a-svc",
1037+
ServicePort: "8080",
1038+
},
1039+
},
1040+
"valid ingress definition with name validIng in namespace default using a service with name a-svc port name b-svc": {
1041+
&ingress.Ingress{
1042+
Ingress: networking.Ingress{
1043+
ObjectMeta: metav1.ObjectMeta{
1044+
Name: "validIng",
1045+
Namespace: apiv1.NamespaceDefault,
1046+
Annotations: map[string]string{
1047+
"ingress.annotation": "ok",
1048+
},
1049+
},
1050+
Spec: networking.IngressSpec{
1051+
DefaultBackend: &networking.IngressBackend{
1052+
Service: &networking.IngressServiceBackend{
1053+
Name: "a-svc",
1054+
Port: networking.ServiceBackendPort{
1055+
Name: "b-svc",
1056+
},
1057+
},
1058+
},
1059+
},
1060+
},
1061+
},
1062+
"host1",
1063+
"",
1064+
&ingressInformation{
1065+
Namespace: "default",
1066+
Rule: "validIng",
1067+
Path: "/",
1068+
Annotations: map[string]string{
1069+
"ingress.annotation": "ok",
1070+
},
1071+
Service: "a-svc",
1072+
ServicePort: "b-svc",
1073+
},
1074+
},
10051075
"valid ingress definition with name validIng in namespace default": {
10061076
&ingress.Ingress{
10071077
Ingress: networking.Ingress{
@@ -1083,6 +1153,56 @@ func TestGetIngressInformation(t *testing.T) {
10831153
ServicePort: "80",
10841154
},
10851155
},
1156+
"valid ingress definition with name demo in namespace something and path /ok using a service with name b-svc port name b-svc-80": {
1157+
&ingress.Ingress{
1158+
Ingress: networking.Ingress{
1159+
ObjectMeta: metav1.ObjectMeta{
1160+
Name: "demo",
1161+
Namespace: "something",
1162+
Annotations: map[string]string{
1163+
"ingress.annotation": "ok",
1164+
},
1165+
},
1166+
Spec: networking.IngressSpec{
1167+
Rules: []networking.IngressRule{
1168+
{
1169+
Host: "foo.bar",
1170+
IngressRuleValue: networking.IngressRuleValue{
1171+
HTTP: &networking.HTTPIngressRuleValue{
1172+
Paths: []networking.HTTPIngressPath{
1173+
{
1174+
Path: "/ok",
1175+
PathType: &pathPrefix,
1176+
Backend: networking.IngressBackend{
1177+
Service: &networking.IngressServiceBackend{
1178+
Name: "b-svc",
1179+
Port: networking.ServiceBackendPort{
1180+
Name: "b-svc-80",
1181+
},
1182+
},
1183+
},
1184+
},
1185+
},
1186+
},
1187+
},
1188+
},
1189+
{},
1190+
},
1191+
},
1192+
},
1193+
},
1194+
"foo.bar",
1195+
"/ok",
1196+
&ingressInformation{
1197+
Namespace: "something",
1198+
Rule: "demo",
1199+
Annotations: map[string]string{
1200+
"ingress.annotation": "ok",
1201+
},
1202+
Service: "b-svc",
1203+
ServicePort: "b-svc-80",
1204+
},
1205+
},
10861206
}
10871207

10881208
for title, testCase := range testcases {

0 commit comments

Comments
 (0)