Skip to content

Commit

Permalink
daemon: set libnetwork sandbox key w/o OCI hook
Browse files Browse the repository at this point in the history
Signed-off-by: Cory Snider <csnider@mirantis.com>
  • Loading branch information
corhere authored and robmry committed Jan 19, 2024
1 parent 31ccdbb commit 3582c6d
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 25 deletions.
24 changes: 0 additions & 24 deletions daemon/oci_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"github.com/docker/docker/oci/caps"
"github.com/docker/docker/pkg/idtools"
"github.com/docker/docker/pkg/rootless/specconv"
"github.com/docker/docker/pkg/stringid"
volumemounts "github.com/docker/docker/volume/mounts"
"github.com/moby/sys/mount"
"github.com/moby/sys/mountinfo"
Expand Down Expand Up @@ -61,28 +60,6 @@ func withRlimits(daemon *Daemon, daemonCfg *dconfig.Config, c *container.Contain
}
}

// withLibnetwork sets the libnetwork hook
func withLibnetwork(daemon *Daemon, daemonCfg *dconfig.Config, c *container.Container) coci.SpecOpts {
return func(ctx context.Context, _ coci.Client, _ *containers.Container, s *coci.Spec) error {
if c.Config.NetworkDisabled {
return nil
}
for _, ns := range s.Linux.Namespaces {
if ns.Type == specs.NetworkNamespace && ns.Path == "" {
if s.Hooks == nil {
s.Hooks = &specs.Hooks{}
}
shortNetCtlrID := stringid.TruncateID(daemon.netController.ID())
s.Hooks.Prestart = append(s.Hooks.Prestart, specs.Hook{
Path: filepath.Join("/proc", strconv.Itoa(os.Getpid()), "exe"),
Args: []string{"libnetwork-setkey", "-exec-root=" + daemonCfg.GetExecRoot(), c.ID, shortNetCtlrID},
})
}
}
return nil
}
}

// withRootless sets the spec to the rootless configuration
func withRootless(daemon *Daemon, daemonCfg *dconfig.Config) coci.SpecOpts {
return func(_ context.Context, _ coci.Client, _ *containers.Container, s *coci.Spec) error {
Expand Down Expand Up @@ -1070,7 +1047,6 @@ func (daemon *Daemon) createSpec(ctx context.Context, daemonCfg *configStore, c
WithCapabilities(c),
WithSeccomp(daemon, c),
withMounts(daemon, daemonCfg, c, mounts),
withLibnetwork(daemon, &daemonCfg.Config, c),
WithApparmor(c),
WithSelinux(c),
WithOOMScore(&c.HostConfig.OomScoreAdj),
Expand Down
16 changes: 16 additions & 0 deletions daemon/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package daemon // import "github.com/docker/docker/daemon"

import (
"context"
"fmt"
"runtime"
"time"

Expand All @@ -13,6 +14,8 @@ import (
"github.com/docker/docker/errdefs"
"github.com/docker/docker/internal/compatcontext"
"github.com/docker/docker/libcontainerd"
"github.com/docker/docker/oci"
"github.com/opencontainers/runtime-spec/specs-go"
"github.com/pkg/errors"
)

Expand Down Expand Up @@ -236,6 +239,19 @@ func (daemon *Daemon) containerStart(ctx context.Context, daemonCfg *configStore
}
}()

if runtime.GOOS == "linux" && !container.Config.NetworkDisabled {
nspath, ok := oci.NamespacePath(spec, specs.NetworkNamespace)
if ok && nspath == "" { // the runtime has been instructed to create a new network namespace for tsk.
sb, err := daemon.netController.GetSandbox(container.ID)
if err != nil {
return errdefs.System(err)
}
if err := sb.SetKey(fmt.Sprintf("/proc/%d/ns/net", tsk.Pid())); err != nil {
return errdefs.System(err)
}
}
}

if err := tsk.Start(context.TODO()); err != nil { // passing ctx caused integration tests to be stuck in the cleanup phase
return setExitCodeFromError(container.SetExitCode, err)
}
Expand Down
6 changes: 5 additions & 1 deletion libnetwork/osl/namespace_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,11 @@ func NewSandbox(key string, osCreate, isRestore bool) (*Namespace, error) {
}

func mountNetworkNamespace(basePath string, lnPath string) error {
return syscall.Mount(basePath, lnPath, "bind", syscall.MS_BIND, "")
err := syscall.Mount(basePath, lnPath, "bind", syscall.MS_BIND, "")
if err != nil {
return fmt.Errorf("bind-mount %s -> %s: %w", basePath, lnPath, err)
}
return nil
}

// GetSandboxForExternalKey returns sandbox object for the supplied path
Expand Down
5 changes: 5 additions & 0 deletions libnetwork/sandbox_unsupported.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,8 @@ func (sb *Sandbox) populateNetworkResources(*Endpoint) error {
// not implemented on Windows (Sandbox.osSbox is always nil)
return nil
}

func (sb *Sandbox) SetKey(basePath string) error {
// not implemented on Windows (Sandbox.osSbox is always nil)
return nil
}
11 changes: 11 additions & 0 deletions oci/namespaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,14 @@ func RemoveNamespace(s *specs.Spec, nsType specs.LinuxNamespaceType) {
}
}
}

// NamespacePath returns the configured Path of the first namespace in
// s.Linux.Namespaces of type nsType.
func NamespacePath(s *specs.Spec, nsType specs.LinuxNamespaceType) (path string, ok bool) {
for _, n := range s.Linux.Namespaces {
if n.Type == nsType {
return n.Path, true
}
}
return "", false
}

0 comments on commit 3582c6d

Please sign in to comment.