Skip to content

Commit

Permalink
feat(controller): support Istio ServiceEntry network node #20270 (#20273
Browse files Browse the repository at this point in the history
)

add serviceentry node to network diagram and link to workload
by workloadSelector label reference

Signed-off-by: zapkub <rungsikorn@me.com>
  • Loading branch information
zapkub authored Oct 10, 2024
1 parent 45bbd46 commit 19613a2
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
18 changes: 18 additions & 0 deletions controller/cache/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ func populateNodeInfo(un *unstructured.Unstructured, res *ResourceInfo, customLa
switch gvk.Kind {
case "VirtualService":
populateIstioVirtualServiceInfo(un, res)
case "ServiceEntry":
populateIstioServiceEntryInfo(un, res)
}
}
}
Expand Down Expand Up @@ -278,6 +280,22 @@ func populateIstioVirtualServiceInfo(un *unstructured.Unstructured, res *Resourc
res.NetworkingInfo = &v1alpha1.ResourceNetworkingInfo{TargetRefs: targets, ExternalURLs: urls}
}

func populateIstioServiceEntryInfo(un *unstructured.Unstructured, res *ResourceInfo) {
targetLabels, ok, err := unstructured.NestedStringMap(un.Object, "spec", "workloadSelector", "labels")
if err != nil {
return
}
if !ok {
return
}
res.NetworkingInfo = &v1alpha1.ResourceNetworkingInfo{
TargetLabels: targetLabels,
TargetRefs: []v1alpha1.ResourceRef{{
Kind: kube.PodKind,
}},
}
}

func populatePodInfo(un *unstructured.Unstructured, res *ResourceInfo) {
pod := v1.Pod{}
err := runtime.DefaultUnstructuredConverter.FromUnstructured(un.Object, &pod)
Expand Down
38 changes: 38 additions & 0 deletions controller/cache/info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,29 @@ spec:
- destination:
host: service
`)

testIstioServiceEntry = strToUnstructured(`
apiVersion: networking.istio.io/v1beta1
kind: ServiceEntry
metadata:
name: echo
spec:
exportTo:
- '*'
hosts:
- echo.internal
location: MESH_INTERNAL
ports:
- name: http
number: 80
protocol: HTTP
targetPort: 5678
resolution: DNS
workloadSelector:
labels:
app.kubernetes.io/name: echo-2
`)
)

func TestGetPodInfo(t *testing.T) {
Expand Down Expand Up @@ -355,6 +378,21 @@ func TestGetIstioVirtualServiceInfo(t *testing.T) {
})
}

func TestGetIstioServiceEntryInfo(t *testing.T) {
info := &ResourceInfo{}
populateNodeInfo(testIstioServiceEntry, info, []string{})
assert.Empty(t, info.Info)
require.NotNil(t, info.NetworkingInfo)
require.NotNil(t, info.NetworkingInfo.TargetRefs)
assert.Contains(t, info.NetworkingInfo.TargetRefs, v1alpha1.ResourceRef{
Kind: kube.PodKind,
})

assert.Equal(t, map[string]string{
"app.kubernetes.io/name": "echo-2",
}, info.NetworkingInfo.TargetLabels)
}

func TestGetIngressInfo(t *testing.T) {
tests := []struct {
Ingress *unstructured.Unstructured
Expand Down

0 comments on commit 19613a2

Please sign in to comment.