From 1680f00091ce57ede54a32a50d06716e472e3bab Mon Sep 17 00:00:00 2001 From: Jakub Coufal Date: Fri, 28 May 2021 00:58:07 +0200 Subject: [PATCH] Fix incorrect behaviour with multi-port endpoint subsets --- ...tching-subset-of-service-pods_endpoint.yml | 20 +++++++++++++ ...atching-subset-of-service-pods_ingress.yml | 15 ++++++++++ ...atching-subset-of-service-pods_service.yml | 14 +++++++++ pkg/provider/kubernetes/ingress/kubernetes.go | 2 +- .../kubernetes/ingress/kubernetes_test.go | 30 +++++++++++++++++++ 5 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 pkg/provider/kubernetes/ingress/fixtures/Ingress-with-a-named-port-matching-subset-of-service-pods_endpoint.yml create mode 100644 pkg/provider/kubernetes/ingress/fixtures/Ingress-with-a-named-port-matching-subset-of-service-pods_ingress.yml create mode 100644 pkg/provider/kubernetes/ingress/fixtures/Ingress-with-a-named-port-matching-subset-of-service-pods_service.yml diff --git a/pkg/provider/kubernetes/ingress/fixtures/Ingress-with-a-named-port-matching-subset-of-service-pods_endpoint.yml b/pkg/provider/kubernetes/ingress/fixtures/Ingress-with-a-named-port-matching-subset-of-service-pods_endpoint.yml new file mode 100644 index 000000000..21004ad0f --- /dev/null +++ b/pkg/provider/kubernetes/ingress/fixtures/Ingress-with-a-named-port-matching-subset-of-service-pods_endpoint.yml @@ -0,0 +1,20 @@ +kind: Endpoints +apiVersion: v1 +metadata: + name: service1 + namespace: testing + +subsets: + - addresses: + - ip: 10.10.0.1 + - ip: 10.10.0.2 + ports: + - name: tchouk + port: 8089 + - addresses: + - ip: 10.10.0.1 + - ip: 10.10.0.2 + - ip: 10.10.0.3 + ports: + - name: carotte + port: 8090 diff --git a/pkg/provider/kubernetes/ingress/fixtures/Ingress-with-a-named-port-matching-subset-of-service-pods_ingress.yml b/pkg/provider/kubernetes/ingress/fixtures/Ingress-with-a-named-port-matching-subset-of-service-pods_ingress.yml new file mode 100644 index 000000000..ddc20ca40 --- /dev/null +++ b/pkg/provider/kubernetes/ingress/fixtures/Ingress-with-a-named-port-matching-subset-of-service-pods_ingress.yml @@ -0,0 +1,15 @@ +kind: Ingress +apiVersion: networking.k8s.io/v1beta1 +metadata: + name: "" + namespace: testing + +spec: + rules: + - host: traefik.tchouk + http: + paths: + - path: /bar + backend: + serviceName: service1 + servicePort: tchouk diff --git a/pkg/provider/kubernetes/ingress/fixtures/Ingress-with-a-named-port-matching-subset-of-service-pods_service.yml b/pkg/provider/kubernetes/ingress/fixtures/Ingress-with-a-named-port-matching-subset-of-service-pods_service.yml new file mode 100644 index 000000000..55437f6b2 --- /dev/null +++ b/pkg/provider/kubernetes/ingress/fixtures/Ingress-with-a-named-port-matching-subset-of-service-pods_service.yml @@ -0,0 +1,14 @@ +kind: Service +apiVersion: v1 +metadata: + name: service1 + namespace: testing + +spec: + ports: + - name: carotte + port: 8082 + - name: tchouk + port: 80 + clusterIP: 10.0.0.1 + diff --git a/pkg/provider/kubernetes/ingress/kubernetes.go b/pkg/provider/kubernetes/ingress/kubernetes.go index d6b2ed225..c828d48d1 100644 --- a/pkg/provider/kubernetes/ingress/kubernetes.go +++ b/pkg/provider/kubernetes/ingress/kubernetes.go @@ -529,8 +529,8 @@ func loadService(client Client, namespace string, backend networkingv1beta1.Ingr return nil, errors.New("subset not found") } - var port int32 for _, subset := range endpoints.Subsets { + var port int32 for _, p := range subset.Ports { if portName == p.Name { port = p.Port diff --git a/pkg/provider/kubernetes/ingress/kubernetes_test.go b/pkg/provider/kubernetes/ingress/kubernetes_test.go index fe8b15230..3b3509037 100644 --- a/pkg/provider/kubernetes/ingress/kubernetes_test.go +++ b/pkg/provider/kubernetes/ingress/kubernetes_test.go @@ -633,6 +633,36 @@ func TestLoadConfigurationFromIngresses(t *testing.T) { }, }, }, + { + desc: "Ingress with a named port matching subset of service pods", + expected: &dynamic.Configuration{ + TCP: &dynamic.TCPConfiguration{}, + HTTP: &dynamic.HTTPConfiguration{ + Middlewares: map[string]*dynamic.Middleware{}, + Routers: map[string]*dynamic.Router{ + "testing-traefik-tchouk-bar": { + Rule: "Host(`traefik.tchouk`) && PathPrefix(`/bar`)", + Service: "testing-service1-tchouk", + }, + }, + Services: map[string]*dynamic.Service{ + "testing-service1-tchouk": { + LoadBalancer: &dynamic.ServersLoadBalancer{ + PassHostHeader: Bool(true), + Servers: []dynamic.Server{ + { + URL: "http://10.10.0.1:8089", + }, + { + URL: "http://10.10.0.2:8089", + }, + }, + }, + }, + }, + }, + }, + }, { desc: "2 ingresses in different namespace with same service name", expected: &dynamic.Configuration{