Skip to content

Commit a22081c

Browse files
committed
Make OCI spec opts available on all platforms
Signed-off-by: Maksym Pavlenko <[email protected]>
1 parent 5ef8bd4 commit a22081c

7 files changed

Lines changed: 435 additions & 463 deletions

File tree

oci/spec_opts.go

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1433,3 +1433,170 @@ func WithWindowsDevice(idType, id string) SpecOpts {
14331433
return nil
14341434
}
14351435
}
1436+
1437+
// WithMemorySwap sets the container's swap in bytes
1438+
func WithMemorySwap(swap int64) SpecOpts {
1439+
return func(ctx context.Context, _ Client, c *containers.Container, s *Spec) error {
1440+
setResources(s)
1441+
if s.Linux.Resources.Memory == nil {
1442+
s.Linux.Resources.Memory = &specs.LinuxMemory{}
1443+
}
1444+
s.Linux.Resources.Memory.Swap = &swap
1445+
return nil
1446+
}
1447+
}
1448+
1449+
// WithPidsLimit sets the container's pid limit or maximum
1450+
func WithPidsLimit(limit int64) SpecOpts {
1451+
return func(ctx context.Context, _ Client, c *containers.Container, s *Spec) error {
1452+
setResources(s)
1453+
if s.Linux.Resources.Pids == nil {
1454+
s.Linux.Resources.Pids = &specs.LinuxPids{}
1455+
}
1456+
s.Linux.Resources.Pids.Limit = limit
1457+
return nil
1458+
}
1459+
}
1460+
1461+
// WithBlockIO sets the container's blkio parameters
1462+
func WithBlockIO(blockio *specs.LinuxBlockIO) SpecOpts {
1463+
return func(ctx context.Context, _ Client, c *containers.Container, s *Spec) error {
1464+
setResources(s)
1465+
s.Linux.Resources.BlockIO = blockio
1466+
return nil
1467+
}
1468+
}
1469+
1470+
// WithCPUShares sets the container's cpu shares
1471+
func WithCPUShares(shares uint64) SpecOpts {
1472+
return func(ctx context.Context, _ Client, c *containers.Container, s *Spec) error {
1473+
setCPU(s)
1474+
s.Linux.Resources.CPU.Shares = &shares
1475+
return nil
1476+
}
1477+
}
1478+
1479+
// WithCPUs sets the container's cpus/cores for use by the container
1480+
func WithCPUs(cpus string) SpecOpts {
1481+
return func(ctx context.Context, _ Client, c *containers.Container, s *Spec) error {
1482+
setCPU(s)
1483+
s.Linux.Resources.CPU.Cpus = cpus
1484+
return nil
1485+
}
1486+
}
1487+
1488+
// WithCPUsMems sets the container's cpu mems for use by the container
1489+
func WithCPUsMems(mems string) SpecOpts {
1490+
return func(ctx context.Context, _ Client, c *containers.Container, s *Spec) error {
1491+
setCPU(s)
1492+
s.Linux.Resources.CPU.Mems = mems
1493+
return nil
1494+
}
1495+
}
1496+
1497+
// WithCPUCFS sets the container's Completely fair scheduling (CFS) quota and period
1498+
func WithCPUCFS(quota int64, period uint64) SpecOpts {
1499+
return func(ctx context.Context, _ Client, c *containers.Container, s *Spec) error {
1500+
setCPU(s)
1501+
s.Linux.Resources.CPU.Quota = &quota
1502+
s.Linux.Resources.CPU.Period = &period
1503+
return nil
1504+
}
1505+
}
1506+
1507+
// WithCPURT sets the container's realtime scheduling (RT) runtime and period.
1508+
func WithCPURT(runtime int64, period uint64) SpecOpts {
1509+
return func(ctx context.Context, _ Client, c *containers.Container, s *Spec) error {
1510+
setCPU(s)
1511+
s.Linux.Resources.CPU.RealtimeRuntime = &runtime
1512+
s.Linux.Resources.CPU.RealtimePeriod = &period
1513+
return nil
1514+
}
1515+
}
1516+
1517+
// WithoutRunMount removes the `/run` inside the spec
1518+
func WithoutRunMount(ctx context.Context, client Client, c *containers.Container, s *Spec) error {
1519+
return WithoutMounts("/run")(ctx, client, c, s)
1520+
}
1521+
1522+
// WithRdt sets the container's RDT parameters
1523+
func WithRdt(closID, l3CacheSchema, memBwSchema string) SpecOpts {
1524+
return func(ctx context.Context, _ Client, c *containers.Container, s *Spec) error {
1525+
s.Linux.IntelRdt = &specs.LinuxIntelRdt{
1526+
ClosID: closID,
1527+
L3CacheSchema: l3CacheSchema,
1528+
MemBwSchema: memBwSchema,
1529+
}
1530+
return nil
1531+
}
1532+
}
1533+
1534+
// WithWindowsCPUCount sets the `Windows.Resources.CPU.Count` section to the
1535+
// `count` specified.
1536+
func WithWindowsCPUCount(count uint64) SpecOpts {
1537+
return func(_ context.Context, _ Client, _ *containers.Container, s *Spec) error {
1538+
setCPUWindows(s)
1539+
s.Windows.Resources.CPU.Count = &count
1540+
return nil
1541+
}
1542+
}
1543+
1544+
// WithWindowsCPUShares sets the `Windows.Resources.CPU.Shares` section to the
1545+
// `shares` specified.
1546+
func WithWindowsCPUShares(shares uint16) SpecOpts {
1547+
return func(_ context.Context, _ Client, _ *containers.Container, s *Spec) error {
1548+
setCPUWindows(s)
1549+
s.Windows.Resources.CPU.Shares = &shares
1550+
return nil
1551+
}
1552+
}
1553+
1554+
// WithWindowsCPUMaximum sets the `Windows.Resources.CPU.Maximum` section to the
1555+
// `max` specified.
1556+
func WithWindowsCPUMaximum(max uint16) SpecOpts {
1557+
return func(_ context.Context, _ Client, _ *containers.Container, s *Spec) error {
1558+
setCPUWindows(s)
1559+
s.Windows.Resources.CPU.Maximum = &max
1560+
return nil
1561+
}
1562+
}
1563+
1564+
// WithWindowsIgnoreFlushesDuringBoot sets `Windows.IgnoreFlushesDuringBoot`.
1565+
func WithWindowsIgnoreFlushesDuringBoot() SpecOpts {
1566+
return func(_ context.Context, _ Client, _ *containers.Container, s *Spec) error {
1567+
if s.Windows == nil {
1568+
s.Windows = &specs.Windows{}
1569+
}
1570+
s.Windows.IgnoreFlushesDuringBoot = true
1571+
return nil
1572+
}
1573+
}
1574+
1575+
// WithWindowNetworksAllowUnqualifiedDNSQuery sets `Windows.Network.AllowUnqualifiedDNSQuery`.
1576+
func WithWindowNetworksAllowUnqualifiedDNSQuery() SpecOpts {
1577+
return func(_ context.Context, _ Client, _ *containers.Container, s *Spec) error {
1578+
if s.Windows == nil {
1579+
s.Windows = &specs.Windows{}
1580+
}
1581+
if s.Windows.Network == nil {
1582+
s.Windows.Network = &specs.WindowsNetwork{}
1583+
}
1584+
1585+
s.Windows.Network.AllowUnqualifiedDNSQuery = true
1586+
return nil
1587+
}
1588+
}
1589+
1590+
// WithWindowsNetworkNamespace sets the network namespace for a Windows container.
1591+
func WithWindowsNetworkNamespace(ns string) SpecOpts {
1592+
return func(_ context.Context, _ Client, _ *containers.Container, s *Spec) error {
1593+
if s.Windows == nil {
1594+
s.Windows = &specs.Windows{}
1595+
}
1596+
if s.Windows.Network == nil {
1597+
s.Windows.Network = &specs.WindowsNetwork{}
1598+
}
1599+
s.Windows.Network.NetworkNamespace = ns
1600+
return nil
1601+
}
1602+
}

oci/spec_opts_linux.go

Lines changed: 0 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -60,86 +60,6 @@ func WithDevices(devicePath, containerPath, permissions string) SpecOpts {
6060
}
6161
}
6262

63-
// WithMemorySwap sets the container's swap in bytes
64-
func WithMemorySwap(swap int64) SpecOpts {
65-
return func(ctx context.Context, _ Client, c *containers.Container, s *Spec) error {
66-
setResources(s)
67-
if s.Linux.Resources.Memory == nil {
68-
s.Linux.Resources.Memory = &specs.LinuxMemory{}
69-
}
70-
s.Linux.Resources.Memory.Swap = &swap
71-
return nil
72-
}
73-
}
74-
75-
// WithPidsLimit sets the container's pid limit or maximum
76-
func WithPidsLimit(limit int64) SpecOpts {
77-
return func(ctx context.Context, _ Client, c *containers.Container, s *Spec) error {
78-
setResources(s)
79-
if s.Linux.Resources.Pids == nil {
80-
s.Linux.Resources.Pids = &specs.LinuxPids{}
81-
}
82-
s.Linux.Resources.Pids.Limit = limit
83-
return nil
84-
}
85-
}
86-
87-
// WithBlockIO sets the container's blkio parameters
88-
func WithBlockIO(blockio *specs.LinuxBlockIO) SpecOpts {
89-
return func(ctx context.Context, _ Client, c *containers.Container, s *Spec) error {
90-
setResources(s)
91-
s.Linux.Resources.BlockIO = blockio
92-
return nil
93-
}
94-
}
95-
96-
// WithCPUShares sets the container's cpu shares
97-
func WithCPUShares(shares uint64) SpecOpts {
98-
return func(ctx context.Context, _ Client, c *containers.Container, s *Spec) error {
99-
setCPU(s)
100-
s.Linux.Resources.CPU.Shares = &shares
101-
return nil
102-
}
103-
}
104-
105-
// WithCPUs sets the container's cpus/cores for use by the container
106-
func WithCPUs(cpus string) SpecOpts {
107-
return func(ctx context.Context, _ Client, c *containers.Container, s *Spec) error {
108-
setCPU(s)
109-
s.Linux.Resources.CPU.Cpus = cpus
110-
return nil
111-
}
112-
}
113-
114-
// WithCPUsMems sets the container's cpu mems for use by the container
115-
func WithCPUsMems(mems string) SpecOpts {
116-
return func(ctx context.Context, _ Client, c *containers.Container, s *Spec) error {
117-
setCPU(s)
118-
s.Linux.Resources.CPU.Mems = mems
119-
return nil
120-
}
121-
}
122-
123-
// WithCPUCFS sets the container's Completely fair scheduling (CFS) quota and period
124-
func WithCPUCFS(quota int64, period uint64) SpecOpts {
125-
return func(ctx context.Context, _ Client, c *containers.Container, s *Spec) error {
126-
setCPU(s)
127-
s.Linux.Resources.CPU.Quota = &quota
128-
s.Linux.Resources.CPU.Period = &period
129-
return nil
130-
}
131-
}
132-
133-
// WithCPURT sets the container's realtime scheduling (RT) runtime and period.
134-
func WithCPURT(runtime int64, period uint64) SpecOpts {
135-
return func(ctx context.Context, _ Client, c *containers.Container, s *Spec) error {
136-
setCPU(s)
137-
s.Linux.Resources.CPU.RealtimeRuntime = &runtime
138-
s.Linux.Resources.CPU.RealtimePeriod = &period
139-
return nil
140-
}
141-
}
142-
14363
// WithAllCurrentCapabilities propagates the effective capabilities of the caller process to the container process.
14464
// The capability set may differ from WithAllKnownCapabilities when running in a container.
14565
var WithAllCurrentCapabilities = func(ctx context.Context, client Client, c *containers.Container, s *Spec) error {
@@ -156,23 +76,6 @@ var WithAllKnownCapabilities = func(ctx context.Context, client Client, c *conta
15676
return WithCapabilities(caps)(ctx, client, c, s)
15777
}
15878

159-
// WithoutRunMount removes the `/run` inside the spec
160-
func WithoutRunMount(ctx context.Context, client Client, c *containers.Container, s *Spec) error {
161-
return WithoutMounts("/run")(ctx, client, c, s)
162-
}
163-
164-
// WithRdt sets the container's RDT parameters
165-
func WithRdt(closID, l3CacheSchema, memBwSchema string) SpecOpts {
166-
return func(ctx context.Context, _ Client, c *containers.Container, s *Spec) error {
167-
s.Linux.IntelRdt = &specs.LinuxIntelRdt{
168-
ClosID: closID,
169-
L3CacheSchema: l3CacheSchema,
170-
MemBwSchema: memBwSchema,
171-
}
172-
return nil
173-
}
174-
}
175-
17679
func escapeAndCombineArgs(args []string) string {
17780
panic("not supported")
17881
}

oci/spec_opts_nonlinux.go

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ package oci
2020

2121
import (
2222
"context"
23-
"errors"
2423

2524
"github.com/containerd/containerd/containers"
2625
)
@@ -35,24 +34,3 @@ var WithAllCurrentCapabilities = func(ctx context.Context, client Client, c *con
3534
var WithAllKnownCapabilities = func(ctx context.Context, client Client, c *containers.Container, s *Spec) error {
3635
return WithCapabilities(nil)(ctx, client, c, s)
3736
}
38-
39-
// WithBlockIO sets the container's blkio parameters
40-
func WithBlockIO(blockio interface{}) SpecOpts {
41-
return func(ctx context.Context, _ Client, c *containers.Container, s *Spec) error {
42-
return errors.New("blkio not supported")
43-
}
44-
}
45-
46-
// WithCPUShares sets the container's cpu shares
47-
func WithCPUShares(shares uint64) SpecOpts {
48-
return func(ctx context.Context, _ Client, c *containers.Container, s *Spec) error {
49-
return nil
50-
}
51-
}
52-
53-
// WithRdt sets the container's RDT parameters
54-
func WithRdt(closID, l3CacheSchema, memBwSchema string) SpecOpts {
55-
return func(_ context.Context, _ Client, _ *containers.Container, _ *Spec) error {
56-
return errors.New("RDT not supported")
57-
}
58-
}

oci/spec_opts_unix.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,6 @@ func WithDevices(devicePath, containerPath, permissions string) SpecOpts {
5050
}
5151
}
5252

53-
// WithCPUCFS sets the container's Completely fair scheduling (CFS) quota and period
54-
func WithCPUCFS(quota int64, period uint64) SpecOpts {
55-
return func(ctx context.Context, _ Client, c *containers.Container, s *Spec) error {
56-
return nil
57-
}
58-
}
59-
6053
func escapeAndCombineArgs(args []string) string {
6154
panic("not supported")
6255
}

0 commit comments

Comments
 (0)