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

bugfix: delete upperDir and workDir when deleting container taken over by pouch #2524

Merged
merged 1 commit into from
Dec 12, 2018
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
8 changes: 8 additions & 0 deletions daemon/mgr/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -1165,6 +1165,14 @@ func (mgr *ContainerManager) Remove(ctx context.Context, name string, options *t
if err := mount.Unmount(c.BaseFS, 0); err != nil {
logrus.Errorf("failed to umount rootfs when remove the container %s: %v", c.ID, err)
}

// Note(ziren): when deleting a container whose rootfs was provided, we also should
// remove the upperDir and workDir of container. because the directories cost disk
// space and the disk space counted into the new container that using the same
// disk quota id.
if err := c.CleanRootfsSnapshotDirs(); err != nil {
logrus.Errorf("failed to clean rootfs: %v", err)
}
} else if err := mgr.Client.RemoveSnapshot(ctx, c.SnapshotKey()); err != nil {
// if the container is created by normal method, remove the
// snapshot when delete it.
Expand Down
42 changes: 39 additions & 3 deletions daemon/mgr/container_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -439,10 +439,11 @@ func (c *Container) FormatStatus() (string, error) {
}

// UnsetMergedDir unsets Snapshot MergedDir. Stop a container will
// delete the containerd container, the merged dir
// will also be deleted, so we should unset the
// container's MergedDir.
// delete the containerd container, the merged dir will also be
// deleted, so we should unset the container's MergedDir.
func (c *Container) UnsetMergedDir() {
c.Lock()
defer c.Unlock()
if c.Snapshotter == nil || c.Snapshotter.Data == nil {
return
}
Expand Down Expand Up @@ -491,6 +492,41 @@ func (c *Container) GetSpecificBasePath(path string) string {
return ""
}

// CleanRootfsSnapshotDirs deletes container's rootfs snapshot MergedDir, UpperDir and
// WorkDir. Since the snapshot of container created by containerd will be cleaned by
// containerd, so we only clean rootfs that is RootFSProvided.
func (c *Container) CleanRootfsSnapshotDirs() error {
// if RootFSProvided is not set or Snapshotter data empty , we no need clean the rootfs
if !c.RootFSProvided || c.Snapshotter == nil || c.Snapshotter.Data == nil {
return nil
}

var (
removeDirs []string
)

c.Lock()
for _, dir := range []string{"MergedDir", "UpperDir", "WorkDir"} {
if v, ok := c.Snapshotter.Data[dir]; ok {
removeDirs = append(removeDirs, v)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about refactor it to

if c.Snapshotter == nil || c.Snapshotter.Data == nil {
return
}

for _, dir := range []string{"MergedDir", "UpperDir", "WorkDir"} {
        ...
        v, ok := c.Snapshotter.Data[dir]
        if !ok {
	        continue
        }
        ...
}

@HusterWan

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zhuangqh Your code haven't unlocked.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the better codes like these:

if c.Snapshotter == nil || c.Snapshotter.Data == nil {
    return
}
lock()
removeDirs []string
for _, dir := range []string{"MergedDir",  "WorkDir", "UpperDir"} {
        ...
        v, ok := c.Snapshotter.Data[dir]
        if !ok {
	        continue
        }
        removeDirs = append(removeDir, v)
}
unlock()

for _, dir := range removeDirs {
    os.Remove(...)
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rudyfly I know. I just briefly point out how to refactor the code structure.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rudyfly @zhuangqh good advice

}
c.Unlock()

var errMsgs []string
for _, dir := range removeDirs {
if err := os.RemoveAll(dir); err != nil {
errMsgs = append(errMsgs, err.Error())
}
}

if len(errMsgs) != 0 {
return fmt.Errorf(strings.Join(errMsgs, "\n"))
}

return nil
}

// ContainerRestartPolicy represents the policy is used to manage container.
type ContainerRestartPolicy types.RestartPolicy

Expand Down