Repository: istio/istio
File: pilot/pkg/xds/endpoints/ep_filters_test.go (Go)

Code before:
				{
					LbEps: []xdstest.LbEpInfo{
						// 3 local endpoints in network2
						{Address: "20.0.0.1", Weight: 6},
						{Address: "20.0.0.2", Weight: 6},
						{Address: "20.0.0.3", Weight: 6},
						// Nothing on network1 since gateway there does not listen on HBONE port
						// 1 endpoint on network4 is not considered reachable in ambient mode without a gateway
					},
					Weight: 18,
				},
			},
			wantWorkloadMetadata: []string{
				";ns;example;;cluster2a",
				";ns;example;;cluster2b",
				";ns;example;;cluster2b",
			},
		},
	}
	// The tests below are calling the endpoints filter from each one of the
	// networks and examines the returned filtered endpoints
	runNetworkFilterTest(t, env, ambientNetworkFiltered, "")
}

type networkFilterCase struct {
	name                 string
	proxy                *model.Proxy
	want                 []xdstest.LocLbEpInfo
	wantWorkloadMetadata []string
}

// runNetworkFilterTest calls the endpoints filter from each one of the
// networks and examines the returned filtered endpoints
func runNetworkFilterTest(t *testing.T, ds *xds.FakeDiscoveryServer, tests []networkFilterCase, subset string) {
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			cn := fmt.Sprintf("outbound|80|%s|example.ns.svc.cluster.local", subset)
			proxy := ds.SetupProxy(tt.proxy)
			b := endpoints.NewEndpointBuilder(cn, proxy, ds.PushContext())
			filtered := b.BuildClusterLoadAssignment(testShards()).Endpoints
			xdstest.CompareEndpointsOrFail(t, cn, filtered, tt.want)
			// check workload metadata
			var gotWorkloadMetadata []string
			for _, llbEndpoints := range filtered {
				for _, ep := range llbEndpoints.LbEndpoints {
					metadata := ep.Metadata.FilterMetadata[util.IstioMetadataKey].Fields["workload"].GetStringValue()
					gotWorkloadMetadata = append(gotWorkloadMetadata, metadata)
				}
			}
			if !slices.Equal(gotWorkloadMetadata, tt.wantWorkloadMetadata) {
				t.Errorf("incorrect workload metadata: got %v, want %v", gotWorkloadMetadata, tt.wantWorkloadMetadata)

Review comment (suggestion):
Without the new condition in `EndpointsByNetworkFilter` the result would be:
```
				{Address: workloadIP, Weight: 1}, // Case 1
				{Address: 10.10.10.10, Weight: 1}, // Case 2
				// Case 3 not included
				// Case 4 not included
```

Code after:
			ServicePortName: "http",
			Namespace:       "ns",
			HostName:        "example.ns.svc.cluster.local",
			EndpointPort:    8080,
			TLSMode:         "istio",
			Labels:          map[string]string{"app": "example"},
		},
	}
	svc.Unlock()

	proxy := ds.SetupProxy(&model.Proxy{
		Metadata: &model.NodeMetadata{
			Network: "",
		},
	})
	cn := "outbound|80||example.ns.svc.cluster.local"
	b := endpoints.NewEndpointBuilder(cn, proxy, ds.PushContext())
	filtered := b.BuildClusterLoadAssignment(shards).Endpoints

	want := []xdstest.LocLbEpInfo{
		{
			LbEps: []xdstest.LbEpInfo{
				{Address: workloadIP, Weight: 1}, // Case 1
				{Address: network1IP, Weight: 1}, // Case 2
				{Address: network2IP, Weight: 1}, // Case 3
				// Case 4 not included
			},
			Weight: 3,
		},
	}
	xdstest.CompareEndpointsOrFail(t, cn, filtered, want)
}

func TestEndpointsByNetworkFilter_AmbientMuiltiNetwork(t *testing.T) {
	test.SetForTest(t, &features.EnableAmbient, true)
	test.SetForTest(t, &features.EnableAmbientMultiNetwork, true)
	test.SetForTest(t, &features.EnableAmbientWaypointMultiNetwork, true)
	env := environment(t)
	env.Env().InitNetworksManager(env.Discovery)
	ambientNetworkFiltered := []networkFilterCase{
		{
			name:  "from_network1_cluster1a",
			proxy: makeWaypointProxy("network1", "cluster1a"),
			want: []xdstest.LocLbEpInfo{
				{
					LbEps: []xdstest.LbEpInfo{
						// 3 local endpoints on network1
						{Address: "10.0.0.1", Weight: 6},
						{Address: "10.0.0.2", Weight: 6},
						{Address: "10.0.0.3", Weight: 6},
						// 1 endpoint on network2, cluster2a