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

Commit

Permalink
Delete works for control planes
Browse files Browse the repository at this point in the history
Signed-off-by: Chuck Ha <chuckh@vmware.com>
  • Loading branch information
chuckha committed Jun 21, 2019
1 parent 7c10ce7 commit bae8376
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 17 deletions.
8 changes: 2 additions & 6 deletions actuators/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,21 +178,17 @@ func (m *Machine) Create(ctx context.Context, c *clusterv1.Cluster, machine *clu

// Delete returns nil when the machine no longer exists or when a successful delete has happened.
func (m *Machine) Delete(ctx context.Context, cluster *clusterv1.Cluster, machine *clusterv1.Machine) error {
if machine.Status.NodeRef != nil {
fmt.Printf("[delete] machine status noderef name: %q/n", machine.Status.NodeRef.Name)
} else {
fmt.Println("[delete] machine noderef is nil...")
}

exists, err := m.Exists(ctx, cluster, machine)
if err != nil {
return err
}
if exists {
setValue := getRole(machine)
if setValue == clusterAPIControlPlaneSetLabel {
fmt.Printf("Deleting a control plane: %q\n", machine.GetName())
return actions.DeleteControlPlane(cluster.Name, machine.GetName())
}
fmt.Printf("Deleting a worker: %q\n", machine.GetName())
return actions.DeleteWorker(cluster.Name, machine.GetName())
}
return nil
Expand Down
19 changes: 11 additions & 8 deletions kind/actions/cluster_actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ func GetNodeRefUID(clusterName, nodeName string) (string, error) {
return strings.TrimSpace(lines[0]), nil
}

// DeleteClusterNode will remove
func DeleteClusterNode(clusterName, nodeName string) error {
// get all control plane nodes
allControlPlanes, err := nodes.List(
Expand Down Expand Up @@ -338,16 +339,18 @@ func DeleteClusterNode(clusterName, nodeName string) error {
return nil
}

func KubeadmReset(clusterName string) error {
allNodes, err := nodes.List(fmt.Sprintf("label=%s=%s", constants.ClusterLabelKey, clusterName))
if err != nil {
return nil
// KubeadmReset will run `kubeadm reset` on the control plane to remove
func KubeadmReset(clusterName, nodeName string) error {
nodeList, err := nodes.List(
fmt.Sprintf("label=%s=%s", constants.ClusterLabelKey, clusterName),
fmt.Sprintf("label=%s=%s", constants.NodeRoleKey, constants.ControlPlaneNodeRoleValue),
fmt.Sprintf("name=^%s$", nodeName),
)
if len(nodeList) < 1 {
return errors.Errorf("could nto find node %q", nodeName)
}
node := nodeList[0]

node, err := nodes.BootstrapControlPlaneNode(allNodes)
if err != nil {
return err
}
cmd := node.Command("kubeadm", "reset", "--force")
lines, err := exec.CombinedOutputLines(cmd)
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions kind/actions/kind.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ func AddWorker(clusterName, machineName, version string) (*nodes.Node, error) {
return worker, nil
}

// DeleteControlPlane will take all steps necessary to remove a control plane node from a cluster.
func DeleteControlPlane(clusterName, nodeName string) error {
nodeList, err := nodes.List(
fmt.Sprintf("label=%s=%s", constants.ClusterLabelKey, clusterName),
Expand All @@ -164,19 +165,18 @@ func DeleteControlPlane(clusterName, nodeName string) error {

// pick the first one, but there should never be multiples since docker requires name to be unique.
node := nodeList[0]
// Delete the infrastructure
if err := DeleteClusterNode(clusterName, nodeName); err != nil {
return err
}
if err := KubeadmReset(clusterName); err != nil {
if err := KubeadmReset(clusterName, nodeName); err != nil {
return err
}

// Delete the infrastructure
if err := nodes.Delete(node); err != nil {
return err
}
return ConfigureLoadBalancer(clusterName)

}

func DeleteWorker(clusterName, nodeName string) error {
Expand Down

0 comments on commit bae8376

Please sign in to comment.