From d13b755df2abc706bf5e61d88df8c54287c1c894 Mon Sep 17 00:00:00 2001 From: Ed Robinson Date: Wed, 18 May 2016 17:30:42 +0100 Subject: [PATCH] Allow k8s ports to be referenced by name as well as number --- provider/kubernetes.go | 15 +++++++++++++-- provider/kubernetes_test.go | 2 +- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/provider/kubernetes.go b/provider/kubernetes.go index 3f2d1f492..4af905084 100644 --- a/provider/kubernetes.go +++ b/provider/kubernetes.go @@ -9,6 +9,7 @@ import ( "io" "io/ioutil" "os" + "strconv" "strings" "text/template" "time" @@ -200,12 +201,12 @@ func (provider *Kubernetes) loadIngresses(k8sClient k8s.Client) (*types.Configur for _, service := range services { protocol := "http" for _, port := range service.Spec.Ports { - if port.Port == pa.Backend.ServicePort.IntValue() { + if equalPorts(port, pa.Backend.ServicePort) { if port.Port == 443 { protocol = "https" } templateObjects.Backends[r.Host+pa.Path].Servers[string(service.UID)] = types.Server{ - URL: protocol + "://" + service.Spec.ClusterIP + ":" + pa.Backend.ServicePort.String(), + URL: protocol + "://" + service.Spec.ClusterIP + ":" + strconv.Itoa(port.Port), Weight: 1, } break @@ -218,6 +219,16 @@ func (provider *Kubernetes) loadIngresses(k8sClient k8s.Client) (*types.Configur return &templateObjects, nil } +func equalPorts(servicePort k8s.ServicePort, ingressPort k8s.IntOrString) bool { + if servicePort.Port == ingressPort.IntValue() { + return true + } + if servicePort.Name != "" && servicePort.Name == ingressPort.String() { + return true + } + return false +} + func (provider *Kubernetes) getPassHostHeader() bool { if provider.disablePassHostHeaders { return false diff --git a/provider/kubernetes_test.go b/provider/kubernetes_test.go index ccd0818d6..0b9b98d62 100644 --- a/provider/kubernetes_test.go +++ b/provider/kubernetes_test.go @@ -21,7 +21,7 @@ func TestLoadIngresses(t *testing.T) { Path: "/bar", Backend: k8s.IngressBackend{ ServiceName: "service1", - ServicePort: k8s.FromInt(801), + ServicePort: k8s.FromString("http"), }, }, },