Merge pull request #100 from cloudnativelabs/99-randomize-service-endpoints

Randomize service endpoint addition when configuring destination on ipvs service
This commit is contained in:
Murali Reddy 2017-08-05 04:01:24 +05:30 committed by GitHub
commit fbdd9e9a6f

View File

@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"io/ioutil"
"math/rand"
"net"
"net/http"
"reflect"
@ -422,6 +423,14 @@ func buildServicesInfo() serviceInfoMap {
return serviceMap
}
func shuffle(endPoints []endpointsInfo) []endpointsInfo {
for index1 := range endPoints {
index2 := rand.Intn(index1 + 1)
endPoints[index1], endPoints[index2] = endPoints[index2], endPoints[index1]
}
return endPoints
}
func buildEndpointsInfo() endpointsInfoMap {
endpointsMap := make(endpointsInfoMap)
for _, ep := range watchers.EndpointsWatcher.List() {
@ -432,7 +441,7 @@ func buildEndpointsInfo() endpointsInfoMap {
for _, addr := range ep_subset.Addresses {
endpoints = append(endpoints, endpointsInfo{ip: addr.IP, port: int(port.Port)})
}
endpointsMap[svcId] = endpoints
endpointsMap[svcId] = shuffle(endpoints)
}
}
}
@ -856,5 +865,7 @@ func NewNetworkServicesController(clientset *kubernetes.Clientset, config *optio
watchers.EndpointsWatcher.RegisterHandler(&nsc)
watchers.ServiceWatcher.RegisterHandler(&nsc)
rand.Seed(time.Now().UnixNano())
return &nsc, nil
}