Remove unnecessary sort in kube-proxy ipvs

Sorting of endpoints before adding them to ipvs is not
needed, nor wanted. It just takes time
This commit is contained in:
Lars Ekman 2023-11-06 08:14:06 +01:00
parent 24e6b03780
commit d78a794be2
2 changed files with 17 additions and 1 deletions

View File

@ -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)

View File

@ -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
}