Skip to content

Commit bf60e2d

Browse files
authored
Merge pull request containerd#6304 from lippertmarkus/fix-ctr-cni-windows
fix(ctr): enable networking for Windows containers
2 parents ff7fd4e + f39b3ac commit bf60e2d

File tree

4 files changed

+47
-5
lines changed

4 files changed

+47
-5
lines changed

cmd/ctr/commands/run/run.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,10 @@ var Command = cli.Command{
123123
Name: "platform",
124124
Usage: "run image for specific platform",
125125
},
126+
cli.BoolFlag{
127+
Name: "cni",
128+
Usage: "enable cni networking for the container",
129+
},
126130
}, append(platformRunFlags,
127131
append(append(commands.SnapshotterFlags, []cli.Flag{commands.SnapshotterLabels}...),
128132
commands.ContainerFlags...)...)...),
@@ -209,7 +213,12 @@ var Command = cli.Command{
209213
}
210214
}
211215
if enableCNI {
212-
if _, err := network.Setup(ctx, fullID(ctx, container), fmt.Sprintf("/proc/%d/ns/net", task.Pid())); err != nil {
216+
netNsPath, err := getNetNSPath(ctx, task)
217+
if err != nil {
218+
return err
219+
}
220+
221+
if _, err := network.Setup(ctx, fullID(ctx, container), netNsPath); err != nil {
213222
return err
214223
}
215224
}

cmd/ctr/commands/run/run_unix.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,6 @@ var platformRunFlags = []cli.Flag{
7979
Usage: "set the cpu shares",
8080
Value: 1024,
8181
},
82-
cli.BoolFlag{
83-
Name: "cni",
84-
Usage: "enable cni networking for the container",
85-
},
8682
}
8783

8884
// NewContainer creates a new container
@@ -449,3 +445,7 @@ func validNamespace(ns string) bool {
449445
return false
450446
}
451447
}
448+
449+
func getNetNSPath(_ gocontext.Context, task containerd.Task) (string, error) {
450+
return fmt.Sprintf("/proc/%d/ns/net", task.Pid()), nil
451+
}

cmd/ctr/commands/run/run_windows.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"github.com/containerd/containerd"
2525
"github.com/containerd/containerd/cmd/ctr/commands"
2626
"github.com/containerd/containerd/oci"
27+
"github.com/containerd/containerd/pkg/netns"
2728
specs "github.com/opencontainers/runtime-spec/specs-go"
2829
"github.com/pkg/errors"
2930
"github.com/sirupsen/logrus"
@@ -116,6 +117,13 @@ func NewContainer(ctx gocontext.Context, client *containerd.Client, context *cli
116117
if context.Bool("net-host") {
117118
return nil, errors.New("Cannot use host mode networking with Windows containers")
118119
}
120+
if context.Bool("cni") {
121+
ns, err := netns.NewNetNS("")
122+
if err != nil {
123+
return nil, err
124+
}
125+
opts = append(opts, oci.WithWindowsNetworkNamespace(ns.GetPath()))
126+
}
119127
if context.Bool("isolated") {
120128
opts = append(opts, oci.WithWindowsHyperV)
121129
}
@@ -149,3 +157,14 @@ func NewContainer(ctx gocontext.Context, client *containerd.Client, context *cli
149157
func getNewTaskOpts(_ *cli.Context) []containerd.NewTaskOpts {
150158
return nil
151159
}
160+
161+
func getNetNSPath(ctx gocontext.Context, t containerd.Task) (string, error) {
162+
s, err := t.Spec(ctx)
163+
if err != nil {
164+
return "", err
165+
}
166+
if s.Windows == nil || s.Windows.Network == nil {
167+
return "", nil
168+
}
169+
return s.Windows.Network.NetworkNamespace, nil
170+
}

oci/spec_opts_windows.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,17 @@ func WithHostDevices(_ context.Context, _ Client, _ *containers.Container, s *Sp
7575
func DeviceFromPath(path string) (*specs.LinuxDevice, error) {
7676
return nil, errors.New("device from path not supported on Windows")
7777
}
78+
79+
// WithWindowsNetworkNamespace sets the network namespace for a Windows container.
80+
func WithWindowsNetworkNamespace(ns string) SpecOpts {
81+
return func(_ context.Context, _ Client, _ *containers.Container, s *Spec) error {
82+
if s.Windows == nil {
83+
s.Windows = &specs.Windows{}
84+
}
85+
if s.Windows.Network == nil {
86+
s.Windows.Network = &specs.WindowsNetwork{}
87+
}
88+
s.Windows.Network.NetworkNamespace = ns
89+
return nil
90+
}
91+
}

0 commit comments

Comments
 (0)