Skip to content

Commit de9344d

Browse files
Merge pull request containerd#6670 from snbc/ctr_run_cni
fix: `ctr run --cni` get failed
2 parents 79d7df7 + 2a0b2ee commit de9344d

File tree

4 files changed

+95
-13
lines changed

4 files changed

+95
-13
lines changed

cmd/ctr/commands/cni.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
Copyright The containerd Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package commands
18+
19+
import (
20+
"context"
21+
"fmt"
22+
23+
"github.com/containerd/containerd"
24+
"github.com/containerd/containerd/namespaces"
25+
"github.com/containerd/typeurl"
26+
)
27+
28+
func init() {
29+
typeurl.Register(&NetworkMetaData{},
30+
"github.com/containerd/containerd/cmd/ctr/commands", "NetworkMetaData")
31+
}
32+
33+
const (
34+
35+
// CtrCniMetadataExtension is an extension name that identify metadata of container in CreateContainerRequest
36+
CtrCniMetadataExtension = "ctr.cni-containerd.metadata"
37+
)
38+
39+
//ctr pass cni network metadata to containerd if ctr run use option of --cni
40+
type NetworkMetaData struct {
41+
EnableCni bool
42+
}
43+
44+
func FullID(ctx context.Context, c containerd.Container) string {
45+
id := c.ID()
46+
ns, ok := namespaces.Namespace(ctx)
47+
if !ok {
48+
return id
49+
}
50+
return fmt.Sprintf("%s-%s", ns, id)
51+
}

cmd/ctr/commands/run/run.go

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package run
1818

1919
import (
20-
"context"
2120
gocontext "context"
2221
"encoding/csv"
2322
"errors"
@@ -31,7 +30,6 @@ import (
3130
"github.com/containerd/containerd/cmd/ctr/commands/tasks"
3231
"github.com/containerd/containerd/containers"
3332
clabels "github.com/containerd/containerd/labels"
34-
"github.com/containerd/containerd/namespaces"
3533
"github.com/containerd/containerd/oci"
3634
gocni "github.com/containerd/go-cni"
3735
specs "github.com/opencontainers/runtime-spec/specs-go"
@@ -196,7 +194,7 @@ var Command = cli.Command{
196194
if !detach {
197195
defer func() {
198196
if enableCNI {
199-
if err := network.Remove(ctx, fullID(ctx, container), ""); err != nil {
197+
if err := network.Remove(ctx, commands.FullID(ctx, container), ""); err != nil {
200198
logrus.WithError(err).Error("network review")
201199
}
202200
}
@@ -218,7 +216,7 @@ var Command = cli.Command{
218216
return err
219217
}
220218

221-
if _, err := network.Setup(ctx, fullID(ctx, container), netNsPath); err != nil {
219+
if _, err := network.Setup(ctx, commands.FullID(ctx, container), netNsPath); err != nil {
222220
return err
223221
}
224222
}
@@ -251,15 +249,6 @@ var Command = cli.Command{
251249
},
252250
}
253251

254-
func fullID(ctx context.Context, c containerd.Container) string {
255-
id := c.ID()
256-
ns, ok := namespaces.Namespace(ctx)
257-
if !ok {
258-
return id
259-
}
260-
return fmt.Sprintf("%s-%s", ns, id)
261-
}
262-
263252
// buildLabel builds the labels from command line labels and the image labels
264253
func buildLabels(cmdLabels, imageLabels map[string]string) map[string]string {
265254
labels := make(map[string]string)

cmd/ctr/commands/run/run_unix.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,10 @@ func NewContainer(ctx gocontext.Context, client *containerd.Client, context *cli
214214
)
215215
}
216216

217+
if context.Bool("cni") {
218+
cniMeta := &commands.NetworkMetaData{EnableCni: true}
219+
cOpts = append(cOpts, containerd.WithContainerExtension(commands.CtrCniMetadataExtension, cniMeta))
220+
}
217221
if caps := context.StringSlice("cap-add"); len(caps) > 0 {
218222
for _, cap := range caps {
219223
if !strings.HasPrefix(cap, "CAP_") {

cmd/ctr/commands/tasks/kill.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,50 @@
1717
package tasks
1818

1919
import (
20+
"context"
2021
"errors"
22+
"fmt"
2123

2224
"github.com/containerd/containerd"
2325
"github.com/containerd/containerd/cmd/ctr/commands"
26+
gocni "github.com/containerd/go-cni"
27+
"github.com/containerd/typeurl"
2428
"github.com/moby/sys/signal"
29+
"github.com/sirupsen/logrus"
2530
"github.com/urfave/cli"
2631
)
2732

2833
const defaultSignal = "SIGTERM"
2934

35+
func RemoveCniNetworkIfExist(ctx context.Context, container containerd.Container) error {
36+
exts, err := container.Extensions(ctx)
37+
if err != nil {
38+
return err
39+
}
40+
networkMeta, ok := exts[commands.CtrCniMetadataExtension]
41+
if !ok {
42+
return nil
43+
}
44+
45+
data, err := typeurl.UnmarshalAny(&networkMeta)
46+
if err != nil {
47+
return fmt.Errorf("failed to unmarshal cni metadata extension %s", commands.CtrCniMetadataExtension)
48+
}
49+
networkMetaData := data.(*commands.NetworkMetaData)
50+
51+
var network gocni.CNI
52+
if networkMetaData.EnableCni {
53+
if network, err = gocni.New(gocni.WithDefaultConf); err != nil {
54+
return err
55+
}
56+
if err := network.Remove(ctx, commands.FullID(ctx, container), ""); err != nil {
57+
logrus.WithError(err).Error("network remove error")
58+
return err
59+
}
60+
}
61+
return nil
62+
}
63+
3064
var killCommand = cli.Command{
3165
Name: "kill",
3266
Usage: "signal a container (default: SIGTERM)",
@@ -93,6 +127,10 @@ var killCommand = cli.Command{
93127
if err != nil {
94128
return err
95129
}
130+
err = RemoveCniNetworkIfExist(ctx, container)
131+
if err != nil {
132+
return err
133+
}
96134
return task.Kill(ctx, sig, opts...)
97135
},
98136
}

0 commit comments

Comments
 (0)