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 15, 2016
1 parent e0fc43b commit df14acb
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 20 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
97 changes: 97 additions & 0 deletions cmd/kubeadm/app/cmd/reset.go
@@ -0,0 +1,97 @@
/*
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, err := initsystem.GetInitSystem()
if err != nil {
fmt.Printf("%v", err)
} else {
fmt.Printf("Stopping the %s service...\n", serviceToStop)
initSystem.ServiceStop(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.Printf("failed to remove directory: [%v]", err)
}
}

dockerCheck := preflight.ServiceCheck{Service: "docker"}
if warnings, errors := dockerCheck.Check(); len(warnings) == 0 && len(errors) == 0 {

fmt.Printf("Stopping all running docker containers...")
if err := exec.Command("sh", "-c", "docker ps | grep 'k8s_' | awk '{print $1}' | xargs docker rm --force --volumes").Run(); err != nil {
fmt.Printf("failed to stop the running containers")
}
} else {
fmt.Printf("docker doesn't seem to be running, skipping the removal of kubernetes containers")
}

return nil
}
39 changes: 22 additions & 17 deletions cmd/kubeadm/app/preflight/checks.go
Expand Up @@ -45,32 +45,32 @@ type PreFlightCheck interface {
// detect a supported init system however, all checks are skipped and a warning is
// returned.
type ServiceCheck struct {
service string
Service string
}

func (sc ServiceCheck) Check() (warnings, errors []error) {
initSystem := initsystem.GetInitSystem()
if initSystem == nil {
return []error{fmt.Errorf("no supported init system detected, skipping service checks for %s", sc.service)}, nil
initSystem, err := initsystem.GetInitSystem()
if err != nil {
return []error{err}, nil
}

warnings = []error{}

if !initSystem.ServiceExists(sc.service) {
warnings = append(warnings, fmt.Errorf("%s service does not exist", sc.service))
if !initSystem.ServiceExists(sc.Service) {
warnings = append(warnings, fmt.Errorf("%s service does not exist", sc.Service))
return warnings, nil
}

if !initSystem.ServiceIsEnabled(sc.service) {
if !initSystem.ServiceIsEnabled(sc.Service) {
warnings = append(warnings,
fmt.Errorf("%s service is not enabled, please run 'systemctl enable %s.service'",
sc.service, sc.service))
sc.Service, sc.Service))
}

if !initSystem.ServiceIsActive(sc.service) {
if !initSystem.ServiceIsActive(sc.Service) {
errors = append(errors,
fmt.Errorf("%s service is not active, please run 'systemctl start %s.service'",
sc.service, sc.service))
sc.Service, sc.Service))
}

return warnings, errors
Expand Down Expand Up @@ -160,8 +160,8 @@ func RunInitMasterChecks() error {
// TODO: Some of these ports should come from kubeadm config eventually:
checks := []PreFlightCheck{
IsRootCheck{root: true},
ServiceCheck{service: "kubelet"},
ServiceCheck{service: "docker"},
ServiceCheck{Service: "kubelet"},
ServiceCheck{Service: "docker"},
PortOpenCheck{port: 443},
PortOpenCheck{port: 2379},
PortOpenCheck{port: 8080},
Expand Down Expand Up @@ -189,12 +189,9 @@ func RunJoinNodeChecks() error {
// TODO: Some of these ports should come from kubeadm config eventually:
checks := []PreFlightCheck{
IsRootCheck{root: true},
ServiceCheck{service: "docker"},
ServiceCheck{service: "kubelet"},
PortOpenCheck{port: 8080},
ServiceCheck{Service: "docker"},
ServiceCheck{Service: "kubelet"},
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
25 changes: 22 additions & 3 deletions pkg/util/initsystem/initsystem.go
Expand Up @@ -17,11 +17,18 @@ limitations under the License.
package initsystem

import (
"fmt"
"os/exec"
"strings"
)

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 +41,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 Expand Up @@ -70,11 +89,11 @@ func (sysd SystemdInitSystem) ServiceIsActive(service string) bool {
// getInitSystem returns an InitSystem for the current system, or nil
// if we cannot detect a supported init system for pre-flight checks.
// This indicates we will skip init system checks, not an error.
func GetInitSystem() InitSystem {
func GetInitSystem() (InitSystem, error) {
// Assume existence of systemctl in path implies this is a systemd system:
_, err := exec.LookPath("systemctl")
if err == nil {
return &SystemdInitSystem{}
return &SystemdInitSystem{}, nil
}
return nil
return nil, fmt.Errorf("no supported init system detected, skipping checking for services")
}

0 comments on commit df14acb

Please sign in to comment.