From 84756df8a2d26d2bf4b4c3bbc281d66e4d740e65 Mon Sep 17 00:00:00 2001 From: Darren Shepherd Date: Mon, 4 Feb 2019 21:41:47 -0700 Subject: [PATCH] Only run two service-lb if there are more than 1 nodes ready --- pkg/servicelb/controller.go | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/pkg/servicelb/controller.go b/pkg/servicelb/controller.go index 36ff1d1355..d7a9f6c301 100644 --- a/pkg/servicelb/controller.go +++ b/pkg/servicelb/controller.go @@ -229,9 +229,23 @@ func (h *handler) resolvePort(svc *core.Service, targetPort core.ServicePort) (i func (h *handler) newDeployment(svc *core.Service) (*apps.Deployment, error) { name := fmt.Sprintf("svclb-%s", svc.Name) - zero := intstr.FromInt(0) - one := intstr.FromInt(1) - two := int32(2) + zeroInt := intstr.FromInt(0) + oneInt := intstr.FromInt(1) + replicas := int32(0) + + nodes, err := h.nodeCache.List("", labels.Everything()) + if err != nil { + return nil, err + } + + for _, node := range nodes { + if Ready.IsTrue(node) { + replicas += 1 + } + if replicas >= 2 { + break + } + } dep := &apps.Deployment{ ObjectMeta: meta.ObjectMeta{ @@ -252,7 +266,7 @@ func (h *handler) newDeployment(svc *core.Service) (*apps.Deployment, error) { APIVersion: "apps/v1", }, Spec: apps.DeploymentSpec{ - Replicas: &two, + Replicas: &replicas, Selector: &meta.LabelSelector{ MatchLabels: map[string]string{ "app": name, @@ -269,8 +283,8 @@ func (h *handler) newDeployment(svc *core.Service) (*apps.Deployment, error) { Strategy: apps.DeploymentStrategy{ Type: apps.RollingUpdateDeploymentStrategyType, RollingUpdate: &apps.RollingUpdateDeployment{ - MaxSurge: &zero, - MaxUnavailable: &one, + MaxSurge: &zeroInt, + MaxUnavailable: &oneInt, }, }, },