@@ -26,6 +26,7 @@ import (
2626 "istio.io/istio/pilot/pkg/serviceregistry/memory"
2727 "istio.io/istio/pilot/pkg/serviceregistry/util/xdsfake"
2828 "istio.io/istio/pkg/config"
29+ "istio.io/istio/pkg/config/constants"
2930 "istio.io/istio/pkg/config/host"
3031 "istio.io/istio/pkg/config/labels"
3132 "istio.io/istio/pkg/config/mesh/meshwatcher"
@@ -427,3 +428,118 @@ func TestFilterIstioEndpoint(t *testing.T) {
427428 })
428429 }
429430}
431+
432+ func TestBuildClusterLoadAssignment_InferenceServicePortFiltering(t *testing.T) {
433+ tests := []struct {
434+ name string
435+ InferencePoolService bool
436+ expectedEndpoints int
437+ }{
438+ {
439+ name: "inference service includes endpoints from all ports",
440+ InferencePoolService: true,
441+ expectedEndpoints: 3,
442+ },
443+ {
444+ name: "regular service filters endpoints by port name",
445+ InferencePoolService: false,
446+ expectedEndpoints: 1,
447+ },
448+ }
449+
450+ for _, tt := range tests {
451+ t.Run(tt.name, func(t *testing.T) {
452+ svcLabels := make(map[string]string)
453+ if tt.InferencePoolService {
454+ svcLabels[constants.InternalServiceSemantics] = constants.ServiceSemanticsInferencePool
455+ }
456+
457+ svc := &model.Service{
458+ Hostname: "example.ns.svc.cluster.local",
459+ Attributes: model.ServiceAttributes{
460+ Name: "example",
461+ Namespace: "ns",
462+ Labels: svcLabels,
463+ },
464+ Ports: model.PortList{
465+ {Port: 80, Protocol: protocol.HTTP, Name: "http-80"},
466+ {Port: 8000, Protocol: protocol.HTTP, Name: "http-8000"},
467+ {Port: 8001, Protocol: protocol.HTTP, Name: "http-8001"},
468+ },
469+ }
470+
471+ proxy := &model.Proxy{
472+ Type: model.SidecarProxy,
473+ IPAddresses: []string{"127.0.0.1"},
474+ Metadata: &model.NodeMetadata{
475+ Namespace: "ns",
476+ NodeName: "example",
477+ },
478+ ConfigNamespace: "ns",
479+ }
480+
481+ endpointIndex := model.NewEndpointIndex(model.NewXdsCache())
482+ shards, _ := endpointIndex.GetOrCreateEndpointShard("example.ns.svc.cluster.local", "ns")
483+ shards.Lock()
484+ shards.Shards[model.ShardKey{Cluster: "cluster1"}] = []*model.IstioEndpoint{
485+ {
486+ Addresses: []string{"10.0.0.1"},
487+ ServicePortName: "http-80",
488+ EndpointPort: 80,
489+ HostName: "example.ns.svc.cluster.local",
490+ Namespace: "ns",
491+ },
492+ {
493+ Addresses: []string{"10.0.0.2"},
494+ ServicePortName: "http-8000",
495+ EndpointPort: 8000,
496+ HostName: "example.ns.svc.cluster.local",
497+ Namespace: "ns",
498+ },
499+ {
500+ Addresses: []string{"10.0.0.3"},
501+ ServicePortName: "http-8001",
502+ EndpointPort: 8001,
503+ HostName: "example.ns.svc.cluster.local",
504+ Namespace: "ns",
505+ },
506+ }
507+ shards.Unlock()
508+
509+ env := model.NewEnvironment()
510+ env.ConfigStore = model.NewFakeStore()
511+ env.Watcher = meshwatcher.NewTestWatcher(&meshconfig.MeshConfig{RootNamespace: "istio-system"})
512+ meshNetworks := meshwatcher.NewFixedNetworksWatcher(nil)
513+ env.NetworksWatcher = meshNetworks
514+ env.ServiceDiscovery = &localServiceDiscovery{
515+ services: []*model.Service{svc},
516+ }
517+ xdsUpdater := xdsfake.NewFakeXDS()
518+ if err := env.InitNetworksManager(xdsUpdater); err != nil {
519+ t.Fatal(err)
520+ }
521+ env.Init()
522+
523+ push := model.NewPushContext()
524+ push.InitContext(env, nil, nil)
525+ env.SetPushContext(push)
526+
527+ builder := NewCDSEndpointBuilder(
528+ proxy, push,
529+ "outbound|80||example.ns.svc.cluster.local",
530+ model.TrafficDirectionOutbound, "", "example.ns.svc.cluster.local", 80,
531+ svc, nil)
532+
533+ cla := builder.BuildClusterLoadAssignment(endpointIndex)
534+
535+ var totalEndpoints int
536+ for _, localityLbEndpoints := range cla.Endpoints {
537+ totalEndpoints += len(localityLbEndpoints.LbEndpoints)
538+ }
539+
540+ if totalEndpoints != tt.expectedEndpoints {
541+ t.Errorf("expected %d endpoints, got %d", tt.expectedEndpoints, totalEndpoints)
542+ }
543+ })
544+ }
545+ }
0 commit comments