diff --git a/client/driver/exec.go b/client/driver/exec.go index 4da62887991..efa846619a8 100644 --- a/client/driver/exec.go +++ b/client/driver/exec.go @@ -46,7 +46,7 @@ func NewExecDriver(ctx *DriverContext) Driver { func (d *ExecDriver) Fingerprint(cfg *config.Config, node *structs.Node) (bool, error) { // Only enable if cgroups are available and we are root - if _, ok := node.Attributes["cgroup.mountpoint"]; !ok { + if _, ok := node.Attributes["unique.cgroup.mountpoint"]; !ok { d.logger.Printf("[DEBUG] driver.exec: cgroups unavailable, disabling") return false, nil } else if syscall.Geteuid() != 0 { diff --git a/client/driver/exec_test.go b/client/driver/exec_test.go index 04d8dca89b1..5b077e9d699 100644 --- a/client/driver/exec_test.go +++ b/client/driver/exec_test.go @@ -23,7 +23,7 @@ func TestExecDriver_Fingerprint(t *testing.T) { d := NewExecDriver(driverCtx) node := &structs.Node{ Attributes: map[string]string{ - "cgroup.mountpoint": "/sys/fs/cgroup", + "unique.cgroup.mountpoint": "/sys/fs/cgroup", }, } apply, err := d.Fingerprint(&config.Config{}, node) diff --git a/client/fingerprint/cgroup.go b/client/fingerprint/cgroup.go index 4137ae641d1..3fb14a2f3b6 100644 --- a/client/fingerprint/cgroup.go +++ b/client/fingerprint/cgroup.go @@ -12,6 +12,7 @@ import ( const ( cgroupAvailable = "available" cgroupUnavailable = "unavailable" + interval = 15 ) type CGroupFingerprint struct { @@ -36,6 +37,7 @@ func (b *DefaultMountPointDetector) MountPoint() (string, error) { return FindCgroupMountpointDir() } +// NewCGroupFingerprint returns a new cgroup fingerprinter func NewCGroupFingerprint(logger *log.Logger) Fingerprint { f := &CGroupFingerprint{ logger: logger, @@ -45,10 +47,11 @@ func NewCGroupFingerprint(logger *log.Logger) Fingerprint { return f } +// Fingerprint tries to find a valid cgroup moint point func (f *CGroupFingerprint) Fingerprint(cfg *client.Config, node *structs.Node) (bool, error) { - // Try to find a valid cgroup mount point mount, err := f.mountPointDetector.MountPoint() if err != nil { + f.clearCGroupAttributes(node) return false, fmt.Errorf("Failed to discover cgroup mount point: %s", err) } @@ -64,7 +67,7 @@ func (f *CGroupFingerprint) Fingerprint(cfg *client.Config, node *structs.Node) return true, nil } - node.Attributes["cgroup.mountpoint"] = mount + node.Attributes["unique.cgroup.mountpoint"] = mount if f.lastState == cgroupUnavailable { f.logger.Printf("[INFO] fingerprint.cgroups: cgroups are available") @@ -73,10 +76,13 @@ func (f *CGroupFingerprint) Fingerprint(cfg *client.Config, node *structs.Node) return true, nil } +// clearCGroupAttributes clears any node attributes related to cgroups that might +// have been set in a previous fingerprint run. func (f *CGroupFingerprint) clearCGroupAttributes(n *structs.Node) { - delete(n.Attributes, "cgroup.mountpoint") + delete(n.Attributes, "unique.cgroup.mountpoint") } +// Periodic determines the interval at which the periodic fingerprinter will run. func (f *CGroupFingerprint) Periodic() (bool, time.Duration) { - return true, 15 * time.Second + return true, interval * time.Second } diff --git a/client/fingerprint/cgroup_linux.go b/client/fingerprint/cgroup_linux.go index 71357b6607b..25171c1f4c3 100644 --- a/client/fingerprint/cgroup_linux.go +++ b/client/fingerprint/cgroup_linux.go @@ -6,6 +6,8 @@ import ( "github.com/opencontainers/runc/libcontainer/cgroups" ) +// FindCgroupMountpointDir is used to find the cgroup mount point on a Linux +// system. func FindCgroupMountpointDir() (string, error) { mount, err := cgroups.FindCgroupMountpointDir() if err != nil { diff --git a/client/fingerprint/cgroup_test.go b/client/fingerprint/cgroup_test.go index d175ae46b9b..be5d251e080 100644 --- a/client/fingerprint/cgroup_test.go +++ b/client/fingerprint/cgroup_test.go @@ -54,7 +54,7 @@ func TestCGroupFingerprint(t *testing.T) { if ok { t.Fatalf("should not apply") } - if a, ok := node.Attributes["cgroup.mountpoint"]; ok { + if a, ok := node.Attributes["unique.cgroup.mountpoint"]; ok { t.Fatalf("unexpected attribute found, %s", a) } @@ -75,7 +75,7 @@ func TestCGroupFingerprint(t *testing.T) { if !ok { t.Fatalf("should apply") } - assertNodeAttributeContains(t, node, "cgroup.mountpoint") + assertNodeAttributeContains(t, node, "unique.cgroup.mountpoint") f = &CGroupFingerprint{ logger: testLogger(), @@ -94,7 +94,7 @@ func TestCGroupFingerprint(t *testing.T) { if !ok { t.Fatalf("should apply") } - if a, ok := node.Attributes["cgroup.mountpoint"]; ok { + if a, ok := node.Attributes["unique.cgroup.mountpoint"]; ok { t.Fatalf("unexpected attribute found, %s", a) } } diff --git a/client/fingerprint/cgroup_universal.go b/client/fingerprint/cgroup_universal.go index c884e91f34d..eeeade435d2 100644 --- a/client/fingerprint/cgroup_universal.go +++ b/client/fingerprint/cgroup_universal.go @@ -2,7 +2,7 @@ package fingerprint -// cgroups only exist on Linux +// FindCgroupMountpointDir returns an empty path on non-Linux systems func FindCgroupMountpointDir() (string, error) { return "", nil } diff --git a/client/fingerprint/fingerprint.go b/client/fingerprint/fingerprint.go index 3737d215e6d..c9195253ba3 100644 --- a/client/fingerprint/fingerprint.go +++ b/client/fingerprint/fingerprint.go @@ -12,7 +12,7 @@ import ( // EmptyDuration is to be used by fingerprinters that are not periodic. const EmptyDuration = time.Duration(0) -// BuiltinFingerprints is a slice containing the key names of all regestered +// BuiltinFingerprints is a slice containing the key names of all registered // fingerprints available, to provided an ordered iteration var BuiltinFingerprints = []string{ "arch", @@ -43,7 +43,6 @@ var builtinFingerprintMap = map[string]Factory{ } // NewFingerprint is used to instantiate and return a new fingerprint - // given the name and a logger func NewFingerprint(name string, logger *log.Logger) (Fingerprint, error) { // Lookup the factory function