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:
Julio Montes 2019-12-09 16:41:12 +00:00
parent 4126968bf9
commit 8057cd72c3
2 changed files with 38 additions and 0 deletions

View File

@ -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
}

View File

@ -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)
}
}