Skip to content

Commit 5b66cd6

Browse files
chrishenziek8s-infra-cherrypick-robot
authored andcommitted
Move cgroup namespace placement higher in spec builder
Moves cgroup namespace addition logic higher in buildLinuxSpec so it runs before any custom spec adjusters (such as WithMounts). This is necessary because subsequent spec adjusters may want to inspect the set of namespaces to make decisions (e.g., configuring mount options based on whether or not they are shared with the host). Signed-off-by: Chris Henzie <[email protected]>
1 parent 5d7b859 commit 5b66cd6

2 files changed

Lines changed: 54 additions & 8 deletions

File tree

internal/cri/server/container_create.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -792,6 +792,14 @@ func (c *criService) buildLinuxSpec(
792792
}
793793
}()
794794

795+
// cgroupns is used for hiding /sys/fs/cgroup from containers.
796+
// For compatibility, cgroupns is not used when running in cgroup v1 mode or in privileged.
797+
// https://github.com/containers/libpod/issues/4363
798+
// https://github.com/kubernetes/enhancements/blob/0e409b47497e398b369c281074485c8de129694f/keps/sig-node/20191118-cgroups-v2.md#cgroup-namespace
799+
if isUnifiedCgroupsMode() && !securityContext.GetPrivileged() {
800+
specOpts = append(specOpts, oci.WithLinuxNamespace(runtimespec.LinuxNamespace{Type: runtimespec.CgroupNamespace}))
801+
}
802+
795803
var ociSpecOpts oci.SpecOpts
796804
if ociRuntime.CgroupWritable {
797805
ociSpecOpts = customopts.WithMountsCgroupWritable(c.os, config, extraMounts, mountLabel, runtimeHandler)
@@ -930,14 +938,6 @@ func (c *criService) buildLinuxSpec(
930938
annotations.DefaultCRIAnnotations(sandboxID, containerName, imageName, sandboxConfig, false)...,
931939
)
932940

933-
// cgroupns is used for hiding /sys/fs/cgroup from containers.
934-
// For compatibility, cgroupns is not used when running in cgroup v1 mode or in privileged.
935-
// https://github.com/containers/libpod/issues/4363
936-
// https://github.com/kubernetes/enhancements/blob/0e409b47497e398b369c281074485c8de129694f/keps/sig-node/20191118-cgroups-v2.md#cgroup-namespace
937-
if isUnifiedCgroupsMode() && !securityContext.GetPrivileged() {
938-
specOpts = append(specOpts, oci.WithLinuxNamespace(runtimespec.LinuxNamespace{Type: runtimespec.CgroupNamespace}))
939-
}
940-
941941
return specOpts, nil
942942
}
943943

internal/cri/server/container_create_linux_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,52 @@ func TestPrivilegedBindMount(t *testing.T) {
487487
}
488488
}
489489

490+
func TestCgroupNamespace(t *testing.T) {
491+
testPid := uint32(1234)
492+
c := newTestCRIService()
493+
testSandboxID := "sandbox-id"
494+
testContainerName := "container-name"
495+
containerConfig, sandboxConfig, imageConfig, _ := getCreateContainerTestData()
496+
ociRuntime := config.Runtime{}
497+
498+
tests := []struct {
499+
desc string
500+
privileged bool
501+
expectCgroupNamespace bool
502+
}{
503+
{
504+
desc: "non-privileged container should get cgroup namespace",
505+
privileged: false,
506+
expectCgroupNamespace: true,
507+
},
508+
{
509+
desc: "privileged container should not get cgroup namespace",
510+
privileged: true,
511+
expectCgroupNamespace: false,
512+
},
513+
}
514+
515+
for _, tt := range tests {
516+
t.Run(tt.desc, func(t *testing.T) {
517+
containerConfig.Linux.SecurityContext.Privileged = tt.privileged
518+
sandboxConfig.Linux.SecurityContext.Privileged = tt.privileged
519+
520+
spec, err := c.buildContainerSpec(currentPlatform, t.Name(), testSandboxID, testPid, "", testContainerName, testImageName, containerConfig, sandboxConfig, imageConfig, nil, ociRuntime, nil)
521+
assert.NoError(t, err)
522+
523+
hasCgroupNS := false
524+
for _, ns := range spec.Linux.Namespaces {
525+
if ns.Type == runtimespec.CgroupNamespace {
526+
hasCgroupNS = true
527+
break
528+
}
529+
}
530+
531+
assert.Equal(t, tt.expectCgroupNamespace, hasCgroupNS)
532+
})
533+
}
534+
}
535+
490536
func TestMountPropagation(t *testing.T) {
491537

492538
sharedLookupMountFn := func(string) (mount.Info, error) {

0 commit comments

Comments
 (0)