-
Notifications
You must be signed in to change notification settings - Fork 38.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Get the etcd data path from kubeadm config or etcd pod manifest on kubeadm reset #70003
Merged
k8s-ci-robot
merged 2 commits into
kubernetes:master
from
yagonobre:kubeadm-clean-etcd-dir
Oct 29, 2018
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,13 +30,17 @@ import ( | |
"github.com/spf13/cobra" | ||
|
||
"k8s.io/apimachinery/pkg/util/sets" | ||
clientset "k8s.io/client-go/kubernetes" | ||
kubeadmapiv1beta1 "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/v1beta1" | ||
"k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/validation" | ||
"k8s.io/kubernetes/cmd/kubeadm/app/cmd/options" | ||
cmdutil "k8s.io/kubernetes/cmd/kubeadm/app/cmd/util" | ||
kubeadmconstants "k8s.io/kubernetes/cmd/kubeadm/app/constants" | ||
"k8s.io/kubernetes/cmd/kubeadm/app/preflight" | ||
kubeadmutil "k8s.io/kubernetes/cmd/kubeadm/app/util" | ||
configutil "k8s.io/kubernetes/cmd/kubeadm/app/util/config" | ||
utilruntime "k8s.io/kubernetes/cmd/kubeadm/app/util/runtime" | ||
utilstaticpod "k8s.io/kubernetes/cmd/kubeadm/app/util/staticpod" | ||
"k8s.io/kubernetes/pkg/util/initsystem" | ||
utilsexec "k8s.io/utils/exec" | ||
) | ||
|
@@ -47,6 +51,7 @@ func NewCmdReset(in io.Reader, out io.Writer) *cobra.Command { | |
var criSocketPath string | ||
var ignorePreflightErrors []string | ||
var forceReset bool | ||
kubeConfigFile := kubeadmconstants.GetAdminKubeConfigPath() | ||
|
||
cmd := &cobra.Command{ | ||
Use: "reset", | ||
|
@@ -55,13 +60,18 @@ func NewCmdReset(in io.Reader, out io.Writer) *cobra.Command { | |
ignorePreflightErrorsSet, err := validation.ValidateIgnorePreflightErrors(ignorePreflightErrors) | ||
kubeadmutil.CheckErr(err) | ||
|
||
kubeConfigFile = cmdutil.FindExistingKubeConfig(kubeConfigFile) | ||
client, err := getClientset(kubeConfigFile, false) | ||
kubeadmutil.CheckErr(err) | ||
|
||
r, err := NewReset(in, ignorePreflightErrorsSet, forceReset, certsDir, criSocketPath) | ||
kubeadmutil.CheckErr(err) | ||
kubeadmutil.CheckErr(r.Run(out)) | ||
kubeadmutil.CheckErr(r.Run(out, client)) | ||
}, | ||
} | ||
|
||
options.AddIgnorePreflightErrorsFlag(cmd.PersistentFlags(), &ignorePreflightErrors) | ||
options.AddKubeConfigFlag(cmd.PersistentFlags(), &kubeConfigFile) | ||
|
||
cmd.PersistentFlags().StringVar( | ||
&certsDir, "cert-dir", kubeadmapiv1beta1.DefaultCertificatesDir, | ||
|
@@ -114,7 +124,19 @@ func NewReset(in io.Reader, ignorePreflightErrors sets.String, forceReset bool, | |
} | ||
|
||
// Run reverts any changes made to this host by "kubeadm init" or "kubeadm join". | ||
func (r *Reset) Run(out io.Writer) error { | ||
func (r *Reset) Run(out io.Writer, client clientset.Interface) error { | ||
var dirsToClean []string | ||
// Only clear etcd data when using local etcd. | ||
etcdManifestPath := filepath.Join(kubeadmconstants.KubernetesDir, kubeadmconstants.ManifestsSubDirName, "etcd.yaml") | ||
|
||
glog.V(1).Infof("[reset] checking for etcd config") | ||
etcdDataDir, err := getEtcdDataDir(etcdManifestPath, client) | ||
if err == nil { | ||
dirsToClean = append(dirsToClean, etcdDataDir) | ||
} else { | ||
fmt.Println("[reset] no etcd config found. Assuming external etcd") | ||
fmt.Println("[reset] please manually reset etcd to prevent further issues") | ||
} | ||
|
||
// Try to stop the kubelet service | ||
glog.V(1).Infof("[reset] getting init system") | ||
|
@@ -144,19 +166,7 @@ func (r *Reset) Run(out io.Writer) error { | |
if err := removeContainers(utilsexec.New(), r.criSocketPath); err != nil { | ||
glog.Errorf("[reset] failed to remove containers: %+v", err) | ||
} | ||
dirsToClean := []string{kubeadmconstants.KubeletRunDirectory, "/etc/cni/net.d", "/var/lib/dockershim", "/var/run/kubernetes"} | ||
|
||
// Only clear etcd data when the etcd manifest is found. In case it is not found, we must assume that the user | ||
// provided external etcd endpoints. In that case, it is their own responsibility to reset etcd | ||
etcdManifestPath := filepath.Join(kubeadmconstants.KubernetesDir, kubeadmconstants.ManifestsSubDirName, "etcd.yaml") | ||
glog.V(1).Infof("[reset] checking for etcd manifest") | ||
if _, err := os.Stat(etcdManifestPath); err == nil { | ||
glog.V(1).Infof("Found one at %s", etcdManifestPath) | ||
dirsToClean = append(dirsToClean, "/var/lib/etcd") | ||
} else { | ||
fmt.Printf("[reset] no etcd manifest found in %q. Assuming external etcd\n", etcdManifestPath) | ||
fmt.Println("[reset] please manually reset etcd to prevent further issues") | ||
} | ||
dirsToClean = append(dirsToClean, []string{kubeadmconstants.KubeletRunDirectory, "/etc/cni/net.d", "/var/lib/dockershim", "/var/run/kubernetes"}...) | ||
|
||
// Then clean contents from the stateful kubelet, etcd and cni directories | ||
fmt.Printf("[reset] deleting contents of stateful directories: %v\n", dirsToClean) | ||
|
@@ -175,6 +185,33 @@ func (r *Reset) Run(out io.Writer) error { | |
return nil | ||
} | ||
|
||
func getEtcdDataDir(manifestPath string, client clientset.Interface) (string, error) { | ||
const etcdVolumeName = "etcd-data" | ||
var dataDir string | ||
|
||
cfg, err := configutil.FetchConfigFromFileOrCluster(client, os.Stdout, "reset", "", false) | ||
if err == nil { | ||
return cfg.Etcd.Local.DataDir, nil | ||
} | ||
glog.Warningf("[reset] Unable to fetch the kubeadm-config ConfigMap, using etcd pod spec as fallback: %v", err) | ||
|
||
etcdPod, err := utilstaticpod.ReadStaticPodFromDisk(manifestPath) | ||
if err != nil { | ||
return "", err | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it's weird to return empty strings vs. nil objects. |
||
} | ||
|
||
for _, volumeMount := range etcdPod.Spec.Volumes { | ||
if volumeMount.Name == etcdVolumeName { | ||
dataDir = volumeMount.HostPath.Path | ||
break | ||
} | ||
} | ||
if dataDir == "" { | ||
return dataDir, fmt.Errorf("invalid etcd pod manifest") | ||
} | ||
return dataDir, nil | ||
} | ||
|
||
func removeContainers(execer utilsexec.Interface, criSocketPath string) error { | ||
containerRuntime, err := utilruntime.NewContainerRuntime(execer, criSocketPath) | ||
if err != nil { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const for this really shouldn't be localized.