kubeadm: Move the method used only in the test to postupgrade_test.go

Signed-off-by: Dave Chen <dave.chen@arm.com>
This commit is contained in:
Dave Chen 2023-08-18 16:53:19 +08:00
parent f563910656
commit 1eb6282016
2 changed files with 24 additions and 23 deletions

View File

@ -296,26 +296,3 @@ func GetKubeletDir(dryRun bool) (string, error) {
}
return kubeadmconstants.KubeletRunDirectory, nil
}
// moveFiles moves files from one directory to another.
func moveFiles(files map[string]string) error {
filesToRecover := make(map[string]string, len(files))
for from, to := range files {
if err := os.Rename(from, to); err != nil {
return rollbackFiles(filesToRecover, err)
}
filesToRecover[to] = from
}
return nil
}
// rollbackFiles moves the files back to the original directory.
func rollbackFiles(files map[string]string, originalErr error) error {
errs := []error{originalErr}
for from, to := range files {
if err := os.Rename(from, to); err != nil {
errs = append(errs, err)
}
}
return errors.Errorf("couldn't move these files: %v. Got errors: %v", files, errorsutil.NewAggregate(errs))
}

View File

@ -25,6 +25,7 @@ import (
"github.com/pkg/errors"
errorsutil "k8s.io/apimachinery/pkg/util/errors"
kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
"k8s.io/kubernetes/cmd/kubeadm/app/componentconfigs"
"k8s.io/kubernetes/cmd/kubeadm/app/constants"
@ -206,3 +207,26 @@ func (cc *componentConfig) IsUserSupplied() bool {
func (cc *componentConfig) SetUserSupplied(userSupplied bool) {
cc.userSupplied = userSupplied
}
// moveFiles moves files from one directory to another.
func moveFiles(files map[string]string) error {
filesToRecover := make(map[string]string, len(files))
for from, to := range files {
if err := os.Rename(from, to); err != nil {
return rollbackFiles(filesToRecover, err)
}
filesToRecover[to] = from
}
return nil
}
// rollbackFiles moves the files back to the original directory.
func rollbackFiles(files map[string]string, originalErr error) error {
errs := []error{originalErr}
for from, to := range files {
if err := os.Rename(from, to); err != nil {
errs = append(errs, err)
}
}
return errors.Errorf("couldn't move these files: %v. Got errors: %v", files, errorsutil.NewAggregate(errs))
}