open-nomad/client/fingerprint/cgroup.go

56 lines
1.5 KiB
Go
Raw Normal View History

2016-01-27 06:22:25 +00:00
package fingerprint
import (
"time"
log "github.com/hashicorp/go-hclog"
2016-01-27 06:22:25 +00:00
)
const (
cgroupUnavailable = "unavailable"
interval = 15
2016-01-27 06:22:25 +00:00
)
type CGroupFingerprint struct {
logger log.Logger
2016-01-27 06:22:25 +00:00
lastState string
mountPointDetector MountPointDetector
}
// An interface to isolate calls to the cgroup library
// This facilitates testing where we can implement
2016-01-27 06:36:58 +00:00
// fake mount points to test various code paths
2016-01-27 06:22:25 +00:00
type MountPointDetector interface {
MountPoint() (string, error)
}
2016-01-27 06:36:58 +00:00
// Implements the interface detector which calls the cgroups library directly
2016-01-27 06:22:25 +00:00
type DefaultMountPointDetector struct {
}
// Call out to the default cgroup library
func (b *DefaultMountPointDetector) MountPoint() (string, error) {
return FindCgroupMountpointDir()
}
// NewCGroupFingerprint returns a new cgroup fingerprinter
func NewCGroupFingerprint(logger log.Logger) Fingerprint {
2016-01-27 06:22:25 +00:00
f := &CGroupFingerprint{
logger: logger.Named("cgroup"),
2016-01-27 06:22:25 +00:00
lastState: cgroupUnavailable,
mountPointDetector: &DefaultMountPointDetector{},
}
return f
}
// clearCGroupAttributes clears any node attributes related to cgroups that might
// have been set in a previous fingerprint run.
func (f *CGroupFingerprint) clearCGroupAttributes(r *FingerprintResponse) {
r.RemoveAttribute("unique.cgroup.mountpoint")
}
// Periodic determines the interval at which the periodic fingerprinter will run.
2016-01-27 06:22:25 +00:00
func (f *CGroupFingerprint) Periodic() (bool, time.Duration) {
return true, interval * time.Second
2016-01-27 06:22:25 +00:00
}