Skip to content

Commit

Permalink
implicitly handle load balancers
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
neolit123 committed Feb 6, 2019
1 parent d1773a7 commit ca72d9a
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
6 changes: 3 additions & 3 deletions pkg/cluster/config/v1alpha2/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}

Expand All @@ -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
Expand Down Expand Up @@ -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"
)
2 changes: 1 addition & 1 deletion pkg/cluster/config/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}

Expand Down
34 changes: 34 additions & 0 deletions pkg/cluster/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}

0 comments on commit ca72d9a

Please sign in to comment.