Skip to content

Commit

Permalink
Added cleanupDataDir function to remove any folders within the data d…
Browse files Browse the repository at this point in the history
…ir that are not in use
  • Loading branch information
derickdiaz committed Dec 16, 2023
1 parent c1494f5 commit d2751ba
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
4 changes: 4 additions & 0 deletions pkg/rke2/rke2.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
47 changes: 47 additions & 0 deletions pkg/rke2/rke2_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
4 changes: 4 additions & 0 deletions pkg/rke2/rke2_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,7 @@ func initExecutor(clx *cli.Context, cfg Config, isServer bool) (*pebinaryexecuto
CNI: "",
}, nil
}

func cleanupDataDir() error {
return nil
}

0 comments on commit d2751ba

Please sign in to comment.