Skip to content

Commit d2dd708

Browse files
committed
Add support for rlimits
Signed-off-by: Samuel Karp <[email protected]>
1 parent efaf36e commit d2dd708

File tree

7 files changed

+124
-2
lines changed

7 files changed

+124
-2
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ The following pieces of container metadata are available to plugins in NRI:
175175
- environment variables
176176
- mounts
177177
- OCI hooks
178+
- rlimits
178179
- linux
179180
- namespace IDs
180181
- devices
@@ -212,6 +213,7 @@ container parameters:
212213
- mounts
213214
- environment variables
214215
- OCI hooks
216+
- rlimits
215217
- linux
216218
- devices
217219
- resources

pkg/adaptation/adaptation_suite_test.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,11 @@ import (
2626

2727
"sigs.k8s.io/yaml"
2828

29-
nri "github.com/containerd/nri/pkg/adaptation"
30-
"github.com/containerd/nri/pkg/api"
3129
. "github.com/onsi/ginkgo/v2"
3230
. "github.com/onsi/gomega"
31+
32+
nri "github.com/containerd/nri/pkg/adaptation"
33+
"github.com/containerd/nri/pkg/api"
3334
)
3435

3536
var _ = Describe("Configuration", func() {
@@ -484,6 +485,9 @@ var _ = Describe("Plugin container creation adjustments", func() {
484485
}
485486
a.AddDevice(dev)
486487

488+
case "rlimit":
489+
a.AddRlimit("nofile", 456, 123)
490+
487491
case "resources/cpu":
488492
a.SetLinuxCPUShares(123)
489493
a.SetLinuxCPUQuota(456)
@@ -623,6 +627,11 @@ var _ = Describe("Plugin container creation adjustments", func() {
623627
},
624628
},
625629
),
630+
Entry("adjust rlimits", "rlimit",
631+
&api.ContainerAdjustment{
632+
Rlimits: []*api.POSIXRlimit{{Type: "nofile", Soft: 123, Hard: 456}},
633+
},
634+
),
626635
Entry("adjust CPU resources", "resources/cpu",
627636
&api.ContainerAdjustment{
628637
Linux: &api.LinuxContainerAdjustment{
@@ -1847,6 +1856,7 @@ func stripAdjustment(a *api.ContainerAdjustment) *api.ContainerAdjustment {
18471856
stripMounts(a)
18481857
stripEnv(a)
18491858
stripHooks(a)
1859+
stripRlimits(a)
18501860
stripLinuxAdjustment(a)
18511861
return a
18521862
}
@@ -1885,6 +1895,12 @@ func stripHooks(a *api.ContainerAdjustment) {
18851895
}
18861896
}
18871897

1898+
func stripRlimits(a *api.ContainerAdjustment) {
1899+
if len(a.Rlimits) == 0 {
1900+
a.Rlimits = nil
1901+
}
1902+
}
1903+
18881904
func stripLinuxAdjustment(a *api.ContainerAdjustment) {
18891905
if a.Linux == nil {
18901906
return

pkg/adaptation/api.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ type (
8181
HugepageLimit = api.HugepageLimit
8282
Hooks = api.Hooks
8383
Hook = api.Hook
84+
POSIXRlimit = api.POSIXRlimit
8485

8586
EventMask = api.EventMask
8687
)

pkg/adaptation/result.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ func collectCreateContainerResult(request *CreateContainerRequest) *result {
5656
if request.Container.Hooks == nil {
5757
request.Container.Hooks = &Hooks{}
5858
}
59+
if request.Container.Rlimits == nil {
60+
request.Container.Rlimits = []*POSIXRlimit{}
61+
}
5962
if request.Container.Linux == nil {
6063
request.Container.Linux = &LinuxContainer{}
6164
}
@@ -85,6 +88,7 @@ func collectCreateContainerResult(request *CreateContainerRequest) *result {
8588
Mounts: []*Mount{},
8689
Env: []*KeyValue{},
8790
Hooks: &Hooks{},
91+
Rlimits: []*POSIXRlimit{},
8892
Linux: &LinuxContainerAdjustment{
8993
Devices: []*LinuxDevice{},
9094
Resources: &LinuxResources{
@@ -210,6 +214,9 @@ func (r *result) adjust(rpl *ContainerAdjustment, plugin string) error {
210214
return err
211215
}
212216
}
217+
if err := r.adjustRlimits(rpl.Rlimits, plugin); err != nil {
218+
return err
219+
}
213220

214221
return nil
215222
}
@@ -659,6 +666,19 @@ func (r *result) adjustCgroupsPath(path, plugin string) error {
659666
return nil
660667
}
661668

669+
func (r *result) adjustRlimits(rlimits []*POSIXRlimit, plugin string) error {
670+
create, id, adjust := r.request.create, r.request.create.Container.Id, r.reply.adjust
671+
for _, l := range rlimits {
672+
if err := r.owners.claimRlimits(id, l.Type, plugin); err != nil {
673+
return err
674+
}
675+
676+
create.Container.Rlimits = append(create.Container.Rlimits, l)
677+
adjust.Rlimits = append(adjust.Rlimits, l)
678+
}
679+
return nil
680+
}
681+
662682
func (r *result) updateResources(reply, u *ContainerUpdate, plugin string) error {
663683
if u.Linux == nil || u.Linux.Resources == nil {
664684
return nil
@@ -873,6 +893,7 @@ type owners struct {
873893
rdtClass string
874894
unified map[string]string
875895
cgroupsPath string
896+
rlimits map[string]string
876897
}
877898

878899
func (ro resultOwners) ownersFor(id string) *owners {
@@ -980,6 +1001,10 @@ func (ro resultOwners) claimCgroupsPath(id, plugin string) error {
9801001
return ro.ownersFor(id).claimCgroupsPath(plugin)
9811002
}
9821003

1004+
func (ro resultOwners) claimRlimits(id, typ, plugin string) error {
1005+
return ro.ownersFor(id).claimRlimit(typ, plugin)
1006+
}
1007+
9831008
func (o *owners) claimAnnotation(key, plugin string) error {
9841009
if o.annotations == nil {
9851010
o.annotations = make(map[string]string)
@@ -1183,6 +1208,17 @@ func (o *owners) claimUnified(key, plugin string) error {
11831208
return nil
11841209
}
11851210

1211+
func (o *owners) claimRlimit(typ, plugin string) error {
1212+
if o.rlimits == nil {
1213+
o.rlimits = make(map[string]string)
1214+
}
1215+
if other, taken := o.rlimits[typ]; taken {
1216+
return conflict(plugin, other, "rlimit", typ)
1217+
}
1218+
o.rlimits[typ] = plugin
1219+
return nil
1220+
}
1221+
11861222
func (o *owners) claimCgroupsPath(plugin string) error {
11871223
if other := o.cgroupsPath; other != "" {
11881224
return conflict(plugin, other, "cgroups path")

pkg/api/adjustment.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,15 @@ func (a *ContainerAdjustment) AddHooks(h *Hooks) {
103103
}
104104
}
105105

106+
func (a *ContainerAdjustment) AddRlimit(typ string, hard, soft uint64) {
107+
a.initRlimits()
108+
a.Rlimits = append(a.Rlimits, &POSIXRlimit{
109+
Type: typ,
110+
Hard: hard,
111+
Soft: soft,
112+
})
113+
}
114+
106115
// AddDevice records the addition of the given device to a container.
107116
func (a *ContainerAdjustment) AddDevice(d *LinuxDevice) {
108117
a.initLinux()
@@ -260,6 +269,12 @@ func (a *ContainerAdjustment) initHooks() {
260269
}
261270
}
262271

272+
func (a *ContainerAdjustment) initRlimits() {
273+
if a.Rlimits == nil {
274+
a.Rlimits = []*POSIXRlimit{}
275+
}
276+
}
277+
263278
func (a *ContainerAdjustment) initLinux() {
264279
if a.Linux == nil {
265280
a.Linux = &LinuxContainerAdjustment{}

pkg/runtime-tools/generate/generate.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,9 @@ func (g *Generator) Adjust(adjust *nri.ContainerAdjustment) error {
119119
if err := g.AdjustMounts(adjust.GetMounts()); err != nil {
120120
return err
121121
}
122+
if err := g.AdjustRlimits(adjust.GetRlimits()); err != nil {
123+
return err
124+
}
122125

123126
return nil
124127
}
@@ -320,6 +323,20 @@ func (g *Generator) AdjustDevices(devices []*nri.LinuxDevice) {
320323
}
321324
}
322325

326+
func (g *Generator) AdjustRlimits(rlimits []*nri.POSIXRlimit) error {
327+
for _, l := range rlimits {
328+
if l == nil {
329+
continue
330+
}
331+
g.Config.Process.Rlimits = append(g.Config.Process.Rlimits, rspec.POSIXRlimit{
332+
Type: l.Type,
333+
Hard: l.Hard,
334+
Soft: l.Soft,
335+
})
336+
}
337+
return nil
338+
}
339+
323340
// AdjustMounts adjusts the mounts in the OCI Spec.
324341
func (g *Generator) AdjustMounts(mounts []*nri.Mount) error {
325342
if len(mounts) == 0 {

pkg/runtime-tools/generate/generate_suite_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,28 @@ var _ = Describe("Adjustment", func() {
6969
})
7070
})
7171

72+
When("has rlimits", func() {
73+
It("adjusts Spec correctly", func() {
74+
var (
75+
spec = makeSpec()
76+
adjust = &api.ContainerAdjustment{
77+
Rlimits: []*api.POSIXRlimit{{
78+
Type: "nofile",
79+
Hard: 456,
80+
Soft: 123,
81+
}},
82+
}
83+
)
84+
85+
rg := &rgen.Generator{Config: spec}
86+
xg := xgen.SpecGenerator(rg)
87+
88+
Expect(xg).ToNot(BeNil())
89+
Expect(xg.Adjust(adjust)).To(Succeed())
90+
Expect(spec).To(Equal(makeSpec(withRlimit("nofile", 456, 123))))
91+
})
92+
})
93+
7294
When("has memory limit", func() {
7395
It("adjusts Spec correctly", func() {
7496
var (
@@ -381,6 +403,19 @@ func withMounts(mounts []rspec.Mount) specOption {
381403
}
382404
}
383405

406+
func withRlimit(typ string, hard, soft uint64) specOption {
407+
return func(spec *rspec.Spec) {
408+
if spec.Process == nil {
409+
return
410+
}
411+
spec.Process.Rlimits = append(spec.Process.Rlimits, rspec.POSIXRlimit{
412+
Type: typ,
413+
Hard: hard,
414+
Soft: soft,
415+
})
416+
}
417+
}
418+
384419
func makeSpec(options ...specOption) *rspec.Spec {
385420
spec := &rspec.Spec{
386421
Process: &rspec.Process{},

0 commit comments

Comments
 (0)