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
45 changes: 0 additions & 45 deletions cmd/nerdctl/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ import (
"github.com/containerd/nerdctl/pkg/clientutil"
"github.com/containerd/nerdctl/pkg/cmd/container"
"github.com/containerd/nerdctl/pkg/cmd/image"
"github.com/containerd/nerdctl/pkg/containerutil"
"github.com/containerd/nerdctl/pkg/defaults"
"github.com/containerd/nerdctl/pkg/errutil"
"github.com/containerd/nerdctl/pkg/idgen"
Expand All @@ -56,7 +55,6 @@ import (
"github.com/containerd/nerdctl/pkg/netutil"
"github.com/containerd/nerdctl/pkg/platformutil"
"github.com/containerd/nerdctl/pkg/referenceutil"
"github.com/containerd/nerdctl/pkg/rootlessutil"
"github.com/containerd/nerdctl/pkg/strutil"
"github.com/containerd/nerdctl/pkg/taskutil"
dopts "github.com/docker/cli/opts"
Expand Down Expand Up @@ -1237,49 +1235,6 @@ func withOSEnv(envs []string) ([]string, error) {
return newEnvs, nil
}

func generateSharingPIDOpts(ctx context.Context, targetCon containerd.Container) ([]oci.SpecOpts, error) {
opts := make([]oci.SpecOpts, 0)

task, err := targetCon.Task(ctx, nil)
if err != nil {
return nil, err
}
status, err := task.Status(ctx)
if err != nil {
return nil, err
}

if status.Status != containerd.Running {
return nil, fmt.Errorf("shared container is not running")
}

spec, err := targetCon.Spec(ctx)
if err != nil {
return nil, err
}

isHost := true
for _, n := range spec.Linux.Namespaces {
if n.Type == specs.PIDNamespace {
isHost = false
}
}
if isHost {
opts = append(opts, oci.WithHostNamespace(specs.PIDNamespace))
if rootlessutil.IsRootless() {
opts = append(opts, containerutil.WithBindMountHostProcfs)
}
} else {
ns := specs.LinuxNamespace{
Type: specs.PIDNamespace,
Path: fmt.Sprintf("/proc/%d/ns/pid", task.Pid()),
}
opts = append(opts, oci.WithLinuxNamespace(ns))
}

return opts, nil
}

// generateEnvs combines environment variables from `--env-file` and `--env`.
// Pass an empty slice if any arg is not used.
func generateEnvs(envFile []string, env []string) ([]string, error) {
Expand Down
2 changes: 1 addition & 1 deletion cmd/nerdctl/run_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ func generatePIDOpts(ctx context.Context, client *containerd.Client, pid string)
return fmt.Errorf("multiple IDs found with provided prefix: %s", found.Req)
}

o, err := generateSharingPIDOpts(ctx, found.Container)
o, err := containerutil.GenerateSharingPIDOpts(ctx, found.Container)
if err != nil {
return err
}
Expand Down
3 changes: 1 addition & 2 deletions cmd/nerdctl/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ import (
"github.com/containerd/nerdctl/pkg/netutil/nettype"
"github.com/containerd/nerdctl/pkg/taskutil"
"github.com/opencontainers/runtime-spec/specs-go"

"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -250,7 +249,7 @@ func reconfigPIDContainer(ctx context.Context, c containerd.Container, client *c
return err
}

opts, err := generateSharingPIDOpts(ctx, targetCon)
opts, err := containerutil.GenerateSharingPIDOpts(ctx, targetCon)
if err != nil {
return err
}
Expand Down
45 changes: 45 additions & 0 deletions pkg/containerutil/containerutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/containerd/containerd/oci"
"github.com/containerd/containerd/runtime/restart"
"github.com/containerd/nerdctl/pkg/portutil"
"github.com/containerd/nerdctl/pkg/rootlessutil"
"github.com/opencontainers/runtime-spec/specs-go"
)

Expand Down Expand Up @@ -129,3 +130,47 @@ func WithBindMountHostProcfs(_ context.Context, _ oci.Client, _ *containers.Cont
s.Linux.ReadonlyPaths = newROP
return nil
}

// GenerateSharingPIDOpts returns the oci.SpecOpts that shares the host linux namespace from `targetCon`
// If `targetCon` doesn't have a `PIDNamespace`, a new one is generated from its `Pid`.
func GenerateSharingPIDOpts(ctx context.Context, targetCon containerd.Container) ([]oci.SpecOpts, error) {
opts := make([]oci.SpecOpts, 0)

task, err := targetCon.Task(ctx, nil)
if err != nil {
return nil, err
}
status, err := task.Status(ctx)
if err != nil {
return nil, err
}

if status.Status != containerd.Running {
return nil, fmt.Errorf("shared container is not running")
}

spec, err := targetCon.Spec(ctx)
if err != nil {
return nil, err
}

isHost := true
for _, n := range spec.Linux.Namespaces {
if n.Type == specs.PIDNamespace {
isHost = false
}
}
if isHost {
opts = append(opts, oci.WithHostNamespace(specs.PIDNamespace))
if rootlessutil.IsRootless() {
opts = append(opts, WithBindMountHostProcfs)
}
} else {
ns := specs.LinuxNamespace{
Type: specs.PIDNamespace,
Path: fmt.Sprintf("/proc/%d/ns/pid", task.Pid()),
}
opts = append(opts, oci.WithLinuxNamespace(ns))
}
return opts, nil
}