Skip to content

Commit 5de6299

Browse files
dgnaslakknutsen
authored andcommitted
tests: add more unit tests for InferencePools with >1 targetPort (#58395)
Followup to #58238
1 parent 02957f6 commit 5de6299

File tree

2 files changed

+138
-0
lines changed

2 files changed

+138
-0
lines changed

pilot/pkg/networking/core/cluster_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,18 @@ func TestBuildClustersForInferencePoolServices(t *testing.T) {
329329
proxyType: model.SidecarProxy,
330330
InferencePoolService: true,
331331
},
332+
{
333+
testName: "InferencePool service creates single cluster for multiple ports",
334+
clusterName: "outbound|8080||*.example.org",
335+
proxyType: model.Router,
336+
InferencePoolService: true,
337+
},
338+
{
339+
testName: "Regular service creates one cluster per port",
340+
clusterName: "outbound|8080||*.example.org",
341+
proxyType: model.Router,
342+
InferencePoolService: false,
343+
},
332344
}
333345
for _, tc := range cases {
334346
t.Run(tc.testName, func(t *testing.T) {
@@ -362,6 +374,16 @@ func TestBuildClustersForInferencePoolServices(t *testing.T) {
362374
g.Expect(overrideHostPolicy.GetOverrideHostSources()).NotTo(BeEmpty())
363375
g.Expect(overrideHostPolicy.GetOverrideHostSources()[0].GetMetadata().GetKey()).To(Equal("envoy.lb"))
364376
g.Expect(overrideHostPolicy.GetOverrideHostSources()[0].GetMetadata().GetPath()[0].GetKey()).To(Equal("x-gateway-destination-endpoint"))
377+
var serviceClusters []string
378+
for _, c := range clusters {
379+
if strings.Contains(c.Name, "*.example.org") && strings.HasPrefix(c.Name, "outbound|") {
380+
serviceClusters = append(serviceClusters, c.Name)
381+
}
382+
}
383+
g.Expect(len(serviceClusters)).To(Equal(1),
384+
"expected single cluster but got %d: %v", len(serviceClusters), serviceClusters)
385+
g.Expect(serviceClusters[0]).To(ContainSubstring("|8080|"),
386+
"expected cluster to use first port 8080 but got %s", serviceClusters[0])
365387
}
366388
})
367389
}

pilot/pkg/xds/endpoints/endpoint_builder_test.go

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)