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
6 changes: 3 additions & 3 deletions cmd/nerdctl/container_restart_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ func TestRestartPIDContainer(t *testing.T) {
base := testutil.NewBase(t)

baseContainerName := testutil.Identifier(t)
base.Cmd("run", "-d", "--name", baseContainerName, testutil.AlpineImage, "sleep", "infinity").Run()
base.Cmd("run", "-d", "--name", baseContainerName, testutil.AlpineImage, "sleep", "infinity").AssertOK()
defer base.Cmd("rm", "-f", baseContainerName).Run()

sharedContainerName := testutil.Identifier(t)
base.Cmd("run", "-d", "--name", sharedContainerName, fmt.Sprintf("--pid=container:%s", baseContainerName), testutil.AlpineImage, "sleep", "infinity").Run()
sharedContainerName := fmt.Sprintf("%s-shared", baseContainerName)
base.Cmd("run", "-d", "--name", sharedContainerName, fmt.Sprintf("--pid=container:%s", baseContainerName), testutil.AlpineImage, "sleep", "infinity").AssertOK()
Comment thread
fahedouch marked this conversation as resolved.
defer base.Cmd("rm", "-f", sharedContainerName).Run()

base.Cmd("restart", baseContainerName).AssertOK()
Expand Down
1 change: 0 additions & 1 deletion cmd/nerdctl/container_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,6 @@ func runAction(cmd *cobra.Command, args []string) error {
return err
}
logURI := lab[labels.LogURI]

task, err := taskutil.NewTask(ctx, client, c, false, flagI, flagT, flagD, con, logURI)
if err != nil {
return err
Expand Down
1 change: 1 addition & 0 deletions cmd/nerdctl/container_run_network_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,7 @@ func TestRunWithInvalidPortThenCleanUp(t *testing.T) {
t.Parallel()
base := testutil.NewBase(t)
containerName := testutil.Identifier(t)
defer base.Cmd("rm", "-f", containerName).Run()
base.Cmd("run", "--rm", "--name", containerName, "-p", "22200-22299:22200-22299", testutil.CommonImage).AssertFail()
base.Cmd("run", "--rm", "--name", containerName, "-p", "22200-22299:22200-22299", testutil.CommonImage).AssertCombinedOutContains(errdefs.ErrInvalidArgument.Error())
base.Cmd("run", "--rm", "--name", containerName, testutil.CommonImage).AssertOK()
Expand Down
4 changes: 3 additions & 1 deletion pkg/cmd/container/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,10 @@ func RemoveContainer(ctx context.Context, c containerd.Container, globalOptions
if retErr != nil {
return
}

if err := os.RemoveAll(stateDir); err != nil {
logrus.WithError(retErr).Warnf("failed to remove container state dir %s", stateDir)
}
// enforce release name here in case the poststop hook name release fails
if name != "" {
if err := namst.Release(name, id); err != nil {
logrus.WithError(retErr).Warnf("failed to release container name %s", name)
Expand All @@ -135,6 +135,8 @@ func RemoveContainer(ctx context.Context, c containerd.Container, globalOptions
logrus.WithError(retErr).Warnf("failed to remove hosts file for container %q", id)
}
}()

// volume removal is not handled by the poststop hook lifecycle because it depends on removeAnonVolumes option
if anonVolumesJSON, ok := l[labels.AnonymousVolumes]; ok && removeAnonVolumes {
var anonVolumes []string
if err := json.Unmarshal([]byte(anonVolumesJSON), &anonVolumes); err != nil {
Expand Down
2 changes: 2 additions & 0 deletions pkg/dnsutil/hostsstore/hostsstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ func (x *store) Acquire(meta Meta) error {
return lockutil.WithDirLock(x.hostsD, fn)
}

// Release is triggered by Poststop hooks.
// It is called after the containerd task is deleted but before the delete operation returns.
func (x *store) Release(ns, id string) error {
fn := func() error {
metaPath := filepath.Join(x.hostsD, ns, id, metaJSON)
Expand Down
11 changes: 10 additions & 1 deletion pkg/ocihook/ocihook.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/containerd/nerdctl/pkg/bypass4netnsutil"
"github.com/containerd/nerdctl/pkg/dnsutil/hostsstore"
"github.com/containerd/nerdctl/pkg/labels"
"github.com/containerd/nerdctl/pkg/namestore"
"github.com/containerd/nerdctl/pkg/netutil"
"github.com/containerd/nerdctl/pkg/netutil/nettype"
"github.com/containerd/nerdctl/pkg/rootlessutil"
Expand Down Expand Up @@ -445,6 +446,7 @@ func onCreateRuntime(opts *handlerOpts) error {

func onPostStop(opts *handlerOpts) error {
ctx := context.Background()
ns := opts.state.Annotations[labels.Namespace]
if opts.cni != nil {
var err error
b4nnEnabled, err := bypass4netnsutil.IsBypass4netnsEnabled(opts.state.Annotations)
Expand Down Expand Up @@ -497,10 +499,17 @@ func onPostStop(opts *handlerOpts) error {
if err != nil {
return err
}
ns := opts.state.Annotations[labels.Namespace]
if err := hs.Release(ns, opts.state.ID); err != nil {
return err
}
}
namst, err := namestore.New(opts.dataStore, ns)
if err != nil {
return err
}
name := opts.state.Annotations[labels.Name]
if err := namst.Release(name, opts.state.ID); err != nil {
return fmt.Errorf("failed to release container name %s: %w", name, err)
}
return nil
}