Skip to content
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

Added cleanupDataDir function to remove any folders within the data d… #5139

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
52 changes: 52 additions & 0 deletions pkg/rke2/rke2_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ import (
"bytes"
"context"
"fmt"
"io/fs"
"io/ioutil"
"net/http"
"os"
"os/exec"
"path/filepath"
"slices"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -507,3 +510,52 @@ 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 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)
})

return err
}

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
}