From d2751ba9199b2164509d3f8ab74e758244027f1d Mon Sep 17 00:00:00 2001 From: Derick Diaz Date: Fri, 15 Dec 2023 20:19:17 -0600 Subject: [PATCH 1/4] Added cleanupDataDir function to remove any folders within the data dir that are not in use --- pkg/rke2/rke2.go | 4 ++++ pkg/rke2/rke2_linux.go | 47 ++++++++++++++++++++++++++++++++++++++++ pkg/rke2/rke2_windows.go | 4 ++++ 3 files changed, 55 insertions(+) diff --git a/pkg/rke2/rke2.go b/pkg/rke2/rke2.go index ef38e92c9e..b4f55a79bc 100644 --- a/pkg/rke2/rke2.go +++ b/pkg/rke2/rke2.go @@ -140,6 +140,10 @@ func setup(clx *cli.Context, cfg Config, isServer bool) error { } executor.Set(ex) + // Clear data directories that are no longer in use + // Errors from this should not prevent the cluster from starting + _ = cleanupDataDir(dataDir) + // check for force restart file var forceRestart bool if _, err := os.Stat(ForceRestartFile(dataDir)); err != nil { diff --git a/pkg/rke2/rke2_linux.go b/pkg/rke2/rke2_linux.go index 1b7b59c41c..b20079f1ae 100644 --- a/pkg/rke2/rke2_linux.go +++ b/pkg/rke2/rke2_linux.go @@ -507,3 +507,50 @@ func hostnameFromMetadataEndpoint(ctx context.Context) string { return strings.TrimSpace(string(b)) } + +func cleanupDataDir(dataDir string) error { + paths, err := getProcessExecutablePaths() + activePaths := []string{} + if err != nil { + return activePaths, err + } + // Ensures only unique path within the data directory are + // captured within the array + for _, path := range paths { + if !strings.HasPrefix(path, dataDir) { + continue + } + arr := strings.Split(path, "/") + rkePath := strings.Join(arr[0:7], "/") + if !slices.Contains(activePaths, rkePath) { + activePaths = append(activePaths, rkePath) + } + } + + // Ensures file is a directory, has rke2 within the name, and is not an active path + // if the function is unable to clear out the diretory, the error is ignored. + err := filepath.WalkDir(path, func(path string, info fs.DirEntry, err error) error { + if !info.IsDir() || !string.Contains(path, "rke2") || slices.Contains(activePaths, path) { + continue + } + _ := os.RemoveAll(path) + }) +} + +func getProcessExecutablePaths() ([]string, error) { + path := "/proc" + executables := []string{} + err := filepath.WalkDir(path, func(path string, dir fs.DirEntry, err error) error { + if !dir.IsDir() { + return nil + } + exePath := filepath.Join(path, "exe") + exeLinkPath, err := os.Readlink(exePath) + if err != nil { + return nil + } + executables = append(executables, exeLinkPath) + return nil + }) + return executables, err +} diff --git a/pkg/rke2/rke2_windows.go b/pkg/rke2/rke2_windows.go index 5b9d095e6c..ec78d54600 100644 --- a/pkg/rke2/rke2_windows.go +++ b/pkg/rke2/rke2_windows.go @@ -72,3 +72,7 @@ func initExecutor(clx *cli.Context, cfg Config, isServer bool) (*pebinaryexecuto CNI: "", }, nil } + +func cleanupDataDir() error { + return nil +} From 57d0ae2f8e8138a3c960a7656af3bfac7b43b3bc Mon Sep 17 00:00:00 2001 From: Derick Diaz Date: Fri, 15 Dec 2023 20:34:57 -0600 Subject: [PATCH 2/4] Added return to cleanupDataDir --- pkg/rke2/rke2_linux.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/pkg/rke2/rke2_linux.go b/pkg/rke2/rke2_linux.go index b20079f1ae..ab095ff9be 100644 --- a/pkg/rke2/rke2_linux.go +++ b/pkg/rke2/rke2_linux.go @@ -7,10 +7,13 @@ import ( "bytes" "context" "fmt" + "io/fs" "io/ioutil" "net/http" + "os" "os/exec" "path/filepath" + "slices" "strconv" "strings" "time" @@ -535,6 +538,8 @@ func cleanupDataDir(dataDir string) error { } _ := os.RemoveAll(path) }) + + return nil } func getProcessExecutablePaths() ([]string, error) { From eef488233fdb9e4a50536f03c032eb71536a5952 Mon Sep 17 00:00:00 2001 From: Derick Diaz Date: Sun, 28 Jan 2024 18:42:16 -0600 Subject: [PATCH 3/4] Removed activepath from return --- pkg/rke2/rke2_linux.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/rke2/rke2_linux.go b/pkg/rke2/rke2_linux.go index ab095ff9be..e6d0e06c36 100644 --- a/pkg/rke2/rke2_linux.go +++ b/pkg/rke2/rke2_linux.go @@ -515,7 +515,7 @@ func cleanupDataDir(dataDir string) error { paths, err := getProcessExecutablePaths() activePaths := []string{} if err != nil { - return activePaths, err + return err } // Ensures only unique path within the data directory are // captured within the array From a7b401a38c6d87e738eff64af6c7da7c3745d31d Mon Sep 17 00:00:00 2001 From: Derick Diaz Date: Sun, 28 Jan 2024 19:22:53 -0600 Subject: [PATCH 4/4] fix error --- pkg/rke2/rke2_linux.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/rke2/rke2_linux.go b/pkg/rke2/rke2_linux.go index e6d0e06c36..f238675690 100644 --- a/pkg/rke2/rke2_linux.go +++ b/pkg/rke2/rke2_linux.go @@ -532,14 +532,14 @@ func cleanupDataDir(dataDir string) error { // Ensures file is a directory, has rke2 within the name, and is not an active path // if the function is unable to clear out the diretory, the error is ignored. - err := filepath.WalkDir(path, func(path string, info fs.DirEntry, err error) error { + err = filepath.WalkDir(path, func(path string, info fs.DirEntry, err error) error { if !info.IsDir() || !string.Contains(path, "rke2") || slices.Contains(activePaths, path) { continue } _ := os.RemoveAll(path) }) - return nil + return err } func getProcessExecutablePaths() ([]string, error) {