Infer systemd cgroup based on path suffix.

Signed-off-by: Lantao Liu <lantaol@google.com>
This commit is contained in:
Lantao Liu 2019-08-06 10:58:27 -07:00
parent f9760af8d4
commit eae5fc360f
6 changed files with 22 additions and 15 deletions

View File

@ -408,8 +408,7 @@ func (c *criService) generateContainerSpec(id string, sandboxID string, sandboxP
} else { } else {
specOpts = append(specOpts, customopts.WithResources(config.GetLinux().GetResources())) specOpts = append(specOpts, customopts.WithResources(config.GetLinux().GetResources()))
if sandboxConfig.GetLinux().GetCgroupParent() != "" { if sandboxConfig.GetLinux().GetCgroupParent() != "" {
cgroupsPath := getCgroupsPath(sandboxConfig.GetLinux().GetCgroupParent(), id, cgroupsPath := getCgroupsPath(sandboxConfig.GetLinux().GetCgroupParent(), id)
c.config.SystemdCgroup)
specOpts = append(specOpts, oci.WithCgroup(cgroupsPath)) specOpts = append(specOpts, oci.WithCgroup(cgroupsPath))
} }
} }

View File

@ -161,7 +161,7 @@ func getCreateContainerTestData() (*runtime.ContainerConfig, *runtime.PodSandbox
assert.Equal(t, spec.Process.NoNewPrivileges, true) assert.Equal(t, spec.Process.NoNewPrivileges, true)
t.Logf("Check cgroup path") t.Logf("Check cgroup path")
assert.Equal(t, getCgroupsPath("/test/cgroup/parent", id, false), spec.Linux.CgroupsPath) assert.Equal(t, getCgroupsPath("/test/cgroup/parent", id), spec.Linux.CgroupsPath)
t.Logf("Check namespaces") t.Logf("Check namespaces")
assert.Contains(t, spec.Linux.Namespaces, runtimespec.LinuxNamespace{ assert.Contains(t, spec.Linux.Namespaces, runtimespec.LinuxNamespace{

View File

@ -142,12 +142,12 @@ func makeContainerName(c *runtime.ContainerMetadata, s *runtime.PodSandboxMetada
} }
// getCgroupsPath generates container cgroups path. // getCgroupsPath generates container cgroups path.
func getCgroupsPath(cgroupsParent, id string, systemdCgroup bool) string { func getCgroupsPath(cgroupsParent, id string) string {
if systemdCgroup { base := path.Base(cgroupsParent)
// Convert a.slice/b.slice/c.slice to c.slice. if strings.HasSuffix(base, ".slice") {
p := path.Base(cgroupsParent) // For a.slice/b.slice/c.slice, base is c.slice.
// runc systemd cgroup path format is "slice:prefix:name". // runc systemd cgroup path format is "slice:prefix:name".
return strings.Join([]string{p, "cri-containerd", id}, ":") return strings.Join([]string{base, "cri-containerd", id}, ":")
} }
return filepath.Join(cgroupsParent, id) return filepath.Join(cgroupsParent, id)
} }

View File

@ -117,22 +117,31 @@ func TestGetCgroupsPath(t *testing.T) {
testID := "test-id" testID := "test-id"
for desc, test := range map[string]struct { for desc, test := range map[string]struct {
cgroupsParent string cgroupsParent string
systemdCgroup bool
expected string expected string
}{ }{
"should support regular cgroup path": { "should support regular cgroup path": {
cgroupsParent: "/a/b", cgroupsParent: "/a/b",
systemdCgroup: false,
expected: "/a/b/test-id", expected: "/a/b/test-id",
}, },
"should support systemd cgroup path": { "should support systemd cgroup path": {
cgroupsParent: "/a.slice/b.slice", cgroupsParent: "/a.slice/b.slice",
systemdCgroup: true,
expected: "b.slice:cri-containerd:test-id", expected: "b.slice:cri-containerd:test-id",
}, },
"should support tailing slash for regular cgroup path": {
cgroupsParent: "/a/b/",
expected: "/a/b/test-id",
},
"should support tailing slash for systemd cgroup path": {
cgroupsParent: "/a.slice/b.slice/",
expected: "b.slice:cri-containerd:test-id",
},
"should treat root cgroup as regular cgroup path": {
cgroupsParent: "/",
expected: "/test-id",
},
} { } {
t.Logf("TestCase %q", desc) t.Logf("TestCase %q", desc)
got := getCgroupsPath(test.cgroupsParent, testID, test.systemdCgroup) got := getCgroupsPath(test.cgroupsParent, testID)
assert.Equal(t, test.expected, got) assert.Equal(t, test.expected, got)
} }
} }

View File

@ -352,8 +352,7 @@ func (c *criService) generateSandboxContainerSpec(id string, config *runtime.Pod
specOpts = append(specOpts, customopts.WithDisabledCgroups) specOpts = append(specOpts, customopts.WithDisabledCgroups)
} else { } else {
if config.GetLinux().GetCgroupParent() != "" { if config.GetLinux().GetCgroupParent() != "" {
cgroupsPath := getCgroupsPath(config.GetLinux().GetCgroupParent(), id, cgroupsPath := getCgroupsPath(config.GetLinux().GetCgroupParent(), id)
c.config.SystemdCgroup)
specOpts = append(specOpts, oci.WithCgroup(cgroupsPath)) specOpts = append(specOpts, oci.WithCgroup(cgroupsPath))
} }
} }

View File

@ -61,7 +61,7 @@ func getRunPodSandboxTestData() (*runtime.PodSandboxConfig, *imagespec.ImageConf
} }
specCheck := func(t *testing.T, id string, spec *runtimespec.Spec) { specCheck := func(t *testing.T, id string, spec *runtimespec.Spec) {
assert.Equal(t, "test-hostname", spec.Hostname) assert.Equal(t, "test-hostname", spec.Hostname)
assert.Equal(t, getCgroupsPath("/test/cgroup/parent", id, false), spec.Linux.CgroupsPath) assert.Equal(t, getCgroupsPath("/test/cgroup/parent", id), spec.Linux.CgroupsPath)
assert.Equal(t, relativeRootfsPath, spec.Root.Path) assert.Equal(t, relativeRootfsPath, spec.Root.Path)
assert.Equal(t, true, spec.Root.Readonly) assert.Equal(t, true, spec.Root.Readonly)
assert.Contains(t, spec.Process.Env, "a=b", "c=d") assert.Contains(t, spec.Process.Env, "a=b", "c=d")