Skip to content

Commit ccb6a60

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

File tree

2 files changed

+137
-0
lines changed

2 files changed

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

0 commit comments

Comments
 (0)