Skip to content

Commit

Permalink
Merge pull request kubernetes#42713 from luxas/kubeadm_fix_reset
Browse files Browse the repository at this point in the history
Automatic merge from submit-queue (batch tested with PRs 43018, 42713)

kubeadm: Don't drain and remove the current node on kubeadm reset

**What this PR does / why we need it**:

**Which issue this PR fixes** *(optional, in `fixes #<issue number>(, fixes #<issue_number>, ...)` format, will close that issue when PR gets merged)*: fixes #

**Special notes for your reviewer**:

In v1.5, `kubeadm reset` would drain your node and remove it from your cluster if you specified, but now in v1.6 we can't do that due to the RBAC rules we have set up.

After conversations with @liggitt, I also agree this functionality was somehow a little mis-placed (though still very convenient to use), so we're removing it for v1.6.

It's the system administrator's duty to drain and remove nodes from the cluster, not the nodes' responsibility.

The current behavior is therefore a bug that needs to be fixed in v1.6

**Release note**:

```release-note
kubeadm: `kubeadm reset` won't drain and remove the current node anymore
```
@liggitt @deads2k @jbeda @dmmcquay @pires @errordeveloper
  • Loading branch information
Kubernetes Submit Queue authored Mar 14, 2017
2 parents 1221779 + c7fc530 commit 5826b09
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 5826b09

Please sign in to comment.