Skip to content
This repository has been archived by the owner on May 12, 2021. It is now read-only.

Commit

Permalink
virtcontainers: add function to identify systemd cgroup path
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
Julio Montes committed Jan 15, 2020
1 parent 4126968 commit 8057cd7
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
11 changes: 11 additions & 0 deletions virtcontainers/cgroups.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"fmt"
"os"
"path/filepath"
"regexp"
"strings"

"github.com/containerd/cgroups"
Expand Down Expand Up @@ -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
}
27 changes: 27 additions & 0 deletions virtcontainers/cgroups_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit 8057cd7

Please sign in to comment.