mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-22 19:31:44 +00:00
Delete EmptyDir volume directly instead of renaming the directory.
The volume operation executor can handle duplicate requests on the same volume now.
This commit is contained in:
parent
d2d84da071
commit
d223c4be37
@ -329,11 +329,9 @@ func (ed *emptyDir) TearDownAt(dir string) error {
|
||||
}
|
||||
|
||||
func (ed *emptyDir) teardownDefault(dir string) error {
|
||||
tmpDir, err := volume.RenameDirectory(dir, ed.volName+".deleting~")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = os.RemoveAll(tmpDir)
|
||||
// Renaming the directory is not required anymore because the operation executor
|
||||
// now handles duplicate operations on the same volume
|
||||
err := os.RemoveAll(dir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -17,14 +17,8 @@ limitations under the License.
|
||||
package volume
|
||||
|
||||
import (
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
filepath "path/filepath"
|
||||
"runtime"
|
||||
"time"
|
||||
|
||||
"github.com/golang/glog"
|
||||
"k8s.io/apimachinery/pkg/api/resource"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/types"
|
||||
@ -234,96 +228,3 @@ func IsDeletedVolumeInUse(err error) bool {
|
||||
func (err deletedVolumeInUseError) Error() string {
|
||||
return string(err)
|
||||
}
|
||||
|
||||
func RenameDirectory(oldPath, newName string) (string, error) {
|
||||
newPath, err := ioutil.TempDir(filepath.Dir(oldPath), newName)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
// os.Rename call fails on windows (https://github.com/golang/go/issues/14527)
|
||||
// Replacing with copyFolder to the newPath and deleting the oldPath directory
|
||||
if runtime.GOOS == "windows" {
|
||||
err = copyFolder(oldPath, newPath)
|
||||
if err != nil {
|
||||
glog.Errorf("Error copying folder from: %s to: %s with error: %v", oldPath, newPath, err)
|
||||
return "", err
|
||||
}
|
||||
os.RemoveAll(oldPath)
|
||||
return newPath, nil
|
||||
}
|
||||
|
||||
err = os.Rename(oldPath, newPath)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return newPath, nil
|
||||
}
|
||||
|
||||
func copyFolder(source string, dest string) (err error) {
|
||||
fi, err := os.Lstat(source)
|
||||
if err != nil {
|
||||
glog.Errorf("Error getting stats for %s. %v", source, err)
|
||||
return err
|
||||
}
|
||||
|
||||
err = os.MkdirAll(dest, fi.Mode())
|
||||
if err != nil {
|
||||
glog.Errorf("Unable to create %s directory %v", dest, err)
|
||||
}
|
||||
|
||||
directory, _ := os.Open(source)
|
||||
|
||||
defer directory.Close()
|
||||
|
||||
objects, err := directory.Readdir(-1)
|
||||
|
||||
for _, obj := range objects {
|
||||
if obj.Mode()&os.ModeSymlink != 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
sourceFilePointer := source + "\\" + obj.Name()
|
||||
destinationFilePointer := dest + "\\" + obj.Name()
|
||||
|
||||
if obj.IsDir() {
|
||||
err = copyFolder(sourceFilePointer, destinationFilePointer)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
err = copyFile(sourceFilePointer, destinationFilePointer)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func copyFile(source string, dest string) (err error) {
|
||||
sourceFile, err := os.Open(source)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
defer sourceFile.Close()
|
||||
|
||||
destFile, err := os.Create(dest)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
defer destFile.Close()
|
||||
|
||||
_, err = io.Copy(destFile, sourceFile)
|
||||
if err == nil {
|
||||
sourceInfo, err := os.Stat(source)
|
||||
if err != nil {
|
||||
err = os.Chmod(dest, sourceInfo.Mode())
|
||||
}
|
||||
|
||||
}
|
||||
return
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user