From eef5bea0e4b53784fd8768303095f43e29b77c00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20Wel=C3=A9n?= Date: Wed, 6 Sep 2023 14:56:17 +0200 Subject: [PATCH] Support namespaced servicetargets MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Thomas Welén --- controller/cache/info.go | 7 ++++++- controller/cache/info_test.go | 36 +++++++++++++++++++++++++++++++---- 2 files changed, 38 insertions(+), 5 deletions(-) diff --git a/controller/cache/info.go b/controller/cache/info.go index 0f70d3748539c..06a933960bfe9 100644 --- a/controller/cache/info.go +++ b/controller/cache/info.go @@ -250,11 +250,16 @@ func populateTraefikIngressRouteInfo(un *unstructured.Unstructured, res *Resourc continue } + namespace, ok := service["namespace"].(string) + if !ok { + namespace = un.GetNamespace() + } + if serviceName, err := getServiceName(service, gvk); err == nil { targetsMap[v1alpha1.ResourceRef{ Group: "", Kind: kube.ServiceKind, - Namespace: un.GetNamespace(), + Namespace: namespace, Name: serviceName, }] = true } diff --git a/controller/cache/info_test.go b/controller/cache/info_test.go index b9367ff145bc9..3877b09306875 100644 --- a/controller/cache/info_test.go +++ b/controller/cache/info_test.go @@ -243,8 +243,7 @@ spec: prefix: "/2" route: - destination: - host: service -`) + host: service`) testTraefikIngressRoute = strToUnstructured(` apiVersion: traefik.containo.us/v1alpha1 @@ -260,8 +259,24 @@ spec: match: Host(\"example.com\") || (Host(\"example.org\") && Path(\"/traefik\")) services: - name: test-svc - port: 80 -`) + port: 80`) + + testTraefikIngressRouteWithNamespacedService = strToUnstructured(` + apiVersion: traefik.containo.us/v1alpha1 + kind: IngressRoute + metadata: + name: traefik-ingress + namespace: demo + spec: + entryPoints: + - web + routes: + - kind: Rule + match: Host(\"example.com\") || (Host(\"example.org\") && Path(\"/traefik\")) + services: + - name: test-svc + namespace: test + port: 80`) ) func TestGetPodInfo(t *testing.T) { @@ -384,6 +399,19 @@ func TestGetTraefikIngressRouteInfo(t *testing.T) { }) } +func TestGetTraefikIngressRouteInfoWithNamespacedService(t *testing.T) { + info := &ResourceInfo{} + populateNodeInfo(testTraefikIngressRouteWithNamespacedService, info, []string{}) + assert.Equal(t, 0, len(info.Info)) + require.NotNil(t, info.NetworkingInfo) + require.NotNil(t, info.NetworkingInfo.TargetRefs) + assert.Contains(t, info.NetworkingInfo.TargetRefs, v1alpha1.ResourceRef{ + Kind: kube.ServiceKind, + Name: "test-svc", + Namespace: "test", + }) +} + func TestGetIngressInfo(t *testing.T) { var tests = []struct { Ingress *unstructured.Unstructured