Skip to content

Commit 7dd7c7f

Browse files
committed
api,runtime-tools: adjust for runtime-spec v1.3.0.
Signed-off-by: Krisztian Litkey <[email protected]>
1 parent 5d5d4c4 commit 7dd7c7f

4 files changed

Lines changed: 37 additions & 9 deletions

File tree

pkg/api/hooks.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func (hooks *Hooks) Append(h *Hooks) *Hooks {
2525
if h == nil {
2626
return hooks
2727
}
28-
hooks.Prestart = append(hooks.Prestart, h.Prestart...)
28+
hooks.Prestart = append(hooks.Prestart, h.Prestart...) //nolint:staticcheck // ignore SA1019: o.Prestart is deprecated
2929
hooks.CreateRuntime = append(hooks.CreateRuntime, h.CreateRuntime...)
3030
hooks.CreateContainer = append(hooks.CreateContainer, h.CreateContainer...)
3131
hooks.StartContainer = append(hooks.StartContainer, h.StartContainer...)
@@ -79,7 +79,7 @@ func FromOCIHooks(o *rspec.Hooks) *Hooks {
7979
return nil
8080
}
8181
return &Hooks{
82-
Prestart: FromOCIHookSlice(o.Prestart),
82+
Prestart: FromOCIHookSlice(o.Prestart), //nolint:staticcheck // ignore SA1019: o.Prestart is deprecated
8383
CreateRuntime: FromOCIHookSlice(o.CreateRuntime),
8484
CreateContainer: FromOCIHookSlice(o.CreateContainer),
8585
StartContainer: FromOCIHookSlice(o.StartContainer),

pkg/api/resources.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ import (
2222
rspec "github.com/opencontainers/runtime-spec/specs-go"
2323
)
2424

25+
const (
26+
// UnlimitedPidsLimit indicates unlimited Linux PIDs limit.
27+
UnlimitedPidsLimit = -1
28+
)
29+
2530
// FromOCILinuxResources returns resources from an OCI runtime Spec.
2631
func FromOCILinuxResources(o *rspec.LinuxResources, _ map[string]string) *LinuxResources {
2732
if o == nil {
@@ -33,7 +38,7 @@ func FromOCILinuxResources(o *rspec.LinuxResources, _ map[string]string) *LinuxR
3338
Limit: Int64(m.Limit),
3439
Reservation: Int64(m.Reservation),
3540
Swap: Int64(m.Swap),
36-
Kernel: Int64(m.Kernel),
41+
Kernel: Int64(m.Kernel), //nolint:staticcheck // ignore SA1019: m.Kernel is deprecated
3742
KernelTcp: Int64(m.KernelTCP),
3843
Swappiness: UInt64(m.Swappiness),
3944
DisableOomKiller: Bool(m.DisableOOMKiller),
@@ -67,8 +72,9 @@ func FromOCILinuxResources(o *rspec.LinuxResources, _ map[string]string) *LinuxR
6772
})
6873
}
6974
if p := o.Pids; p != nil {
70-
l.Pids = &LinuxPids{
71-
Limit: p.Limit,
75+
l.Pids = &LinuxPids{}
76+
if p.Limit != nil && *p.Limit != 0 {
77+
l.Pids.Limit = *p.Limit
7278
}
7379
}
7480
if len(o.Unified) != 0 {
@@ -134,8 +140,10 @@ func (r *LinuxResources) ToOCI() *rspec.LinuxResources {
134140
})
135141
}
136142
if r.Pids != nil {
137-
o.Pids = &rspec.LinuxPids{
138-
Limit: r.Pids.Limit,
143+
o.Pids = &rspec.LinuxPids{}
144+
if r.Pids.Limit > UnlimitedPidsLimit {
145+
limit := r.Pids.Limit
146+
o.Pids.Limit = &limit
139147
}
140148
}
141149
return o

pkg/runtime-tools/generate/generate.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ import (
2929
nri "github.com/containerd/nri/pkg/api"
3030
)
3131

32+
const (
33+
// UnlimitedPidsLimit indicates unlimited Linux PIDs limit.
34+
UnlimitedPidsLimit = -1
35+
)
36+
3237
// GeneratorOption is an option for Generator().
3338
type GeneratorOption func(*Generator)
3439

@@ -611,6 +616,21 @@ func (g *Generator) SetProcessIOPriority(ioprio *rspec.LinuxIOPriority) {
611616
g.Config.Process.IOPriority = ioprio
612617
}
613618

619+
// SetLinuxResourcesPidsLimit sets Linux PID limit. Starting with
620+
// v1.3.0 opencontainers/runtime-spec switched the PID limit to
621+
// *int64 from int64 with nil meaning "unlimited". We don't want
622+
// to change our API types though, so instead we use a dedicated
623+
// value for unlimited.
624+
func (g *Generator) SetLinuxResourcesPidsLimit(limit int64) {
625+
g.initConfigLinuxResources()
626+
if g.Config.Linux.Resources.Pids == nil {
627+
g.Config.Linux.Resources.Pids = &rspec.LinuxPids{}
628+
}
629+
if limit > UnlimitedPidsLimit {
630+
g.Config.Linux.Resources.Pids.Limit = &limit
631+
}
632+
}
633+
614634
func (g *Generator) initConfig() {
615635
if g.Config == nil {
616636
g.Config = &rspec.Spec{}

pkg/runtime-tools/generate/generate_suite_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,7 @@ func withPidsLimit(v int64) specOption {
565565
spec.Linux.Resources = &rspec.LinuxResources{}
566566
}
567567
spec.Linux.Resources.Pids = &rspec.LinuxPids{
568-
Limit: v,
568+
Limit: &v,
569569
}
570570
}
571571
}
@@ -605,7 +605,7 @@ func makeSpec(options ...specOption) *rspec.Spec {
605605
Mems: "0-4",
606606
},
607607
Pids: &rspec.LinuxPids{
608-
Limit: 1,
608+
Limit: func(v int64) *int64 { return &v }(1),
609609
},
610610
},
611611
},

0 commit comments

Comments
 (0)