From 073cfdfac909e30bec8bbc0978b67d803c60c038 Mon Sep 17 00:00:00 2001 From: "Lubomir I. Ivanov" Date: Sat, 6 Jan 2024 10:53:58 +0200 Subject: [PATCH] kubeadm: skip unmount on missing /var/lib/kubelet If the user deletes the /var/lib/kubelet manually, "reset" will throw an error that the dir is missing. Instead of handling this error, print it as a warning and skip unmount of directories inside it. This allows "reset" to continue to be reentrant and can be called even even if "init/join" are not called yet and some of the k8s directories on a node do not exist. Continue to error on individual unmount errors. Remove the function absoluteKubeletRunDirectory() and call filepath.EvalSymlinks() directly. --- .../app/cmd/phases/reset/cleanupnode.go | 31 ++++++++----------- 1 file changed, 13 insertions(+), 18 deletions(-) diff --git a/cmd/kubeadm/app/cmd/phases/reset/cleanupnode.go b/cmd/kubeadm/app/cmd/phases/reset/cleanupnode.go index 4979186dea4..d33a85c5761 100644 --- a/cmd/kubeadm/app/cmd/phases/reset/cleanupnode.go +++ b/cmd/kubeadm/app/cmd/phases/reset/cleanupnode.go @@ -81,18 +81,21 @@ func runCleanupNode(c workflow.RunData) error { } if !r.DryRun() { - // Try to unmount mounted directories under kubeadmconstants.KubeletRunDirectory in order to be able to remove the kubeadmconstants.KubeletRunDirectory directory later - fmt.Printf("[reset] Unmounting mounted directories in %q\n", kubeadmconstants.KubeletRunDirectory) - // In case KubeletRunDirectory holds a symbolic link, evaluate it - kubeletRunDirectory, err := absoluteKubeletRunDirectory() + // In case KubeletRunDirectory holds a symbolic link, evaluate it. + // This would also throw an error if the directory does not exist. + kubeletRunDirectory, err := filepath.EvalSymlinks(kubeadmconstants.KubeletRunDirectory) if err != nil { - return err + klog.Warningf("[reset] Skipping unmount of directories in %q: %v\n", + kubeadmconstants.KubeletRunDirectory, err) + } else { + // Unmount all mount paths under kubeletRunDirectory. + fmt.Printf("[reset] Unmounting mounted directories in %q\n", kubeadmconstants.KubeletRunDirectory) + if err := unmountKubeletDirectory(kubeletRunDirectory, r.ResetCfg().UnmountFlags); err != nil { + return err + } + // Clean the kubeletRunDirectory. + dirsToClean = append(dirsToClean, kubeletRunDirectory) } - // Unmount all mount paths under kubeletRunDirectory - if err := unmountKubeletDirectory(kubeletRunDirectory, r.ResetCfg().UnmountFlags); err != nil { - return err - } - dirsToClean = append(dirsToClean, kubeletRunDirectory) } else { fmt.Printf("[reset] Would unmount mounted directories in %q\n", kubeadmconstants.KubeletRunDirectory) } @@ -132,14 +135,6 @@ func runCleanupNode(c workflow.RunData) error { return nil } -func absoluteKubeletRunDirectory() (string, error) { - absoluteKubeletRunDirectory, err := filepath.EvalSymlinks(kubeadmconstants.KubeletRunDirectory) - if err != nil { - return "", errors.Wrapf(err, "failed to evaluate the %q directory", kubeadmconstants.KubeletRunDirectory) - } - return absoluteKubeletRunDirectory, nil -} - func removeContainers(execer utilsexec.Interface, criSocketPath string) error { containerRuntime, err := utilruntime.NewContainerRuntime(execer, criSocketPath) if err != nil {