Skip to content
Merged
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
44 changes: 34 additions & 10 deletions Cubelet/storage/hostdir.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ package storage

import (
"context"
"errors"
"fmt"
"os"
"path/filepath"

cubebox "github.com/tencentcloud/CubeSandbox/Cubelet/api/services/cubebox/v1"
"github.com/tencentcloud/CubeSandbox/Cubelet/pkg/log"
"github.com/tencentcloud/CubeSandbox/Cubelet/pkg/utils"
"github.com/tencentcloud/CubeSandbox/Cubelet/plugins/workflow"
"golang.org/x/sys/unix"
)
Expand Down Expand Up @@ -94,24 +96,46 @@ func (l *local) prepareHostDirVolume(ctx context.Context, opts *workflow.CreateC
return nil
}

func (l *local) cleanupHostDirVolumes(ctx context.Context, sandboxID string) {
if sandboxID == "" {
return
func (l *local) cleanupHostDirVolumes(ctx context.Context, info *StorageInfo) error {
if info == nil || info.SandboxID == "" || len(info.HostDirBackendInfos) == 0 {
return nil
}
sandboxDir := filepath.Join(hostDirBasePath, sandboxID)

sandboxDir := filepath.Join(hostDirBasePath, info.SandboxID)
if _, err := os.Stat(sandboxDir); os.IsNotExist(err) {
return
return nil
}
_ = filepath.WalkDir(sandboxDir, func(path string, d os.DirEntry, err error) error {
if err != nil || !d.IsDir() || path == sandboxDir {

if err := filepath.WalkDir(sandboxDir, func(path string, d os.DirEntry, err error) error {
if err != nil {
return err
}
if !d.IsDir() || path == sandboxDir {
return nil
}
if umErr := unix.Unmount(path, unix.MNT_DETACH); umErr != nil {
log.G(ctx).Warnf("cleanupHostDirVolumes: unmount %s: %v", path, umErr)
mounted, err := utils.IsMountPoint(path)
if err != nil {
if os.IsNotExist(err) {
return nil
}
return fmt.Errorf("check mount point %s: %w", path, err)
}
if mounted {
if err := unix.Unmount(path, unix.MNT_DETACH); err != nil &&
!errors.Is(err, unix.EINVAL) && !os.IsNotExist(err) {
return fmt.Errorf("unmount %s: %w", path, err)
}
return filepath.SkipDir
}
return nil
})
}); err != nil {
log.G(ctx).Warnf("cleanupHostDirVolumes: skip removeAll %s: %v", sandboxDir, err)
return err
}

if err := os.RemoveAll(sandboxDir); err != nil {
log.G(ctx).Warnf("cleanupHostDirVolumes: removeAll %s: %v", sandboxDir, err)
return err
}
return nil
}
4 changes: 4 additions & 0 deletions Cubelet/storage/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -1230,6 +1230,10 @@ func (l *local) destroy(ctx context.Context, info *StorageInfo, opts *workflow.D
errs = errors.Join(errs, err)
}

if err := l.cleanupHostDirVolumes(ctx, info); err != nil {
errs = errors.Join(errs, err)
}

if errs != nil {
return ret.Err(errorcode.ErrorCode_DestroyStorageFailed, errs.Error())
}
Expand Down
Loading