Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Show Traefik IngressRoute in network view #15369

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions controller/cache/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ func populateNodeInfo(un *unstructured.Unstructured, res *ResourceInfo, customLa
case kube.IngressKind:
populateIngressInfo(un, res)
}
case "traefik.containo.us":
fallthrough
case "traefik.io":
switch gvk.Kind {
case "IngressRoute":
populateTraefikIngressRouteInfo(un, res)
}
case "networking.istio.io":
switch gvk.Kind {
case "VirtualService":
Expand Down Expand Up @@ -116,6 +123,13 @@ func getServiceName(backend map[string]interface{}, gvk schema.GroupVersionKind)
return fmt.Sprintf("%s", service["name"]), nil
}
}
case "traefik.containo.us":
fallthrough
case "traefik.io":
switch gvk.Version {
case "v1alpha1":
return fmt.Sprintf("%s", backend["name"]), nil
}
}
return "", errors.New("unable to resolve string")
}
Expand Down Expand Up @@ -226,6 +240,52 @@ func populateIngressInfo(un *unstructured.Unstructured, res *ResourceInfo) {
res.NetworkingInfo = &v1alpha1.ResourceNetworkingInfo{TargetRefs: targets, Ingress: ingress, ExternalURLs: urls}
}

func populateTraefikIngressRouteInfo(un *unstructured.Unstructured, res *ResourceInfo) {
targetsMap := make(map[v1alpha1.ResourceRef]bool)
gvk := un.GroupVersionKind()

if routes, ok, err := unstructured.NestedSlice(un.Object, "spec", "routes"); ok && err == nil {
for i := range routes {
route, ok := routes[i].(map[string]interface{})
if !ok {
continue
}

services, ok, err := unstructured.NestedSlice(route, "services")
if !ok || err != nil {
continue
}
for i := range services {
service, ok := services[i].(map[string]interface{})
if !ok {
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: namespace,
Name: serviceName,
}] = true
}
}
}
}

targets := make([]v1alpha1.ResourceRef, 0)
for target := range targetsMap {
targets = append(targets, target)
}

res.NetworkingInfo = &v1alpha1.ResourceNetworkingInfo{TargetRefs: targets}
}

func populateIstioVirtualServiceInfo(un *unstructured.Unstructured, res *ResourceInfo) {
targetsMap := make(map[v1alpha1.ResourceRef]bool)

Expand Down
62 changes: 60 additions & 2 deletions controller/cache/info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,40 @@ spec:
prefix: "/2"
route:
- destination:
host: service
`)
host: service`)

testTraefikIngressRoute = strToUnstructured(`
apiVersion: traefik.io/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
port: 80`)

testTraefikIngressRouteWithNamespacedService = strToUnstructured(`
apiVersion: traefik.io/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 @@ -354,6 +386,32 @@ func TestGetIstioVirtualServiceInfo(t *testing.T) {
})
}

func TestGetTraefikIngressRouteInfo(t *testing.T) {
info := &ResourceInfo{}
populateNodeInfo(testTraefikIngressRoute, 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: "demo",
})
}

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
Loading