diff --git a/pkg/adaptation/adaptation_suite_test.go b/pkg/adaptation/adaptation_suite_test.go index 9288928e..7e262d95 100644 --- a/pkg/adaptation/adaptation_suite_test.go +++ b/pkg/adaptation/adaptation_suite_test.go @@ -448,7 +448,19 @@ var _ = Describe("Plugin container creation adjustments", func() { adjust := func(subject string, p *mockPlugin, _ *api.PodSandbox, c *api.Container, overwrite bool) (*api.ContainerAdjustment, []*api.ContainerUpdate, error) { plugin := p.idx + "-" + p.name a := &api.ContainerAdjustment{} + if plugin == "00-all-nil-adjust" { + return nil, nil, nil + } switch subject { + case "all-nil-adjustment": + return nil, nil, nil + + case "00-nil-adjustment", "10-nil-adjustment": + if subject[:2] == p.idx { + return nil, nil, nil + } + a.AddAnnotation("non-nil-adjustment", p.idx+"-"+p.name) + case "annotation": if overwrite { a.RemoveAnnotation("key") @@ -619,14 +631,19 @@ var _ = Describe("Plugin container creation adjustments", func() { When("there is a single plugin", func() { BeforeEach(func() { - s.Prepare(&mockRuntime{}, &mockPlugin{idx: "00", name: "test"}) + + s.Prepare( + &mockRuntime{}, + &mockPlugin{idx: "00", name: "all-nil-adjust"}, + &mockPlugin{idx: "00", name: "test"}, + ) }) DescribeTable("should be successfully collected without conflicts", func(subject string, expected *api.ContainerAdjustment) { var ( runtime = s.runtime - plugin = s.plugins[0] + plugins = s.plugins ctx = context.Background() pod = &api.PodSandbox{ @@ -679,7 +696,8 @@ var _ = Describe("Plugin container creation adjustments", func() { return adjust(subject, p, pod, ctr, false) } - plugin.createContainer = create + plugins[0].createContainer = create + plugins[1].createContainer = create s.Startup() @@ -695,6 +713,10 @@ var _ = Describe("Plugin container creation adjustments", func() { protoDiff(reply.Adjust, expected)) }, + Entry("nil adjustment", "all-nil-adjustment", + nil, + ), + Entry("adjust annotations", "annotation", &api.ContainerAdjustment{ Annotations: map[string]string{ @@ -955,6 +977,7 @@ var _ = Describe("Plugin container creation adjustments", func() { BeforeEach(func() { s.Prepare( &mockRuntime{}, + &mockPlugin{idx: "00", name: "all-nil-adjust"}, &mockPlugin{idx: "10", name: "foo"}, &mockPlugin{idx: "00", name: "bar"}, ) @@ -1007,11 +1030,12 @@ var _ = Describe("Plugin container creation adjustments", func() { ) create := func(p *mockPlugin, pod *api.PodSandbox, ctr *api.Container) (*api.ContainerAdjustment, []*api.ContainerUpdate, error) { - return adjust(subject, p, pod, ctr, p == plugins[0] && remove) + return adjust(subject, p, pod, ctr, p == plugins[1] && remove) } plugins[0].createContainer = create plugins[1].createContainer = create + plugins[2].createContainer = create s.Startup() @@ -1031,6 +1055,25 @@ var _ = Describe("Plugin container creation adjustments", func() { } }, + Entry("all nil adjustment", "all-nil-adjustment", false, false, + nil, + ), + + Entry("00 nil adjustment", "00-nil-adjustment", false, false, + &api.ContainerAdjustment{ + Annotations: map[string]string{ + "non-nil-adjustment": "10-foo", + }, + }, + ), + Entry("10 nil adjustment", "10-nil-adjustment", false, false, + &api.ContainerAdjustment{ + Annotations: map[string]string{ + "non-nil-adjustment": "00-bar", + }, + }, + ), + Entry("adjust annotations (conflicts)", "annotation", false, true, nil), Entry("adjust annotations", "annotation", true, false, &api.ContainerAdjustment{ @@ -1232,6 +1275,7 @@ var _ = Describe("Plugin container creation adjustments", func() { ), }, }, + &mockPlugin{idx: "00", name: "all-nil-adjust"}, &mockPlugin{idx: "00", name: "foo"}, &mockPlugin{idx: "10", name: "validator1"}, &mockPlugin{idx: "20", name: "validator2"}, diff --git a/pkg/adaptation/result.go b/pkg/adaptation/result.go index 636359be..707eabc0 100644 --- a/pkg/adaptation/result.go +++ b/pkg/adaptation/result.go @@ -90,27 +90,7 @@ func collectCreateContainerResult(request *CreateContainerRequest) *result { request: resultRequest{ create: request, }, - reply: resultReply{ - adjust: &ContainerAdjustment{ - Annotations: map[string]string{}, - Mounts: []*Mount{}, - Env: []*KeyValue{}, - Hooks: &Hooks{}, - Rlimits: []*POSIXRlimit{}, - CDIDevices: []*CDIDevice{}, - Linux: &LinuxContainerAdjustment{ - Devices: []*LinuxDevice{}, - Resources: &LinuxResources{ - Memory: &LinuxMemory{}, - Cpu: &LinuxCPU{}, - HugepageLimits: []*HugepageLimit{}, - Unified: map[string]string{}, - }, - Namespaces: []*LinuxNamespace{}, - NetDevices: map[string]*LinuxNetDevice{}, - }, - }, - }, + reply: resultReply{}, updates: map[string]*ContainerUpdate{}, owners: api.NewOwningPlugins(), } @@ -278,6 +258,8 @@ func (r *result) adjustAnnotations(annotations map[string]string, plugin string) return nil } + r.initAdjustAnnotations() + create, id := r.request.create, r.request.create.Container.Id del := map[string]struct{}{} for k := range annotations { @@ -313,6 +295,8 @@ func (r *result) adjustMounts(mounts []*Mount, plugin string) error { return nil } + r.initAdjustMounts() + create, id := r.request.create, r.request.create.Container.Id // first split removals from the rest of adjustments @@ -378,6 +362,8 @@ func (r *result) adjustDevices(devices []*LinuxDevice, plugin string) error { return nil } + r.initAdjustDevices() + create, id := r.request.create, r.request.create.Container.Id // first split removals from the rest of adjustments @@ -436,6 +422,8 @@ func (r *result) adjustNamespaces(namespaces []*LinuxNamespace, plugin string) e return nil } + r.initAdjustNamespaces() + create, id := r.request.create, r.request.create.Container.Id creatensmap := map[string]*LinuxNamespace{} @@ -504,6 +492,8 @@ func (r *result) adjustCDIDevices(devices []*CDIDevice, plugin string) error { return nil } + r.initAdjustCDIDevices() + // Notes: // CDI devices are opaque references, typically to vendor specific // devices. They get resolved to actual devices and potential related @@ -534,6 +524,8 @@ func (r *result) adjustEnv(env []*KeyValue, plugin string) error { return nil } + r.initAdjustEnv() + create, id := r.request.create, r.request.create.Container.Id // first split removals from the rest of adjustments @@ -606,6 +598,8 @@ func (r *result) adjustArgs(args []string, plugin string) error { return nil } + r.initAdjustArgs() + create, id := r.request.create, r.request.create.Container.Id if args[0] == "" { @@ -628,6 +622,8 @@ func (r *result) adjustHooks(hooks *Hooks, plugin string) error { return nil } + r.initAdjustHooks() + reply := r.reply.adjust container := r.request.create.Container claim := false @@ -828,6 +824,8 @@ func (r *result) adjustResources(resources *LinuxResources, plugin string) error return nil } + r.initAdjustResources() + create, id := r.request.create, r.request.create.Container.Id container := create.Container.Linux.Resources reply := r.reply.adjust.Linux.Resources @@ -895,6 +893,8 @@ func (r *result) adjustCgroupsPath(path, plugin string) error { return nil } + r.initAdjustCgroupsPath() + create, id := r.request.create, r.request.create.Container.Id if err := r.owners.ClaimCgroupsPath(id, plugin); err != nil { @@ -912,6 +912,8 @@ func (r *result) adjustOomScoreAdj(OomScoreAdj *OptionalInt, plugin string) erro return nil } + r.initAdjustOomScoreAdj() + create, id := r.request.create, r.request.create.Container.Id if err := r.owners.ClaimOomScoreAdj(id, plugin); err != nil { @@ -929,6 +931,8 @@ func (r *result) adjustIOPriority(priority *LinuxIOPriority, plugin string) erro return nil } + r.initAdjustIOPriority() + create, id := r.request.create, r.request.create.Container.Id if err := r.owners.ClaimIOPriority(id, plugin); err != nil { @@ -945,6 +949,9 @@ func (r *result) adjustSeccompPolicy(adjustment *LinuxSeccomp, plugin string) er if adjustment == nil { return nil } + + r.initAdjustLinuxSeccompPolicy() + create, id := r.request.create, r.request.create.Container.Id if err := r.owners.ClaimSeccompPolicy(id, plugin); err != nil { @@ -962,6 +969,8 @@ func (r *result) adjustLinuxScheduler(sch *LinuxScheduler, plugin string) error return nil } + r.initAdjustLinuxScheduler() + create, id := r.request.create, r.request.create.Container.Id if err := r.owners.ClaimLinuxScheduler(id, plugin); err != nil { @@ -975,6 +984,12 @@ func (r *result) adjustLinuxScheduler(sch *LinuxScheduler, plugin string) error } func (r *result) adjustRlimits(rlimits []*POSIXRlimit, plugin string) error { + if len(rlimits) == 0 { + return nil + } + + r.initAdjustRlimits() + create, id, adjust := r.request.create, r.request.create.Container.Id, r.reply.adjust for _, l := range rlimits { if err := r.owners.ClaimRlimit(id, l.Type, plugin); err != nil { @@ -992,6 +1007,8 @@ func (r *result) adjustLinuxNetDevices(devices map[string]*LinuxNetDevice, plugi return nil } + r.initAdjustLinuxNetDevices() + create, id := r.request.create, r.request.create.Container.Id del := map[string]struct{}{} for k := range devices { @@ -1022,6 +1039,99 @@ func (r *result) adjustLinuxNetDevices(devices map[string]*LinuxNetDevice, plugi return nil } +func (r *result) initAdjust() { + if r.reply.adjust == nil { + r.reply.adjust = &ContainerAdjustment{} + } +} + +func (r *result) initAdjustAnnotations() { + r.initAdjust() + if r.reply.adjust.Annotations == nil { + r.reply.adjust.Annotations = map[string]string{} + } +} + +func (r *result) initAdjustMounts() { + r.initAdjust() +} + +func (r *result) initAdjustLinux() { + r.initAdjust() + if r.reply.adjust.Linux == nil { + r.reply.adjust.Linux = &LinuxContainerAdjustment{} + } +} + +func (r *result) initAdjustDevices() { + r.initAdjustLinux() +} + +func (r *result) initAdjustEnv() { + r.initAdjust() +} + +func (r *result) initAdjustArgs() { + r.initAdjust() +} + +func (r *result) initAdjustHooks() { + r.initAdjust() + if r.reply.adjust.Hooks == nil { + r.reply.adjust.Hooks = &Hooks{} + } +} + +func (r *result) initAdjustResources() { + r.initAdjustLinux() + if r.reply.adjust.Linux.Resources == nil { + r.reply.adjust.Linux.Resources = &LinuxResources{ + Memory: &LinuxMemory{}, + Cpu: &LinuxCPU{}, + Unified: map[string]string{}, + } + } +} + +func (r *result) initAdjustCgroupsPath() { + r.initAdjustLinux() +} + +func (r *result) initAdjustOomScoreAdj() { + r.initAdjustLinux() +} + +func (r *result) initAdjustIOPriority() { + r.initAdjustLinux() +} + +func (r *result) initAdjustLinuxSeccompPolicy() { + r.initAdjustLinux() +} + +func (r *result) initAdjustLinuxScheduler() { + r.initAdjustLinux() +} + +func (r *result) initAdjustNamespaces() { + r.initAdjustLinux() +} + +func (r *result) initAdjustRlimits() { + r.initAdjust() +} + +func (r *result) initAdjustLinuxNetDevices() { + r.initAdjustLinux() + if r.reply.adjust.Linux.NetDevices == nil { + r.reply.adjust.Linux.NetDevices = map[string]*LinuxNetDevice{} + } +} + +func (r *result) initAdjustCDIDevices() { + r.initAdjust() +} + func (r *result) updateResources(reply, u *ContainerUpdate, plugin string) error { if u.Linux == nil || u.Linux.Resources == nil { return nil