github.com/kiali/kiali@v1.84.0/graph/telemetry/istio/appender/service_entry_test.go (about)

     1  package appender
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  	api_networking_v1beta1 "istio.io/api/networking/v1beta1"
     9  	networking_v1beta1 "istio.io/client-go/pkg/apis/networking/v1beta1"
    10  	core_v1 "k8s.io/api/core/v1"
    11  	meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    12  	"k8s.io/apimachinery/pkg/runtime"
    13  
    14  	"github.com/kiali/kiali/business"
    15  	"github.com/kiali/kiali/config"
    16  	"github.com/kiali/kiali/graph"
    17  	"github.com/kiali/kiali/kubernetes"
    18  	"github.com/kiali/kiali/kubernetes/kubetest"
    19  )
    20  
    21  func setupBusinessLayer(t *testing.T, istioObjects ...runtime.Object) *business.Layer {
    22  	conf := config.NewConfig()
    23  	conf.KubernetesConfig.ClusterName = config.DefaultClusterID
    24  	conf.ExternalServices.Istio.IstioAPIEnabled = false
    25  	config.Set(conf)
    26  
    27  	istioObjects = append(istioObjects,
    28  		&core_v1.Namespace{ObjectMeta: meta_v1.ObjectMeta{Name: "testNamespace"}},
    29  		&core_v1.Namespace{ObjectMeta: meta_v1.ObjectMeta{Name: "otherNamespace"}})
    30  	k8s := kubetest.NewFakeK8sClient(istioObjects...)
    31  	business.SetupBusinessLayer(t, k8s, *conf)
    32  	k8sclients := map[string]kubernetes.ClientInterface{
    33  		config.DefaultClusterID: kubetest.NewFakeK8sClient(
    34  			&core_v1.Namespace{ObjectMeta: meta_v1.ObjectMeta{Name: "testNamespace"}},
    35  			&core_v1.Namespace{ObjectMeta: meta_v1.ObjectMeta{Name: "otherNamespace"}},
    36  		),
    37  	}
    38  	businessLayer := business.NewWithBackends(k8sclients, k8sclients, nil, nil)
    39  	return businessLayer
    40  }
    41  
    42  func setupServiceEntries(t *testing.T, namespace string, exportTo []string) *business.Layer {
    43  	externalSE := &networking_v1beta1.ServiceEntry{}
    44  	externalSE.Name = "externalSE"
    45  	externalSE.Namespace = namespace
    46  	externalSE.Spec.ExportTo = exportTo
    47  	externalSE.Spec.Hosts = []string{
    48  		"host1.external.com",
    49  		"host2.external.com",
    50  	}
    51  	externalSE.Spec.Location = api_networking_v1beta1.ServiceEntry_MESH_EXTERNAL
    52  
    53  	internalSE := &networking_v1beta1.ServiceEntry{}
    54  	internalSE.Name = "internalSE"
    55  	internalSE.Namespace = namespace
    56  	internalSE.Spec.Hosts = []string{
    57  		"internalHost1",
    58  		"internalHost2.namespace.svc.cluster.local",
    59  	}
    60  	internalSE.Spec.Location = api_networking_v1beta1.ServiceEntry_MESH_INTERNAL
    61  
    62  	return setupBusinessLayer(t, externalSE, internalSE)
    63  }
    64  
    65  func serviceEntriesTrafficMap() map[string]*graph.Node {
    66  	// VersionedApp graph
    67  	trafficMap := make(map[string]*graph.Node)
    68  
    69  	// testNode
    70  	n0, _ := graph.NewNode(config.DefaultClusterID, graph.Unknown, "test", "testNamespace", "test-v1", "test", "v1", graph.GraphTypeVersionedApp)
    71  
    72  	// NotSE serviceNode
    73  	n1, _ := graph.NewNode(config.DefaultClusterID, "testNamespace", "NotSE", "testNamespace", "", "", "", graph.GraphTypeVersionedApp)
    74  
    75  	// NotSE appNode
    76  	n2, _ := graph.NewNode(config.DefaultClusterID, "testNamespace", "NotSE", "testNamespace", "NotSE-v1", "NotSE", "v1", graph.GraphTypeVersionedApp)
    77  
    78  	// externalSE host1 serviceNode
    79  	n3, _ := graph.NewNode(config.DefaultClusterID, "testNamespace", "host1.external.com", "testNamespace", "", "", "", graph.GraphTypeVersionedApp)
    80  	n3.Metadata = graph.NewMetadata()
    81  	destServices := graph.NewDestServicesMetadata()
    82  	destService := graph.ServiceName{Namespace: n3.Namespace, Name: n3.Service}
    83  	destServices[destService.Key()] = destService
    84  	n3.Metadata[graph.DestServices] = destServices
    85  
    86  	// externalSE host2 serviceNode
    87  	n4, _ := graph.NewNode(config.DefaultClusterID, "testNamespace", "host2.external.com", "testNamespace", "", "", "", graph.GraphTypeVersionedApp)
    88  	n4.Metadata = graph.NewMetadata()
    89  	destServices = graph.NewDestServicesMetadata()
    90  	destService = graph.ServiceName{Namespace: n4.Namespace, Name: n4.Service}
    91  	destServices[destService.Key()] = destService
    92  	n4.Metadata[graph.DestServices] = destServices
    93  
    94  	// non-service-entry (ALLOW_ANY) serviceNode
    95  	n5, _ := graph.NewNode(config.DefaultClusterID, "testNamespace", "hostX.external.com", "testNamespace", "", "", "", graph.GraphTypeVersionedApp)
    96  
    97  	// internalSE host1 serviceNode
    98  	n6, _ := graph.NewNode(config.DefaultClusterID, "testNamespace", "internalHost1", "testNamespace", "", "", "", graph.GraphTypeVersionedApp)
    99  	n6.Metadata = graph.NewMetadata()
   100  	destServices = graph.NewDestServicesMetadata()
   101  	destService = graph.ServiceName{Namespace: n6.Namespace, Name: n6.Service}
   102  	destServices[destService.Key()] = destService
   103  	n6.Metadata[graph.DestServices] = destServices
   104  
   105  	// internalSE host2 serviceNode (test prefix)
   106  	n7, _ := graph.NewNode(config.DefaultClusterID, "testNamespace", "internalHost2", "testNamespace", "", "", "", graph.GraphTypeVersionedApp)
   107  	n7.Metadata = graph.NewMetadata()
   108  	destServices = graph.NewDestServicesMetadata()
   109  	destService = graph.ServiceName{Namespace: n7.Namespace, Name: n7.Service}
   110  	destServices[destService.Key()] = destService
   111  	n7.Metadata[graph.DestServices] = destServices
   112  
   113  	trafficMap[n0.ID] = n0
   114  	trafficMap[n1.ID] = n1
   115  	trafficMap[n2.ID] = n2
   116  	trafficMap[n3.ID] = n3
   117  	trafficMap[n4.ID] = n4
   118  	trafficMap[n5.ID] = n5
   119  	trafficMap[n6.ID] = n6
   120  	trafficMap[n7.ID] = n7
   121  
   122  	n0.AddEdge(n1).Metadata[graph.ProtocolKey] = graph.HTTP.Name
   123  	n1.AddEdge(n2).Metadata[graph.ProtocolKey] = graph.HTTP.Name
   124  	n2.AddEdge(n3).Metadata[graph.ProtocolKey] = graph.HTTP.Name
   125  	n2.AddEdge(n4).Metadata[graph.ProtocolKey] = graph.HTTP.Name
   126  	n2.AddEdge(n5).Metadata[graph.ProtocolKey] = graph.HTTP.Name
   127  	n2.AddEdge(n6).Metadata[graph.ProtocolKey] = graph.HTTP.Name
   128  	n2.AddEdge(n7).Metadata[graph.ProtocolKey] = graph.TCP.Name
   129  
   130  	return trafficMap
   131  }
   132  
   133  func TestServiceEntry(t *testing.T) {
   134  	assert := assert.New(t)
   135  
   136  	businessLayer := setupServiceEntries(t, "testNamespace", nil)
   137  	trafficMap := serviceEntriesTrafficMap()
   138  
   139  	assert.Equal(8, len(trafficMap))
   140  
   141  	testID, _, _ := graph.Id(config.DefaultClusterID, graph.Unknown, "test", "testNamespace", "test-v1", "test", "v1", graph.GraphTypeVersionedApp)
   142  	testNode, found0 := trafficMap[testID]
   143  	assert.Equal(true, found0)
   144  	assert.Equal(1, len(testNode.Edges))
   145  	assert.Equal(nil, testNode.Metadata[graph.IsServiceEntry])
   146  
   147  	notSEServiceID, _, _ := graph.Id(config.DefaultClusterID, "testNamespace", "NotSE", "testNamespace", "", "", "", graph.GraphTypeVersionedApp)
   148  	notSEServiceNode, found1 := trafficMap[notSEServiceID]
   149  	assert.Equal(true, found1)
   150  	assert.Equal(1, len(notSEServiceNode.Edges))
   151  	assert.Equal(nil, notSEServiceNode.Metadata[graph.IsServiceEntry])
   152  
   153  	notSEAppID, _, _ := graph.Id(config.DefaultClusterID, "testNamespace", "NotSE", "testNamespace", "NotSE-v1", "NotSE", "v1", graph.GraphTypeVersionedApp)
   154  	notSEAppNode, found2 := trafficMap[notSEAppID]
   155  	assert.Equal(true, found2)
   156  	assert.Equal(5, len(notSEAppNode.Edges))
   157  	assert.Equal(nil, notSEAppNode.Metadata[graph.IsServiceEntry])
   158  
   159  	externalSEHost1ServiceID, _, _ := graph.Id(config.DefaultClusterID, "testNamespace", "host1.external.com", "testNamespace", "", "", "", graph.GraphTypeVersionedApp)
   160  	externalSEHost1ServiceNode, found3 := trafficMap[externalSEHost1ServiceID]
   161  	assert.Equal(true, found3)
   162  	assert.Equal(0, len(externalSEHost1ServiceNode.Edges))
   163  	assert.Equal(nil, externalSEHost1ServiceNode.Metadata[graph.IsServiceEntry])
   164  
   165  	externalSEHost2ServiceID, _, _ := graph.Id(config.DefaultClusterID, "testNamespace", "host2.external.com", "testNamespace", "", "", "", graph.GraphTypeVersionedApp)
   166  	externalSEHost2ServiceNode, found4 := trafficMap[externalSEHost2ServiceID]
   167  	assert.Equal(true, found4)
   168  	assert.Equal(0, len(externalSEHost2ServiceNode.Edges))
   169  	assert.Equal(nil, externalSEHost2ServiceNode.Metadata[graph.IsServiceEntry])
   170  
   171  	externalHostXServiceID, _, _ := graph.Id(config.DefaultClusterID, "testNamespace", "hostX.external.com", "testNamespace", "", "", "", graph.GraphTypeVersionedApp)
   172  	externalHostXServiceNode, found5 := trafficMap[externalHostXServiceID]
   173  	assert.Equal(true, found5)
   174  	assert.Equal(0, len(externalHostXServiceNode.Edges))
   175  	assert.Equal(nil, externalHostXServiceNode.Metadata[graph.IsServiceEntry])
   176  
   177  	internalSEHost1ServiceID, _, _ := graph.Id(config.DefaultClusterID, "testNamespace", "internalHost1", "testNamespace", "", "", "", graph.GraphTypeVersionedApp)
   178  	internalSEHost1ServiceNode, found6 := trafficMap[internalSEHost1ServiceID]
   179  	assert.Equal(true, found6)
   180  	assert.Equal(0, len(internalSEHost1ServiceNode.Edges))
   181  	assert.Equal(nil, internalSEHost1ServiceNode.Metadata[graph.IsServiceEntry])
   182  
   183  	internalSEHost2ServiceID, _, _ := graph.Id(config.DefaultClusterID, "testNamespace", "internalHost2", "testNamespace", "", "", "", graph.GraphTypeVersionedApp)
   184  	internalSEHost2ServiceNode, found7 := trafficMap[internalSEHost2ServiceID]
   185  	assert.Equal(true, found7)
   186  	assert.Equal(0, len(internalSEHost2ServiceNode.Edges))
   187  	assert.Equal(nil, internalSEHost2ServiceNode.Metadata[graph.IsServiceEntry])
   188  
   189  	globalInfo := graph.NewAppenderGlobalInfo()
   190  	globalInfo.Business = businessLayer
   191  	namespaceInfo := graph.NewAppenderNamespaceInfo("testNamespace")
   192  	key := graph.GetClusterSensitiveKey(config.DefaultClusterID, "testNamespace")
   193  
   194  	// Run the appender...
   195  	a := ServiceEntryAppender{
   196  		AccessibleNamespaces: graph.AccessibleNamespaces{
   197  			key: &graph.AccessibleNamespace{
   198  				Cluster:           config.DefaultClusterID,
   199  				CreationTimestamp: time.Now(),
   200  				Name:              "testNamespace",
   201  			}}}
   202  	a.AppendGraph(trafficMap, globalInfo, namespaceInfo)
   203  
   204  	assert.Equal(6, len(trafficMap))
   205  
   206  	testID, _, _ = graph.Id(config.DefaultClusterID, graph.Unknown, "test", "testNamespace", "test-v1", "test", "v1", graph.GraphTypeVersionedApp)
   207  	testNode, found0 = trafficMap[testID]
   208  	assert.Equal(true, found0)
   209  	assert.Equal(1, len(testNode.Edges))
   210  	assert.Equal(nil, testNode.Metadata[graph.IsServiceEntry])
   211  
   212  	notSEServiceID, _, _ = graph.Id(config.DefaultClusterID, "testNamespace", "NotSE", "testNamespace", "", "", "", graph.GraphTypeVersionedApp)
   213  	notSEServiceNode, found1 = trafficMap[notSEServiceID]
   214  	assert.Equal(true, found1)
   215  	assert.Equal(1, len(notSEServiceNode.Edges))
   216  	assert.Equal(nil, notSEServiceNode.Metadata[graph.IsServiceEntry])
   217  
   218  	notSEAppID, _, _ = graph.Id(config.DefaultClusterID, "testNamespace", "NotSE", "testNamespace", "NotSE-v1", "NotSE", "v1", graph.GraphTypeVersionedApp)
   219  	notSEAppNode, found2 = trafficMap[notSEAppID]
   220  	assert.Equal(true, found2)
   221  	assert.Equal(4, len(notSEAppNode.Edges))
   222  	assert.Equal(nil, notSEAppNode.Metadata[graph.IsServiceEntry])
   223  
   224  	externalSEServiceEntryID, _, _ := graph.Id(config.DefaultClusterID, "testNamespace", "externalSE", "testNamespace", "", "", "", graph.GraphTypeVersionedApp)
   225  	externalSEServiceEntryNode, found3 := trafficMap[externalSEServiceEntryID]
   226  	assert.Equal(true, found3)
   227  	assert.Equal(0, len(externalSEServiceEntryNode.Edges))
   228  	assert.Equal("MESH_EXTERNAL", externalSEServiceEntryNode.Metadata[graph.IsServiceEntry].(*graph.SEInfo).Location)
   229  	externalHosts := externalSEServiceEntryNode.Metadata[graph.IsServiceEntry].(*graph.SEInfo).Hosts
   230  	assert.Equal("host1.external.com", externalHosts[0])
   231  	assert.Equal("host2.external.com", externalHosts[1])
   232  	assert.Equal(2, len(externalSEServiceEntryNode.Metadata[graph.DestServices].(graph.DestServicesMetadata)))
   233  
   234  	externalHostXServiceID, _, _ = graph.Id(config.DefaultClusterID, "testNamespace", "hostX.external.com", "testNamespace", "", "", "", graph.GraphTypeVersionedApp)
   235  	externalHostXServiceNode, found4 = trafficMap[externalHostXServiceID]
   236  	assert.Equal(true, found4)
   237  	assert.Equal(0, len(externalHostXServiceNode.Edges))
   238  	assert.Equal(nil, externalHostXServiceNode.Metadata[graph.IsServiceEntry])
   239  
   240  	internalSEServiceEntryID, _, _ := graph.Id(config.DefaultClusterID, "testNamespace", "internalSE", "testNamespace", "", "", "", graph.GraphTypeVersionedApp)
   241  	internalSEServiceEntryNode, found5 := trafficMap[internalSEServiceEntryID]
   242  	assert.Equal(true, found5)
   243  	assert.Equal(0, len(internalSEServiceEntryNode.Edges))
   244  	assert.Equal("MESH_INTERNAL", internalSEServiceEntryNode.Metadata[graph.IsServiceEntry].(*graph.SEInfo).Location)
   245  	internalHosts := externalSEServiceEntryNode.Metadata[graph.IsServiceEntry].(*graph.SEInfo).Hosts
   246  	assert.Equal("host1.external.com", internalHosts[0])
   247  	assert.Equal("host2.external.com", internalHosts[1])
   248  	assert.Equal(2, len(internalSEServiceEntryNode.Metadata[graph.DestServices].(graph.DestServicesMetadata)))
   249  }
   250  
   251  func TestServiceEntryExportAll(t *testing.T) {
   252  	assert := assert.New(t)
   253  
   254  	businessLayer := setupServiceEntries(t, "testNamespace", []string{"*"})
   255  	trafficMap := serviceEntriesTrafficMap()
   256  
   257  	assert.Equal(8, len(trafficMap))
   258  
   259  	testID, _, _ := graph.Id(config.DefaultClusterID, graph.Unknown, "test", "testNamespace", "test-v1", "test", "v1", graph.GraphTypeVersionedApp)
   260  	testNode, found0 := trafficMap[testID]
   261  	assert.Equal(true, found0)
   262  	assert.Equal(1, len(testNode.Edges))
   263  	assert.Equal(nil, testNode.Metadata[graph.IsServiceEntry])
   264  
   265  	notSEServiceID, _, _ := graph.Id(config.DefaultClusterID, "testNamespace", "NotSE", "testNamespace", "", "", "", graph.GraphTypeVersionedApp)
   266  	notSEServiceNode, found1 := trafficMap[notSEServiceID]
   267  	assert.Equal(true, found1)
   268  	assert.Equal(1, len(notSEServiceNode.Edges))
   269  	assert.Equal(nil, notSEServiceNode.Metadata[graph.IsServiceEntry])
   270  
   271  	notSEAppID, _, _ := graph.Id(config.DefaultClusterID, "testNamespace", "NotSE", "testNamespace", "NotSE-v1", "NotSE", "v1", graph.GraphTypeVersionedApp)
   272  	notSEAppNode, found2 := trafficMap[notSEAppID]
   273  	assert.Equal(true, found2)
   274  	assert.Equal(5, len(notSEAppNode.Edges))
   275  	assert.Equal(nil, notSEAppNode.Metadata[graph.IsServiceEntry])
   276  
   277  	externalSEHost1ServiceID, _, _ := graph.Id(config.DefaultClusterID, "testNamespace", "host1.external.com", "testNamespace", "", "", "", graph.GraphTypeVersionedApp)
   278  	externalSEHost1ServiceNode, found3 := trafficMap[externalSEHost1ServiceID]
   279  	assert.Equal(true, found3)
   280  	assert.Equal(0, len(externalSEHost1ServiceNode.Edges))
   281  	assert.Equal(nil, externalSEHost1ServiceNode.Metadata[graph.IsServiceEntry])
   282  
   283  	externalSEHost2ServiceID, _, _ := graph.Id(config.DefaultClusterID, "testNamespace", "host2.external.com", "testNamespace", "", "", "", graph.GraphTypeVersionedApp)
   284  	externalSEHost2ServiceNode, found4 := trafficMap[externalSEHost2ServiceID]
   285  	assert.Equal(true, found4)
   286  	assert.Equal(0, len(externalSEHost2ServiceNode.Edges))
   287  	assert.Equal(nil, externalSEHost2ServiceNode.Metadata[graph.IsServiceEntry])
   288  
   289  	externalHostXServiceID, _, _ := graph.Id(config.DefaultClusterID, "testNamespace", "hostX.external.com", "testNamespace", "", "", "", graph.GraphTypeVersionedApp)
   290  	externalHostXServiceNode, found5 := trafficMap[externalHostXServiceID]
   291  	assert.Equal(true, found5)
   292  	assert.Equal(0, len(externalHostXServiceNode.Edges))
   293  	assert.Equal(nil, externalHostXServiceNode.Metadata[graph.IsServiceEntry])
   294  
   295  	internalSEHost1ServiceID, _, _ := graph.Id(config.DefaultClusterID, "testNamespace", "internalHost1", "testNamespace", "", "", "", graph.GraphTypeVersionedApp)
   296  	internalSEHost1ServiceNode, found6 := trafficMap[internalSEHost1ServiceID]
   297  	assert.Equal(true, found6)
   298  	assert.Equal(0, len(internalSEHost1ServiceNode.Edges))
   299  	assert.Equal(nil, internalSEHost1ServiceNode.Metadata[graph.IsServiceEntry])
   300  
   301  	internalSEHost2ServiceID, _, _ := graph.Id(config.DefaultClusterID, "testNamespace", "internalHost2", "testNamespace", "", "", "", graph.GraphTypeVersionedApp)
   302  	internalSEHost2ServiceNode, found7 := trafficMap[internalSEHost2ServiceID]
   303  	assert.Equal(true, found7)
   304  	assert.Equal(0, len(internalSEHost2ServiceNode.Edges))
   305  	assert.Equal(nil, internalSEHost2ServiceNode.Metadata[graph.IsServiceEntry])
   306  
   307  	globalInfo := graph.NewAppenderGlobalInfo()
   308  	globalInfo.Business = businessLayer
   309  	namespaceInfo := graph.NewAppenderNamespaceInfo("testNamespace")
   310  	key := graph.GetClusterSensitiveKey(config.DefaultClusterID, "testNamespace")
   311  
   312  	// Run the appender...
   313  	a := ServiceEntryAppender{
   314  		AccessibleNamespaces: graph.AccessibleNamespaces{
   315  			key: &graph.AccessibleNamespace{
   316  				Cluster:           config.DefaultClusterID,
   317  				CreationTimestamp: time.Now(),
   318  				Name:              "testNamespace",
   319  			}}}
   320  	a.AppendGraph(trafficMap, globalInfo, namespaceInfo)
   321  
   322  	assert.Equal(6, len(trafficMap))
   323  
   324  	testID, _, _ = graph.Id(config.DefaultClusterID, graph.Unknown, "test", "testNamespace", "test-v1", "test", "v1", graph.GraphTypeVersionedApp)
   325  	testNode, found0 = trafficMap[testID]
   326  	assert.Equal(true, found0)
   327  	assert.Equal(1, len(testNode.Edges))
   328  	assert.Equal(nil, testNode.Metadata[graph.IsServiceEntry])
   329  
   330  	notSEServiceID, _, _ = graph.Id(config.DefaultClusterID, "testNamespace", "NotSE", "testNamespace", "", "", "", graph.GraphTypeVersionedApp)
   331  	notSEServiceNode, found1 = trafficMap[notSEServiceID]
   332  	assert.Equal(true, found1)
   333  	assert.Equal(1, len(notSEServiceNode.Edges))
   334  	assert.Equal(nil, notSEServiceNode.Metadata[graph.IsServiceEntry])
   335  
   336  	notSEAppID, _, _ = graph.Id(config.DefaultClusterID, "testNamespace", "NotSE", "testNamespace", "NotSE-v1", "NotSE", "v1", graph.GraphTypeVersionedApp)
   337  	notSEAppNode, found2 = trafficMap[notSEAppID]
   338  	assert.Equal(true, found2)
   339  	assert.Equal(4, len(notSEAppNode.Edges))
   340  	assert.Equal(nil, notSEAppNode.Metadata[graph.IsServiceEntry])
   341  
   342  	externalSEServiceEntryID, _, _ := graph.Id(config.DefaultClusterID, "testNamespace", "externalSE", "testNamespace", "", "", "", graph.GraphTypeVersionedApp)
   343  	externalSEServiceEntryNode, found3 := trafficMap[externalSEServiceEntryID]
   344  	assert.Equal(true, found3)
   345  	assert.Equal(0, len(externalSEServiceEntryNode.Edges))
   346  	assert.Equal("MESH_EXTERNAL", externalSEServiceEntryNode.Metadata[graph.IsServiceEntry].(*graph.SEInfo).Location)
   347  	assert.Equal(2, len(externalSEServiceEntryNode.Metadata[graph.DestServices].(graph.DestServicesMetadata)))
   348  
   349  	externalHostXServiceID, _, _ = graph.Id(config.DefaultClusterID, "testNamespace", "hostX.external.com", "testNamespace", "", "", "", graph.GraphTypeVersionedApp)
   350  	externalHostXServiceNode, found4 = trafficMap[externalHostXServiceID]
   351  	assert.Equal(true, found4)
   352  	assert.Equal(0, len(externalHostXServiceNode.Edges))
   353  	assert.Equal(nil, externalHostXServiceNode.Metadata[graph.IsServiceEntry])
   354  
   355  	internalSEServiceEntryID, _, _ := graph.Id(config.DefaultClusterID, "testNamespace", "internalSE", "testNamespace", "", "", "", graph.GraphTypeVersionedApp)
   356  	internalSEServiceEntryNode, found5 := trafficMap[internalSEServiceEntryID]
   357  	assert.Equal(true, found5)
   358  	assert.Equal(0, len(internalSEServiceEntryNode.Edges))
   359  	assert.Equal("MESH_INTERNAL", internalSEServiceEntryNode.Metadata[graph.IsServiceEntry].(*graph.SEInfo).Location)
   360  	assert.Equal(2, len(internalSEServiceEntryNode.Metadata[graph.DestServices].(graph.DestServicesMetadata)))
   361  }
   362  
   363  func TestServiceEntryExportNamespaceFound(t *testing.T) {
   364  	assert := assert.New(t)
   365  
   366  	businessLayer := setupServiceEntries(t, "testNamespace", []string{"fooNamespace", "testNamespace"})
   367  	trafficMap := serviceEntriesTrafficMap()
   368  
   369  	assert.Equal(8, len(trafficMap))
   370  
   371  	testID, _, _ := graph.Id(config.DefaultClusterID, graph.Unknown, "test", "testNamespace", "test-v1", "test", "v1", graph.GraphTypeVersionedApp)
   372  	testNode, found0 := trafficMap[testID]
   373  	assert.Equal(true, found0)
   374  	assert.Equal(1, len(testNode.Edges))
   375  	assert.Equal(nil, testNode.Metadata[graph.IsServiceEntry])
   376  
   377  	notSEServiceID, _, _ := graph.Id(config.DefaultClusterID, "testNamespace", "NotSE", "testNamespace", "", "", "", graph.GraphTypeVersionedApp)
   378  	notSEServiceNode, found1 := trafficMap[notSEServiceID]
   379  	assert.Equal(true, found1)
   380  	assert.Equal(1, len(notSEServiceNode.Edges))
   381  	assert.Equal(nil, notSEServiceNode.Metadata[graph.IsServiceEntry])
   382  
   383  	notSEAppID, _, _ := graph.Id(config.DefaultClusterID, "testNamespace", "NotSE", "testNamespace", "NotSE-v1", "NotSE", "v1", graph.GraphTypeVersionedApp)
   384  	notSEAppNode, found2 := trafficMap[notSEAppID]
   385  	assert.Equal(true, found2)
   386  	assert.Equal(5, len(notSEAppNode.Edges))
   387  	assert.Equal(nil, notSEAppNode.Metadata[graph.IsServiceEntry])
   388  
   389  	externalSEHost1ServiceID, _, _ := graph.Id(config.DefaultClusterID, "testNamespace", "host1.external.com", "testNamespace", "", "", "", graph.GraphTypeVersionedApp)
   390  	externalSEHost1ServiceNode, found3 := trafficMap[externalSEHost1ServiceID]
   391  	assert.Equal(true, found3)
   392  	assert.Equal(0, len(externalSEHost1ServiceNode.Edges))
   393  	assert.Equal(nil, externalSEHost1ServiceNode.Metadata[graph.IsServiceEntry])
   394  
   395  	externalSEHost2ServiceID, _, _ := graph.Id(config.DefaultClusterID, "testNamespace", "host2.external.com", "testNamespace", "", "", "", graph.GraphTypeVersionedApp)
   396  	externalSEHost2ServiceNode, found4 := trafficMap[externalSEHost2ServiceID]
   397  	assert.Equal(true, found4)
   398  	assert.Equal(0, len(externalSEHost2ServiceNode.Edges))
   399  	assert.Equal(nil, externalSEHost2ServiceNode.Metadata[graph.IsServiceEntry])
   400  
   401  	externalHostXServiceID, _, _ := graph.Id(config.DefaultClusterID, "testNamespace", "hostX.external.com", "testNamespace", "", "", "", graph.GraphTypeVersionedApp)
   402  	externalHostXServiceNode, found5 := trafficMap[externalHostXServiceID]
   403  	assert.Equal(true, found5)
   404  	assert.Equal(0, len(externalHostXServiceNode.Edges))
   405  	assert.Equal(nil, externalHostXServiceNode.Metadata[graph.IsServiceEntry])
   406  
   407  	internalSEHost1ServiceID, _, _ := graph.Id(config.DefaultClusterID, "testNamespace", "internalHost1", "testNamespace", "", "", "", graph.GraphTypeVersionedApp)
   408  	internalSEHost1ServiceNode, found6 := trafficMap[internalSEHost1ServiceID]
   409  	assert.Equal(true, found6)
   410  	assert.Equal(0, len(internalSEHost1ServiceNode.Edges))
   411  	assert.Equal(nil, internalSEHost1ServiceNode.Metadata[graph.IsServiceEntry])
   412  
   413  	internalSEHost2ServiceID, _, _ := graph.Id(config.DefaultClusterID, "testNamespace", "internalHost2", "testNamespace", "", "", "", graph.GraphTypeVersionedApp)
   414  	internalSEHost2ServiceNode, found7 := trafficMap[internalSEHost2ServiceID]
   415  	assert.Equal(true, found7)
   416  	assert.Equal(0, len(internalSEHost2ServiceNode.Edges))
   417  	assert.Equal(nil, internalSEHost2ServiceNode.Metadata[graph.IsServiceEntry])
   418  
   419  	globalInfo := graph.NewAppenderGlobalInfo()
   420  	globalInfo.Business = businessLayer
   421  	namespaceInfo := graph.NewAppenderNamespaceInfo("testNamespace")
   422  	key := graph.GetClusterSensitiveKey(config.DefaultClusterID, "testNamespace")
   423  
   424  	// Run the appender...
   425  	a := ServiceEntryAppender{
   426  		AccessibleNamespaces: graph.AccessibleNamespaces{
   427  			key: &graph.AccessibleNamespace{
   428  				Cluster:           config.DefaultClusterID,
   429  				CreationTimestamp: time.Now(),
   430  				Name:              "testNamespace",
   431  			}}}
   432  	a.AppendGraph(trafficMap, globalInfo, namespaceInfo)
   433  
   434  	assert.Equal(6, len(trafficMap))
   435  
   436  	testID, _, _ = graph.Id(config.DefaultClusterID, graph.Unknown, "test", "testNamespace", "test-v1", "test", "v1", graph.GraphTypeVersionedApp)
   437  	testNode, found0 = trafficMap[testID]
   438  	assert.Equal(true, found0)
   439  	assert.Equal(1, len(testNode.Edges))
   440  	assert.Equal(nil, testNode.Metadata[graph.IsServiceEntry])
   441  
   442  	notSEServiceID, _, _ = graph.Id(config.DefaultClusterID, "testNamespace", "NotSE", "testNamespace", "", "", "", graph.GraphTypeVersionedApp)
   443  	notSEServiceNode, found1 = trafficMap[notSEServiceID]
   444  	assert.Equal(true, found1)
   445  	assert.Equal(1, len(notSEServiceNode.Edges))
   446  	assert.Equal(nil, notSEServiceNode.Metadata[graph.IsServiceEntry])
   447  
   448  	notSEAppID, _, _ = graph.Id(config.DefaultClusterID, "testNamespace", "NotSE", "testNamespace", "NotSE-v1", "NotSE", "v1", graph.GraphTypeVersionedApp)
   449  	notSEAppNode, found2 = trafficMap[notSEAppID]
   450  	assert.Equal(true, found2)
   451  	assert.Equal(4, len(notSEAppNode.Edges))
   452  	assert.Equal(nil, notSEAppNode.Metadata[graph.IsServiceEntry])
   453  
   454  	externalSEServiceEntryID, _, _ := graph.Id(config.DefaultClusterID, "testNamespace", "externalSE", "testNamespace", "", "", "", graph.GraphTypeVersionedApp)
   455  	externalSEServiceEntryNode, found3 := trafficMap[externalSEServiceEntryID]
   456  	assert.Equal(true, found3)
   457  	assert.Equal(0, len(externalSEServiceEntryNode.Edges))
   458  	assert.Equal("MESH_EXTERNAL", externalSEServiceEntryNode.Metadata[graph.IsServiceEntry].(*graph.SEInfo).Location)
   459  	assert.Equal(2, len(externalSEServiceEntryNode.Metadata[graph.DestServices].(graph.DestServicesMetadata)))
   460  
   461  	externalHostXServiceID, _, _ = graph.Id(config.DefaultClusterID, "testNamespace", "hostX.external.com", "testNamespace", "", "", "", graph.GraphTypeVersionedApp)
   462  	externalHostXServiceNode, found4 = trafficMap[externalHostXServiceID]
   463  	assert.Equal(true, found4)
   464  	assert.Equal(0, len(externalHostXServiceNode.Edges))
   465  	assert.Equal(nil, externalHostXServiceNode.Metadata[graph.IsServiceEntry])
   466  
   467  	internalSEServiceEntryID, _, _ := graph.Id(config.DefaultClusterID, "testNamespace", "internalSE", "testNamespace", "", "", "", graph.GraphTypeVersionedApp)
   468  	internalSEServiceEntryNode, found5 := trafficMap[internalSEServiceEntryID]
   469  	assert.Equal(true, found5)
   470  	assert.Equal(0, len(internalSEServiceEntryNode.Edges))
   471  	assert.Equal("MESH_INTERNAL", internalSEServiceEntryNode.Metadata[graph.IsServiceEntry].(*graph.SEInfo).Location)
   472  	assert.Equal(2, len(internalSEServiceEntryNode.Metadata[graph.DestServices].(graph.DestServicesMetadata)))
   473  }
   474  
   475  func TestServiceEntryExportDefinitionNamespace(t *testing.T) {
   476  	assert := assert.New(t)
   477  
   478  	businessLayer := setupServiceEntries(t, "testNamespace", []string{"."})
   479  	trafficMap := serviceEntriesTrafficMap()
   480  
   481  	assert.Equal(8, len(trafficMap))
   482  
   483  	testID, _, _ := graph.Id(config.DefaultClusterID, graph.Unknown, "test", "testNamespace", "test-v1", "test", "v1", graph.GraphTypeVersionedApp)
   484  	testNode, found0 := trafficMap[testID]
   485  	assert.Equal(true, found0)
   486  	assert.Equal(1, len(testNode.Edges))
   487  	assert.Equal(nil, testNode.Metadata[graph.IsServiceEntry])
   488  
   489  	notSEServiceID, _, _ := graph.Id(config.DefaultClusterID, "testNamespace", "NotSE", "testNamespace", "", "", "", graph.GraphTypeVersionedApp)
   490  	notSEServiceNode, found1 := trafficMap[notSEServiceID]
   491  	assert.Equal(true, found1)
   492  	assert.Equal(1, len(notSEServiceNode.Edges))
   493  	assert.Equal(nil, notSEServiceNode.Metadata[graph.IsServiceEntry])
   494  
   495  	notSEAppID, _, _ := graph.Id(config.DefaultClusterID, "testNamespace", "NotSE", "testNamespace", "NotSE-v1", "NotSE", "v1", graph.GraphTypeVersionedApp)
   496  	notSEAppNode, found2 := trafficMap[notSEAppID]
   497  	assert.Equal(true, found2)
   498  	assert.Equal(5, len(notSEAppNode.Edges))
   499  	assert.Equal(nil, notSEAppNode.Metadata[graph.IsServiceEntry])
   500  
   501  	externalSEHost1ServiceID, _, _ := graph.Id(config.DefaultClusterID, "testNamespace", "host1.external.com", "testNamespace", "", "", "", graph.GraphTypeVersionedApp)
   502  	externalSEHost1ServiceNode, found3 := trafficMap[externalSEHost1ServiceID]
   503  	assert.Equal(true, found3)
   504  	assert.Equal(0, len(externalSEHost1ServiceNode.Edges))
   505  	assert.Equal(nil, externalSEHost1ServiceNode.Metadata[graph.IsServiceEntry])
   506  
   507  	externalSEHost2ServiceID, _, _ := graph.Id(config.DefaultClusterID, "testNamespace", "host2.external.com", "testNamespace", "", "", "", graph.GraphTypeVersionedApp)
   508  	externalSEHost2ServiceNode, found4 := trafficMap[externalSEHost2ServiceID]
   509  	assert.Equal(true, found4)
   510  	assert.Equal(0, len(externalSEHost2ServiceNode.Edges))
   511  	assert.Equal(nil, externalSEHost2ServiceNode.Metadata[graph.IsServiceEntry])
   512  
   513  	externalHostXServiceID, _, _ := graph.Id(config.DefaultClusterID, "testNamespace", "hostX.external.com", "testNamespace", "", "", "", graph.GraphTypeVersionedApp)
   514  	externalHostXServiceNode, found5 := trafficMap[externalHostXServiceID]
   515  	assert.Equal(true, found5)
   516  	assert.Equal(0, len(externalHostXServiceNode.Edges))
   517  	assert.Equal(nil, externalHostXServiceNode.Metadata[graph.IsServiceEntry])
   518  
   519  	internalSEHost1ServiceID, _, _ := graph.Id(config.DefaultClusterID, "testNamespace", "internalHost1", "testNamespace", "", "", "", graph.GraphTypeVersionedApp)
   520  	internalSEHost1ServiceNode, found6 := trafficMap[internalSEHost1ServiceID]
   521  	assert.Equal(true, found6)
   522  	assert.Equal(0, len(internalSEHost1ServiceNode.Edges))
   523  	assert.Equal(nil, internalSEHost1ServiceNode.Metadata[graph.IsServiceEntry])
   524  
   525  	internalSEHost2ServiceID, _, _ := graph.Id(config.DefaultClusterID, "testNamespace", "internalHost2", "testNamespace", "", "", "", graph.GraphTypeVersionedApp)
   526  	internalSEHost2ServiceNode, found7 := trafficMap[internalSEHost2ServiceID]
   527  	assert.Equal(true, found7)
   528  	assert.Equal(0, len(internalSEHost2ServiceNode.Edges))
   529  	assert.Equal(nil, internalSEHost2ServiceNode.Metadata[graph.IsServiceEntry])
   530  
   531  	globalInfo := graph.NewAppenderGlobalInfo()
   532  	globalInfo.Business = businessLayer
   533  	namespaceInfo := graph.NewAppenderNamespaceInfo("testNamespace")
   534  	key := graph.GetClusterSensitiveKey(config.DefaultClusterID, "testNamespace")
   535  
   536  	// Run the appender...
   537  	a := ServiceEntryAppender{
   538  		AccessibleNamespaces: graph.AccessibleNamespaces{
   539  			key: &graph.AccessibleNamespace{
   540  				Cluster:           config.DefaultClusterID,
   541  				CreationTimestamp: time.Now(),
   542  				Name:              "testNamespace",
   543  			}}}
   544  	a.AppendGraph(trafficMap, globalInfo, namespaceInfo)
   545  
   546  	assert.Equal(6, len(trafficMap))
   547  
   548  	testID, _, _ = graph.Id(config.DefaultClusterID, graph.Unknown, "test", "testNamespace", "test-v1", "test", "v1", graph.GraphTypeVersionedApp)
   549  	testNode, found0 = trafficMap[testID]
   550  	assert.Equal(true, found0)
   551  	assert.Equal(1, len(testNode.Edges))
   552  	assert.Equal(nil, testNode.Metadata[graph.IsServiceEntry])
   553  
   554  	notSEServiceID, _, _ = graph.Id(config.DefaultClusterID, "testNamespace", "NotSE", "testNamespace", "", "", "", graph.GraphTypeVersionedApp)
   555  	notSEServiceNode, found1 = trafficMap[notSEServiceID]
   556  	assert.Equal(true, found1)
   557  	assert.Equal(1, len(notSEServiceNode.Edges))
   558  	assert.Equal(nil, notSEServiceNode.Metadata[graph.IsServiceEntry])
   559  
   560  	notSEAppID, _, _ = graph.Id(config.DefaultClusterID, "testNamespace", "NotSE", "testNamespace", "NotSE-v1", "NotSE", "v1", graph.GraphTypeVersionedApp)
   561  	notSEAppNode, found2 = trafficMap[notSEAppID]
   562  	assert.Equal(true, found2)
   563  	assert.Equal(4, len(notSEAppNode.Edges))
   564  	assert.Equal(nil, notSEAppNode.Metadata[graph.IsServiceEntry])
   565  
   566  	externalSEServiceEntryID, _, _ := graph.Id(config.DefaultClusterID, "testNamespace", "externalSE", "testNamespace", "", "", "", graph.GraphTypeVersionedApp)
   567  	externalSEServiceEntryNode, found3 := trafficMap[externalSEServiceEntryID]
   568  	assert.Equal(true, found3)
   569  	assert.Equal(0, len(externalSEServiceEntryNode.Edges))
   570  	assert.Equal("MESH_EXTERNAL", externalSEServiceEntryNode.Metadata[graph.IsServiceEntry].(*graph.SEInfo).Location)
   571  	assert.Equal(2, len(externalSEServiceEntryNode.Metadata[graph.DestServices].(graph.DestServicesMetadata)))
   572  
   573  	externalHostXServiceID, _, _ = graph.Id(config.DefaultClusterID, "testNamespace", "hostX.external.com", "testNamespace", "", "", "", graph.GraphTypeVersionedApp)
   574  	externalHostXServiceNode, found4 = trafficMap[externalHostXServiceID]
   575  	assert.Equal(true, found4)
   576  	assert.Equal(0, len(externalHostXServiceNode.Edges))
   577  	assert.Equal(nil, externalHostXServiceNode.Metadata[graph.IsServiceEntry])
   578  
   579  	internalSEServiceEntryID, _, _ := graph.Id(config.DefaultClusterID, "testNamespace", "internalSE", "testNamespace", "", "", "", graph.GraphTypeVersionedApp)
   580  	internalSEServiceEntryNode, found5 := trafficMap[internalSEServiceEntryID]
   581  	assert.Equal(true, found5)
   582  	assert.Equal(0, len(internalSEServiceEntryNode.Edges))
   583  	assert.Equal("MESH_INTERNAL", internalSEServiceEntryNode.Metadata[graph.IsServiceEntry].(*graph.SEInfo).Location)
   584  	assert.Equal(2, len(internalSEServiceEntryNode.Metadata[graph.DestServices].(graph.DestServicesMetadata)))
   585  }
   586  
   587  func TestServiceEntryExportNamespaceNotFound(t *testing.T) {
   588  	assert := assert.New(t)
   589  
   590  	businessLayer := setupServiceEntries(t, "testNamespace", []string{"fooNamespace", "barNamespace"})
   591  	trafficMap := serviceEntriesTrafficMap()
   592  
   593  	assert.Equal(8, len(trafficMap))
   594  
   595  	testID, _, _ := graph.Id(config.DefaultClusterID, graph.Unknown, "test", "testNamespace", "test-v1", "test", "v1", graph.GraphTypeVersionedApp)
   596  	testNode, found0 := trafficMap[testID]
   597  	assert.Equal(true, found0)
   598  	assert.Equal(1, len(testNode.Edges))
   599  	assert.Equal(nil, testNode.Metadata[graph.IsServiceEntry])
   600  
   601  	notSEServiceID, _, _ := graph.Id(config.DefaultClusterID, "testNamespace", "NotSE", "testNamespace", "", "", "", graph.GraphTypeVersionedApp)
   602  	notSEServiceNode, found1 := trafficMap[notSEServiceID]
   603  	assert.Equal(true, found1)
   604  	assert.Equal(1, len(notSEServiceNode.Edges))
   605  	assert.Equal(nil, notSEServiceNode.Metadata[graph.IsServiceEntry])
   606  
   607  	notSEAppID, _, _ := graph.Id(config.DefaultClusterID, "testNamespace", "NotSE", "testNamespace", "NotSE-v1", "NotSE", "v1", graph.GraphTypeVersionedApp)
   608  	notSEAppNode, found2 := trafficMap[notSEAppID]
   609  	assert.Equal(true, found2)
   610  	assert.Equal(5, len(notSEAppNode.Edges))
   611  	assert.Equal(nil, notSEAppNode.Metadata[graph.IsServiceEntry])
   612  
   613  	externalSEHost1ServiceID, _, _ := graph.Id(config.DefaultClusterID, "testNamespace", "host1.external.com", "testNamespace", "", "", "", graph.GraphTypeVersionedApp)
   614  	externalSEHost1ServiceNode, found3 := trafficMap[externalSEHost1ServiceID]
   615  	assert.Equal(true, found3)
   616  	assert.Equal(0, len(externalSEHost1ServiceNode.Edges))
   617  	assert.Equal(nil, externalSEHost1ServiceNode.Metadata[graph.IsServiceEntry])
   618  
   619  	externalSEHost2ServiceID, _, _ := graph.Id(config.DefaultClusterID, "testNamespace", "host2.external.com", "testNamespace", "", "", "", graph.GraphTypeVersionedApp)
   620  	externalSEHost2ServiceNode, found4 := trafficMap[externalSEHost2ServiceID]
   621  	assert.Equal(true, found4)
   622  	assert.Equal(0, len(externalSEHost2ServiceNode.Edges))
   623  	assert.Equal(nil, externalSEHost2ServiceNode.Metadata[graph.IsServiceEntry])
   624  
   625  	externalHostXServiceID, _, _ := graph.Id(config.DefaultClusterID, "testNamespace", "hostX.external.com", "testNamespace", "", "", "", graph.GraphTypeVersionedApp)
   626  	externalHostXServiceNode, found5 := trafficMap[externalHostXServiceID]
   627  	assert.Equal(true, found5)
   628  	assert.Equal(0, len(externalHostXServiceNode.Edges))
   629  	assert.Equal(nil, externalHostXServiceNode.Metadata[graph.IsServiceEntry])
   630  
   631  	internalSEHost1ServiceID, _, _ := graph.Id(config.DefaultClusterID, "testNamespace", "internalHost1", "testNamespace", "", "", "", graph.GraphTypeVersionedApp)
   632  	internalSEHost1ServiceNode, found6 := trafficMap[internalSEHost1ServiceID]
   633  	assert.Equal(true, found6)
   634  	assert.Equal(0, len(internalSEHost1ServiceNode.Edges))
   635  	assert.Equal(nil, internalSEHost1ServiceNode.Metadata[graph.IsServiceEntry])
   636  
   637  	internalSEHost2ServiceID, _, _ := graph.Id(config.DefaultClusterID, "testNamespace", "internalHost2", "testNamespace", "", "", "", graph.GraphTypeVersionedApp)
   638  	internalSEHost2ServiceNode, found7 := trafficMap[internalSEHost2ServiceID]
   639  	assert.Equal(true, found7)
   640  	assert.Equal(0, len(internalSEHost2ServiceNode.Edges))
   641  	assert.Equal(nil, internalSEHost2ServiceNode.Metadata[graph.IsServiceEntry])
   642  
   643  	globalInfo := graph.NewAppenderGlobalInfo()
   644  	globalInfo.Business = businessLayer
   645  	namespaceInfo := graph.NewAppenderNamespaceInfo("testNamespace")
   646  	key := graph.GetClusterSensitiveKey(config.DefaultClusterID, "testNamespace")
   647  
   648  	// Run the appender...
   649  	a := ServiceEntryAppender{
   650  		AccessibleNamespaces: graph.AccessibleNamespaces{
   651  			key: &graph.AccessibleNamespace{
   652  				Cluster:           config.DefaultClusterID,
   653  				CreationTimestamp: time.Now(),
   654  				Name:              "testNamespace",
   655  			}}}
   656  	a.AppendGraph(trafficMap, globalInfo, namespaceInfo)
   657  
   658  	assert.Equal(7, len(trafficMap))
   659  
   660  	testID, _, _ = graph.Id(config.DefaultClusterID, graph.Unknown, "test", "testNamespace", "test-v1", "test", "v1", graph.GraphTypeVersionedApp)
   661  	testNode, found0 = trafficMap[testID]
   662  	assert.Equal(true, found0)
   663  	assert.Equal(1, len(testNode.Edges))
   664  	assert.Equal(nil, testNode.Metadata[graph.IsServiceEntry])
   665  
   666  	notSEServiceID, _, _ = graph.Id(config.DefaultClusterID, "testNamespace", "NotSE", "testNamespace", "", "", "", graph.GraphTypeVersionedApp)
   667  	notSEServiceNode, found1 = trafficMap[notSEServiceID]
   668  	assert.Equal(true, found1)
   669  	assert.Equal(1, len(notSEServiceNode.Edges))
   670  	assert.Equal(nil, notSEServiceNode.Metadata[graph.IsServiceEntry])
   671  
   672  	notSEAppID, _, _ = graph.Id(config.DefaultClusterID, "testNamespace", "NotSE", "testNamespace", "NotSE-v1", "NotSE", "v1", graph.GraphTypeVersionedApp)
   673  	notSEAppNode, found2 = trafficMap[notSEAppID]
   674  	assert.Equal(true, found2)
   675  	assert.Equal(5, len(notSEAppNode.Edges))
   676  	assert.Equal(nil, notSEAppNode.Metadata[graph.IsServiceEntry])
   677  
   678  	externalSEServiceEntryID, _, _ := graph.Id(config.DefaultClusterID, "testNamespace", "externalSE", "testNamespace", "", "", "", graph.GraphTypeVersionedApp)
   679  	_, found3 = trafficMap[externalSEServiceEntryID]
   680  	assert.Equal(false, found3)
   681  
   682  	externalHostXServiceID, _, _ = graph.Id(config.DefaultClusterID, "testNamespace", "hostX.external.com", "testNamespace", "", "", "", graph.GraphTypeVersionedApp)
   683  	externalHostXServiceNode, found4 = trafficMap[externalHostXServiceID]
   684  	assert.Equal(true, found4)
   685  	assert.Equal(0, len(externalHostXServiceNode.Edges))
   686  	assert.Equal(nil, externalHostXServiceNode.Metadata[graph.IsServiceEntry])
   687  
   688  	internalSEServiceEntryID, _, _ := graph.Id(config.DefaultClusterID, "testNamespace", "internalSE", "testNamespace", "", "", "", graph.GraphTypeVersionedApp)
   689  	internalSEServiceEntryNode, found5 := trafficMap[internalSEServiceEntryID]
   690  	assert.Equal(true, found5)
   691  	assert.Equal(0, len(internalSEServiceEntryNode.Edges))
   692  	assert.Equal("MESH_INTERNAL", internalSEServiceEntryNode.Metadata[graph.IsServiceEntry].(*graph.SEInfo).Location)
   693  	assert.Equal(2, len(internalSEServiceEntryNode.Metadata[graph.DestServices].(graph.DestServicesMetadata)))
   694  }
   695  
   696  // test scenario where ServiceEntry is defined in a namespace other than the requesting node, but has default ExportTo
   697  func TestKiali7153_1(t *testing.T) {
   698  	assert := assert.New(t)
   699  
   700  	businessLayer := setupServiceEntries(t, "otherNamespace", nil)
   701  	trafficMap := serviceEntriesTrafficMap()
   702  
   703  	assert.Equal(8, len(trafficMap))
   704  
   705  	testID, _, _ := graph.Id(config.DefaultClusterID, graph.Unknown, "test", "testNamespace", "test-v1", "test", "v1", graph.GraphTypeVersionedApp)
   706  	testNode, found0 := trafficMap[testID]
   707  	assert.Equal(true, found0)
   708  	assert.Equal(1, len(testNode.Edges))
   709  	assert.Equal(nil, testNode.Metadata[graph.IsServiceEntry])
   710  
   711  	notSEServiceID, _, _ := graph.Id(config.DefaultClusterID, "testNamespace", "NotSE", "testNamespace", "", "", "", graph.GraphTypeVersionedApp)
   712  	notSEServiceNode, found1 := trafficMap[notSEServiceID]
   713  	assert.Equal(true, found1)
   714  	assert.Equal(1, len(notSEServiceNode.Edges))
   715  	assert.Equal(nil, notSEServiceNode.Metadata[graph.IsServiceEntry])
   716  
   717  	notSEAppID, _, _ := graph.Id(config.DefaultClusterID, "testNamespace", "NotSE", "testNamespace", "NotSE-v1", "NotSE", "v1", graph.GraphTypeVersionedApp)
   718  	notSEAppNode, found2 := trafficMap[notSEAppID]
   719  	assert.Equal(true, found2)
   720  	assert.Equal(5, len(notSEAppNode.Edges))
   721  	assert.Equal(nil, notSEAppNode.Metadata[graph.IsServiceEntry])
   722  
   723  	externalSEHost1ServiceID, _, _ := graph.Id(config.DefaultClusterID, "testNamespace", "host1.external.com", "testNamespace", "", "", "", graph.GraphTypeVersionedApp)
   724  	externalSEHost1ServiceNode, found3 := trafficMap[externalSEHost1ServiceID]
   725  	assert.Equal(true, found3)
   726  	assert.Equal(0, len(externalSEHost1ServiceNode.Edges))
   727  	assert.Equal(nil, externalSEHost1ServiceNode.Metadata[graph.IsServiceEntry])
   728  
   729  	externalSEHost2ServiceID, _, _ := graph.Id(config.DefaultClusterID, "testNamespace", "host2.external.com", "testNamespace", "", "", "", graph.GraphTypeVersionedApp)
   730  	externalSEHost2ServiceNode, found4 := trafficMap[externalSEHost2ServiceID]
   731  	assert.Equal(true, found4)
   732  	assert.Equal(0, len(externalSEHost2ServiceNode.Edges))
   733  	assert.Equal(nil, externalSEHost2ServiceNode.Metadata[graph.IsServiceEntry])
   734  
   735  	externalHostXServiceID, _, _ := graph.Id(config.DefaultClusterID, "testNamespace", "hostX.external.com", "testNamespace", "", "", "", graph.GraphTypeVersionedApp)
   736  	externalHostXServiceNode, found5 := trafficMap[externalHostXServiceID]
   737  	assert.Equal(true, found5)
   738  	assert.Equal(0, len(externalHostXServiceNode.Edges))
   739  	assert.Equal(nil, externalHostXServiceNode.Metadata[graph.IsServiceEntry])
   740  
   741  	internalSEHost1ServiceID, _, _ := graph.Id(config.DefaultClusterID, "testNamespace", "internalHost1", "testNamespace", "", "", "", graph.GraphTypeVersionedApp)
   742  	internalSEHost1ServiceNode, found6 := trafficMap[internalSEHost1ServiceID]
   743  	assert.Equal(true, found6)
   744  	assert.Equal(0, len(internalSEHost1ServiceNode.Edges))
   745  	assert.Equal(nil, internalSEHost1ServiceNode.Metadata[graph.IsServiceEntry])
   746  
   747  	internalSEHost2ServiceID, _, _ := graph.Id(config.DefaultClusterID, "testNamespace", "internalHost2", "testNamespace", "", "", "", graph.GraphTypeVersionedApp)
   748  	internalSEHost2ServiceNode, found7 := trafficMap[internalSEHost2ServiceID]
   749  	assert.Equal(true, found7)
   750  	assert.Equal(0, len(internalSEHost2ServiceNode.Edges))
   751  	assert.Equal(nil, internalSEHost2ServiceNode.Metadata[graph.IsServiceEntry])
   752  
   753  	globalInfo := graph.NewAppenderGlobalInfo()
   754  	globalInfo.Business = businessLayer
   755  
   756  	testNamespaceInfo := graph.NewAppenderNamespaceInfo("testNamespace")
   757  
   758  	testNamespaceKey := graph.GetClusterSensitiveKey(config.DefaultClusterID, "testNamespace")
   759  	otherNamespaceKey := graph.GetClusterSensitiveKey(config.DefaultClusterID, "otherNamespace")
   760  
   761  	// Run the appender...
   762  	a := ServiceEntryAppender{
   763  		AccessibleNamespaces: graph.AccessibleNamespaces{
   764  			testNamespaceKey: &graph.AccessibleNamespace{
   765  				Cluster:           config.DefaultClusterID,
   766  				CreationTimestamp: time.Now(),
   767  				Name:              "testNamespace"},
   768  			otherNamespaceKey: &graph.AccessibleNamespace{
   769  				Cluster:           config.DefaultClusterID,
   770  				CreationTimestamp: time.Now(),
   771  				Name:              "otherNamespace"},
   772  		}}
   773  	a.AppendGraph(trafficMap, globalInfo, testNamespaceInfo)
   774  
   775  	assert.Equal(6, len(trafficMap))
   776  
   777  	testID, _, _ = graph.Id(config.DefaultClusterID, graph.Unknown, "test", "testNamespace", "test-v1", "test", "v1", graph.GraphTypeVersionedApp)
   778  	testNode, found0 = trafficMap[testID]
   779  	assert.Equal(true, found0)
   780  	assert.Equal(1, len(testNode.Edges))
   781  	assert.Equal(nil, testNode.Metadata[graph.IsServiceEntry])
   782  
   783  	notSEServiceID, _, _ = graph.Id(config.DefaultClusterID, "testNamespace", "NotSE", "testNamespace", "", "", "", graph.GraphTypeVersionedApp)
   784  	notSEServiceNode, found1 = trafficMap[notSEServiceID]
   785  	assert.Equal(true, found1)
   786  	assert.Equal(1, len(notSEServiceNode.Edges))
   787  	assert.Equal(nil, notSEServiceNode.Metadata[graph.IsServiceEntry])
   788  
   789  	notSEAppID, _, _ = graph.Id(config.DefaultClusterID, "testNamespace", "NotSE", "testNamespace", "NotSE-v1", "NotSE", "v1", graph.GraphTypeVersionedApp)
   790  	notSEAppNode, found2 = trafficMap[notSEAppID]
   791  	assert.Equal(true, found2)
   792  	assert.Equal(4, len(notSEAppNode.Edges))
   793  	assert.Equal(nil, notSEAppNode.Metadata[graph.IsServiceEntry])
   794  
   795  	externalSEServiceEntryID, _, _ := graph.Id(config.DefaultClusterID, "testNamespace", "externalSE", "testNamespace", "", "", "", graph.GraphTypeVersionedApp)
   796  	externalSEServiceEntryNode, found3 := trafficMap[externalSEServiceEntryID]
   797  	assert.Equal(true, found3)
   798  	assert.Equal(0, len(externalSEServiceEntryNode.Edges))
   799  	assert.Equal("MESH_EXTERNAL", externalSEServiceEntryNode.Metadata[graph.IsServiceEntry].(*graph.SEInfo).Location)
   800  	externalHosts := externalSEServiceEntryNode.Metadata[graph.IsServiceEntry].(*graph.SEInfo).Hosts
   801  	assert.Equal("host1.external.com", externalHosts[0])
   802  	assert.Equal("host2.external.com", externalHosts[1])
   803  	assert.Equal(2, len(externalSEServiceEntryNode.Metadata[graph.DestServices].(graph.DestServicesMetadata)))
   804  
   805  	externalHostXServiceID, _, _ = graph.Id(config.DefaultClusterID, "testNamespace", "hostX.external.com", "testNamespace", "", "", "", graph.GraphTypeVersionedApp)
   806  	externalHostXServiceNode, found4 = trafficMap[externalHostXServiceID]
   807  	assert.Equal(true, found4)
   808  	assert.Equal(0, len(externalHostXServiceNode.Edges))
   809  	assert.Equal(nil, externalHostXServiceNode.Metadata[graph.IsServiceEntry])
   810  
   811  	internalSEServiceEntryID, _, _ := graph.Id(config.DefaultClusterID, "testNamespace", "internalSE", "testNamespace", "", "", "", graph.GraphTypeVersionedApp)
   812  	internalSEServiceEntryNode, found5 := trafficMap[internalSEServiceEntryID]
   813  	assert.Equal(true, found5)
   814  	assert.Equal(0, len(internalSEServiceEntryNode.Edges))
   815  	assert.Equal("MESH_INTERNAL", internalSEServiceEntryNode.Metadata[graph.IsServiceEntry].(*graph.SEInfo).Location)
   816  	internalHosts := externalSEServiceEntryNode.Metadata[graph.IsServiceEntry].(*graph.SEInfo).Hosts
   817  	assert.Equal("host1.external.com", internalHosts[0])
   818  	assert.Equal("host2.external.com", internalHosts[1])
   819  	assert.Equal(2, len(internalSEServiceEntryNode.Metadata[graph.DestServices].(graph.DestServicesMetadata)))
   820  }
   821  
   822  // TestDisjointGlobalEntries checks that a service node representing traffic to a remote cluster
   823  // is correctly identified as a ServiceEntry. Also checks that a service node representing traffic
   824  // to an internal service is not mixed with the node for the remote cluster.
   825  func TestDisjointMulticlusterEntries(t *testing.T) {
   826  	assert := assert.New(t)
   827  
   828  	remoteSE := &networking_v1beta1.ServiceEntry{}
   829  	remoteSE.Name = "externalSE"
   830  	remoteSE.Namespace = "namespace"
   831  	remoteSE.Spec.Hosts = []string{
   832  		"svc1.namespace.global",
   833  	}
   834  	remoteSE.Spec.Location = api_networking_v1beta1.ServiceEntry_MESH_INTERNAL
   835  	k8s := kubetest.NewFakeK8sClient(remoteSE, &core_v1.Namespace{ObjectMeta: meta_v1.ObjectMeta{Name: "namespace"}})
   836  
   837  	conf := config.NewConfig()
   838  	conf.KubernetesConfig.ClusterName = config.DefaultClusterID
   839  	conf.ExternalServices.Istio.IstioAPIEnabled = false
   840  	config.Set(conf)
   841  
   842  	business.SetupBusinessLayer(t, k8s, *conf)
   843  	k8sclients := map[string]kubernetes.ClientInterface{
   844  		config.DefaultClusterID: kubetest.NewFakeK8sClient(
   845  			&core_v1.Namespace{ObjectMeta: meta_v1.ObjectMeta{Name: "namespace"}},
   846  		),
   847  	}
   848  	businessLayer := business.NewWithBackends(k8sclients, k8sclients, nil, nil)
   849  
   850  	// Create a VersionedApp traffic map where a workload is calling a remote service entry and also an internal one
   851  	trafficMap := make(map[string]*graph.Node)
   852  
   853  	n0, _ := graph.NewNode(config.DefaultClusterID, "namespace", "source", "namespace", "wk0", "source", "v0", graph.GraphTypeVersionedApp)
   854  	n1, _ := graph.NewNode(config.DefaultClusterID, "namespace", "svc1.namespace.global", "unknown", "unknown", "unknown", "unknown", graph.GraphTypeVersionedApp)
   855  	n2, _ := graph.NewNode(config.DefaultClusterID, "namespace", "svc1", "unknown", "unknown", "unknown", "unknown", graph.GraphTypeVersionedApp)
   856  
   857  	trafficMap[n0.ID] = n0
   858  	trafficMap[n1.ID] = n1
   859  	trafficMap[n2.ID] = n2
   860  
   861  	n0.AddEdge(n1).Metadata[graph.ProtocolKey] = graph.HTTP.Name
   862  	n0.AddEdge(n2).Metadata[graph.ProtocolKey] = graph.HTTP.Name
   863  
   864  	// Run the appender
   865  	globalInfo := graph.NewAppenderGlobalInfo()
   866  	globalInfo.Business = businessLayer
   867  	namespaceInfo := graph.NewAppenderNamespaceInfo("namespace")
   868  	key := graph.GetClusterSensitiveKey(config.DefaultClusterID, "testNamespace")
   869  
   870  	a := ServiceEntryAppender{
   871  		AccessibleNamespaces: graph.AccessibleNamespaces{
   872  			key: &graph.AccessibleNamespace{
   873  				Cluster:           config.DefaultClusterID,
   874  				CreationTimestamp: time.Now(),
   875  				Name:              "namespace",
   876  			}}}
   877  	a.AppendGraph(trafficMap, globalInfo, namespaceInfo)
   878  
   879  	// Assertions
   880  	assert.Len(n0.Edges, 2)   // Check that source node still has two edges
   881  	assert.Len(trafficMap, 3) // Check that traffic map still has three nodes
   882  
   883  	// Check that there is a node for the local svc1.
   884  	numMatches := 0
   885  	for _, n := range trafficMap {
   886  		if n.Service == "svc1" {
   887  			numMatches++
   888  			assert.Equal(n, n2)
   889  		}
   890  	}
   891  	assert.Equal(1, numMatches)
   892  
   893  	// Check that there is a node for the remote svc1 and is was matched against the remote SE.
   894  	numMatches = 0
   895  	for _, n := range trafficMap {
   896  		if n.Service == "externalSE" {
   897  			numMatches++
   898  			assert.Equal("MESH_INTERNAL", n.Metadata[graph.IsServiceEntry].(*graph.SEInfo).Location)
   899  			assert.Equal("namespace", n.Metadata[graph.IsServiceEntry].(*graph.SEInfo).Namespace)
   900  		}
   901  	}
   902  	assert.Equal(1, numMatches)
   903  }
   904  
   905  func TestServiceEntrySameHostMatchNamespace(t *testing.T) {
   906  	SE1 := &networking_v1beta1.ServiceEntry{}
   907  	SE1.Name = "SE1"
   908  	SE1.Namespace = "fooNamespace"
   909  	SE1.Spec.ExportTo = []string{"*"}
   910  	SE1.Spec.Hosts = []string{
   911  		"host1.external.com",
   912  	}
   913  	SE1.Spec.Location = api_networking_v1beta1.ServiceEntry_MESH_EXTERNAL
   914  
   915  	SE2 := &networking_v1beta1.ServiceEntry{}
   916  	SE2.Name = "SE2"
   917  	SE2.Namespace = "testNamespace"
   918  	SE2.Spec.ExportTo = []string{"."}
   919  	SE2.Spec.Hosts = []string{
   920  		"host1.external.com",
   921  		"host2.external.com",
   922  	}
   923  	SE2.Spec.Location = api_networking_v1beta1.ServiceEntry_MESH_EXTERNAL
   924  
   925  	k8s := kubetest.NewFakeK8sClient(
   926  		&core_v1.Namespace{ObjectMeta: meta_v1.ObjectMeta{Name: "otherNamespace"}},
   927  		&core_v1.Namespace{ObjectMeta: meta_v1.ObjectMeta{Name: "testNamespace"}},
   928  		SE1,
   929  		SE2,
   930  	)
   931  
   932  	conf := config.NewConfig()
   933  	conf.KubernetesConfig.ClusterName = config.DefaultClusterID
   934  	conf.ExternalServices.Istio.IstioAPIEnabled = false
   935  	config.Set(conf)
   936  
   937  	business.SetupBusinessLayer(t, k8s, *conf)
   938  	k8sclients := map[string]kubernetes.ClientInterface{
   939  		config.DefaultClusterID: kubetest.NewFakeK8sClient(
   940  			&core_v1.Namespace{ObjectMeta: meta_v1.ObjectMeta{Name: "testNamespace"}},
   941  		),
   942  	}
   943  	businessLayer := business.NewWithBackends(k8sclients, k8sclients, nil, nil)
   944  
   945  	assert := assert.New(t)
   946  
   947  	trafficMap := make(map[string]*graph.Node)
   948  
   949  	// Test SourceNode
   950  	n0, _ := graph.NewNode(config.DefaultClusterID, graph.Unknown, "test", "testNamespace", "test-v1", "test", "v1", graph.GraphTypeVersionedApp)
   951  
   952  	// NotSE host3 serviceNode
   953  	n1, _ := graph.NewNode(config.DefaultClusterID, "testNamespace", "host3.external.com", "testNamespace", "", "", "", graph.GraphTypeVersionedApp)
   954  
   955  	// SE2 host1 serviceNode
   956  	n2, _ := graph.NewNode(config.DefaultClusterID, "testNamespace", "host1.external.com", "testNamespace", "", "", "", graph.GraphTypeVersionedApp)
   957  	n2.Metadata = graph.NewMetadata()
   958  	destServices := graph.NewDestServicesMetadata()
   959  	destService := graph.ServiceName{Namespace: n2.Namespace, Name: n2.Service}
   960  	destServices[destService.Key()] = destService
   961  	n2.Metadata[graph.DestServices] = destServices
   962  
   963  	// SE2 host2 serviceNode
   964  	n3, _ := graph.NewNode(config.DefaultClusterID, "testNamespace", "host2.external.com", "testNamespace", "", "", "", graph.GraphTypeVersionedApp)
   965  	n3.Metadata = graph.NewMetadata()
   966  	destServices = graph.NewDestServicesMetadata()
   967  	destService = graph.ServiceName{Namespace: n3.Namespace, Name: n3.Service}
   968  	destServices[destService.Key()] = destService
   969  	n3.Metadata[graph.DestServices] = destServices
   970  
   971  	trafficMap[n0.ID] = n0
   972  	trafficMap[n1.ID] = n1
   973  	trafficMap[n2.ID] = n2
   974  	trafficMap[n3.ID] = n3
   975  
   976  	n0.AddEdge(n1).Metadata[graph.ProtocolKey] = graph.HTTP.Name
   977  	n0.AddEdge(n2).Metadata[graph.ProtocolKey] = graph.HTTP.Name
   978  	n0.AddEdge(n3).Metadata[graph.ProtocolKey] = graph.HTTP.Name
   979  
   980  	assert.Equal(4, len(trafficMap))
   981  
   982  	testID, _, _ := graph.Id(config.DefaultClusterID, graph.Unknown, "test", "testNamespace", "test-v1", "test", "v1", graph.GraphTypeVersionedApp)
   983  	testNode, found0 := trafficMap[testID]
   984  	assert.Equal(true, found0)
   985  	assert.Equal(3, len(testNode.Edges))
   986  	assert.Equal(nil, testNode.Metadata[graph.IsServiceEntry])
   987  
   988  	notSEServiceID, _, _ := graph.Id(config.DefaultClusterID, "testNamespace", "host3.external.com", "testNamespace", "", "", "", graph.GraphTypeVersionedApp)
   989  	notSEServiceNode, found1 := trafficMap[notSEServiceID]
   990  	assert.Equal(true, found1)
   991  	assert.Equal(0, len(notSEServiceNode.Edges))
   992  	assert.Equal(nil, notSEServiceNode.Metadata[graph.IsServiceEntry])
   993  
   994  	SE2Host1ServiceID, _, _ := graph.Id(config.DefaultClusterID, "testNamespace", "host1.external.com", "testNamespace", "", "", "", graph.GraphTypeVersionedApp)
   995  	SE2Host1ServiceNode, found2 := trafficMap[SE2Host1ServiceID]
   996  	assert.Equal(true, found2)
   997  	assert.Equal(0, len(SE2Host1ServiceNode.Edges))
   998  	assert.Equal(nil, SE2Host1ServiceNode.Metadata[graph.IsServiceEntry])
   999  
  1000  	SE2Host2ServiceID, _, _ := graph.Id(config.DefaultClusterID, "testNamespace", "host2.external.com", "testNamespace", "", "", "", graph.GraphTypeVersionedApp)
  1001  	SE2Host2ServiceNode, found3 := trafficMap[SE2Host2ServiceID]
  1002  	assert.Equal(true, found3)
  1003  	assert.Equal(0, len(SE2Host2ServiceNode.Edges))
  1004  	assert.Equal(nil, SE2Host2ServiceNode.Metadata[graph.IsServiceEntry])
  1005  
  1006  	globalInfo := graph.NewAppenderGlobalInfo()
  1007  	globalInfo.Business = businessLayer
  1008  	namespaceInfo := graph.NewAppenderNamespaceInfo("testNamespace")
  1009  	key := graph.GetClusterSensitiveKey(config.DefaultClusterID, "testNamespace")
  1010  
  1011  	// Run the appender...
  1012  	a := ServiceEntryAppender{
  1013  		AccessibleNamespaces: graph.AccessibleNamespaces{
  1014  			key: &graph.AccessibleNamespace{
  1015  				Cluster:           config.DefaultClusterID,
  1016  				CreationTimestamp: time.Now(),
  1017  				Name:              "testNamespace",
  1018  			}}}
  1019  	a.AppendGraph(trafficMap, globalInfo, namespaceInfo)
  1020  
  1021  	assert.Equal(3, len(trafficMap))
  1022  
  1023  	testID, _, _ = graph.Id(config.DefaultClusterID, graph.Unknown, "test", "testNamespace", "test-v1", "test", "v1", graph.GraphTypeVersionedApp)
  1024  	testNode, found0 = trafficMap[testID]
  1025  	assert.Equal(true, found0)
  1026  	assert.Equal(2, len(testNode.Edges))
  1027  	assert.Equal(nil, testNode.Metadata[graph.IsServiceEntry])
  1028  
  1029  	notSEServiceID, _, _ = graph.Id(config.DefaultClusterID, "testNamespace", "host3.external.com", "testNamespace", "", "", "", graph.GraphTypeVersionedApp)
  1030  	notSEServiceNode, found1 = trafficMap[notSEServiceID]
  1031  	assert.Equal(true, found1)
  1032  	assert.Equal(0, len(notSEServiceNode.Edges))
  1033  	assert.Equal(nil, notSEServiceNode.Metadata[graph.IsServiceEntry])
  1034  
  1035  	SE2ID, _, _ := graph.Id(config.DefaultClusterID, "testNamespace", "SE2", "testNamespace", "", "", "", graph.GraphTypeVersionedApp)
  1036  	SE2Node, found2 := trafficMap[SE2ID]
  1037  	assert.Equal(true, found2)
  1038  	assert.Equal(0, len(SE2Node.Edges))
  1039  	assert.Equal("MESH_EXTERNAL", SE2Node.Metadata[graph.IsServiceEntry].(*graph.SEInfo).Location)
  1040  	assert.Equal("testNamespace", SE2Node.Metadata[graph.IsServiceEntry].(*graph.SEInfo).Namespace)
  1041  	assert.Equal(2, len(SE2Node.Metadata[graph.DestServices].(graph.DestServicesMetadata)))
  1042  }
  1043  
  1044  func TestServiceEntrySameHostNoMatchNamespace(t *testing.T) {
  1045  	SE1 := &networking_v1beta1.ServiceEntry{}
  1046  	SE1.Name = "SE1"
  1047  	SE1.Namespace = "otherNamespace"
  1048  	SE1.Spec.ExportTo = []string{"."}
  1049  	SE1.Spec.Hosts = []string{
  1050  		"host1.external.com",
  1051  	}
  1052  	SE1.Spec.Location = api_networking_v1beta1.ServiceEntry_MESH_EXTERNAL
  1053  
  1054  	SE2 := &networking_v1beta1.ServiceEntry{}
  1055  	SE2.Name = "SE2"
  1056  	SE2.Namespace = "testNamespace"
  1057  	SE2.Spec.ExportTo = []string{"*"}
  1058  	SE2.Spec.Hosts = []string{
  1059  		"host1.external.com",
  1060  		"host2.external.com",
  1061  	}
  1062  	SE2.Spec.Location = api_networking_v1beta1.ServiceEntry_MESH_EXTERNAL
  1063  
  1064  	conf := config.NewConfig()
  1065  	conf.KubernetesConfig.ClusterName = config.DefaultClusterID
  1066  	conf.ExternalServices.Istio.IstioAPIEnabled = false
  1067  	config.Set(conf)
  1068  
  1069  	istioObjects := append([]runtime.Object{SE1, SE2}, &core_v1.Namespace{ObjectMeta: meta_v1.ObjectMeta{Name: "otherNamespace"}})
  1070  	k8s := kubetest.NewFakeK8sClient(istioObjects...)
  1071  	business.SetupBusinessLayer(t, k8s, *conf)
  1072  	k8sclients := map[string]kubernetes.ClientInterface{
  1073  		config.DefaultClusterID: kubetest.NewFakeK8sClient(
  1074  			&core_v1.Namespace{ObjectMeta: meta_v1.ObjectMeta{Name: "otherNamespace"}},
  1075  		),
  1076  	}
  1077  	businessLayer := business.NewWithBackends(k8sclients, k8sclients, nil, nil)
  1078  
  1079  	assert := assert.New(t)
  1080  
  1081  	trafficMap := make(map[string]*graph.Node)
  1082  
  1083  	// testNode
  1084  	n0, _ := graph.NewNode(config.DefaultClusterID, graph.Unknown, "test", "otherNamespace", "test-v1", "test", "v1", graph.GraphTypeVersionedApp)
  1085  
  1086  	// NotSE host3 serviceNode
  1087  	n1, _ := graph.NewNode(config.DefaultClusterID, "testNamespace", "host3.external.com", "testNamespace", "", "", "", graph.GraphTypeVersionedApp)
  1088  
  1089  	// SE1 host1 serviceNode
  1090  	n2, _ := graph.NewNode(config.DefaultClusterID, "testNamespace", "host1.external.com", "testNamespace", "", "", "", graph.GraphTypeVersionedApp)
  1091  	n2.Metadata = graph.NewMetadata()
  1092  	destServices := graph.NewDestServicesMetadata()
  1093  	destService := graph.ServiceName{Namespace: n2.Namespace, Name: n2.Service}
  1094  	destServices[destService.Key()] = destService
  1095  	n2.Metadata[graph.DestServices] = destServices
  1096  
  1097  	// Not SE host2 serviceNode
  1098  	n3, _ := graph.NewNode(config.DefaultClusterID, "testNamespace", "host2.external.com", "testNamespace", "", "", "", graph.GraphTypeVersionedApp)
  1099  
  1100  	trafficMap[n0.ID] = n0
  1101  	trafficMap[n1.ID] = n1
  1102  	trafficMap[n2.ID] = n2
  1103  	trafficMap[n3.ID] = n3
  1104  
  1105  	n0.AddEdge(n1).Metadata[graph.ProtocolKey] = graph.HTTP.Name
  1106  	n0.AddEdge(n2).Metadata[graph.ProtocolKey] = graph.HTTP.Name
  1107  	n0.AddEdge(n3).Metadata[graph.ProtocolKey] = graph.HTTP.Name
  1108  
  1109  	assert.Equal(4, len(trafficMap))
  1110  
  1111  	testID, _, _ := graph.Id(config.DefaultClusterID, graph.Unknown, "test", "otherNamespace", "test-v1", "test", "v1", graph.GraphTypeVersionedApp)
  1112  	testNode, found0 := trafficMap[testID]
  1113  	assert.Equal(true, found0)
  1114  	assert.Equal(3, len(testNode.Edges))
  1115  	assert.Equal(nil, testNode.Metadata[graph.IsServiceEntry])
  1116  
  1117  	notSEHost3ServiceID, _, _ := graph.Id(config.DefaultClusterID, "testNamespace", "host3.external.com", "testNamespace", "", "", "", graph.GraphTypeVersionedApp)
  1118  	notSEHost3ServiceNode, found1 := trafficMap[notSEHost3ServiceID]
  1119  	assert.Equal(true, found1)
  1120  	assert.Equal(0, len(notSEHost3ServiceNode.Edges))
  1121  	assert.Equal(nil, notSEHost3ServiceNode.Metadata[graph.IsServiceEntry])
  1122  
  1123  	SE1Host1ServiceID, _, _ := graph.Id(config.DefaultClusterID, "testNamespace", "host1.external.com", "testNamespace", "", "", "", graph.GraphTypeVersionedApp)
  1124  	SE1Host1ServiceNode, found2 := trafficMap[SE1Host1ServiceID]
  1125  	assert.Equal(true, found2)
  1126  	assert.Equal(0, len(SE1Host1ServiceNode.Edges))
  1127  	assert.Equal(nil, SE1Host1ServiceNode.Metadata[graph.IsServiceEntry])
  1128  
  1129  	notSEHost2ServiceID, _, _ := graph.Id(config.DefaultClusterID, "testNamespace", "host2.external.com", "testNamespace", "", "", "", graph.GraphTypeVersionedApp)
  1130  	notSEHost2ServiceNode, found3 := trafficMap[notSEHost2ServiceID]
  1131  	assert.Equal(true, found3)
  1132  	assert.Equal(0, len(notSEHost2ServiceNode.Edges))
  1133  	assert.Equal(nil, notSEHost2ServiceNode.Metadata[graph.IsServiceEntry])
  1134  
  1135  	globalInfo := graph.NewAppenderGlobalInfo()
  1136  	globalInfo.Business = businessLayer
  1137  	namespaceInfo := graph.NewAppenderNamespaceInfo("otherNamespace")
  1138  	key := graph.GetClusterSensitiveKey(config.DefaultClusterID, "testNamespace")
  1139  
  1140  	// Run the appender...
  1141  	a := ServiceEntryAppender{
  1142  		AccessibleNamespaces: graph.AccessibleNamespaces{
  1143  			key: &graph.AccessibleNamespace{
  1144  				Cluster:           config.DefaultClusterID,
  1145  				CreationTimestamp: time.Now(),
  1146  				Name:              "otherNamespace",
  1147  			}}}
  1148  	a.AppendGraph(trafficMap, globalInfo, namespaceInfo)
  1149  
  1150  	assert.Equal(4, len(trafficMap))
  1151  
  1152  	testID, _, _ = graph.Id(config.DefaultClusterID, graph.Unknown, "test", "otherNamespace", "test-v1", "test", "v1", graph.GraphTypeVersionedApp)
  1153  	testNode, found0 = trafficMap[testID]
  1154  	assert.Equal(true, found0)
  1155  	assert.Equal(3, len(testNode.Edges))
  1156  	assert.Equal(nil, testNode.Metadata[graph.IsServiceEntry])
  1157  
  1158  	notSEHost3ServiceID, _, _ = graph.Id(config.DefaultClusterID, "testNamespace", "host3.external.com", "testNamespace", "", "", "", graph.GraphTypeVersionedApp)
  1159  	notSEHost3ServiceNode, found1 = trafficMap[notSEHost3ServiceID]
  1160  	assert.Equal(true, found1)
  1161  	assert.Equal(0, len(notSEHost3ServiceNode.Edges))
  1162  	assert.Equal(nil, notSEHost3ServiceNode.Metadata[graph.IsServiceEntry])
  1163  
  1164  	SE1ID, _, _ := graph.Id(config.DefaultClusterID, "otherNamespace", "SE1", "otherNamespace", "", "", "", graph.GraphTypeVersionedApp)
  1165  	SE1Node, found2 := trafficMap[SE1ID]
  1166  	assert.Equal(true, found2)
  1167  	assert.Equal(0, len(SE1Node.Edges))
  1168  	assert.Equal("MESH_EXTERNAL", SE1Node.Metadata[graph.IsServiceEntry].(*graph.SEInfo).Location)
  1169  	assert.Equal("otherNamespace", SE1Node.Metadata[graph.IsServiceEntry].(*graph.SEInfo).Namespace)
  1170  	assert.Equal(1, len(SE1Node.Metadata[graph.DestServices].(graph.DestServicesMetadata)))
  1171  
  1172  	notSEHost2ServiceID, _, _ = graph.Id(config.DefaultClusterID, "testNamespace", "host2.external.com", "testNamespace", "", "", "", graph.GraphTypeVersionedApp)
  1173  	notSEHost2ServiceNode, found3 = trafficMap[notSEHost2ServiceID]
  1174  	assert.Equal(true, found3)
  1175  	assert.Equal(0, len(notSEHost2ServiceNode.Edges))
  1176  	assert.Equal(nil, notSEHost2ServiceNode.Metadata[graph.IsServiceEntry])
  1177  }
  1178  
  1179  // TestServiceEntryMultipleEdges ensures that a service entry node gets created
  1180  // for nodes with multiple outgoing edges such as when an internal service entry
  1181  // routes to multiple versions of a workload.
  1182  func TestServiceEntryMultipleEdges(t *testing.T) {
  1183  	assert := assert.New(t)
  1184  
  1185  	const (
  1186  		namespace     = "testNamespace"
  1187  		seServiceName = "reviews"
  1188  		app           = "reviews"
  1189  	)
  1190  
  1191  	internalSE := &networking_v1beta1.ServiceEntry{}
  1192  	internalSE.Name = seServiceName
  1193  	internalSE.Namespace = namespace
  1194  	internalSE.Spec.Hosts = []string{
  1195  		"reviews",
  1196  		"reviews.testNamespace.svc.cluster.local",
  1197  	}
  1198  	internalSE.Spec.WorkloadSelector = &api_networking_v1beta1.WorkloadSelector{
  1199  		Labels: map[string]string{
  1200  			"app": "reviews",
  1201  		},
  1202  	}
  1203  	internalSE.Spec.Location = api_networking_v1beta1.ServiceEntry_MESH_INTERNAL
  1204  
  1205  	businessLayer := setupBusinessLayer(t, internalSE)
  1206  
  1207  	// VersionedApp graph
  1208  	trafficMap := make(map[string]*graph.Node)
  1209  
  1210  	// appNode for interal service v1
  1211  	v1, _ := graph.NewNode(config.DefaultClusterID, namespace, seServiceName, namespace, "reviews-v1", app, "v1", graph.GraphTypeVersionedApp)
  1212  	// appNode for interal service v2
  1213  	v2, _ := graph.NewNode(config.DefaultClusterID, namespace, seServiceName, namespace, "reviews-v2", app, "v2", graph.GraphTypeVersionedApp)
  1214  
  1215  	// reviews serviceNode
  1216  	svc, _ := graph.NewNode(config.DefaultClusterID, namespace, seServiceName, namespace, "", "", "", graph.GraphTypeVersionedApp)
  1217  
  1218  	trafficMap[svc.ID] = svc
  1219  	trafficMap[v1.ID] = v1
  1220  	trafficMap[v2.ID] = v2
  1221  
  1222  	svc.AddEdge(v1).Metadata[graph.ProtocolKey] = graph.HTTP.Name
  1223  	svc.AddEdge(v2).Metadata[graph.ProtocolKey] = graph.HTTP.Name
  1224  
  1225  	assert.Equal(3, len(trafficMap))
  1226  
  1227  	seSVCID, _, _ := graph.Id(config.DefaultClusterID, namespace, seServiceName, namespace, "", "", "", graph.GraphTypeVersionedApp)
  1228  	svcNode, svcNodeFound := trafficMap[seSVCID]
  1229  	assert.Equal(true, svcNodeFound)
  1230  	assert.Equal(2, len(svcNode.Edges))
  1231  	assert.Equal(nil, svcNode.Metadata[graph.IsServiceEntry])
  1232  
  1233  	v1ID, _, _ := graph.Id(config.DefaultClusterID, namespace, seServiceName, namespace, "reviews-v1", app, "v1", graph.GraphTypeVersionedApp)
  1234  	v1Node, v1NodeFound := trafficMap[v1ID]
  1235  	assert.Equal(true, v1NodeFound)
  1236  	assert.Equal(0, len(v1Node.Edges))
  1237  	assert.Equal(nil, v1Node.Metadata[graph.IsServiceEntry])
  1238  
  1239  	v2ID, _, _ := graph.Id(config.DefaultClusterID, namespace, seServiceName, namespace, "reviews-v2", app, "v2", graph.GraphTypeVersionedApp)
  1240  	v2Node, v2NodeFound := trafficMap[v2ID]
  1241  	assert.Equal(true, v2NodeFound)
  1242  	assert.Equal(0, len(v2Node.Edges))
  1243  	assert.Equal(nil, v2Node.Metadata[graph.IsServiceEntry])
  1244  
  1245  	globalInfo := graph.NewAppenderGlobalInfo()
  1246  	globalInfo.Business = businessLayer
  1247  	namespaceInfo := graph.NewAppenderNamespaceInfo("testNamespace")
  1248  	key := graph.GetClusterSensitiveKey(config.DefaultClusterID, "testNamespace")
  1249  
  1250  	// Run the appender...
  1251  	a := ServiceEntryAppender{
  1252  		AccessibleNamespaces: graph.AccessibleNamespaces{
  1253  			key: &graph.AccessibleNamespace{
  1254  				Cluster:           config.DefaultClusterID,
  1255  				CreationTimestamp: time.Now(),
  1256  				Name:              "testNamespace",
  1257  			}}}
  1258  	a.AppendGraph(trafficMap, globalInfo, namespaceInfo)
  1259  
  1260  	assert.Equal(3, len(trafficMap))
  1261  
  1262  	seSVCID, _, _ = graph.Id(config.DefaultClusterID, namespace, seServiceName, namespace, "", "", "", graph.GraphTypeVersionedApp)
  1263  	svcNode, svcNodeFound = trafficMap[seSVCID]
  1264  	assert.Equal(true, svcNodeFound)
  1265  	assert.Equal("MESH_INTERNAL", svcNode.Metadata[graph.IsServiceEntry].(*graph.SEInfo).Location)
  1266  	internalHosts := svcNode.Metadata[graph.IsServiceEntry].(*graph.SEInfo).Hosts
  1267  	assert.Equal("reviews", internalHosts[0])
  1268  	assert.Equal("reviews.testNamespace.svc.cluster.local", internalHosts[1])
  1269  	assert.Equal(2, len(svcNode.Edges))
  1270  
  1271  	v1ID, _, _ = graph.Id(config.DefaultClusterID, namespace, seServiceName, namespace, "reviews-v1", app, "v1", graph.GraphTypeVersionedApp)
  1272  	v1Node, v1NodeFound = trafficMap[v1ID]
  1273  	assert.Equal(true, v1NodeFound)
  1274  	assert.Equal(0, len(v1Node.Edges))
  1275  	assert.Equal(nil, v1Node.Metadata[graph.IsServiceEntry])
  1276  
  1277  	v2ID, _, _ = graph.Id(config.DefaultClusterID, namespace, seServiceName, namespace, "reviews-v2", app, "v2", graph.GraphTypeVersionedApp)
  1278  	v2Node, v2NodeFound = trafficMap[v2ID]
  1279  	assert.Equal(true, v2NodeFound)
  1280  	assert.Equal(0, len(v2Node.Edges))
  1281  	assert.Equal(nil, v2Node.Metadata[graph.IsServiceEntry])
  1282  }