Skip to content

Commit

Permalink
fix: Cleaning up orphaned directories and files
Browse files Browse the repository at this point in the history
This commit refactored the methods to remove the following directory.

  - `/var/lib/nerdctl/1935db59/etchosts/<namespace>/<container id>`

It has also been modified so that the above-mentioned directory is also
cleaned up when the container is removed.

Signed-off-by: Hayato Kiwata <[email protected]>
  • Loading branch information
haytok committed Sep 24, 2024
1 parent 81fe5a4 commit 9baf881
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 21 deletions.
2 changes: 1 addition & 1 deletion pkg/cmd/container/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -835,7 +835,7 @@ func generateRemoveOrphanedDirsFunc(ctx context.Context, id, dataStore string, i
hs, err := hostsstore.New(dataStore, internalLabels.namespace)
if err != nil {
log.G(ctx).WithError(err).Warnf("failed to instantiate hostsstore for %q", internalLabels.namespace)
} else if err = hs.DeallocHostsDir(id); err != nil {
} else if err = hs.Delete(id); err != nil {
log.G(ctx).WithError(err).Warnf("failed to remove an etchosts directory for container %q", id)
}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/container/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ func RemoveContainer(ctx context.Context, c containerd.Container, globalOptions
hs, err := hostsstore.New(dataStore, containerNamespace)
if err != nil {
log.G(ctx).WithError(err).Warnf("failed to instantiate hostsstore for %q", containerNamespace)
} else if err = hs.DeallocHostsFile(id); err != nil {
} else if err = hs.Delete(id); err != nil {
// De-allocate hosts file - soft failure
log.G(ctx).WithError(err).Warnf("failed to remove hosts file for container %q", id)
}
Expand Down
26 changes: 7 additions & 19 deletions pkg/dnsutil/hostsstore/hostsstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,7 @@ type Store interface {
Release(id string) error
Update(id, newName string) error
HostsPath(id string) (location string, err error)
DeallocHostsFile(id string) (err error)
DeallocHostsDir(id string) (err error)
Delete(id string) (err error)
AllocHostsFile(id string, content []byte) (location string, err error)
}

Expand Down Expand Up @@ -186,24 +185,13 @@ func (x *hostsStore) AllocHostsFile(id string, content []byte) (location string,
return x.safeStore.Location(id, hostsFile)
}

func (x *hostsStore) DeallocHostsFile(id string) (err error) {
defer func() {
if err != nil {
err = errors.Join(ErrHostsStore, err)
}
}()

return x.safeStore.WithLock(func() error { return x.safeStore.Delete(id, hostsFile) })
}

func (x *hostsStore) DeallocHostsDir(id string) (err error) {
defer func() {
if err != nil {
err = errors.Join(ErrHostsStore, err)
}
}()
func (x *hostsStore) Delete(id string) (err error) {
err = x.safeStore.WithLock(func() error { return x.safeStore.Delete(id) })
if err != nil {
err = errors.Join(ErrHostsStore, err)
}

return x.safeStore.WithLock(func() error { return x.safeStore.Delete(id) })
return err
}

func (x *hostsStore) HostsPath(id string) (location string, err error) {
Expand Down

0 comments on commit 9baf881

Please sign in to comment.