Skip to content

Commit d7ea58b

Browse files
committed
feat: run.WithCPUSet and run.WithMems
Signed-off-by: thediveo <thediveo@gmx.eu>
1 parent 6241a31 commit d7ea58b

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

run/doc.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
/*
22
Package run provides configuration options for running containers.
3+
4+
In many cases, Docker's documentation on [docker container run] and the
5+
[containers/create API endpoint] can give useful hints as to what various API
6+
configuration settings might do.
7+
8+
[docker container run]: https://docs.docker.com/reference/cli/docker/container/run/
9+
[containers/create API endpoint]: https://docs.docker.com/reference/api/engine/version/v1.39/#tag/Container/operation/ContainerCreate
310
*/
411
package run

run/options.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,37 @@ func WithAllPortsPublished() Opt {
632632
}
633633
}
634634

635+
// WithCPUSet configures the [set of CPUs] on which processes of the container
636+
// are allowed to execute. The list of CPUs is a comma-separated list of CPU
637+
// numbers and ranges of numbers, where the numbers are decimal numbers. For
638+
// instance, “1,3,5”, "0-42,666", et cetera.
639+
//
640+
// To avoid stuttering this option is simply named “WithCPUSet” instead of
641+
// “WithCpusetCPUs”, or similar awkward letter salads.
642+
//
643+
// [set of CPUs]: https://man7.org/linux/man-pages/man7/cpuset.7.html
644+
func WithCPUSet(cpulist string) Opt {
645+
return func(o *Options) error {
646+
o.Host.CpusetCpus = cpulist
647+
return nil
648+
}
649+
}
650+
651+
// WithMems configures the [set of memory nodes] on which processes of the
652+
// container are allowed to allocate memory. The list of memory nodes is a
653+
// comma-separated list of memory node numbers and ranges of numbers, where the
654+
// numbers are decimal numbers. For instance, “1,3,5”, "0-42,666", et cetera.
655+
//
656+
// [set of memory nodes]: https://man7.org/linux/man-pages/man7/cpuset.7.html
657+
func WithMems(memlist string) Opt {
658+
return func(o *Options) error {
659+
o.Host.CpusetMems = memlist
660+
return nil
661+
}
662+
}
663+
664+
// WithCustomInit instructs Docker to run an init inside the container that
665+
// forwards signals and reaps processes.
635666
func WithCustomInit() Opt {
636667
return func(o *Options) error {
637668
customInit := true

run/options_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ var _ = Describe("run (container) options", func() {
112112
WithPublishedPort("[fe80::dead:beef]:2345:1234"),
113113
WithPublishedPort("127.0.0.1:1234"),
114114
WithPublishedPort("127.0.0.2:12345/udp"),
115+
WithCPUSet("1,3,5,99"),
116+
WithMems("6,66"),
115117
WithCustomInit(),
116118
)
117119

@@ -167,6 +169,8 @@ var _ = Describe("run (container) options", func() {
167169
nat.Port("12345/udp"),
168170
ConsistOf(nat.PortBinding{HostIP: "127.0.0.2", HostPort: "0"})))
169171

172+
Expect(o.Host.CpusetCpus).To(Equal("1,3,5,99"))
173+
Expect(o.Host.CpusetMems).To(Equal("6,66"))
170174
Expect(o.Host.Init).To(gstruct.PointTo(BeTrue()))
171175

172176
o = opts(WithLabel("foo=bar"))

0 commit comments

Comments
 (0)