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

Commit

Permalink
proper delete
Browse files Browse the repository at this point in the history
Signed-off-by: Chuck Ha <[email protected]>
  • Loading branch information
chuckha committed Jun 21, 2019
1 parent d180db9 commit 7c10ce7
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 13 deletions.
5 changes: 1 addition & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# Cluster API Provider Docker

A temporary home for CAPD

## Manager Container Image

A sample is built and hosted at `gcr.io/kubernetes1-226021/capd-manager:latest`
Expand All @@ -23,7 +21,7 @@ Requires a google cloud project

# Testing out CAPD

Tested on: Linux, OS X
Tested on: Linux, works ok on OS X sometimes

Requirements: `kind` and `kubectl`

Expand Down Expand Up @@ -58,4 +56,3 @@ The kubeconfig is on the management cluster in secrets. Grab it and write it to
`kubectl get secrets -o jsonpath='{.data.kubeconfig}' kubeconfig-my-cluster | base64 --decode > ~/.kube/kind-config-my-cluster`

`kubectl get po --all-namespaces --kubeconfig ~/.kube/kind-config-my-cluster`

6 changes: 5 additions & 1 deletion actuators/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,11 @@ func (m *Machine) Delete(ctx context.Context, cluster *clusterv1.Cluster, machin
return err
}
if exists {
return actions.DeleteNode(cluster.Name, machine.GetName())
setValue := getRole(machine)
if setValue == clusterAPIControlPlaneSetLabel {
return actions.DeleteControlPlane(cluster.Name, machine.GetName())
}
return actions.DeleteWorker(cluster.Name, machine.GetName())
}
return nil
}
Expand Down
30 changes: 27 additions & 3 deletions kind/actions/cluster_actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,13 +308,16 @@ func GetNodeRefUID(clusterName, nodeName string) (string, error) {

func DeleteClusterNode(clusterName, nodeName string) error {
// get all control plane nodes
allNodes, err := nodes.List(fmt.Sprintf("label=%s=%s", constants.ClusterLabelKey, clusterName))
allControlPlanes, err := nodes.List(
fmt.Sprintf("label=%s=%s", constants.ClusterLabelKey, clusterName),
fmt.Sprintf("label=%s=%s", constants.NodeRoleKey, constants.ControlPlaneNodeRoleValue),
)
if err != nil {
return err
}
var node nodes.Node
// pick one that doesn't match the node name we are trying to delete
for _, n := range allNodes {
for _, n := range allControlPlanes {
if n.Name() != nodeName {
node = n
break
Expand All @@ -330,7 +333,28 @@ func DeleteClusterNode(clusterName, nodeName string) error {
for _, line := range lines {
fmt.Println(line)
}
return errors.Wrap(err, "failed update providerID")
return errors.Wrap(err, "failed to delete cluster node")
}
return nil
}

func KubeadmReset(clusterName string) error {
allNodes, err := nodes.List(fmt.Sprintf("label=%s=%s", constants.ClusterLabelKey, clusterName))
if err != nil {
return nil
}

node, err := nodes.BootstrapControlPlaneNode(allNodes)
if err != nil {
return err
}
cmd := node.Command("kubeadm", "reset", "--force")
lines, err := exec.CombinedOutputLines(cmd)
if err != nil {
for _, line := range lines {
fmt.Println(line)
}
return errors.Wrap(err, "failed to reset node")
}

return nil
Expand Down
35 changes: 30 additions & 5 deletions kind/actions/kind.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,7 @@ func AddWorker(clusterName, machineName, version string) (*nodes.Node, error) {
return worker, nil
}

// DeleteNode removes a node from a cluster and cleans up docker.
func DeleteNode(clusterName, nodeName string) error {
func DeleteControlPlane(clusterName, nodeName string) error {
nodeList, err := nodes.List(
fmt.Sprintf("label=%s=%s", constants.ClusterLabelKey, clusterName),
fmt.Sprintf("name=%s$", nodeName),
Expand All @@ -166,15 +165,41 @@ func DeleteNode(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 {
return err
}

if err := nodes.Delete(node); err != nil {
return err
}
// TODO: deal with not found better
// delete the kubernetes node reference from the cluster
return ConfigureLoadBalancer(clusterName)

}

func DeleteWorker(clusterName, nodeName string) error {
nodeList, err := nodes.List(
fmt.Sprintf("label=%s=%s", constants.ClusterLabelKey, clusterName),
fmt.Sprintf("name=%s$", nodeName),
)
if err != nil {
return nil
}

// assume it's already deleted
if len(nodeList) < 1 {
return nil
}
// pick the first one, but there should never be multiples since docker requires name to be unique.
node := nodeList[0]

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

func ListControlPlanes(clusterName string) ([]nodes.Node, error) {
Expand Down

0 comments on commit 7c10ce7

Please sign in to comment.