Skip to content

Commit

Permalink
Drain node on kubeadm reset and make it possible to specify if the no…
Browse files Browse the repository at this point in the history
…de should be removed from the cluster as well
  • Loading branch information
luxas committed Dec 4, 2016
1 parent 4e22177 commit 65edcc0
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 6 deletions.
54 changes: 48 additions & 6 deletions cmd/kubeadm/app/cmd/reset.go
Expand Up @@ -33,12 +33,12 @@ import (

// NewCmdReset returns the "kubeadm reset" command
func NewCmdReset(out io.Writer) *cobra.Command {
var skipPreFlight bool
var skipPreFlight, removeNode bool
cmd := &cobra.Command{
Use: "reset",
Short: "Run this to revert any changes made to this host by 'kubeadm init' or 'kubeadm join'.",
Run: func(cmd *cobra.Command, args []string) {
r, err := NewReset(skipPreFlight)
r, err := NewReset(skipPreFlight, removeNode)
kubeadmutil.CheckErr(err)
kubeadmutil.CheckErr(r.Run(out))
},
Expand All @@ -49,12 +49,19 @@ func NewCmdReset(out io.Writer) *cobra.Command {
"skip preflight checks normally run before modifying the system",
)

cmd.PersistentFlags().BoolVar(
&removeNode, "remove-node", true,
"remove this node from the pool of nodes in this cluster",
)

return cmd
}

type Reset struct{}
type Reset struct {
removeNode bool
}

func NewReset(skipPreFlight bool) (*Reset, error) {
func NewReset(skipPreFlight, removeNode bool) (*Reset, error) {
if !skipPreFlight {
fmt.Println("[preflight] Running pre-flight checks...")

Expand All @@ -65,11 +72,20 @@ func NewReset(skipPreFlight bool) (*Reset, error) {
fmt.Println("[preflight] Skipping pre-flight checks...")
}

return &Reset{}, nil
return &Reset{
removeNode: removeNode,
}, nil
}

// Run reverts any changes made to this host by "kubeadm init" or "kubeadm join".
func (r *Reset) Run(out io.Writer) error {

// Drain and maybe remove the node from the cluster
err := drainNode(r.removeNode)
if err != nil {
fmt.Printf("[reset] Failed to drain node: [%v]\n", err)
}

serviceToStop := "kubelet"
initSystem, err := initsystem.GetInitSystem()
if err != nil {
Expand Down Expand Up @@ -98,7 +114,7 @@ func (r *Reset) Run(out io.Writer) error {
if _, err := os.Stat("/etc/kubernetes/manifests/etcd.json"); os.IsNotExist(err) {
dirsToClean = append(dirsToClean, "/var/lib/etcd")
} else {
fmt.Println("[reset] No etcd manifest found in %q, assuming external etcd.", "/etc/kubernetes/manifests/etcd.json")
fmt.Printf("[reset] No etcd manifest found in %q, assuming external etcd.\n", "/etc/kubernetes/manifests/etcd.json")
}

fmt.Printf("[reset] Deleting contents of stateful directories: %v\n", dirsToClean)
Expand All @@ -119,6 +135,32 @@ func (r *Reset) Run(out io.Writer) error {
return nil
}

func drainNode(removeNode bool) error {

fmt.Println("[reset] Draining this node...")
hostname, err := os.Hostname()
if err != nil {
return fmt.Errorf("failed to detect the hostname of this node. Won't drain this node and remove it from the cluster.")
}

kubeConfigPath := path.Join(kubeadmapi.GlobalEnvParams.KubernetesDir, "kubelet.conf")
_, err = exec.Command("kubectl", "--kubeconfig", kubeConfigPath, "drain", hostname, "--delete-local-data", "--force", "--ignore-daemonsets").Output()
if err != nil {
return fmt.Errorf("failed to drain this node [%v]", err)
}

if removeNode {
fmt.Println("[reset] Removing this node from the cluster...")

_, err = exec.Command("kubectl", "--kubeconfig", kubeConfigPath, "delete", "node", hostname).Output()
if err != nil {
return fmt.Errorf("failed to remove this node [%v]", err)
}
}

return nil
}

// cleanDir removes everything in a directory, but not the directory itself
func cleanDir(filepath string) error {
// If the directory doesn't even exist there's nothing to do, and we do
Expand Down
1 change: 1 addition & 0 deletions hack/verify-flags/known-flags.txt
Expand Up @@ -488,6 +488,7 @@ registry-burst
registry-qps
reject-methods
reject-paths
remove-node
repair-malformed-updates
replicaset-lookup-cache-size
replication-controller-lookup-cache-size
Expand Down

0 comments on commit 65edcc0

Please sign in to comment.