Add named port support to Kubernetes IngressRoute CRDs

This commit is contained in:
Cirrith 2021-01-15 06:54:04 -08:00 committed by GitHub
parent b1ddd0e038
commit bbee63fcf3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 154 additions and 57 deletions

View file

@ -18,6 +18,7 @@ import (
"github.com/traefik/traefik/v2/pkg/tls"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/intstr"
kubefake "k8s.io/client-go/kubernetes/fake"
)
@ -3738,7 +3739,7 @@ func TestGetServicePort(t *testing.T) {
testCases := []struct {
desc string
svc *corev1.Service
port int32
port intstr.IntOrString
expected *corev1.ServicePort
expectError bool
}{
@ -3757,7 +3758,7 @@ func TestGetServicePort(t *testing.T) {
},
},
},
port: 80,
port: intstr.FromInt(80),
expected: &corev1.ServicePort{
Port: 80,
},
@ -3785,12 +3786,57 @@ func TestGetServicePort(t *testing.T) {
},
expectError: true,
},
{
desc: "Matching named port",
svc: &corev1.Service{
Spec: corev1.ServiceSpec{
Ports: []corev1.ServicePort{
{
Name: "http",
Port: 80,
},
},
},
},
port: intstr.FromString("http"),
expected: &corev1.ServicePort{
Name: "http",
Port: 80,
},
},
{
desc: "Matching named port (with external name)",
svc: &corev1.Service{
Spec: corev1.ServiceSpec{
Type: corev1.ServiceTypeExternalName,
Ports: []corev1.ServicePort{
{
Name: "http",
Port: 80,
},
},
},
},
port: intstr.FromString("http"),
expected: &corev1.ServicePort{
Name: "http",
Port: 80,
},
},
{
desc: "Mismatching, only port(Ingress) defined",
svc: &corev1.Service{
Spec: corev1.ServiceSpec{},
},
port: 80,
port: intstr.FromInt(80),
expectError: true,
},
{
desc: "Mismatching, only named port(Ingress) defined",
svc: &corev1.Service{
Spec: corev1.ServiceSpec{},
},
port: intstr.FromString("http"),
expectError: true,
},
{
@ -3800,11 +3846,21 @@ func TestGetServicePort(t *testing.T) {
Type: corev1.ServiceTypeExternalName,
},
},
port: 80,
port: intstr.FromInt(80),
expected: &corev1.ServicePort{
Port: 80,
},
},
{
desc: "Mismatching, only named port(Ingress) defined with external name",
svc: &corev1.Service{
Spec: corev1.ServiceSpec{
Type: corev1.ServiceTypeExternalName,
},
},
port: intstr.FromString("http"),
expectError: true,
},
{
desc: "Mismatching, only Service port defined",
svc: &corev1.Service{
@ -3843,7 +3899,22 @@ func TestGetServicePort(t *testing.T) {
},
},
},
port: 443,
port: intstr.FromInt(443),
expectError: true,
},
{
desc: "Two different named ports defined",
svc: &corev1.Service{
Spec: corev1.ServiceSpec{
Ports: []corev1.ServicePort{
{
Name: "foo",
Port: 80,
},
},
},
},
port: intstr.FromString("bar"),
expectError: true,
},
{
@ -3858,11 +3929,27 @@ func TestGetServicePort(t *testing.T) {
},
},
},
port: 443,
port: intstr.FromInt(443),
expected: &corev1.ServicePort{
Port: 443,
},
},
{
desc: "Two different named ports defined (with external name)",
svc: &corev1.Service{
Spec: corev1.ServiceSpec{
Type: corev1.ServiceTypeExternalName,
Ports: []corev1.ServicePort{
{
Name: "foo",
Port: 80,
},
},
},
},
port: intstr.FromString("bar"),
expectError: true,
},
}
for _, test := range testCases {
test := test