Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 48 additions & 4 deletions pkg/adaptation/adaptation_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -594,14 +606,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{
Expand Down Expand Up @@ -635,7 +652,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()

Expand All @@ -651,6 +669,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{
Expand Down Expand Up @@ -875,6 +897,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"},
)
Expand Down Expand Up @@ -908,11 +931,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()

Expand All @@ -932,6 +956,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{
Expand Down Expand Up @@ -1117,6 +1160,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"},
Expand Down
136 changes: 116 additions & 20 deletions pkg/adaptation/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,26 +87,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{},
},
},
},
reply: resultReply{},
updates: map[string]*ContainerUpdate{},
owners: api.NewOwningPlugins(),
}
Expand Down Expand Up @@ -265,6 +246,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 {
Expand Down Expand Up @@ -300,6 +283,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
Expand Down Expand Up @@ -365,6 +350,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
Expand Down Expand Up @@ -423,6 +410,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{}
Expand Down Expand Up @@ -456,6 +445,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
Expand Down Expand Up @@ -486,6 +477,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
Expand Down Expand Up @@ -558,6 +551,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] == "" {
Expand All @@ -580,6 +575,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
Expand Down Expand Up @@ -627,6 +624,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
Expand Down Expand Up @@ -796,6 +795,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 {
Expand All @@ -813,6 +814,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 {
Expand All @@ -830,6 +833,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 {
Expand All @@ -846,6 +851,9 @@ func (r *result) adjustSeccompPolicy(adjustment *LinuxSeccomp, plugin string) er
if adjustment == nil {
return nil
}

r.initAdjustSeccompPolicy()

create, id := r.request.create, r.request.create.Container.Id

if err := r.owners.ClaimSeccompPolicy(id, plugin); err != nil {
Expand All @@ -859,6 +867,12 @@ func (r *result) adjustSeccompPolicy(adjustment *LinuxSeccomp, plugin string) er
}

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 {
Expand All @@ -871,6 +885,88 @@ func (r *result) adjustRlimits(rlimits []*POSIXRlimit, plugin string) error {
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) initAdjustSeccompPolicy() {
r.initAdjustLinux()
}

func (r *result) initAdjustNamespaces() {
r.initAdjustLinux()
}

func (r *result) initAdjustRlimits() {
r.initAdjust()
}

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
Expand Down
Loading