mirror of
https://github.com/kata-containers/kata-containers.git
synced 2025-06-26 15:32:30 +00:00
virtcontainers: add function to identify systemd cgroup path
Add function to identify if the given cgroup path is a systemd cgroup path. We need to parse the cgroup path to know which cgroup manager we have to use, since some container engines do not use `--systemd-cgroup` runtime option. Signed-off-by: Julio Montes <julio.montes@intel.com>
This commit is contained in:
parent
4126968bf9
commit
8057cd72c3
@ -11,6 +11,7 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/containerd/cgroups"
|
||||
@ -182,3 +183,13 @@ func renameCgroupPath(path string) (string, error) {
|
||||
return filepath.Join(cgroupPathDir, cgroupPathName), nil
|
||||
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
@ -197,3 +197,30 @@ func TestUpdateCgroups(t *testing.T) {
|
||||
err = s.cgroupsDelete()
|
||||
assert.NoError(err)
|
||||
}
|
||||
|
||||
func TestIsSystemdCgroup(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
tests := []struct {
|
||||
path string
|
||||
expected bool
|
||||
}{
|
||||
{"slice:kata:afhts2e5d4g5s", true},
|
||||
{"slice.system:kata:afhts2e5d4g5s", true},
|
||||
{"/kata/afhts2e5d4g5s", false},
|
||||
{"a:b:c:d", false},
|
||||
{":::", false},
|
||||
{"", false},
|
||||
{":", false},
|
||||
{"::", false},
|
||||
{":::", false},
|
||||
{"a:b", false},
|
||||
{"a:b:", false},
|
||||
{":a:b", false},
|
||||
{"@:@:@", false},
|
||||
}
|
||||
|
||||
for _, t := range tests {
|
||||
assert.Equal(t.expected, isSystemdCgroup(t.path), "invalid systemd cgroup path: %v", t.path)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user