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

Commit

Permalink
Delete nodes and finish rename
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 19, 2019
1 parent 5bef5f9 commit 6b8d570
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 26 deletions.
8 changes: 4 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ ADD go.sum .
RUN go mod download
RUN curl -L https://dl.k8s.io/v1.14.3/kubernetes-client-linux-amd64.tar.gz | tar xvz
ADD cmd cmd
ADD actuators capkactuators
ADD actuators actuators
ADD kind kind
ADD execer execer
ADD third_party third_party

RUN go install -v ./cmd/capk-manager
RUN go install -v ./cmd/capd-manager
RUN GO111MODULE="on" go get sigs.k8s.io/kind@v0.3.0
RUN curl https://get.docker.com | sh

FROM golang:1.12.5
COPY --from=0 /cluster-api-upgrade-tool/kubernetes/client/bin/kubectl /usr/local/bin
COPY --from=0 /go/bin/capk-manager /usr/local/bin
COPY --from=0 /go/bin/capd-manager /usr/local/bin
COPY --from=0 /go/bin/kind /usr/local/bin
COPY --from=0 /usr/bin/docker /usr/local/bin
ENTRYPOINT ["capk-manager"]
ENTRYPOINT ["capd-manager"]
1 change: 1 addition & 0 deletions actuators/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ func providerID(name string) string {
return fmt.Sprintf("docker://%s", name)
}

// TODO there is a better way to do this.
func CAPIroleToKindRole(CAPIRole string) string {
if CAPIRole == clusterAPIControlPlaneSetLabel {
return constants.ControlPlaneNodeRoleValue
Expand Down
31 changes: 22 additions & 9 deletions cmd/capdctl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"fmt"
"io/ioutil"
"os"
"strings"

"github.com/chuckha/cluster-api-provider-docker/execer"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -148,7 +149,7 @@ func machineYAML(opts *machineOptions) string {
Namespace: *opts.namespace,
Labels: map[string]string{
"cluster.k8s.io/cluster-name": *opts.clusterName,
"set": *opts.set,
"set": *opts.set,
},
},
Spec: v1alpha1.MachineSpec{
Expand All @@ -172,6 +173,18 @@ func machineYAML(opts *machineOptions) string {

func makeManagementCluster(clusterName string) {
kind := execer.NewClient("kind")
// if a cluster named kind already exists then we assume we're good to go:
clusters, err := kind.RunCommandReturnOutput("get", "clusters")
if err != nil {
panic(err)
}
for _, cluster := range strings.Split(clusters, "\n") {
if strings.TrimSpace(cluster) == "kind" {
fmt.Println("Management cluster detected")
return
}
}

// start kind with docker mount
kindConfig, err := kindConfigFile()
if err != nil {
Expand Down Expand Up @@ -222,7 +235,7 @@ kind: Namespace
metadata:
labels:
controller-tools.k8s.io: "1.0"
name: kind-provider-system
name: docker-provider-system
---
apiVersion: v1
kind: Namespace
Expand All @@ -236,13 +249,13 @@ kind: StatefulSet
metadata:
labels:
control-plane: controller-manager
name: kind-provider-controller-manager
namespace: kind-provider-system
name: docker-provider-controller-manager
namespace: docker-provider-system
spec:
selector:
matchLabels:
control-plane: controller-manager
serviceName: kind-provider-controller-manager-service
serviceName: docker-provider-controller-manager-service
template:
metadata:
labels:
Expand Down Expand Up @@ -1214,7 +1227,7 @@ status:
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: kind-provider-manager-role
name: docker-provider-manager-role
rules:
- apiGroups:
- cluster.k8s.io
Expand Down Expand Up @@ -1377,15 +1390,15 @@ apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
creationTimestamp: null
name: kind-provider-manager-rolebinding
name: docker-provider-manager-rolebinding
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: kind-provider-manager-role
name: docker-provider-manager-role
subjects:
- kind: ServiceAccount
name: default
namespace: kind-provider-system
namespace: docker-provider-system
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
Expand Down
27 changes: 27 additions & 0 deletions kind/actions/cluster_actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,3 +273,30 @@ func SetNodeRef(clusterName, nodeName string) error {

return nil
}

func RemoveNode(clusterName, nodeName 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(
"kubectl",
"--kubeconfig", "/etc/kubernetes/admin.conf",
"delete",
"node", nodeName,
)
lines, err := exec.CombinedOutputLines(cmd)
if err != nil {
for _, line := range lines {
fmt.Println(line)
}
return errors.Wrap(err, "failed to remove node from cluster")
}

return nil
}
27 changes: 14 additions & 13 deletions kind/actions/kind.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,25 +152,26 @@ func AddWorker(clusterName, version string) (*nodes.Node, error) {

// DeleteNode removes a node from a cluster and cleans up docker.
func DeleteNode(clusterName, nodeName string) error {
ns, err := nodes.List(
nodeList, err := nodes.List(
fmt.Sprintf("label=%s=%s", constants.ClusterLabelKey, clusterName),
fmt.Sprintf("name=%s", nodeName))
fmt.Sprintf("name=%s$", nodeName),
)
if err != nil {
panic(err)
return nil
}
if len(ns) == 0 {
fmt.Println("Could not find node", nodeName)

if len(nodeList) < 1 {
return errors.Errorf("could not find node %q", nodeName)
}
deleteCmd := exec.Command("kubectl", "delete", "node", nodeName, "--kubeconfig", KubeConfigPath(clusterName))
if err := deleteCmd.SetStderr(os.Stdout).SetStdout(os.Stdout).Run(); err != nil {
fmt.Printf("%+v\n", err)
panic(err)
if len(nodeList) > 1 {
return errors.Errorf("duplicate nodes named %q, cannot continue", nodeName)
}
if err := nodes.Delete(ns...); err != nil {
panic(err)

node := nodeList[0]
if err := RemoveNode(clusterName, nodeName); err != nil {
return err
}
fmt.Println("Deleted a node")
return nil
return nodes.Delete(node)
}

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

0 comments on commit 6b8d570

Please sign in to comment.