Skip to content

Commit 362c075

Browse files
committed
adaptation: return nil as no-op adjustment.
Don't create an empty adjustment a priori. Only create once a plugin makes an adjustment to the container being created. In particular, this should result in a nil adjustment if no plugin touches a container. Signed-off-by: Krisztian Litkey <[email protected]>
1 parent edfd91b commit 362c075

File tree

1 file changed

+92
-19
lines changed

1 file changed

+92
-19
lines changed

pkg/adaptation/result.go

Lines changed: 92 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -84,25 +84,7 @@ func collectCreateContainerResult(request *CreateContainerRequest) *result {
8484
request: resultRequest{
8585
create: request,
8686
},
87-
reply: resultReply{
88-
adjust: &ContainerAdjustment{
89-
Annotations: map[string]string{},
90-
Mounts: []*Mount{},
91-
Env: []*KeyValue{},
92-
Hooks: &Hooks{},
93-
Rlimits: []*POSIXRlimit{},
94-
CDIDevices: []*CDIDevice{},
95-
Linux: &LinuxContainerAdjustment{
96-
Devices: []*LinuxDevice{},
97-
Resources: &LinuxResources{
98-
Memory: &LinuxMemory{},
99-
Cpu: &LinuxCPU{},
100-
HugepageLimits: []*HugepageLimit{},
101-
Unified: map[string]string{},
102-
},
103-
},
104-
},
105-
},
87+
reply: resultReply{},
10688
updates: map[string]*ContainerUpdate{},
10789
owners: resultOwners{},
10890
}
@@ -249,6 +231,8 @@ func (r *result) adjustAnnotations(annotations map[string]string, plugin string)
249231
return nil
250232
}
251233

234+
r.initAdjustAnnotations()
235+
252236
create, id := r.request.create, r.request.create.Container.Id
253237
del := map[string]struct{}{}
254238
for k := range annotations {
@@ -284,6 +268,8 @@ func (r *result) adjustMounts(mounts []*Mount, plugin string) error {
284268
return nil
285269
}
286270

271+
r.initAdjustMounts()
272+
287273
create, id := r.request.create, r.request.create.Container.Id
288274

289275
// first split removals from the rest of adjustments
@@ -349,6 +335,8 @@ func (r *result) adjustDevices(devices []*LinuxDevice, plugin string) error {
349335
return nil
350336
}
351337

338+
r.initAdjustDevices()
339+
352340
create, id := r.request.create, r.request.create.Container.Id
353341

354342
// first split removals from the rest of adjustments
@@ -407,6 +395,8 @@ func (r *result) adjustCDIDevices(devices []*CDIDevice, plugin string) error {
407395
return nil
408396
}
409397

398+
r.initAdjustCDIDevices()
399+
410400
// Notes:
411401
// CDI devices are opaque references, typically to vendor specific
412402
// devices. They get resolved to actual devices and potential related
@@ -437,6 +427,8 @@ func (r *result) adjustEnv(env []*KeyValue, plugin string) error {
437427
return nil
438428
}
439429

430+
r.initAdjustEnv()
431+
440432
create, id := r.request.create, r.request.create.Container.Id
441433

442434
// first split removals from the rest of adjustments
@@ -509,6 +501,8 @@ func (r *result) adjustHooks(hooks *Hooks) error {
509501
return nil
510502
}
511503

504+
r.initAdjustHooks()
505+
512506
reply := r.reply.adjust
513507
container := r.request.create.Container
514508

@@ -545,6 +539,8 @@ func (r *result) adjustResources(resources *LinuxResources, plugin string) error
545539
return nil
546540
}
547541

542+
r.initAdjustResources()
543+
548544
create, id := r.request.create, r.request.create.Container.Id
549545
container := create.Container.Linux.Resources
550546
reply := r.reply.adjust.Linux.Resources
@@ -709,6 +705,8 @@ func (r *result) adjustCgroupsPath(path, plugin string) error {
709705
return nil
710706
}
711707

708+
r.initAdjustCgroupsPath()
709+
712710
create, id := r.request.create, r.request.create.Container.Id
713711

714712
if err := r.owners.claimCgroupsPath(id, plugin); err != nil {
@@ -726,6 +724,8 @@ func (r *result) adjustOomScoreAdj(OomScoreAdj *OptionalInt, plugin string) erro
726724
return nil
727725
}
728726

727+
r.initAdjustOomScoreAdj()
728+
729729
create, id := r.request.create, r.request.create.Container.Id
730730

731731
if err := r.owners.claimOomScoreAdj(id, plugin); err != nil {
@@ -739,6 +739,12 @@ func (r *result) adjustOomScoreAdj(OomScoreAdj *OptionalInt, plugin string) erro
739739
}
740740

741741
func (r *result) adjustRlimits(rlimits []*POSIXRlimit, plugin string) error {
742+
if len(rlimits) == 0 {
743+
return nil
744+
}
745+
746+
r.initAdjustRlimits()
747+
742748
create, id, adjust := r.request.create, r.request.create.Container.Id, r.reply.adjust
743749
for _, l := range rlimits {
744750
if err := r.owners.claimRlimits(id, l.Type, plugin); err != nil {
@@ -751,6 +757,73 @@ func (r *result) adjustRlimits(rlimits []*POSIXRlimit, plugin string) error {
751757
return nil
752758
}
753759

760+
func (r *result) initAdjust() {
761+
if r.reply.adjust == nil {
762+
r.reply.adjust = &ContainerAdjustment{}
763+
}
764+
}
765+
766+
func (r *result) initAdjustAnnotations() {
767+
r.initAdjust()
768+
if r.reply.adjust.Annotations == nil {
769+
r.reply.adjust.Annotations = map[string]string{}
770+
}
771+
}
772+
773+
func (r *result) initAdjustMounts() {
774+
r.initAdjust()
775+
}
776+
777+
func (r *result) initAdjustEnv() {
778+
r.initAdjust()
779+
}
780+
781+
func (r *result) initAdjustHooks() {
782+
r.initAdjust()
783+
if r.reply.adjust.Hooks == nil {
784+
r.reply.adjust.Hooks = &Hooks{}
785+
}
786+
}
787+
788+
func (r *result) initAdjustLinux() {
789+
r.initAdjust()
790+
if r.reply.adjust.Linux == nil {
791+
r.reply.adjust.Linux = &LinuxContainerAdjustment{}
792+
}
793+
}
794+
795+
func (r *result) initAdjustDevices() {
796+
r.initAdjustLinux()
797+
}
798+
799+
func (r *result) initAdjustResources() {
800+
r.initAdjustLinux()
801+
if r.reply.adjust.Linux.Resources == nil {
802+
r.reply.adjust.Linux.Resources = &LinuxResources{
803+
Memory: &LinuxMemory{},
804+
Cpu: &LinuxCPU{},
805+
HugepageLimits: []*HugepageLimit{},
806+
Unified: map[string]string{},
807+
}
808+
}
809+
}
810+
811+
func (r *result) initAdjustCgroupsPath() {
812+
r.initAdjustLinux()
813+
}
814+
815+
func (r *result) initAdjustOomScoreAdj() {
816+
r.initAdjustLinux()
817+
}
818+
819+
func (r *result) initAdjustRlimits() {
820+
r.initAdjustLinux()
821+
}
822+
823+
func (r *result) initAdjustCDIDevices() {
824+
r.initAdjust()
825+
}
826+
754827
func (r *result) updateResources(reply, u *ContainerUpdate, plugin string) error {
755828
if u.Linux == nil || u.Linux.Resources == nil {
756829
return nil

0 commit comments

Comments
 (0)