From bc26d9f0de3cb82604ffad36e5aca34a343187e2 Mon Sep 17 00:00:00 2001 From: Ludovic Fernandez Date: Tue, 22 May 2018 09:20:03 +0200 Subject: [PATCH] Enhance Load-balancing method validation log. --- server/server.go | 2 +- types/types.go | 21 +++++++++++++-------- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/server/server.go b/server/server.go index 201368be9..b55c1d2b1 100644 --- a/server/server.go +++ b/server/server.go @@ -1438,7 +1438,7 @@ func configureBackends(backends map[string]*types.Backend) { } } } else { - log.Debugf("Validation of load balancer method for backend %s failed: %s. Using default method wrr.", backendName, err) + log.Debugf("Backend %s: %v", backendName, err) var stickiness *types.Stickiness if backend.LoadBalancer != nil { diff --git a/types/types.go b/types/types.go index 518d5a365..eed737bee 100644 --- a/types/types.go +++ b/types/types.go @@ -211,16 +211,21 @@ var loadBalancerMethodNames = []string{ // NewLoadBalancerMethod create a new LoadBalancerMethod from a given LoadBalancer. func NewLoadBalancerMethod(loadBalancer *LoadBalancer) (LoadBalancerMethod, error) { - var method string - if loadBalancer != nil { - method = loadBalancer.Method - for i, name := range loadBalancerMethodNames { - if strings.EqualFold(name, method) { - return LoadBalancerMethod(i), nil - } + if loadBalancer == nil { + return Wrr, errors.New("no load-balancer defined, fallback to 'wrr' method") + } + + if len(loadBalancer.Method) == 0 { + return Wrr, errors.New("no load-balancing method defined, fallback to 'wrr' method") + } + + for i, name := range loadBalancerMethodNames { + if strings.EqualFold(name, loadBalancer.Method) { + return LoadBalancerMethod(i), nil } } - return Wrr, fmt.Errorf("invalid load-balancing method '%s'", method) + + return Wrr, fmt.Errorf("invalid load-balancing method %q, fallback to 'wrr' method", loadBalancer.Method) } // Configurations is for currentConfigurations Map