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 create a new cgroup manager
Browse files Browse the repository at this point in the history
Add function to create a new cgroup manager depending on the cgroups path and
if the runtime is running rootless.

Signed-off-by: Julio Montes <[email protected]>
  • Loading branch information
Julio Montes committed Jan 15, 2020
1 parent 8057cd7 commit 8c63c18
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions virtcontainers/cgroups.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ import (
"strings"

"github.com/containerd/cgroups"
"github.com/kata-containers/runtime/pkg/rootless"
libcontcgroups "github.com/opencontainers/runc/libcontainer/cgroups"
libcontcgroupsfs "github.com/opencontainers/runc/libcontainer/cgroups/fs"
libcontcgroupssystemd "github.com/opencontainers/runc/libcontainer/cgroups/systemd"
"github.com/opencontainers/runc/libcontainer/configs"
specconv "github.com/opencontainers/runc/libcontainer/specconv"
specs "github.com/opencontainers/runtime-spec/specs-go"
)

Expand Down Expand Up @@ -193,3 +199,46 @@ func isSystemdCgroup(cgroupPath string) bool {
// it's a correct systemd cgroup path.
return found != nil && cgroupPath[found[0]:found[1]] == cgroupPath
}

func newCgroupManager(cgroups *configs.Cgroup, cgroupPaths map[string]string, spec *specs.Spec) (libcontcgroups.Manager, error) {
var err error

rootless := rootless.IsRootless()
systemdCgroup := isSystemdCgroup(spec.Linux.CgroupsPath)

// Create a new cgroup if the current one is nil
// this cgroups must be saved later
if cgroups == nil {
if cgroups, err = specconv.CreateCgroupConfig(&specconv.CreateOpts{
// cgroup name is taken from spec
CgroupName: "",
UseSystemdCgroup: systemdCgroup,
Spec: spec,
RootlessCgroups: rootless,
}); err != nil {
return nil, fmt.Errorf("Could not create cgroup config: %v", err)
}
}

// Set cgroupPaths to nil when the map is empty, it can and will be
// populated by `Manager.Apply()` when the runtime or any other process
// is moved to the cgroup. See sandbox.setupSandboxCgroup().
if len(cgroupPaths) == 0 {
cgroupPaths = nil
}

if systemdCgroup {
systemdCgroupFunc, err := libcontcgroupssystemd.NewSystemdCgroupsManager()
if err != nil {
return nil, fmt.Errorf("Could not create systemd cgroup manager: %v", err)
}
libcontcgroupssystemd.UseSystemd()
return systemdCgroupFunc(cgroups, cgroupPaths), nil
}

return &libcontcgroupsfs.Manager{
Cgroups: cgroups,
Rootless: rootless,
Paths: cgroupPaths,
}, nil
}

0 comments on commit 8c63c18

Please sign in to comment.