diff --git a/cmd/kubeadm/app/util/users/users_linux.go b/cmd/kubeadm/app/util/users/users_linux.go index 2819c50959e..9684849395b 100644 --- a/cmd/kubeadm/app/util/users/users_linux.go +++ b/cmd/kubeadm/app/util/users/users_linux.go @@ -23,6 +23,7 @@ import ( "fmt" "io" "os" + "path/filepath" "sort" "strconv" "strings" @@ -629,3 +630,21 @@ func writeFile(f *os.File, str string) error { } return nil } + +// UpdatePathOwnerAndPermissions updates the owner and permissions of the given path. +// If the path is a directory it updates its contents recursively. +func UpdatePathOwnerAndPermissions(dirPath string, uid, gid int64, perms uint32) error { + err := filepath.WalkDir(dirPath, func(path string, d os.DirEntry, err error) error { + if err := os.Chown(path, int(uid), int(gid)); err != nil { + return errors.Wrapf(err, "failed to update owner of %q to uid: %d and gid: %d", path, uid, gid) + } + + fm := os.FileMode(perms) + if err := os.Chmod(path, fm); err != nil { + return errors.Wrapf(err, "failed to update permissions of %q to %s", path, fm.String()) + } + return nil + }) + + return err +} diff --git a/cmd/kubeadm/app/util/users/users_other.go b/cmd/kubeadm/app/util/users/users_other.go index 76f55fc8a3a..5614230feb4 100644 --- a/cmd/kubeadm/app/util/users/users_other.go +++ b/cmd/kubeadm/app/util/users/users_other.go @@ -43,3 +43,8 @@ func AddUsersAndGroups() (*UsersAndGroups, error) { func RemoveUsersAndGroups() error { return nil } + +// UpdatePathOwnerAndPermissions is a NO-OP on non-Linux. +func UpdatePathOwnerAndPermissions(path string, uid, gid int64, perms uint32) error { + return nil +}