From ca72d9abec84e05d5c568f21a7ff57f1a6e03bcb Mon Sep 17 00:00:00 2001 From: "Lubomir I. Ivanov" Date: Wed, 6 Feb 2019 05:33:20 +0200 Subject: [PATCH] implicitly handle load balancers This preserves load balancers as being part of the node lists, but makes it possible to handle the case where the user did not provide a LB in the config. In such a case the function populateLoadBalancer() adds this "kind node" automatically. Includes minor cleanup in types.go and validate.go. --- pkg/cluster/config/v1alpha2/types.go | 6 ++--- pkg/cluster/config/validate.go | 2 +- pkg/cluster/context.go | 34 ++++++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 4 deletions(-) diff --git a/pkg/cluster/config/v1alpha2/types.go b/pkg/cluster/config/v1alpha2/types.go index c25cdafb46..5450b01cf6 100644 --- a/pkg/cluster/config/v1alpha2/types.go +++ b/pkg/cluster/config/v1alpha2/types.go @@ -28,7 +28,7 @@ type Config struct { // TypeMeta representing the type of the object and its API schema version. metav1.TypeMeta `json:",inline"` - // nodes constains the list of nodes defined in the `kind` Config + // Nodes constains the list of nodes defined in the `kind` Config Nodes []Node `json:"nodes"` } @@ -39,7 +39,7 @@ type Node struct { // Replicas is the number of desired node replicas. // Defaults to 1 Replicas *int32 `json:"replicas,omitempty"` - // Role defines the role of the nodw in the in the Kubernetes cluster managed by `kind` + // Role defines the role of the node in the in the Kubernetes cluster managed by `kind` // Defaults to "control-plane" Role NodeRole `json:"role,omitempty"` // Image is the node image to use when running the cluster @@ -70,7 +70,7 @@ const ( ExternalEtcdRole NodeRole = "external-etcd" // ExternalLoadBalancerRole identifies a node that hosts an external load balancer for API server // in HA configurations. - // WARNING: this node type is not yet implemented! + // If multiple control-plane nodes exists, a node with this role will be automatically if missing. // Please note that `kind` nodes hosting external load balancer are not kubernetes nodes ExternalLoadBalancerRole NodeRole = "external-load-balancer" ) diff --git a/pkg/cluster/config/validate.go b/pkg/cluster/config/validate.go index e881c44045..2c29b203a7 100644 --- a/pkg/cluster/config/validate.go +++ b/pkg/cluster/config/validate.go @@ -30,7 +30,7 @@ func (c *Config) Validate() error { // All nodes in the config should be valid for i, n := range c.Nodes { if err := n.Validate(); err != nil { - errs = append(errs, errors.Errorf("invalid configuration for node %d", i)) + errs = append(errs, errors.Errorf("invalid configuration for node %d: %v", i, err)) } } diff --git a/pkg/cluster/context.go b/pkg/cluster/context.go index ecda19ab4d..5b9121d1da 100644 --- a/pkg/cluster/context.go +++ b/pkg/cluster/context.go @@ -106,6 +106,9 @@ func (c *Context) Create(cfg *config.Config, retain bool, wait time.Duration) er // may be constructed in memory rather than from disk) encoding.Scheme.Default(cfg) + // check if an automatic load balancer has to be added + populateLoadBalancer(cfg) + // then validate if err := cfg.Validate(); err != nil { return err @@ -207,3 +210,34 @@ func (c *Context) CollectLogs(dir string) error { } return logs.Collect(nodes, dir) } + +// populateLoadBalancer automatically adds a load balancer node to the list +// of nodes in a config in a case where multiple control plane replicas exist. +// The node uses the image of the first control-plane node in the list. +func populateLoadBalancer(cfg *config.Config) { + var cpCount int32 + var image string + for _, node := range cfg.Nodes { + switch node.Role { + case config.ExternalLoadBalancerRole: + return // found lb; no need to add one + case config.ControlPlaneRole: + if image == "" { + image = node.Image + } + if node.Replicas == nil { + cpCount++ + } else { + cpCount += *node.Replicas + } + } + } + if cpCount > 1 { + log.Info("Automatically creating an external load balancer node") + n := config.Node{ + Role: config.ExternalLoadBalancerRole, + Image: image, + } + cfg.Nodes = append(cfg.Nodes, n) + } +}