This repository has been archived by the owner on May 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 373
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
virtcontainers: add new package for cgroups
virtcontainers/pkg/cgroups contains functions and structures needed to deal with cgroups and virtual containers Signed-off-by: Julio Montes <[email protected]>
- Loading branch information
Julio Montes
committed
Feb 21, 2020
1 parent
d54723a
commit 03cdf6c
Showing
7 changed files
with
179 additions
and
201 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// Copyright (c) 2020 Intel Corporation | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
package cgroups | ||
|
||
import ( | ||
"fmt" | ||
"path/filepath" | ||
"regexp" | ||
) | ||
|
||
// prepend a kata specific string to oci cgroup path to | ||
// form a different cgroup path, thus cAdvisor couldn't | ||
// find kata containers cgroup path on host to prevent it | ||
// from grabbing the stats data. | ||
const CgroupKataPrefix = "kata" | ||
|
||
// DefaultCgroupPath runtime-determined location in the cgroups hierarchy. | ||
const DefaultCgroupPath = "/vc" | ||
|
||
func RenameCgroupPath(path string) (string, error) { | ||
if path == "" { | ||
return "", fmt.Errorf("Cgroup path is empty") | ||
} | ||
|
||
cgroupPathDir := filepath.Dir(path) | ||
cgroupPathName := fmt.Sprintf("%s_%s", CgroupKataPrefix, filepath.Base(path)) | ||
return filepath.Join(cgroupPathDir, cgroupPathName), nil | ||
|
||
} | ||
|
||
// validCgroupPath returns a valid cgroup path. | ||
// see https://github.com/opencontainers/runtime-spec/blob/master/config-linux.md#cgroups-path | ||
func ValidCgroupPath(path string, systemdCgroup bool) (string, error) { | ||
if IsSystemdCgroup(path) { | ||
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 RenameCgroupPath(filepath.Clean(path)) | ||
} | ||
|
||
// 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 DefaultCgroupPath | ||
return RenameCgroupPath(filepath.Join(DefaultCgroupPath, filepath.Clean("/"+path))) | ||
} | ||
|
||
func IsSystemdCgroup(cgroupPath string) bool { | ||
// systemd cgroup path: slice:prefix:name | ||
re := regexp.MustCompile(`([[:alnum:]]|\.)+:([[:alnum:]]|\.)+:([[:alnum:]]|\.)+`) | ||
found := re.FindStringIndex(cgroupPath) | ||
|
||
// if found string is equal to cgroupPath then | ||
// it's a correct systemd cgroup path. | ||
return found != nil && cgroupPath[found[0]:found[1]] == cgroupPath | ||
} |
Oops, something went wrong.