Handle named (non-numeric) ports correctly (#7311)

Signed-off-by: Carlos Panato <ctadeu@gmail.com>
This commit is contained in:
Carlos Tadeu Panato Junior 2021-07-04 23:06:18 +02:00 committed by GitHub
parent bc220f7366
commit 5acc0ab622
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 200 additions and 1 deletions

View file

@ -988,6 +988,8 @@ func getIngressInformation(i, h, p interface{}) *ingressInformation {
info.Service = ing.Spec.DefaultBackend.Service.Name
if ing.Spec.DefaultBackend.Service.Port.Number > 0 {
info.ServicePort = strconv.Itoa(int(ing.Spec.DefaultBackend.Service.Port.Number))
} else {
info.ServicePort = ing.Spec.DefaultBackend.Service.Port.Name
}
}
@ -1022,6 +1024,8 @@ func getIngressInformation(i, h, p interface{}) *ingressInformation {
info.Service = rPath.Backend.Service.Name
if rPath.Backend.Service.Port.Number > 0 {
info.ServicePort = strconv.Itoa(int(rPath.Backend.Service.Port.Number))
} else {
info.ServicePort = rPath.Backend.Service.Port.Name
}
return info

View file

@ -940,6 +940,76 @@ func TestGetIngressInformation(t *testing.T) {
10,
&ingressInformation{},
},
"valid ingress definition with name validIng in namespace default using a service with name a-svc port number 8080": {
&ingress.Ingress{
Ingress: networking.Ingress{
ObjectMeta: metav1.ObjectMeta{
Name: "validIng",
Namespace: apiv1.NamespaceDefault,
Annotations: map[string]string{
"ingress.annotation": "ok",
},
},
Spec: networking.IngressSpec{
DefaultBackend: &networking.IngressBackend{
Service: &networking.IngressServiceBackend{
Name: "a-svc",
Port: networking.ServiceBackendPort{
Number: 8080,
},
},
},
},
},
},
"host1",
"",
&ingressInformation{
Namespace: "default",
Rule: "validIng",
Path: "/",
Annotations: map[string]string{
"ingress.annotation": "ok",
},
Service: "a-svc",
ServicePort: "8080",
},
},
"valid ingress definition with name validIng in namespace default using a service with name a-svc port name b-svc": {
&ingress.Ingress{
Ingress: networking.Ingress{
ObjectMeta: metav1.ObjectMeta{
Name: "validIng",
Namespace: apiv1.NamespaceDefault,
Annotations: map[string]string{
"ingress.annotation": "ok",
},
},
Spec: networking.IngressSpec{
DefaultBackend: &networking.IngressBackend{
Service: &networking.IngressServiceBackend{
Name: "a-svc",
Port: networking.ServiceBackendPort{
Name: "b-svc",
},
},
},
},
},
},
"host1",
"",
&ingressInformation{
Namespace: "default",
Rule: "validIng",
Path: "/",
Annotations: map[string]string{
"ingress.annotation": "ok",
},
Service: "a-svc",
ServicePort: "b-svc",
},
},
"valid ingress definition with name validIng in namespace default": {
&ingress.Ingress{
Ingress: networking.Ingress{
@ -1021,6 +1091,56 @@ func TestGetIngressInformation(t *testing.T) {
ServicePort: "80",
},
},
"valid ingress definition with name demo in namespace something and path /ok using a service with name b-svc port name b-svc-80": {
&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: "/ok",
PathType: &pathPrefix,
Backend: networking.IngressBackend{
Service: &networking.IngressServiceBackend{
Name: "b-svc",
Port: networking.ServiceBackendPort{
Name: "b-svc-80",
},
},
},
},
},
},
},
},
{},
},
},
},
},
"foo.bar",
"/ok",
&ingressInformation{
Namespace: "something",
Rule: "demo",
Annotations: map[string]string{
"ingress.annotation": "ok",
},
Service: "b-svc",
ServicePort: "b-svc-80",
},
},
}
for title, testCase := range testcases {