Skip to content

Commit

Permalink
Fix build on non-Linux OSes
Browse files Browse the repository at this point in the history
Signed-off-by: Matthew Heon <matthew.heon@gmail.com>

Closes: #1266
Approved by: baude
  • Loading branch information
mheon authored and rh-atomic-bot committed Aug 15, 2018
1 parent 0998808 commit 74cf870
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 58 deletions.
58 changes: 0 additions & 58 deletions libpod/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@ import (
"strings"
"time"

"github.com/containerd/cgroups"
"github.com/containers/image/signature"
"github.com/containers/image/types"
spec "github.com/opencontainers/runtime-spec/specs-go"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)

// Runtime API constants
Expand Down Expand Up @@ -125,62 +123,6 @@ func WaitForFile(path string, timeout time.Duration) error {
}
}

// systemdSliceFromPath makes a new systemd slice under the given parent with
// the given name.
// The parent must be a slice. The name must NOT include ".slice"
func systemdSliceFromPath(parent, name string) (string, error) {
cgroupPath, err := assembleSystemdCgroupName(parent, name)
if err != nil {
return "", err
}

logrus.Debugf("Created cgroup path %s for parent %s and name %s", cgroupPath, parent, name)

if err := makeSystemdCgroup(cgroupPath); err != nil {
return "", errors.Wrapf(err, "error creating cgroup %s", cgroupPath)
}

logrus.Debugf("Created cgroup %s", cgroupPath)

return cgroupPath, nil
}

// makeSystemdCgroup creates a systemd CGroup at the given location.
func makeSystemdCgroup(path string) error {
controller, err := cgroups.NewSystemd(SystemdDefaultCgroupParent)
if err != nil {
return err
}

return controller.Create(path, &spec.LinuxResources{})
}

// deleteSystemdCgroup deletes the systemd cgroup at the given location
func deleteSystemdCgroup(path string) error {
controller, err := cgroups.NewSystemd(SystemdDefaultCgroupParent)
if err != nil {
return err
}

return controller.Delete(path)
}

// assembleSystemdCgroupName creates a systemd cgroup path given a base and
// a new component to add.
// The base MUST be systemd slice (end in .slice)
func assembleSystemdCgroupName(baseSlice, newSlice string) (string, error) {
const sliceSuffix = ".slice"

if !strings.HasSuffix(baseSlice, sliceSuffix) {
return "", errors.Wrapf(ErrInvalidArg, "cannot assemble cgroup path with base %q - must end in .slice", baseSlice)
}

noSlice := strings.TrimSuffix(baseSlice, sliceSuffix)
final := fmt.Sprintf("%s/%s-%s%s", baseSlice, noSlice, newSlice, sliceSuffix)

return final, nil
}

type byDestination []spec.Mount

func (m byDestination) Len() int {
Expand Down
69 changes: 69 additions & 0 deletions libpod/util_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// +build linux

package libpod

import (
"fmt"
"strings"

"github.com/containerd/cgroups"
spec "github.com/opencontainers/runtime-spec/specs-go"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)

// systemdSliceFromPath makes a new systemd slice under the given parent with
// the given name.
// The parent must be a slice. The name must NOT include ".slice"
func systemdSliceFromPath(parent, name string) (string, error) {
cgroupPath, err := assembleSystemdCgroupName(parent, name)
if err != nil {
return "", err
}

logrus.Debugf("Created cgroup path %s for parent %s and name %s", cgroupPath, parent, name)

if err := makeSystemdCgroup(cgroupPath); err != nil {
return "", errors.Wrapf(err, "error creating cgroup %s", cgroupPath)
}

logrus.Debugf("Created cgroup %s", cgroupPath)

return cgroupPath, nil
}

// makeSystemdCgroup creates a systemd CGroup at the given location.
func makeSystemdCgroup(path string) error {
controller, err := cgroups.NewSystemd(SystemdDefaultCgroupParent)
if err != nil {
return err
}

return controller.Create(path, &spec.LinuxResources{})
}

// deleteSystemdCgroup deletes the systemd cgroup at the given location
func deleteSystemdCgroup(path string) error {
controller, err := cgroups.NewSystemd(SystemdDefaultCgroupParent)
if err != nil {
return err
}

return controller.Delete(path)
}

// assembleSystemdCgroupName creates a systemd cgroup path given a base and
// a new component to add.
// The base MUST be systemd slice (end in .slice)
func assembleSystemdCgroupName(baseSlice, newSlice string) (string, error) {
const sliceSuffix = ".slice"

if !strings.HasSuffix(baseSlice, sliceSuffix) {
return "", errors.Wrapf(ErrInvalidArg, "cannot assemble cgroup path with base %q - must end in .slice", baseSlice)
}

noSlice := strings.TrimSuffix(baseSlice, sliceSuffix)
final := fmt.Sprintf("%s/%s-%s%s", baseSlice, noSlice, newSlice, sliceSuffix)

return final, nil
}
23 changes: 23 additions & 0 deletions libpod/util_unsupported.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// +build !linux

package libpod

import (
"github.com/pkg/errors"
)

func systemdSliceFromPath(parent, name string) (string, error) {
return "", errors.Wrapf(ErrOSNotSupported, "cgroups are not supported on non-linux OSes")
}

func makeSystemdCgroup(path string) error {
return errors.Wrapf(ErrOSNotSupported, "cgroups are not supported on non-linux OSes")
}

func deleteSystemdCgroup(path string) error {
return errors.Wrapf(ErrOSNotSupported, "cgroups are not supported on non-linux OSes")
}

func assembleSystemdCgroupName(baseSlice, newSlice string) (string, error) {
return "", errors.Wrapf(ErrOSNotSupported, "cgroups are not supported on non-linux OSes")
}

0 comments on commit 74cf870

Please sign in to comment.