diff --git a/pkg/proxy/ipvs/proxier.go b/pkg/proxy/ipvs/proxier.go index ba5cbfa2532..739f36947b7 100644 --- a/pkg/proxy/ipvs/proxier.go +++ b/pkg/proxy/ipvs/proxier.go @@ -1865,7 +1865,7 @@ func (proxier *Proxier) syncEndpoint(svcPortName proxy.ServicePortName, onlyNode } // Create new endpoints - for _, ep := range sets.List(newEndpoints) { + for _, ep := range newEndpoints.UnsortedList() { ip, port, err := net.SplitHostPort(ep) if err != nil { klog.ErrorS(err, "Failed to parse endpoint", "endpoint", ep) diff --git a/pkg/proxy/ipvs/util/testing/fake.go b/pkg/proxy/ipvs/util/testing/fake.go index 5594659c9ad..dc80cf38eec 100644 --- a/pkg/proxy/ipvs/util/testing/fake.go +++ b/pkg/proxy/ipvs/util/testing/fake.go @@ -19,6 +19,7 @@ package testing import ( "fmt" "net" + "sort" "strconv" "time" @@ -53,6 +54,19 @@ func (r *RealServerKey) String() string { return net.JoinHostPort(r.Address.String(), strconv.Itoa(int(r.Port))) } +// Implement https://pkg.go.dev/sort#Interface +type byAddress []*utilipvs.RealServer + +func (a byAddress) Len() int { + return len(a) +} +func (a byAddress) Less(i, j int) bool { + return a[i].String() < a[j].String() +} +func (a byAddress) Swap(i, j int) { + a[i], a[j] = a[j], a[i] +} + // NewFake creates a fake ipvs implementation - a cache store. func NewFake() *FakeIPVS { return &FakeIPVS{ @@ -155,6 +169,8 @@ func (f *FakeIPVS) AddRealServer(serv *utilipvs.VirtualServer, dest *utilipvs.Re f.Destinations[key] = dests } f.Destinations[key] = append(f.Destinations[key], dest) + // The tests assumes that the slice is sorted + sort.Sort(byAddress(f.Destinations[key])) return nil }