Skip to content

Commit

Permalink
Merge pull request #100 from cloudnativelabs/99-randomize-service-end…
Browse files Browse the repository at this point in the history
…points

Randomize service endpoint addition when configuring destination on ipvs service
  • Loading branch information
murali-reddy authored Aug 4, 2017
2 parents 9e75203 + 7934759 commit fbdd9e9
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion app/controllers/network_services_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"io/ioutil"
"math/rand"
"net"
"net/http"
"reflect"
Expand Down Expand Up @@ -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() {
Expand All @@ -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)
}
}
}
Expand Down Expand Up @@ -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
}

0 comments on commit fbdd9e9

Please sign in to comment.