Skip to content

Commit 5acc0ab

Browse files
authored
Handle named (non-numeric) ports correctly (#7311)
Signed-off-by: Carlos Panato <[email protected]>
1 parent bc220f7 commit 5acc0ab

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
@@ -988,6 +988,8 @@ func getIngressInformation(i, h, p interface{}) *ingressInformation {
988988
info.Service = ing.Spec.DefaultBackend.Service.Name
989989
if ing.Spec.DefaultBackend.Service.Port.Number > 0 {
990990
info.ServicePort = strconv.Itoa(int(ing.Spec.DefaultBackend.Service.Port.Number))
991+
} else {
992+
info.ServicePort = ing.Spec.DefaultBackend.Service.Port.Name
991993
}
992994
}
993995

@@ -1022,6 +1024,8 @@ func getIngressInformation(i, h, p interface{}) *ingressInformation {
10221024
info.Service = rPath.Backend.Service.Name
10231025
if rPath.Backend.Service.Port.Number > 0 {
10241026
info.ServicePort = strconv.Itoa(int(rPath.Backend.Service.Port.Number))
1027+
} else {
1028+
info.ServicePort = rPath.Backend.Service.Port.Name
10251029
}
10261030

10271031
return info

internal/ingress/controller/template/template_test.go

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -940,6 +940,76 @@ func TestGetIngressInformation(t *testing.T) {
940940
10,
941941
&ingressInformation{},
942942
},
943+
"valid ingress definition with name validIng in namespace default using a service with name a-svc port number 8080": {
944+
&ingress.Ingress{
945+
Ingress: networking.Ingress{
946+
ObjectMeta: metav1.ObjectMeta{
947+
Name: "validIng",
948+
Namespace: apiv1.NamespaceDefault,
949+
Annotations: map[string]string{
950+
"ingress.annotation": "ok",
951+
},
952+
},
953+
Spec: networking.IngressSpec{
954+
DefaultBackend: &networking.IngressBackend{
955+
Service: &networking.IngressServiceBackend{
956+
Name: "a-svc",
957+
Port: networking.ServiceBackendPort{
958+
Number: 8080,
959+
},
960+
},
961+
},
962+
},
963+
},
964+
},
965+
"host1",
966+
"",
967+
&ingressInformation{
968+
Namespace: "default",
969+
Rule: "validIng",
970+
Path: "/",
971+
Annotations: map[string]string{
972+
"ingress.annotation": "ok",
973+
},
974+
Service: "a-svc",
975+
ServicePort: "8080",
976+
},
977+
},
978+
"valid ingress definition with name validIng in namespace default using a service with name a-svc port name b-svc": {
979+
&ingress.Ingress{
980+
Ingress: networking.Ingress{
981+
ObjectMeta: metav1.ObjectMeta{
982+
Name: "validIng",
983+
Namespace: apiv1.NamespaceDefault,
984+
Annotations: map[string]string{
985+
"ingress.annotation": "ok",
986+
},
987+
},
988+
Spec: networking.IngressSpec{
989+
DefaultBackend: &networking.IngressBackend{
990+
Service: &networking.IngressServiceBackend{
991+
Name: "a-svc",
992+
Port: networking.ServiceBackendPort{
993+
Name: "b-svc",
994+
},
995+
},
996+
},
997+
},
998+
},
999+
},
1000+
"host1",
1001+
"",
1002+
&ingressInformation{
1003+
Namespace: "default",
1004+
Rule: "validIng",
1005+
Path: "/",
1006+
Annotations: map[string]string{
1007+
"ingress.annotation": "ok",
1008+
},
1009+
Service: "a-svc",
1010+
ServicePort: "b-svc",
1011+
},
1012+
},
9431013
"valid ingress definition with name validIng in namespace default": {
9441014
&ingress.Ingress{
9451015
Ingress: networking.Ingress{
@@ -1021,6 +1091,56 @@ func TestGetIngressInformation(t *testing.T) {
10211091
ServicePort: "80",
10221092
},
10231093
},
1094+
"valid ingress definition with name demo in namespace something and path /ok using a service with name b-svc port name b-svc-80": {
1095+
&ingress.Ingress{
1096+
Ingress: networking.Ingress{
1097+
ObjectMeta: metav1.ObjectMeta{
1098+
Name: "demo",
1099+
Namespace: "something",
1100+
Annotations: map[string]string{
1101+
"ingress.annotation": "ok",
1102+
},
1103+
},
1104+
Spec: networking.IngressSpec{
1105+
Rules: []networking.IngressRule{
1106+
{
1107+
Host: "foo.bar",
1108+
IngressRuleValue: networking.IngressRuleValue{
1109+
HTTP: &networking.HTTPIngressRuleValue{
1110+
Paths: []networking.HTTPIngressPath{
1111+
{
1112+
Path: "/ok",
1113+
PathType: &pathPrefix,
1114+
Backend: networking.IngressBackend{
1115+
Service: &networking.IngressServiceBackend{
1116+
Name: "b-svc",
1117+
Port: networking.ServiceBackendPort{
1118+
Name: "b-svc-80",
1119+
},
1120+
},
1121+
},
1122+
},
1123+
},
1124+
},
1125+
},
1126+
},
1127+
{},
1128+
},
1129+
},
1130+
},
1131+
},
1132+
"foo.bar",
1133+
"/ok",
1134+
&ingressInformation{
1135+
Namespace: "something",
1136+
Rule: "demo",
1137+
Annotations: map[string]string{
1138+
"ingress.annotation": "ok",
1139+
},
1140+
Service: "b-svc",
1141+
ServicePort: "b-svc-80",
1142+
},
1143+
},
10241144
}
10251145

10261146
for title, testCase := range testcases {

0 commit comments

Comments
 (0)