From 8c3bb17f56e4420d935e01f8274f7674238d7c24 Mon Sep 17 00:00:00 2001 From: Joao Morais Date: Wed, 12 Jul 2017 21:35:36 -0300 Subject: [PATCH] Add sort-backends command line option --- core/pkg/ingress/controller/controller.go | 17 +++++++++++++---- core/pkg/ingress/controller/launch.go | 4 ++++ 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/core/pkg/ingress/controller/controller.go b/core/pkg/ingress/controller/controller.go index 0b3577b0f4..0453325c96 100644 --- a/core/pkg/ingress/controller/controller.go +++ b/core/pkg/ingress/controller/controller.go @@ -146,6 +146,7 @@ type Configuration struct { UpdateStatus bool ElectionID string UpdateStatusOnShutdown bool + SortBackends bool } // newIngressController creates an Ingress controller @@ -737,6 +738,9 @@ func (ic *GenericController) getBackendServers() ([]*ingress.Backend, []*ingress } aUpstreams = append(aUpstreams, value) } + if ic.cfg.SortBackends { + sort.Sort(ingress.BackendByNameServers(aUpstreams)) + } aServers := make([]*ingress.Server, 0, len(servers)) for _, value := range servers { @@ -886,15 +890,20 @@ func (ic *GenericController) serviceEndpoints(svcKey, backendPort string, glog.Warningf("service %v does not have any active endpoints", svcKey) } + if ic.cfg.SortBackends { + sort.Sort(ingress.EndpointByAddrPort(endps)) + } upstreams = append(upstreams, endps...) break } } - rand.Seed(time.Now().UnixNano()) - for i := range upstreams { - j := rand.Intn(i + 1) - upstreams[i], upstreams[j] = upstreams[j], upstreams[i] + if !ic.cfg.SortBackends { + rand.Seed(time.Now().UnixNano()) + for i := range upstreams { + j := rand.Intn(i + 1) + upstreams[i], upstreams[j] = upstreams[j], upstreams[i] + } } return upstreams, nil diff --git a/core/pkg/ingress/controller/launch.go b/core/pkg/ingress/controller/launch.go index bb5807fd30..fbda376c62 100644 --- a/core/pkg/ingress/controller/launch.go +++ b/core/pkg/ingress/controller/launch.go @@ -93,6 +93,9 @@ func NewIngressController(backend ingress.Controller) *GenericController { UpdateStatusOnShutdown = flags.Bool("update-status-on-shutdown", true, `Indicates if the ingress controller should update the Ingress status IP/hostname when the controller is being stopped. Default is true`) + + SortBackends = flags.Bool("sort-backends", false, + `Defines if backends and it's endpoints should be sorted`) ) flags.AddGoFlagSet(flag.CommandLine) @@ -169,6 +172,7 @@ func NewIngressController(backend ingress.Controller) *GenericController { Backend: backend, ForceNamespaceIsolation: *forceIsolation, UpdateStatusOnShutdown: *UpdateStatusOnShutdown, + SortBackends: *SortBackends, } ic := newIngressController(config)