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 d5686a8
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 8 deletions.
4 changes: 3 additions & 1 deletion cmd/ctr/commands/cni.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"github.com/containerd/containerd"
"github.com/containerd/containerd/namespaces"
gocni "github.com/containerd/go-cni"
"github.com/containerd/typeurl/v2"
)

Expand All @@ -38,7 +39,8 @@ const (

// ctr pass cni network metadata to containerd if ctr run use option of --cni
type NetworkMetaData struct {
EnableCni bool
EnableCni bool
CniBandwidthConf gocni.BandWidth
}

func FullID(ctx context.Context, c containerd.Container) string {
Expand Down
43 changes: 37 additions & 6 deletions cmd/ctr/commands/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,26 @@ var Command = cli.Command{
Name: "cni",
Usage: "Enable cni networking for the container",
},
cli.BoolFlag{
Name: "bandwidth",
Usage: "Enable bandwidth plugin for cni.",
},
cli.Uint64Flag{
Name: "ingress-rate",
Usage: "Ingress rate for cni bandwidth plugin",
},
cli.Uint64Flag{
Name: "egress-rate",
Usage: "Egress rate for cni bandwidth plugin",
},
cli.Uint64Flag{
Name: "ingress-burst",
Usage: "Ingress burst for cni bandwidth plugin",
},
cli.Uint64Flag{
Name: "egress-burst",
Usage: "Egress burst for cni bandwidth plugin",
},
}, append(platformRunFlags,
append(append(commands.SnapshotterFlags, []cli.Flag{commands.SnapshotterLabels}...),
commands.ContainerFlags...)...)...),
Expand All @@ -132,11 +152,12 @@ var Command = cli.Command{
id string
ref string

rm = context.Bool("rm")
tty = context.Bool("tty")
detach = context.Bool("detach")
config = context.IsSet("config")
enableCNI = context.Bool("cni")
rm = context.Bool("rm")
tty = context.Bool("tty")
detach = context.Bool("detach")
config = context.IsSet("config")
enableCNI = context.Bool("cni")
enableBandwidth = context.Bool("bandwidth")
)

if config {
Expand Down Expand Up @@ -220,7 +241,17 @@ var Command = cli.Command{
return err
}

if _, err := network.Setup(ctx, commands.FullID(ctx, container), netNsPath); err != nil {
var namespaceOpts []gocni.NamespaceOpts
if enableBandwidth {
namespaceOpts = append(namespaceOpts, gocni.WithCapabilityBandWidth(gocni.BandWidth{
IngressRate: context.Uint64("ingress-rate"),
EgressRate: context.Uint64("egress-rate"),
IngressBurst: context.Uint64("ingress-burst"),
EgressBurst: context.Uint64("egress-burst"),
}))
}

if _, err := network.Setup(ctx, commands.FullID(ctx, container), netNsPath, namespaceOpts...); err != nil {
return err
}
}
Expand Down
9 changes: 9 additions & 0 deletions cmd/ctr/commands/run/run_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import (
"github.com/containerd/containerd/platforms"
"github.com/containerd/containerd/runtime/v2/runc/options"
"github.com/containerd/containerd/snapshots"
gocni "github.com/containerd/go-cni"
"github.com/intel/goresctrl/pkg/blockio"
"github.com/opencontainers/runtime-spec/specs-go"
"github.com/urfave/cli"
Expand Down Expand Up @@ -245,6 +246,14 @@ func NewContainer(ctx gocontext.Context, client *containerd.Client, context *cli

if context.Bool("cni") {
cniMeta := &commands.NetworkMetaData{EnableCni: true}
if context.Bool("bandwidth") {
cniMeta.CniBandwidthConf = gocni.BandWidth{
IngressRate: context.Uint64("ingress-rate"),
EgressRate: context.Uint64("egress-rate"),
IngressBurst: context.Uint64("ingress-burst"),
EgressBurst: context.Uint64("egress-burst"),
}
}
cOpts = append(cOpts, containerd.WithContainerExtension(commands.CtrCniMetadataExtension, cniMeta))
}
if caps := context.StringSlice("cap-add"); len(caps) > 0 {
Expand Down
2 changes: 1 addition & 1 deletion cmd/ctr/commands/tasks/kill.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func RemoveCniNetworkIfExist(ctx context.Context, container containerd.Container
if network, err = gocni.New(gocni.WithDefaultConf); err != nil {
return err
}
if err := network.Remove(ctx, commands.FullID(ctx, container), ""); err != nil {
if err := network.Remove(ctx, commands.FullID(ctx, container), "", gocni.WithCapabilityBandWidth(networkMetaData.CniBandwidthConf)); err != nil {
logrus.WithError(err).Error("network remove error")
return err
}
Expand Down

0 comments on commit d5686a8

Please sign in to comment.