Skip to content
This repository has been archived by the owner on Sep 24, 2021. It is now read-only.

Commit

Permalink
Cluster reconciliation should reconcile exited ELB
Browse files Browse the repository at this point in the history
* get only running ELB containers
* remove exited ELB containers with name conflict

Signed-off-by: Ashish Amarnath <[email protected]>
  • Loading branch information
ashish-amarnath committed Jul 3, 2019
1 parent f60f797 commit 3aa185e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
7 changes: 7 additions & 0 deletions actuators/actuators.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ import (
"sigs.k8s.io/kind/pkg/cluster/nodes"
)

const (
containerRunningStatus = "running"
)

func getRole(machine *clusterv1.Machine) string {
// Figure out what kind of node we're making
labels := machine.GetLabels()
Expand All @@ -40,9 +44,11 @@ func getRole(machine *clusterv1.Machine) string {
}

func getExternalLoadBalancerNode(clusterName string) (*nodes.Node, error) {
fmt.Printf("Getting external load balancer node for cluster %q\n", clusterName)
elb, err := nodes.List(
fmt.Sprintf("label=%s=%s", constants.NodeRoleKey, constants.ExternalLoadBalancerNodeRoleValue),
fmt.Sprintf("label=%s=%s", constants.ClusterLabelKey, clusterName),
fmt.Sprintf("status=%s", containerRunningStatus),
)
if err != nil {
return nil, err
Expand All @@ -53,6 +59,7 @@ func getExternalLoadBalancerNode(clusterName string) (*nodes.Node, error) {
if len(elb) > 1 {
return nil, errors.New("too many external load balancers")
}
fmt.Printf("External loadbalancer node for cluster %q is %v\n", clusterName, elb[0])
return &elb[0], nil
}

Expand Down
1 change: 1 addition & 0 deletions actuators/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type Cluster struct {

// Reconcile setups an external load balancer for the cluster if needed
func (c *Cluster) Reconcile(cluster *clusterv1.Cluster) error {
fmt.Printf("Reconciling cluster %s/%s\n", cluster.Namespace, cluster.Name)
elb, err := getExternalLoadBalancerNode(cluster.Name)
if err != nil {
c.Log.Error(err, "Error getting external load balancer node")
Expand Down
24 changes: 24 additions & 0 deletions kind/actions/kind.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,34 @@ func AddControlPlane(clusterName, machineName, version string) (*nodes.Node, err
return controlPlane, nil
}

func removeExitedELBWithNameConflict(name string) error {
exitedLB, err := nodes.List(
fmt.Sprintf("label=%s=%s", constants.NodeRoleKey, constants.ExternalLoadBalancerNodeRoleValue),
fmt.Sprintf("name=%s", name),
fmt.Sprintf("status=exited"),
)

if err != nil {
return errors.Wrapf(err, "failed to list exited external load balancer nodes with name %q", name)
}

if len(exitedLB) > 0 {
// only one container with given name should exist, if any.
fmt.Printf("Removing exited ELB %q\n", exitedLB[0].Name())
return nodes.Delete(exitedLB[0])
}
return nil
}

// SetUpLoadBalancer creates a load balancer but does not configure it.
func SetUpLoadBalancer(clusterName string) (*nodes.Node, error) {
clusterLabel := fmt.Sprintf("%s=%s", constants.ClusterLabelKey, clusterName)
name := fmt.Sprintf("%s-%s", clusterName, constants.ExternalLoadBalancerNodeRoleValue)
err := removeExitedELBWithNameConflict(name)
if err != nil {
return nil, errors.Wrapf(err, "failed to delete exited load balancer node %q", name)
}

return nodes.CreateExternalLoadBalancerNode(
name,
loadbalancer.Image,
Expand Down

0 comments on commit 3aa185e

Please sign in to comment.