Skip to content

Commit

Permalink
feat: add Istio virtual service to the network view - part of #4675 (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
OmerKahani authored Oct 28, 2020
1 parent cfd59ad commit cd302fd
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 0 deletions.
53 changes: 53 additions & 0 deletions controller/cache/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ func populateNodeInfo(un *unstructured.Unstructured, res *ResourceInfo) {
populateIngressInfo(un, res)
return
}
case "networking.istio.io":
switch gvk.Kind {
case "VirtualService":
populateIstioVirtualServiceInfo(un, res)
return
}
}

for k, v := range un.GetAnnotations() {
Expand Down Expand Up @@ -164,6 +170,53 @@ func populateIngressInfo(un *unstructured.Unstructured, res *ResourceInfo) {
res.NetworkingInfo = &v1alpha1.ResourceNetworkingInfo{TargetRefs: targets, Ingress: ingress, ExternalURLs: urls}
}

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

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

if hostName, ok, err := unstructured.NestedString(route, "destination", "host"); ok && err == nil {
hostSplits := strings.Split(hostName, ".")
serviceName := hostSplits[0]

var namespace string
if len(hostSplits) >= 2 {
namespace = hostSplits[1]
} else {
namespace = un.GetNamespace()
}

targetsMap[v1alpha1.ResourceRef{
Kind: kube.ServiceKind,
Name: serviceName,
Namespace: namespace,
}] = true
}
}
}
}
targets := make([]v1alpha1.ResourceRef, 0)
for target := range targetsMap {
targets = append(targets, target)
}

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

func populatePodInfo(un *unstructured.Unstructured, res *ResourceInfo) {
pod := v1.Pod{}
err := runtime.DefaultUnstructuredConverter.FromUnstructured(un.Object, &pod)
Expand Down
46 changes: 46 additions & 0 deletions controller/cache/info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,30 @@ var (
loadBalancer:
ingress:
- ip: 107.178.210.11`)

testIstioVirtualService = strToUnstructured(`
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: hello-world
namespace: demo
spec:
http:
- match:
- uri:
prefix: "/1"
route:
- destination:
host: service_full.demo.svc.cluster.local
- destination:
host: service_namespace.namespace
- match:
- uri:
prefix: "/2"
route:
- destination:
host: service
`)
)

func TestGetPodInfo(t *testing.T) {
Expand Down Expand Up @@ -166,6 +190,28 @@ func TestGetServiceInfo(t *testing.T) {
}, info.NetworkingInfo)
}

func TestGetIstioVirtualServiceInfo(t *testing.T) {
info := &ResourceInfo{}
populateNodeInfo(testIstioVirtualService, info)
assert.Equal(t, 0, len(info.Info))
assert.Equal(t, &v1alpha1.ResourceNetworkingInfo{
TargetRefs: []v1alpha1.ResourceRef{
{
Kind: kube.ServiceKind,
Name: "service_full",
Namespace: "demo"},
{
Kind: kube.ServiceKind,
Name: "service_namespace",
Namespace: "namespace"},
{
Kind: kube.ServiceKind,
Name: "service",
Namespace: "demo"},
},
}, info.NetworkingInfo)
}

func TestGetIngressInfo(t *testing.T) {
info := &ResourceInfo{}
populateNodeInfo(testIngress, info)
Expand Down

0 comments on commit cd302fd

Please sign in to comment.