Skip to content

Commit

Permalink
Add comments and small improvements to cgroup fingerprinter
Browse files Browse the repository at this point in the history
  • Loading branch information
iverberk committed Jan 29, 2016
1 parent 5266f6d commit 0289252
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 12 deletions.
2 changes: 1 addition & 1 deletion client/driver/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion client/driver/exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
14 changes: 10 additions & 4 deletions client/fingerprint/cgroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
const (
cgroupAvailable = "available"
cgroupUnavailable = "unavailable"
interval = 15
)

type CGroupFingerprint struct {
Expand All @@ -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,
Expand All @@ -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)
}

Expand All @@ -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")
Expand All @@ -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
}
2 changes: 2 additions & 0 deletions client/fingerprint/cgroup_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions client/fingerprint/cgroup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand All @@ -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(),
Expand All @@ -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)
}
}
2 changes: 1 addition & 1 deletion client/fingerprint/cgroup_universal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
3 changes: 1 addition & 2 deletions client/fingerprint/fingerprint.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 0289252

Please sign in to comment.