runtime: merged ValidCgroupPath method

Merged ValidCgroupPath method to handle cgroupv1 and cgroupv2.

Fixes: #8930

Signed-off-by: yaoyinnan <35447132+yaoyinnan@users.noreply.github.com>
This commit is contained in:
yaoyinnan 2024-01-31 14:37:13 +08:00
parent bf54a02e16
commit feed5c8ff9
2 changed files with 9 additions and 29 deletions

View File

@ -136,7 +136,7 @@ func NewResourceController(path string, resources *specs.LinuxResources) (Resour
var cgroupPath string
if cgroups.Mode() == cgroups.Legacy || cgroups.Mode() == cgroups.Hybrid {
cgroupPath, err = ValidCgroupPathV1(path, IsSystemdCgroup(path))
cgroupPath, err = ValidCgroupPath(path, false, IsSystemdCgroup(path))
if err != nil {
return nil, err
}
@ -145,7 +145,7 @@ func NewResourceController(path string, resources *specs.LinuxResources) (Resour
return nil, err
}
} else if cgroups.Mode() == cgroups.Unified {
cgroupPath, err = ValidCgroupPathV2(path, IsSystemdCgroup(path))
cgroupPath, err = ValidCgroupPath(path, true, IsSystemdCgroup(path))
if err != nil {
return nil, err
}

View File

@ -21,35 +21,15 @@ import (
// DefaultResourceControllerID runtime-determined location in the cgroups hierarchy.
const DefaultResourceControllerID = "/vc"
// ValidCgroupPathV1 returns a valid cgroup path for cgroup v1.
// ValidCgroupPath returns a valid cgroup path.
// see https://github.com/opencontainers/runtime-spec/blob/master/config-linux.md#cgroups-path
func ValidCgroupPathV1(path string, systemdCgroup bool) (string, error) {
func ValidCgroupPath(path string, isCgroupV2 bool, systemdCgroup bool) (string, error) {
if IsSystemdCgroup(path) {
if isCgroupV2 {
return filepath.Join("/", path), nil
} else {
return path, nil
}
if systemdCgroup {
return "", fmt.Errorf("malformed systemd path '%v': expected to be of form 'slice:prefix:name'", path)
}
// In the case of an absolute path (starting with /), the runtime MUST
// take the path to be relative to the cgroups mount point.
if filepath.IsAbs(path) {
return filepath.Clean(path), nil
}
// In the case of a relative path (not starting with /), the runtime MAY
// interpret the path relative to a runtime-determined location in the cgroups hierarchy.
// clean up path and return a new path relative to DefaultResourceControllerID
return filepath.Join(DefaultResourceControllerID, filepath.Clean("/"+path)), nil
}
// ValidCgroupPathV2 returns a valid cgroup path for cgroup v2.
// see https://github.com/opencontainers/runtime-spec/blob/master/config-linux.md#cgroups-path
func ValidCgroupPathV2(path string, systemdCgroup bool) (string, error) {
// In cgroup v2path must be a "clean" absolute path starts with "/".
if IsSystemdCgroup(path) {
return filepath.Join("/", path), nil
}
if systemdCgroup {