Skip to content

Commit d7d06b4

Browse files
committed
Add support for remaining blkio settings
Signed-off-by: Swagat Bora <[email protected]>
1 parent 8814dec commit d7d06b4

File tree

11 files changed

+679
-52
lines changed

11 files changed

+679
-52
lines changed

cmd/nerdctl/container/container_create.go

+26-3
Original file line numberDiff line numberDiff line change
@@ -199,19 +199,42 @@ func createOptions(cmd *cobra.Command) (types.ContainerCreateOptions, error) {
199199
if err != nil {
200200
return opt, err
201201
}
202+
opt.Cgroupns, err = cmd.Flags().GetString("cgroupns")
203+
if err != nil {
204+
return opt, err
205+
}
206+
opt.CgroupParent, err = cmd.Flags().GetString("cgroup-parent")
207+
if err != nil {
208+
return opt, err
209+
}
210+
opt.Device, err = cmd.Flags().GetStringSlice("device")
211+
if err != nil {
212+
return opt, err
213+
}
214+
// #endregion
215+
216+
// #region for blkio flags
202217
opt.BlkioWeight, err = cmd.Flags().GetUint16("blkio-weight")
203218
if err != nil {
204219
return opt, err
205220
}
206-
opt.Cgroupns, err = cmd.Flags().GetString("cgroupns")
221+
opt.BlkioWeightDevice, err = cmd.Flags().GetStringArray("blkio-weight-device")
207222
if err != nil {
208223
return opt, err
209224
}
210-
opt.CgroupParent, err = cmd.Flags().GetString("cgroup-parent")
225+
opt.BlkioDeviceReadBps, err = cmd.Flags().GetStringArray("device-read-bps")
211226
if err != nil {
212227
return opt, err
213228
}
214-
opt.Device, err = cmd.Flags().GetStringSlice("device")
229+
opt.BlkioDeviceWriteBps, err = cmd.Flags().GetStringArray("device-write-bps")
230+
if err != nil {
231+
return opt, err
232+
}
233+
opt.BlkioDeviceReadIOps, err = cmd.Flags().GetStringArray("device-read-iops")
234+
if err != nil {
235+
return opt, err
236+
}
237+
opt.BlkioDeviceWriteIOps, err = cmd.Flags().GetStringArray("device-write-iops")
215238
if err != nil {
216239
return opt, err
217240
}

cmd/nerdctl/container/container_run.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,6 @@ func setCreateFlags(cmd *cobra.Command) {
155155
})
156156
cmd.Flags().Int64("pids-limit", -1, "Tune container pids limit (set -1 for unlimited)")
157157
cmd.Flags().StringSlice("cgroup-conf", nil, "Configure cgroup v2 (key=value)")
158-
cmd.Flags().Uint16("blkio-weight", 0, "Block IO (relative weight), between 10 and 1000, or 0 to disable (default 0)")
159158
cmd.Flags().String("cgroupns", defaults.CgroupnsMode(), `Cgroup namespace to use, the default depends on the cgroup version ("host"|"private")`)
160159
cmd.Flags().String("cgroup-parent", "", "Optional parent cgroup for the container")
161160
cmd.RegisterFlagCompletionFunc("cgroupns", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
@@ -173,6 +172,15 @@ func setCreateFlags(cmd *cobra.Command) {
173172
cmd.Flags().String("rdt-class", "", "Name of the RDT class (or CLOS) to associate the container with")
174173
// #endregion
175174

175+
// #region blkio flags
176+
cmd.Flags().Uint16("blkio-weight", 0, "Block IO (relative weight), between 10 and 1000, or 0 to disable (default 0)")
177+
cmd.Flags().StringArray("blkio-weight-device", []string{}, "Block IO weight (relative device weight) (default [])")
178+
cmd.Flags().StringArray("device-read-bps", []string{}, "Limit read rate (bytes per second) from a device (default [])")
179+
cmd.Flags().StringArray("device-read-iops", []string{}, "Limit read rate (IO per second) from a device (default [])")
180+
cmd.Flags().StringArray("device-write-bps", []string{}, "Limit write rate (bytes per second) to a device (default [])")
181+
cmd.Flags().StringArray("device-write-iops", []string{}, "Limit write rate (IO per second) to a device (default [])")
182+
// #endregion
183+
176184
// user flags
177185
cmd.Flags().StringP("user", "u", "", "Username or UID (format: <name|uid>[:<group|gid>])")
178186
cmd.Flags().String("umask", "", "Set the umask inside the container. Defaults to 0022")

cmd/nerdctl/container/container_run_cgroup_linux_test.go

+128
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"os"
2424
"path/filepath"
2525
"strconv"
26+
"strings"
2627
"testing"
2728

2829
"gotest.tools/v3/assert"
@@ -477,3 +478,130 @@ func TestRunBlkioWeightCgroupV2(t *testing.T) {
477478
base.Cmd("update", containerName, "--blkio-weight", "400").AssertOK()
478479
base.Cmd("exec", containerName, "cat", "io.bfq.weight").AssertOutExactly("default 400\n")
479480
}
481+
482+
func TestRunBlkioSettingCgroupV2(t *testing.T) {
483+
testCase := nerdtest.Setup()
484+
485+
testCase.SubTests = []*test.Case{
486+
{
487+
Description: "blkio-weight",
488+
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
489+
return helpers.Command("run", "-d", "--name", data.Identifier(),
490+
"--blkio-weight", "150",
491+
testutil.AlpineImage, "sleep", "infinity")
492+
},
493+
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
494+
return &test.Expected{
495+
ExitCode: 0,
496+
Output: expect.All(
497+
func(stdout string, info string, t *testing.T) {
498+
assert.Assert(t, strings.Contains(helpers.Capture("inspect", "--format", "{{.HostConfig.BlkioWeight}}", data.Identifier()), "150"))
499+
},
500+
),
501+
}
502+
},
503+
},
504+
{
505+
Description: "blkio-weight-device",
506+
Require: nerdtest.CGroupV2,
507+
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
508+
return helpers.Command("run", "-d", "--name", data.Identifier(),
509+
"--blkio-weight-device", "/dev/sda:100",
510+
testutil.AlpineImage, "sleep", "infinity")
511+
},
512+
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
513+
return &test.Expected{
514+
ExitCode: 0,
515+
Output: expect.All(
516+
func(stdout string, info string, t *testing.T) {
517+
inspectOut := helpers.Capture("inspect", "--format", "{{range .HostConfig.BlkioWeightDevice}}{{.Weight}}{{end}}", data.Identifier())
518+
assert.Assert(t, strings.Contains(inspectOut, "100"))
519+
},
520+
),
521+
}
522+
},
523+
},
524+
{
525+
Description: "device-read-bps",
526+
Require: nerdtest.CGroupV2,
527+
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
528+
return helpers.Command("run", "-d", "--name", data.Identifier(),
529+
"--device-read-bps", "/dev/sda:1048576",
530+
testutil.AlpineImage, "sleep", "infinity")
531+
},
532+
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
533+
return &test.Expected{
534+
ExitCode: 0,
535+
Output: expect.All(
536+
func(stdout string, info string, t *testing.T) {
537+
inspectOut := helpers.Capture("inspect", "--format", "{{range .HostConfig.BlkioDeviceReadBps}}{{.Rate}}{{end}}", data.Identifier())
538+
assert.Assert(t, strings.Contains(inspectOut, "1048576"))
539+
},
540+
),
541+
}
542+
},
543+
},
544+
{
545+
Description: "device-write-bps",
546+
Require: nerdtest.CGroupV2,
547+
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
548+
return helpers.Command("run", "-d", "--name", data.Identifier(),
549+
"--device-write-bps", "/dev/sda:2097152",
550+
testutil.AlpineImage, "sleep", "infinity")
551+
},
552+
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
553+
return &test.Expected{
554+
ExitCode: 0,
555+
Output: expect.All(
556+
func(stdout string, info string, t *testing.T) {
557+
inspectOut := helpers.Capture("inspect", "--format", "{{range .HostConfig.BlkioDeviceWriteBps}}{{.Rate}}{{end}}", data.Identifier())
558+
assert.Assert(t, strings.Contains(inspectOut, "2097152"))
559+
},
560+
),
561+
}
562+
},
563+
},
564+
{
565+
Description: "device-read-iops",
566+
Require: nerdtest.CGroupV2,
567+
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
568+
return helpers.Command("run", "-d", "--name", data.Identifier(),
569+
"--device-read-iops", "/dev/sda:1000",
570+
testutil.AlpineImage, "sleep", "infinity")
571+
},
572+
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
573+
return &test.Expected{
574+
ExitCode: 0,
575+
Output: expect.All(
576+
func(stdout string, info string, t *testing.T) {
577+
inspectOut := helpers.Capture("inspect", "--format", "{{range .HostConfig.BlkioDeviceReadIOps}}{{.Rate}}{{end}}", data.Identifier())
578+
assert.Assert(t, strings.Contains(inspectOut, "1000"))
579+
},
580+
),
581+
}
582+
},
583+
},
584+
{
585+
Description: "device-write-iops",
586+
Require: nerdtest.CGroupV2,
587+
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
588+
return helpers.Command("run", "-d", "--name", data.Identifier(),
589+
"--device-write-iops", "/dev/sda:2000",
590+
testutil.AlpineImage, "sleep", "infinity")
591+
},
592+
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
593+
return &test.Expected{
594+
ExitCode: 0,
595+
Output: expect.All(
596+
func(stdout string, info string, t *testing.T) {
597+
inspectOut := helpers.Capture("inspect", "--format", "{{range .HostConfig.BlkioDeviceWriteIOps}}{{.Rate}}{{end}}", data.Identifier())
598+
assert.Assert(t, strings.Contains(inspectOut, "2000"))
599+
},
600+
),
601+
}
602+
},
603+
},
604+
}
605+
606+
testCase.Run(t)
607+
}

docs/command-reference.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,11 @@ Resource flags:
213213
- :whale: `--pids-limit`: Tune container pids limit
214214
- :nerd_face: `--cgroup-conf`: Configure cgroup v2 (key=value)
215215
- :whale: `--blkio-weight`: Block IO (relative weight), between 10 and 1000, or 0 to disable (default 0)
216+
- :whale: `--blkio-weight-device`: Block IO weight (relative device weight)
217+
- :whale: `--device-read-bps`: Limit read rate (bytes per second) from a device
218+
- :whale: `--device-read-iops`: Limit read rate (IO per second) from a device
219+
- :whale: `--device-write-bps`: Limit write rate (bytes per second) to a device
220+
- :whale: `--device-write-iops`: Limit write rate (IO per second) to a device
216221
- :whale: `--cgroupns=(host|private)`: Cgroup namespace to use
217222
- Default: "private" on cgroup v2 hosts, "host" on cgroup v1 hosts
218223
- :whale: `--cgroup-parent`: Optional parent cgroup for the container
@@ -414,7 +419,7 @@ IPFS flags:
414419
- :nerd_face: `--ipfs-address`: Multiaddr of IPFS API (default uses `$IPFS_PATH` env variable if defined or local directory `~/.ipfs`)
415420

416421
Unimplemented `docker run` flags:
417-
`--blkio-weight-device`, `--cpu-rt-*`, `--device-*`,
422+
`--cpu-rt-*`, `--device-cgroup-rule`,
418423
`--disable-content-trust`, `--expose`, `--health-*`, `--isolation`, `--no-healthcheck`,
419424
`--link*`, `--publish-all`, `--storage-opt`,
420425
`--userns`, `--volume-driver`

pkg/api/types/container_types.go

+15-2
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,6 @@ type ContainerCreateOptions struct {
140140
PidsLimit int64
141141
// CgroupConf specifies to configure cgroup v2 (key=value)
142142
CgroupConf []string
143-
// BlkioWeight specifies the block IO (relative weight), between 10 and 1000, or 0 to disable (default 0)
144-
BlkioWeight uint16
145143
// Cgroupns specifies the cgroup namespace to use
146144
Cgroupns string
147145
// CgroupParent specifies the optional parent cgroup for the container
@@ -150,6 +148,21 @@ type ContainerCreateOptions struct {
150148
Device []string
151149
// #endregion
152150

151+
// #region for blkio related flags
152+
// BlkioWeight specifies the block IO (relative weight), between 10 and 1000, or 0 to disable (default 0)
153+
BlkioWeight uint16
154+
// BlkioWeightDevice specifies the Block IO weight (relative device weight)
155+
BlkioWeightDevice []string
156+
// BlkioDeviceReadBps specifies the Block IO read rate limit(bytes per second) of a device
157+
BlkioDeviceReadBps []string
158+
// BlkioDeviceWriteBps specifies the Block IO write rate limit(bytes per second) of a device
159+
BlkioDeviceWriteBps []string
160+
// BlkioDeviceReadIOps specifies the Block IO read rate limit(IO per second) of a device
161+
BlkioDeviceReadIOps []string
162+
// BlkioDeviceWriteIOps specifies the Block IO read rate limit(IO per second) of a device
163+
BlkioDeviceWriteIOps []string
164+
// #endregion
165+
153166
// #region for intel RDT flags
154167
// RDTClass specifies the Intel Resource Director Technology (RDT) class
155168
RDTClass string

pkg/cmd/container/create.go

+4-11
Original file line numberDiff line numberDiff line change
@@ -331,8 +331,6 @@ func Create(ctx context.Context, client *containerd.Client, args []string, netMa
331331

332332
internalLabels.rm = containerutil.EncodeContainerRmOptLabel(options.Rm)
333333

334-
internalLabels.blkioWeight = options.BlkioWeight
335-
336334
// TODO: abolish internal labels and only use annotations
337335
ilOpt, err := withInternalLabels(internalLabels)
338336
if err != nil {
@@ -624,11 +622,10 @@ func withStop(stopSignal string, stopTimeout int, ensuredImage *imgutil.EnsuredI
624622

625623
type internalLabels struct {
626624
// labels from cmd options
627-
namespace string
628-
platform string
629-
extraHosts []string
630-
pidFile string
631-
blkioWeight uint16
625+
namespace string
626+
platform string
627+
extraHosts []string
628+
pidFile string
632629
// labels from cmd options or automatically set
633630
name string
634631
hostname string
@@ -754,10 +751,6 @@ func withInternalLabels(internalLabels internalLabels) (containerd.NewContainerO
754751
m[labels.ContainerAutoRemove] = internalLabels.rm
755752
}
756753

757-
if internalLabels.blkioWeight > 0 {
758-
hostConfigLabel.BlkioWeight = internalLabels.blkioWeight
759-
}
760-
761754
if internalLabels.cidFile != "" {
762755
hostConfigLabel.CidFile = internalLabels.cidFile
763756
}

0 commit comments

Comments
 (0)