fix: ctr run --cni get failed
when user executes ctr run --cni to start a container,it will call cni plugin to create network .But when user kills it,the network won’t be removed. if we run a container with same namespace and name again will trigger a bug. we should remove the network when user kills task if it enables cni plugin. Fix:#6604 Signed-off-by: SongJiang Han <songjiang.dark@gmail.com>
This commit is contained in:
@@ -17,16 +17,50 @@
|
||||
package tasks
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/containerd/containerd"
|
||||
"github.com/containerd/containerd/cmd/ctr/commands"
|
||||
gocni "github.com/containerd/go-cni"
|
||||
"github.com/containerd/typeurl"
|
||||
"github.com/moby/sys/signal"
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/urfave/cli"
|
||||
)
|
||||
|
||||
const defaultSignal = "SIGTERM"
|
||||
|
||||
func RemoveCniNetworkIfExist(ctx context.Context, container containerd.Container) error {
|
||||
exts, err := container.Extensions(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
networkMeta, ok := exts[commands.CtrCniMetadataExtension]
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
|
||||
data, err := typeurl.UnmarshalAny(&networkMeta)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to unmarshal cni metadata extension %s", commands.CtrCniMetadataExtension)
|
||||
}
|
||||
networkMetaData := data.(*commands.NetworkMetaData)
|
||||
|
||||
var network gocni.CNI
|
||||
if networkMetaData.EnableCni {
|
||||
if network, err = gocni.New(gocni.WithDefaultConf); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := network.Remove(ctx, commands.FullID(ctx, container), ""); err != nil {
|
||||
logrus.WithError(err).Error("network remove error")
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
var killCommand = cli.Command{
|
||||
Name: "kill",
|
||||
Usage: "signal a container (default: SIGTERM)",
|
||||
@@ -93,6 +127,10 @@ var killCommand = cli.Command{
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = RemoveCniNetworkIfExist(ctx, container)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return task.Kill(ctx, sig, opts...)
|
||||
},
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user