From d2f3b71468f385e535e6db31b430bfeeb973dead Mon Sep 17 00:00:00 2001 From: Michael Crosby Date: Mon, 9 Aug 2021 12:21:21 -0400 Subject: [PATCH] add cpu-shares to ctr This allows the cpu shares to be modified via ctr. Signed-off-by: Michael Crosby --- cmd/ctr/commands/run/run_unix.go | 9 +++++++++ oci/spec_opts_nonlinux.go | 8 ++++++++ 2 files changed, 17 insertions(+) diff --git a/cmd/ctr/commands/run/run_unix.go b/cmd/ctr/commands/run/run_unix.go index cb5abd876..5782a751f 100644 --- a/cmd/ctr/commands/run/run_unix.go +++ b/cmd/ctr/commands/run/run_unix.go @@ -70,6 +70,11 @@ var platformRunFlags = []cli.Flag{ Usage: "set the CFS cpu quota", Value: 0.0, }, + cli.IntFlag{ + Name: "cpu-shares", + Usage: "set the cpu shares", + Value: 1024, + }, cli.BoolFlag{ Name: "cni", Usage: "enable cni networking for the container", @@ -226,6 +231,10 @@ func NewContainer(ctx gocontext.Context, client *containerd.Client, context *cli opts = append(opts, oci.WithCPUCFS(quota, period)) } + if shares := context.Int("cpu-shares"); shares > 0 { + opts = append(opts, oci.WithCPUShares(uint64(shares))) + } + quota := context.Int64("cpu-quota") period := context.Uint64("cpu-period") if quota != -1 || period != 0 { diff --git a/oci/spec_opts_nonlinux.go b/oci/spec_opts_nonlinux.go index 77a163638..85dee007f 100644 --- a/oci/spec_opts_nonlinux.go +++ b/oci/spec_opts_nonlinux.go @@ -36,3 +36,11 @@ var WithAllCurrentCapabilities = func(ctx context.Context, client Client, c *con var WithAllKnownCapabilities = func(ctx context.Context, client Client, c *containers.Container, s *Spec) error { return WithCapabilities(nil)(ctx, client, c, s) } + +// WithCPUShares sets the container's cpu shares +//nolint: deadcode, unused +func WithCPUShares(shares uint64) SpecOpts { + return func(ctx context.Context, _ Client, c *containers.Container, s *Spec) error { + return nil + } +}