github.com/kiali/kiali@v1.84.0/graph/telemetry/istio/appender/ambient_test.go (about) 1 package appender 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/require" 7 core_v1 "k8s.io/api/core/v1" 8 meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1" 9 10 "github.com/kiali/kiali/business" 11 "github.com/kiali/kiali/config" 12 "github.com/kiali/kiali/graph" 13 "github.com/kiali/kiali/kubernetes" 14 "github.com/kiali/kiali/kubernetes/kubetest" 15 ) 16 17 const ( 18 defaultCluster = "Kubernetes" 19 appName = "productpage" 20 appNamespace = "ambientNamespace" 21 ) 22 23 func setupWorkloadEntries(t *testing.T) *business.Layer { 24 k8spod1 := &core_v1.Pod{ 25 ObjectMeta: meta_v1.ObjectMeta{ 26 Name: "workloadA", 27 Namespace: appNamespace, 28 Labels: map[string]string{"apps": "workloadA", "version": "v1"}, 29 Annotations: map[string]string{"sidecar.istio.io/status": "{\"version\":\"\",\"initContainers\":[\"istio-init\",\"enable-core-dump\"],\"containers\":[\"istio-proxy\"],\"volumes\":[\"istio-envoy\",\"istio-certs\"]}"}}, 30 Spec: core_v1.PodSpec{ 31 Containers: []core_v1.Container{ 32 {Name: "workloadA", Image: "whatever"}, 33 }, 34 }} 35 k8spod2 := &core_v1.Pod{ 36 ObjectMeta: meta_v1.ObjectMeta{ 37 Name: "workloadB", 38 Namespace: appNamespace, 39 Labels: map[string]string{"apps": "workloadB", "version": "v2"}, 40 Annotations: map[string]string{"sidecar.istio.io/status": "{\"version\":\"\",\"initContainers\":[\"istio-init\",\"enable-core-dump\"],\"containers\":[\"istio-proxy\"],\"volumes\":[\"istio-envoy\",\"istio-certs\"]}"}}, 41 Spec: core_v1.PodSpec{ 42 Containers: []core_v1.Container{ 43 {Name: "workloadB", Image: "whatever"}, 44 }, 45 }} 46 k8spod3 := &core_v1.Pod{ 47 ObjectMeta: meta_v1.ObjectMeta{ 48 Name: "fake-istio-waypoint", 49 Namespace: appNamespace, 50 Labels: map[string]string{"apps": "fake-istio-waypoint", "version": "v1"}, 51 Annotations: map[string]string{"sidecar.istio.io/status": "{\"version\":\"\",\"initContainers\":[\"istio-init\",\"enable-core-dump\"],\"containers\":[\"istio-proxy\"],\"volumes\":[\"istio-envoy\",\"istio-certs\"]}"}}, 52 Spec: core_v1.PodSpec{ 53 Containers: []core_v1.Container{ 54 {Name: "fake-istio-waypoint", Image: "whatever"}, 55 }, 56 }} 57 k8spod4 := &core_v1.Pod{ 58 ObjectMeta: meta_v1.ObjectMeta{ 59 Name: "namespace-istio-waypoint", 60 Namespace: appNamespace, 61 Labels: map[string]string{"apps": "namespace-istio-waypoint", "version": "v1", config.WaypointLabel: config.WaypointLabelValue}, 62 Annotations: map[string]string{"sidecar.istio.io/status": "{\"version\":\"\",\"initContainers\":[\"istio-init\",\"enable-core-dump\"],\"containers\":[\"istio-proxy\"],\"volumes\":[\"istio-envoy\",\"istio-certs\"]}"}}, 63 Spec: core_v1.PodSpec{ 64 Containers: []core_v1.Container{ 65 {Name: "namespace-istio-waypoint", Image: "whatever"}, 66 }, 67 }} 68 69 ns := &core_v1.Namespace{ObjectMeta: meta_v1.ObjectMeta{Name: appNamespace}} 70 71 k8s := kubetest.NewFakeK8sClient(k8spod1, k8spod2, k8spod3, k8spod4, ns) 72 conf := config.NewConfig() 73 conf.ExternalServices.Istio.IstioAPIEnabled = false 74 conf.KubernetesConfig.ClusterName = defaultCluster 75 config.Set(conf) 76 77 business.SetupBusinessLayer(t, k8s, *conf) 78 k8sclients := make(map[string]kubernetes.ClientInterface) 79 k8sclients[defaultCluster] = k8s 80 businessLayer := business.NewWithBackends(k8sclients, k8sclients, nil, nil) 81 return businessLayer 82 } 83 84 func workloadEntriesTrafficMap() map[string]*graph.Node { 85 conf := config.NewConfig() 86 conf.KubernetesConfig.ClusterName = defaultCluster 87 config.Set(conf) 88 89 // VersionedApp graph 90 trafficMap := make(map[string]*graph.Node) 91 92 // 1 service, 3 workloads. v1 and v2 are workload entries. v3 is a waypoint proxy but with no labels. v4 has the right labels. 93 94 // Service node 95 n0, _ := graph.NewNode(defaultCluster, appNamespace, appName, appNamespace, "", "", "", graph.GraphTypeVersionedApp) 96 // v1 Workload 97 n1, _ := graph.NewNode(defaultCluster, appNamespace, appName, appNamespace, "ratings-v1", appName, "v1", graph.GraphTypeVersionedApp) 98 // v2 Workload 99 n2, _ := graph.NewNode(defaultCluster, appNamespace, appName, appNamespace, "ratings-v2", appName, "v2", graph.GraphTypeVersionedApp) 100 // v3 Workload with waypoint name 101 n3, _ := graph.NewNode(defaultCluster, appNamespace, appName, appNamespace, "fake-istio-waypoint", appName, "v2", graph.GraphTypeVersionedApp) 102 // v4 Waypoint proxy 103 n4, _ := graph.NewNode(defaultCluster, appNamespace, appName, appNamespace, "namespace-istio-waypoint", appName, "v2", graph.GraphTypeVersionedApp) 104 105 trafficMap[n0.ID] = n0 106 trafficMap[n1.ID] = n1 107 trafficMap[n2.ID] = n2 108 trafficMap[n3.ID] = n3 109 trafficMap[n4.ID] = n4 110 111 n0.AddEdge(n1).Metadata[graph.ProtocolKey] = graph.HTTP.Name 112 n0.AddEdge(n2).Metadata[graph.ProtocolKey] = graph.HTTP.Name 113 n0.AddEdge(n3).Metadata[graph.ProtocolKey] = graph.HTTP.Name 114 n0.AddEdge(n4).Metadata[graph.ProtocolKey] = graph.HTTP.Name 115 // Need to put some metadata in here to ensure it gets counted as a workload 116 117 return trafficMap 118 } 119 120 func TestRemoveWaypoint(t *testing.T) { 121 assert := require.New(t) 122 123 businessLayer := setupWorkloadEntries(t) 124 trafficMap := workloadEntriesTrafficMap() 125 126 globalInfo := graph.NewAppenderGlobalInfo() 127 globalInfo.Business = businessLayer 128 namespaceInfo := graph.NewAppenderNamespaceInfo(appNamespace) 129 130 assert.Equal(5, len(trafficMap)) 131 132 // Run the appender... 133 134 a := AmbientAppender{Waypoints: false} 135 a.AppendGraph(trafficMap, globalInfo, namespaceInfo) 136 137 assert.Equal(4, len(trafficMap)) 138 139 waypointWorkloadID, _, _ := graph.Id(defaultCluster, appNamespace, appName, appNamespace, "namespace-istio-waypoint", appName, "v2", graph.GraphTypeVersionedApp) 140 _, found := trafficMap[waypointWorkloadID] 141 assert.False(found) 142 } 143 144 func TestIsWaypoint(t *testing.T) { 145 assert := require.New(t) 146 147 businessLayer := setupWorkloadEntries(t) 148 trafficMap := workloadEntriesTrafficMap() 149 150 globalInfo := graph.NewAppenderGlobalInfo() 151 globalInfo.Business = businessLayer 152 namespaceInfo := graph.NewAppenderNamespaceInfo(appNamespace) 153 154 assert.Equal(5, len(trafficMap)) 155 156 // Run the appender... 157 158 a := AmbientAppender{Waypoints: true} 159 a.AppendGraph(trafficMap, globalInfo, namespaceInfo) 160 161 assert.Equal(5, len(trafficMap)) 162 163 waypointWorkloadID, _, _ := graph.Id(defaultCluster, appNamespace, appName, appNamespace, "namespace-istio-waypoint", appName, "v2", graph.GraphTypeVersionedApp) 164 waypointNode, found := trafficMap[waypointWorkloadID] 165 assert.True(found) 166 assert.Contains(waypointNode.Metadata, graph.IsWaypoint) 167 168 fakeWaypointWorkloadID, _, _ := graph.Id(defaultCluster, appNamespace, appName, appNamespace, "fake-istio-waypoint", appName, "v2", graph.GraphTypeVersionedApp) 169 fakeWaypointNode, found := trafficMap[fakeWaypointWorkloadID] 170 assert.True(found) 171 assert.NotContains(fakeWaypointNode.Metadata, graph.IsWaypoint) 172 }