Skip to content

Commit

Permalink
Directly use go cni bandwidth options and expose them with ctr args
Browse files Browse the repository at this point in the history
Signed-off-by: Yakul Garg <[email protected]>
  • Loading branch information
2000yeshu committed Mar 28, 2024
1 parent 70024e7 commit 5101b94
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 4 deletions.
3 changes: 2 additions & 1 deletion cmd/ctr/commands/cni.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ const (

// ctr pass cni network metadata to containerd if ctr run use option of --cni
type NetworkMetaData struct {
EnableCni bool
EnableCni bool
CniBandwidthConfFile string
}

func FullID(ctx context.Context, c containerd.Container) string {
Expand Down
6 changes: 5 additions & 1 deletion cmd/ctr/commands/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ var Command = cli.Command{
Name: "cni",
Usage: "Enable cni networking for the container",
},
cli.Uint64Flag{
Name: "cni-bandwidth-conf",
Usage: "Cni conf for bandwidth plugin",
},
}, append(platformRunFlags,
append(append(commands.SnapshotterFlags, []cli.Flag{commands.SnapshotterLabels}...),
commands.ContainerFlags...)...)...),
Expand Down Expand Up @@ -182,7 +186,7 @@ var Command = cli.Command{
}
var network gocni.CNI
if enableCNI {
if network, err = gocni.New(gocni.WithDefaultConf); err != nil {
if network, err = gocni.New(gocni.WithDefaultConf, gocni.WithConfFile(context.String("cni-bandwidth-conf"))); err != nil {
return err
}
}
Expand Down
3 changes: 3 additions & 0 deletions cmd/ctr/commands/run/run_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,9 @@ func NewContainer(ctx gocontext.Context, client *containerd.Client, context *cli

if context.Bool("cni") {
cniMeta := &commands.NetworkMetaData{EnableCni: true}

cniMeta.CniBandwidthConfFile = context.String("cni-bandwidth-conf")

cOpts = append(cOpts, containerd.WithContainerExtension(commands.CtrCniMetadataExtension, cniMeta))
}
if caps := context.StringSlice("cap-add"); len(caps) > 0 {
Expand Down
11 changes: 9 additions & 2 deletions cmd/ctr/commands/tasks/kill.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,16 @@ func RemoveCniNetworkIfExist(ctx context.Context, container containerd.Container

var network gocni.CNI
if networkMetaData.EnableCni {
if network, err = gocni.New(gocni.WithDefaultConf); err != nil {
return err
if networkMetaData.CniBandwidthConfFile == "" {
if network, err = gocni.New(gocni.WithDefaultConf); err != nil {
return err
}
} else {
if network, err = gocni.New(gocni.WithDefaultConf, gocni.WithConfFile(networkMetaData.CniBandwidthConfFile)); err != nil {
return err
}
}

if err := network.Remove(ctx, commands.FullID(ctx, container), ""); err != nil {
logrus.WithError(err).Error("network remove error")
return err
Expand Down

0 comments on commit 5101b94

Please sign in to comment.