Skip to content

Commit

Permalink
Implement kubeadm reset
Browse files Browse the repository at this point in the history
  • Loading branch information
luxas committed Oct 14, 2016
1 parent e0fc43b commit 7460ab2
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 4 deletions.
1 change: 1 addition & 0 deletions cmd/kubeadm/app/cmd/cmd.go
Expand Up @@ -80,6 +80,7 @@ func NewKubeadmCommand(f cmdutil.Factory, in io.Reader, out, err io.Writer) *cob

cmds.AddCommand(NewCmdInit(out))
cmds.AddCommand(NewCmdJoin(out))
cmds.AddCommand(NewCmdReset(out))
cmds.AddCommand(NewCmdVersion(out))

return cmds
Expand Down
2 changes: 1 addition & 1 deletion cmd/kubeadm/app/cmd/init.go
Expand Up @@ -150,7 +150,7 @@ func NewInit(cfgPath string, cfg *kubeadmapi.MasterConfiguration, skipPreFlight
}

if !skipPreFlight {
fmt.Println("<cmd/init> Running pre-flight checks")
fmt.Println("Running pre-flight checks")
err := preflight.RunInitMasterChecks()
if err != nil {
return nil, err
Expand Down
91 changes: 91 additions & 0 deletions cmd/kubeadm/app/cmd/reset.go
@@ -0,0 +1,91 @@
/*
Copyright 2014 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package cmd

import (
"fmt"
"io"
"os"
"os/exec"

"github.com/spf13/cobra"

"k8s.io/kubernetes/cmd/kubeadm/app/preflight"
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
"k8s.io/kubernetes/pkg/util/initsystem"
)

func NewCmdReset(out io.Writer) *cobra.Command {
var skipPreFlight bool
cmd := &cobra.Command{
Use: "reset",
Short: "Revert the actions kubeadm init or join made to the machine",
Run: func(cmd *cobra.Command, args []string) {
err := RunReset(out, cmd, skipPreFlight)
cmdutil.CheckErr(err)
},
}

cmd.PersistentFlags().BoolVar(
&skipPreFlight, "skip-preflight-checks", false,
"skip preflight checks normally run before modifying the system",
)

return cmd
}

func RunReset(out io.Writer, cmd *cobra.Command, skipPreFlight bool) error {

if !skipPreFlight {
fmt.Println("Running pre-flight checks")
err := preflight.RunResetCheck()
if err != nil {
return err
}
} else {
fmt.Println("Skipping pre-flight checks")
}

serviceToStop := "kubelet"
initSystem := initsystem.GetInitSystem()
if initSystem != nil {
fmt.Printf("Stopping the %s service...\n", serviceToStop)
initSystem.ServiceStop(serviceToStop)
} else {
fmt.Printf("no supported init system detected, skipping service checks for %s. kubeadm reset may not have full effect now.\n", serviceToStop)
}

fmt.Printf("Unmounting directories in /var/lib/kubelet...\n")
// Don't check for errors here, since umount will return a non-zero exit code if there is no directories to umount
exec.Command("sh", "-c", "cat /proc/mounts | awk '{print $2}' | grep '/var/lib/kubelet' | xargs umount").Run()

dirsToRemove := []string{"/var/lib/kubelet", "/var/lib/etcd", "/etc/kubernetes"}
fmt.Printf("Deleting the stateful directories: [%v]\n", dirsToRemove)
for _, dir := range dirsToRemove {
err := os.RemoveAll(dir)
if err != nil {
fmt.Errorf("failed to remove directory: [%v]", err)
}
}

fmt.Printf("Stopping all running docker containers...")
if err := exec.Command("sh", "-c", "docker ps -q | xargs docker kill").Run(); err != nil {
fmt.Printf("failed to stop the running containers")
}

return nil
}
11 changes: 8 additions & 3 deletions cmd/kubeadm/app/preflight/checks.go
Expand Up @@ -191,10 +191,7 @@ func RunJoinNodeChecks() error {
IsRootCheck{root: true},
ServiceCheck{service: "docker"},
ServiceCheck{service: "kubelet"},
PortOpenCheck{port: 8080},
PortOpenCheck{port: 10250},
PortOpenCheck{port: 10251},
PortOpenCheck{port: 10252},
DirAvailableCheck{path: "/etc/kubernetes"},
DirAvailableCheck{path: "/var/lib/kubelet"},
InPathCheck{executable: "ebtables", mandatory: true},
Expand All @@ -211,6 +208,14 @@ func RunJoinNodeChecks() error {
return runChecks(checks)
}

func RunResetCheck() error {
checks := []PreFlightCheck{
IsRootCheck{root: true},
}

return runChecks(checks)
}

// runChecks runs each check, displays it's warnings/errors, and once all
// are processed will exit if any errors occurred.
func runChecks(checks []PreFlightCheck) error {
Expand Down
18 changes: 18 additions & 0 deletions pkg/util/initsystem/initsystem.go
Expand Up @@ -22,6 +22,12 @@ import (
)

type InitSystem interface {
// ServiceStart tries to start a specific service
ServiceStart(service string) error

// ServiceStop tries to stop a specific service
ServiceStop(service string) error

// ServiceExists ensures the service is defined for this init system.
ServiceExists(service string) bool

Expand All @@ -34,6 +40,18 @@ type InitSystem interface {

type SystemdInitSystem struct{}

func (sysd SystemdInitSystem) ServiceStart(service string) error {
args := []string{"start", service}
_, err := exec.Command("systemctl", args...).Output()
return err
}

func (sysd SystemdInitSystem) ServiceStop(service string) error {
args := []string{"stop", service}
_, err := exec.Command("systemctl", args...).Output()
return err
}

func (sysd SystemdInitSystem) ServiceExists(service string) bool {
args := []string{"status", service}
outBytes, _ := exec.Command("systemctl", args...).Output()
Expand Down

0 comments on commit 7460ab2

Please sign in to comment.