Skip to content

Commit

Permalink
kubeadm: Don't drain and remove the current node on kubeadm reset
Browse files Browse the repository at this point in the history
  • Loading branch information
luxas committed Mar 8, 2017
1 parent 55d500e commit c7fc530
Showing 1 changed file with 5 additions and 57 deletions.
62 changes: 5 additions & 57 deletions cmd/kubeadm/app/cmd/reset.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"os"
"os/exec"
"path/filepath"
"strings"

"github.com/spf13/cobra"

Expand All @@ -36,13 +35,13 @@ import (

// NewCmdReset returns the "kubeadm reset" command
func NewCmdReset(out io.Writer) *cobra.Command {
var skipPreFlight, removeNode bool
var skipPreFlight bool
var certsDir string
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, removeNode, certsDir)
r, err := NewReset(skipPreFlight, certsDir)
kubeadmutil.CheckErr(err)
kubeadmutil.CheckErr(r.Run(out))
},
Expand All @@ -53,11 +52,6 @@ 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",
)

cmd.PersistentFlags().StringVar(
&certsDir, "cert-dir", kubeadmapiext.DefaultCertificatesDir,
"The path to the directory where the certificates are stored. If specified, clean this directory.",
Expand All @@ -67,11 +61,10 @@ func NewCmdReset(out io.Writer) *cobra.Command {
}

type Reset struct {
removeNode bool
certsDir string
certsDir string
}

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

Expand All @@ -83,20 +76,13 @@ func NewReset(skipPreFlight, removeNode bool, certsDir string) (*Reset, error) {
}

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

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

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

// Try to stop the kubelet service
initSystem, err := initsystem.GetInitSystem()
if err != nil {
Expand Down Expand Up @@ -151,44 +137,6 @@ func (r *Reset) Run(out io.Writer) error {
return nil
}

func drainAndRemoveNode(removeNode bool) error {
hostname, err := os.Hostname()
if err != nil {
return fmt.Errorf("failed to detect node hostname")
}
hostname = strings.ToLower(hostname)

// TODO: Use the "native" k8s client for this once we're confident the versioned is working
kubeConfigPath := filepath.Join(kubeadmapi.GlobalEnvParams.KubernetesDir, kubeadmconstants.KubeletKubeConfigFileName)

getNodesCmd := fmt.Sprintf("kubectl --kubeconfig %s get nodes | grep %s", kubeConfigPath, hostname)
output, err := exec.Command("sh", "-c", getNodesCmd).Output()
if err != nil {
// kubeadm shouldn't drain and/or remove the node when it doesn't exist anymore
return fmt.Errorf("failed to list nodes: %v", err)
}
if len(output) == 0 {
return fmt.Errorf("list nodes request returned zero entries")
}

fmt.Printf("[reset] Draining node: %q\n", hostname)

_, err = exec.Command("kubectl", "--kubeconfig", kubeConfigPath, "drain", hostname, "--delete-local-data", "--force", "--ignore-daemonsets").Output()
if err != nil {
return fmt.Errorf("failed to drain node %q: %v", hostname, err)
}

if removeNode {
fmt.Printf("[reset] Removing node: %q\n", hostname)
_, err = exec.Command("kubectl", "--kubeconfig", kubeConfigPath, "delete", "node", hostname).Output()
if err != nil {
return fmt.Errorf("failed to remove node %q: %v", hostname, 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

0 comments on commit c7fc530

Please sign in to comment.