open-nomad/client/fingerprint/cgroup_linux.go

58 lines
1.2 KiB
Go
Raw Normal View History

2016-01-27 06:22:25 +00:00
// +build linux
package fingerprint
import (
"fmt"
2016-01-27 06:22:25 +00:00
"github.com/opencontainers/runc/libcontainer/cgroups"
)
2018-06-01 22:23:10 +00:00
const (
cgroupAvailable = "available"
)
// FindCgroupMountpointDir is used to find the cgroup mount point on a Linux
// system.
2016-01-27 06:22:25 +00:00
func FindCgroupMountpointDir() (string, error) {
2020-08-19 15:57:26 +00:00
mount, err := cgroups.GetCgroupMounts(false)
2016-01-27 06:22:25 +00:00
if err != nil {
2020-08-19 15:57:26 +00:00
return "", err
}
// It's okay if the mount point is not discovered
if len(mount) == 0 {
return "", nil
2016-01-27 06:22:25 +00:00
}
2020-08-19 15:57:26 +00:00
return mount[0].Mountpoint, nil
2016-01-27 06:22:25 +00:00
}
2018-03-11 18:27:18 +00:00
// Fingerprint tries to find a valid cgroup mount point
func (f *CGroupFingerprint) Fingerprint(req *FingerprintRequest, resp *FingerprintResponse) error {
mount, err := f.mountPointDetector.MountPoint()
if err != nil {
f.clearCGroupAttributes(resp)
return fmt.Errorf("Failed to discover cgroup mount point: %s", err)
}
// Check if a cgroup mount point was found
if mount == "" {
f.clearCGroupAttributes(resp)
if f.lastState == cgroupAvailable {
f.logger.Info("cgroups are unavailable")
}
f.lastState = cgroupUnavailable
return nil
}
resp.AddAttribute("unique.cgroup.mountpoint", mount)
2018-01-31 22:03:55 +00:00
resp.Detected = true
if f.lastState == cgroupUnavailable {
f.logger.Info("cgroups are available")
}
f.lastState = cgroupAvailable
return nil
}