Skip to content

Commit

Permalink
Support namespaced servicetargets
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Welén <[email protected]>
  • Loading branch information
2mas committed Sep 8, 2023
1 parent 8f35bc8 commit eef5bea
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
7 changes: 6 additions & 1 deletion controller/cache/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
36 changes: 32 additions & 4 deletions controller/cache/info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,7 @@ spec:
prefix: "/2"
route:
- destination:
host: service
`)
host: service`)

testTraefikIngressRoute = strToUnstructured(`
apiVersion: traefik.containo.us/v1alpha1
Expand All @@ -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) {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit eef5bea

Please sign in to comment.