From c37b040217435c1f1683fa7466b6d4ac3002d878 Mon Sep 17 00:00:00 2001 From: Gildas Cherruel Date: Fri, 18 May 2018 01:46:03 +0900 Subject: [PATCH] Mapping ExternalNames to custom ports --- provider/kubernetes/builder_service_test.go | 54 ++++++++++++++++++--- provider/kubernetes/kubernetes.go | 5 +- 2 files changed, 52 insertions(+), 7 deletions(-) diff --git a/provider/kubernetes/builder_service_test.go b/provider/kubernetes/builder_service_test.go index a3bcc6a74..84511080a 100644 --- a/provider/kubernetes/builder_service_test.go +++ b/provider/kubernetes/builder_service_test.go @@ -105,11 +105,11 @@ func TestBuildService(t *testing.T) { assert.EqualValues(t, sampleService1(), actual1) actual2 := buildService( - sName("service3"), + sName("service2"), sNamespace("testing"), - sUID("3"), + sUID("2"), sSpec( - clusterIP("10.0.0.3"), + clusterIP("10.0.0.2"), sType("ExternalName"), sExternalName("example.com"), sPorts( @@ -120,6 +120,23 @@ func TestBuildService(t *testing.T) { ) assert.EqualValues(t, sampleService2(), actual2) + + actual3 := buildService( + sName("service3"), + sNamespace("testing"), + sUID("3"), + sSpec( + clusterIP("10.0.0.3"), + sType("ExternalName"), + sExternalName("example.com"), + sPorts( + sPort(8080, "http"), + sPort(8443, "https"), + ), + ), + ) + + assert.EqualValues(t, sampleService3(), actual3) } func sampleService1() *corev1.Service { @@ -143,12 +160,12 @@ func sampleService1() *corev1.Service { func sampleService2() *corev1.Service { return &corev1.Service{ ObjectMeta: metav1.ObjectMeta{ - Name: "service3", - UID: "3", + Name: "service2", + UID: "2", Namespace: "testing", }, Spec: corev1.ServiceSpec{ - ClusterIP: "10.0.0.3", + ClusterIP: "10.0.0.2", Type: "ExternalName", ExternalName: "example.com", Ports: []corev1.ServicePort{ @@ -164,3 +181,28 @@ func sampleService2() *corev1.Service { }, } } + +func sampleService3() *corev1.Service { + return &corev1.Service{ + ObjectMeta: metav1.ObjectMeta{ + Name: "service3", + UID: "3", + Namespace: "testing", + }, + Spec: corev1.ServiceSpec{ + ClusterIP: "10.0.0.3", + Type: "ExternalName", + ExternalName: "example.com", + Ports: []corev1.ServicePort{ + { + Name: "http", + Port: 8080, + }, + { + Name: "https", + Port: 8443, + }, + }, + }, + } +} diff --git a/provider/kubernetes/kubernetes.go b/provider/kubernetes/kubernetes.go index c7485a8b5..9861a1e27 100644 --- a/provider/kubernetes/kubernetes.go +++ b/provider/kubernetes/kubernetes.go @@ -270,13 +270,16 @@ func (p *Provider) loadIngresses(k8sClient Client) (*types.Configuration, error) protocol := label.DefaultProtocol for _, port := range service.Spec.Ports { if equalPorts(port, pa.Backend.ServicePort) { - if port.Port == 443 { + if port.Port == 443 || strings.HasPrefix(port.Name, "https") { protocol = "https" } if service.Spec.Type == "ExternalName" { url := protocol + "://" + service.Spec.ExternalName name := url + if port.Port != 443 && port.Port != 80 { + url = fmt.Sprintf("%s:%d", url, port.Port) + } templateObjects.Backends[baseName].Servers[name] = types.Server{ URL: url,