Skip to content

Commit ad002e8

Browse files
committed
Enable adjusting the default memory policy
1 parent 7371298 commit ad002e8

File tree

9 files changed

+744
-304
lines changed

9 files changed

+744
-304
lines changed

go.mod

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ go 1.22.0
55
require (
66
github.com/containerd/ttrpc v1.2.7
77
github.com/knqyf263/go-plugin v0.8.1-0.20240827022226-114c6257e441
8-
github.com/moby/sys/mountinfo v0.6.2
8+
github.com/moby/sys/mountinfo v0.7.2
99
github.com/onsi/ginkgo/v2 v2.19.1
1010
github.com/onsi/gomega v1.34.0
1111
github.com/opencontainers/runtime-spec v1.1.0
@@ -39,4 +39,7 @@ require (
3939
gopkg.in/yaml.v3 v3.0.1 // indirect
4040
)
4141

42-
replace github.com/opencontainers/runtime-tools v0.9.0 => github.com/opencontainers/runtime-tools v0.0.0-20221026201742-946c877fa809
42+
replace (
43+
github.com/opencontainers/runtime-spec => github.com/askervin/runtime-spec v1.0.3-0.20250423073229-57c949588e88
44+
github.com/opencontainers/runtime-tools => github.com/askervin/runtime-tools v0.0.0-20250423115353-73be79a869ac
45+
)

go.sum

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
github.com/askervin/runtime-spec v1.0.3-0.20250423073229-57c949588e88 h1:Rs1b8ssNo+RX0wq0ROF+OGpQ4aks2gmi9906a5tNBZk=
2+
github.com/askervin/runtime-spec v1.0.3-0.20250423073229-57c949588e88/go.mod h1:0ccwhiCQXxLwvWvVsdVdxTe+IFfXyJTjr/wNue5fNJY=
3+
github.com/askervin/runtime-tools v0.0.0-20250423115353-73be79a869ac h1:3bWCwSNBce3auuyOuU5LlntyXZ2yq7DxJBMHox+83zg=
4+
github.com/askervin/runtime-tools v0.0.0-20250423115353-73be79a869ac/go.mod h1:G3Xi3U8V+jH58EREIXFz907r/zI72Mt9bsAJdmPErwc=
15
github.com/blang/semver/v4 v4.0.0 h1:1PFHFE6yCCTv8C1TeyNNarDzntLi7wMI5i/pzqYIsAM=
26
github.com/blang/semver/v4 v4.0.0/go.mod h1:IbckMUScFkM3pff0VJDNKRiT6TG/YpiHIM2yvyW5YoQ=
37
github.com/containerd/log v0.1.0 h1:TCJt7ioM2cr/tfR8GPbGf9/VRAX8D2B4PjzCpfX540I=
@@ -21,7 +25,6 @@ github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
2125
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
2226
github.com/google/pprof v0.0.0-20240424215950-a892ee059fd6 h1:k7nVchz72niMH6YLQNvHSdIE7iqsQxK1P41mySCvssg=
2327
github.com/google/pprof v0.0.0-20240424215950-a892ee059fd6/go.mod h1:kf6iHlnVGwgKolg33glAes7Yg/8iWP8ukqeldJSO7jw=
24-
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
2528
github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/UYA=
2629
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
2730
github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo=
@@ -32,41 +35,30 @@ github.com/knqyf263/go-plugin v0.8.1-0.20240827022226-114c6257e441 h1:Q/sZeuWkXp
3235
github.com/knqyf263/go-plugin v0.8.1-0.20240827022226-114c6257e441/go.mod h1:CvCrNDMiKFlAlLFLmcoEfsTROEfNKbEZAMMrwQnLXCM=
3336
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
3437
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
35-
github.com/mndrix/tap-go v0.0.0-20171203230836-629fa407e90b/go.mod h1:pzzDgJWZ34fGzaAZGFW22KVZDfyrYW+QABMrWnJBnSs=
36-
github.com/moby/sys/mountinfo v0.6.2 h1:BzJjoreD5BMFNmD9Rus6gdd1pLuecOFPt8wC+Vygl78=
37-
github.com/moby/sys/mountinfo v0.6.2/go.mod h1:IJb6JQeOklcdMU9F5xQ8ZALD+CUr5VlGpwtX+VE0rpI=
38-
github.com/mrunalp/fileutils v0.5.0/go.mod h1:M1WthSahJixYnrXQl/DFQuteStB1weuxD2QJNHXfbSQ=
38+
github.com/moby/sys/mountinfo v0.7.2 h1:1shs6aH5s4o5H2zQLn796ADW1wMrIwHsyJ2v9KouLrg=
39+
github.com/moby/sys/mountinfo v0.7.2/go.mod h1:1YOa8w8Ih7uW0wALDUgT1dTTSBrZ+HiBLGws92L2RU4=
3940
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs=
4041
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
4142
github.com/onsi/ginkgo/v2 v2.19.1 h1:QXgq3Z8Crl5EL1WBAC98A5sEBHARrAJNzAmMxzLcRF0=
4243
github.com/onsi/ginkgo/v2 v2.19.1/go.mod h1:O3DtEWQkPa/F7fBMgmZQKKsluAy8pd3rEQdrjkPb9zA=
4344
github.com/onsi/gomega v1.34.0 h1:eSSPsPNp6ZpsG8X1OVmOTxig+CblTc4AxpPBykhe2Os=
4445
github.com/onsi/gomega v1.34.0/go.mod h1:MIKI8c+f+QLWk+hxbePD4i0LMJSExPaZOVfkoex4cAo=
45-
github.com/opencontainers/runtime-spec v1.0.3-0.20220825212826-86290f6a00fb/go.mod h1:jwyrGlmzljRJv/Fgzds9SsS/C5hL+LL3ko9hs6T5lQ0=
46-
github.com/opencontainers/runtime-spec v1.1.0 h1:HHUyrt9mwHUjtasSbXSMvs4cyFxh+Bll4AjJ9odEGpg=
47-
github.com/opencontainers/runtime-spec v1.1.0/go.mod h1:jwyrGlmzljRJv/Fgzds9SsS/C5hL+LL3ko9hs6T5lQ0=
48-
github.com/opencontainers/runtime-tools v0.0.0-20221026201742-946c877fa809 h1:WSwkWIIS4s+E/dPF6HuVZ/hnq1WfXN371eESjREnU8k=
49-
github.com/opencontainers/runtime-tools v0.0.0-20221026201742-946c877fa809/go.mod h1:BRHJJd0E+cx42OybVYSgUvZmU0B8P9gZuRXlZUP7TKI=
5046
github.com/opencontainers/selinux v1.9.1 h1:b4VPEF3O5JLZgdTDBmGepaaIbAo0GqoF6EBRq5f/g3Y=
5147
github.com/opencontainers/selinux v1.9.1/go.mod h1:2i0OySw99QjzBBQByd1Gr9gSjvuho1lHsJxIJ3gGbJI=
5248
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
5349
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
5450
github.com/prometheus/procfs v0.6.0 h1:mxy4L2jP6qMonqmq+aTtOx1ifVWUgG/TAmntgbh3xv4=
5551
github.com/prometheus/procfs v0.6.0/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA=
56-
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
5752
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
5853
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
5954
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
60-
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
61-
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
6255
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
6356
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
6457
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
6558
github.com/syndtr/gocapability v0.0.0-20200815063812-42c35b437635 h1:kdXcSzyDtseVEc4yCz2qF8ZrQvIDBJLl4S1c3GCXmoI=
6659
github.com/syndtr/gocapability v0.0.0-20200815063812-42c35b437635/go.mod h1:hkRG7XYTFWNJGYcbNJQlaLq0fg1yr4J4t/NcTQtrfww=
6760
github.com/tetratelabs/wazero v1.9.0 h1:IcZ56OuxrtaEz8UYNRHBrUa9bYeX9oVY93KspZZBf/I=
6861
github.com/tetratelabs/wazero v1.9.0/go.mod h1:TSbcXCfFP0L2FGkRPxHphadXPjo1T6W+CseNNY7EkjM=
69-
github.com/urfave/cli v1.19.1/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA=
7062
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f h1:J9EGpcZtP0E/raorCMxlFGSTBrsSlaDGf3jU/qvAE2c=
7163
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU=
7264
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 h1:EzJWgHovont7NscjpAxXsDA8S8BMYve8Y5+7cuRE7R0=
@@ -86,18 +78,12 @@ golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLL
8678
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
8779
golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac=
8880
golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM=
89-
<<<<<<< HEAD
90-
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
91-
golang.org/x/sys v0.0.0-20191115151921-52ab43148777/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
92-
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
93-
=======
9481
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
9582
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
9683
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
9784
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
9885
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
9986
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
100-
>>>>>>> 7dec8f9 (DO NOT MERGE: Revert "api: remove NRI/CRI resource conversion functions.")
10187
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
10288
golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws=
10389
golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=

pkg/adaptation/api.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ type (
8080
LinuxResources = api.LinuxResources
8181
LinuxCPU = api.LinuxCPU
8282
LinuxMemory = api.LinuxMemory
83+
LinuxMemoryPolicy = api.LinuxMemoryPolicy
8384
LinuxDevice = api.LinuxDevice
8485
LinuxDeviceCgroup = api.LinuxDeviceCgroup
8586
CDIDevice = api.CDIDevice

pkg/adaptation/result.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ func collectCreateContainerResult(request *CreateContainerRequest) *result {
101101
HugepageLimits: []*HugepageLimit{},
102102
Unified: map[string]string{},
103103
},
104+
MemoryPolicy: &LinuxMemoryPolicy{},
104105
},
105106
},
106107
},
@@ -223,6 +224,9 @@ func (r *result) adjust(rpl *ContainerAdjustment, plugin string) error {
223224
if err := r.adjustOomScoreAdj(rpl.Linux.OomScoreAdj, plugin); err != nil {
224225
return err
225226
}
227+
if err := r.adjustMemoryPolicy(rpl.Linux.MemoryPolicy, plugin); err != nil {
228+
return err
229+
}
226230
}
227231
if err := r.adjustRlimits(rpl.Rlimits, plugin); err != nil {
228232
return err
@@ -764,6 +768,22 @@ func (r *result) adjustOomScoreAdj(OomScoreAdj *OptionalInt, plugin string) erro
764768
return nil
765769
}
766770

771+
func (r *result) adjustMemoryPolicy(memoryPolicy *LinuxMemoryPolicy, plugin string) error {
772+
if memoryPolicy == nil {
773+
return nil
774+
}
775+
776+
id := r.request.create.Container.Id
777+
778+
if err := r.owners.claimMemoryPolicy(id, plugin); err != nil {
779+
return err
780+
}
781+
782+
r.reply.adjust.Linux.MemoryPolicy = memoryPolicy
783+
784+
return nil
785+
}
786+
767787
func (r *result) adjustRlimits(rlimits []*POSIXRlimit, plugin string) error {
768788
create, id, adjust := r.request.create, r.request.create.Container.Id, r.reply.adjust
769789
for _, l := range rlimits {
@@ -1003,6 +1023,7 @@ type owners struct {
10031023
unified map[string]string
10041024
cgroupsPath string
10051025
oomScoreAdj string
1026+
memoryPolicy string
10061027
rlimits map[string]string
10071028
}
10081029

@@ -1127,6 +1148,10 @@ func (ro resultOwners) claimOomScoreAdj(id, plugin string) error {
11271148
return ro.ownersFor(id).claimOomScoreAdj(plugin)
11281149
}
11291150

1151+
func (ro resultOwners) claimMemoryPolicy(id, plugin string) error {
1152+
return ro.ownersFor(id).claimMemoryPolicy(plugin)
1153+
}
1154+
11301155
func (ro resultOwners) claimRlimits(id, typ, plugin string) error {
11311156
return ro.ownersFor(id).claimRlimit(typ, plugin)
11321157
}
@@ -1388,6 +1413,14 @@ func (o *owners) claimOomScoreAdj(plugin string) error {
13881413
return nil
13891414
}
13901415

1416+
func (o *owners) claimMemoryPolicy(plugin string) error {
1417+
if other := o.memoryPolicy; other != "" {
1418+
return conflict(plugin, other, "memory policy")
1419+
}
1420+
o.memoryPolicy = plugin
1421+
return nil
1422+
}
1423+
13911424
func (ro resultOwners) clearAnnotation(id, key string) {
13921425
ro.ownersFor(id).clearAnnotation(key)
13931426
}

pkg/api/adjustment.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,24 @@ func (a *ContainerAdjustment) AddCDIDevice(d *CDIDevice) {
147147
a.CDIDevices = append(a.CDIDevices, d) // TODO: should we dup d here ?
148148
}
149149

150+
// SetLinuxMemoryPolicyMode records setting the memory policy mode for a container.
151+
func (a *ContainerAdjustment) SetLinuxMemoryPolicyMode(mode string) {
152+
a.initLinuxMemoryPolicy()
153+
a.Linux.MemoryPolicy.Mode = mode
154+
}
155+
156+
// SetLinuxMemoryPolicyNodes records setting memory policy nodes for a container.
157+
func (a *ContainerAdjustment) SetLinuxMemoryPolicyNodes(nodes string) {
158+
a.initLinuxMemoryPolicy()
159+
a.Linux.MemoryPolicy.Nodes = nodes
160+
}
161+
162+
// SetLinuxMemoryPolicyFlags records setting memory policy flags for a container.
163+
func (a *ContainerAdjustment) SetLinuxMemoryPolicyFlags(flags []string) {
164+
a.initLinuxMemoryPolicy()
165+
a.Linux.MemoryPolicy.Flags = flags
166+
}
167+
150168
// SetLinuxMemoryLimit records setting the memory limit for a container.
151169
func (a *ContainerAdjustment) SetLinuxMemoryLimit(value int64) {
152170
a.initLinuxResourcesMemory()
@@ -311,6 +329,13 @@ func (a *ContainerAdjustment) initLinux() {
311329
}
312330
}
313331

332+
func (a *ContainerAdjustment) initLinuxMemoryPolicy() {
333+
a.initLinux()
334+
if a.Linux.MemoryPolicy == nil {
335+
a.Linux.MemoryPolicy = &LinuxMemoryPolicy{}
336+
}
337+
}
338+
314339
func (a *ContainerAdjustment) initLinuxResources() {
315340
a.initLinux()
316341
if a.Linux.Resources == nil {

0 commit comments

Comments
 (0)