Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add periodic cgroup fingerprinter #712

Merged
merged 4 commits into from
Jan 29, 2016
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions client/driver/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@ import (
"fmt"
"log"
"path/filepath"
"runtime"
"syscall"
"time"

"github.com/hashicorp/nomad/client/allocdir"
"github.com/hashicorp/nomad/client/config"
"github.com/hashicorp/nomad/client/driver/executor"
cstructs "github.com/hashicorp/nomad/client/driver/structs"
"github.com/hashicorp/nomad/client/fingerprint"
"github.com/hashicorp/nomad/client/getter"
"github.com/hashicorp/nomad/nomad/structs"
"github.com/mitchellh/mapstructure"
Expand All @@ -23,8 +21,8 @@ import (
// features.
type ExecDriver struct {
DriverContext
fingerprint.StaticFingerprinter
}

type ExecDriverConfig struct {
ArtifactSource string `mapstructure:"artifact_source"`
Checksum string `mapstructure:"checksum"`
Expand All @@ -47,9 +45,9 @@ func NewExecDriver(ctx *DriverContext) Driver {
}

func (d *ExecDriver) Fingerprint(cfg *config.Config, node *structs.Node) (bool, error) {
// Only enable if we are root on linux.
if runtime.GOOS != "linux" {
d.logger.Printf("[DEBUG] driver.exec: only available on linux, disabling")
// Only enable if cgroups are available and we are root
if _, ok := node.Attributes["cgroup.mountpoint"]; !ok {
d.logger.Printf("[DEBUG] driver.exec: cgroups unavailable, disabling")
return false, nil
} else if syscall.Geteuid() != 0 {
d.logger.Printf("[DEBUG] driver.exec: must run as root user, disabling")
Expand All @@ -60,6 +58,10 @@ func (d *ExecDriver) Fingerprint(cfg *config.Config, node *structs.Node) (bool,
return true, nil
}

func (d *ExecDriver) Periodic() (bool, time.Duration) {
return true, 15 * time.Second
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extract 15 in a constant.

}

func (d *ExecDriver) Start(ctx *ExecContext, task *structs.Task) (DriverHandle, error) {
var driverConfig ExecDriverConfig
if err := mapstructure.WeakDecode(task.Config, &driverConfig); err != nil {
Expand Down
4 changes: 3 additions & 1 deletion client/driver/exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ func TestExecDriver_Fingerprint(t *testing.T) {
driverCtx, _ := testDriverContexts(&structs.Task{Name: "foo"})
d := NewExecDriver(driverCtx)
node := &structs.Node{
Attributes: make(map[string]string),
Attributes: map[string]string{
"cgroup.mountpoint": "/sys/fs/cgroup",
},
}
apply, err := d.Fingerprint(&config.Config{}, node)
if err != nil {
Expand Down
82 changes: 82 additions & 0 deletions client/fingerprint/cgroup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package fingerprint

import (
"fmt"
"log"
"time"

client "github.com/hashicorp/nomad/client/config"
"github.com/hashicorp/nomad/nomad/structs"
)

const (
cgroupAvailable = "available"
cgroupUnavailable = "unavailable"
)

type CGroupFingerprint struct {
logger *log.Logger
lastState string
mountPointDetector MountPointDetector
}

// An interface to isolate calls to the cgroup library
// This facilitates testing where we can implement
// fake mount points to test various code paths
type MountPointDetector interface {
MountPoint() (string, error)
}

// Implements the interface detector which calls the cgroups library directly
type DefaultMountPointDetector struct {
}

// Call out to the default cgroup library
func (b *DefaultMountPointDetector) MountPoint() (string, error) {
return FindCgroupMountpointDir()
}

func NewCGroupFingerprint(logger *log.Logger) Fingerprint {
f := &CGroupFingerprint{
logger: logger,
lastState: cgroupUnavailable,
mountPointDetector: &DefaultMountPointDetector{},
}
return f
}

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 {
return false, fmt.Errorf("Failed to discover cgroup mount point: %s", err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clear the attribute in the error case?

}

// Check if a cgroup mount point was found
if mount == "" {
// Clear any attributes from the previous fingerprint.
f.clearCGroupAttributes(node)

if f.lastState == cgroupAvailable {
f.logger.Printf("[INFO] fingerprint.cgroups: cgroups are unavailable")
}
f.lastState = cgroupUnavailable
return true, nil
}

node.Attributes["cgroup.mountpoint"] = mount
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you make the attribute unique.cgroup.mountpoint. The unique. prefixed is used on attributes to exclude them from computed node classes which is an optimization being introduced for 0.3. You can see #708 for more detail


if f.lastState == cgroupUnavailable {
f.logger.Printf("[INFO] fingerprint.cgroups: cgroups are available")
}
f.lastState = cgroupAvailable
return true, nil
}

func (f *CGroupFingerprint) clearCGroupAttributes(n *structs.Node) {
delete(n.Attributes, "cgroup.mountpoint")
}

func (f *CGroupFingerprint) Periodic() (bool, time.Duration) {
return true, 15 * time.Second
}
22 changes: 22 additions & 0 deletions client/fingerprint/cgroup_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// +build linux

package fingerprint

import (
"github.com/opencontainers/runc/libcontainer/cgroups"
)

func FindCgroupMountpointDir() (string, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add comment to all the methods

mount, err := cgroups.FindCgroupMountpointDir()
if err != nil {
switch e := err.(type) {
case *cgroups.NotFoundError:
// It's okay if the mount point is not discovered
return "", nil
default:
// All other errors are passed back as is
return "", e
}
}
return mount, nil
}
100 changes: 100 additions & 0 deletions client/fingerprint/cgroup_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package fingerprint

import (
"fmt"
"testing"

"github.com/hashicorp/nomad/client/config"
"github.com/hashicorp/nomad/nomad/structs"
)

// A fake mount point detector that returns an empty path
type MountPointDetectorNoMountPoint struct{}

func (m *MountPointDetectorNoMountPoint) MountPoint() (string, error) {
return "", nil
}

// A fake mount point detector that returns an error
type MountPointDetectorMountPointFail struct{}

func (m *MountPointDetectorMountPointFail) MountPoint() (string, error) {
return "", fmt.Errorf("cgroup mountpoint discovery failed")
}

// A fake mount point detector that returns a valid path
type MountPointDetectorValidMountPoint struct{}

func (m *MountPointDetectorValidMountPoint) MountPoint() (string, error) {
return "/sys/fs/cgroup", nil
}

// A fake mount point detector that returns an empty path
type MountPointDetectorEmptyMountPoint struct{}

func (m *MountPointDetectorEmptyMountPoint) MountPoint() (string, error) {
return "", nil
}

func TestCGroupFingerprint(t *testing.T) {
f := &CGroupFingerprint{
logger: testLogger(),
lastState: cgroupUnavailable,
mountPointDetector: &MountPointDetectorMountPointFail{},
}

node := &structs.Node{
Attributes: make(map[string]string),
}

ok, err := f.Fingerprint(&config.Config{}, node)
if err == nil {
t.Fatalf("expected an error")
}
if ok {
t.Fatalf("should not apply")
}
if a, ok := node.Attributes["cgroup.mountpoint"]; ok {
t.Fatalf("unexpected attribute found, %s", a)
}

f = &CGroupFingerprint{
logger: testLogger(),
lastState: cgroupUnavailable,
mountPointDetector: &MountPointDetectorValidMountPoint{},
}

node = &structs.Node{
Attributes: make(map[string]string),
}

ok, err = f.Fingerprint(&config.Config{}, node)
if err != nil {
t.Fatalf("unexpected error, %s", err)
}
if !ok {
t.Fatalf("should apply")
}
assertNodeAttributeContains(t, node, "cgroup.mountpoint")

f = &CGroupFingerprint{
logger: testLogger(),
lastState: cgroupUnavailable,
mountPointDetector: &MountPointDetectorEmptyMountPoint{},
}

node = &structs.Node{
Attributes: make(map[string]string),
}

ok, err = f.Fingerprint(&config.Config{}, node)
if err != nil {
t.Fatalf("unexpected error, %s", err)
}
if !ok {
t.Fatalf("should apply")
}
if a, ok := node.Attributes["cgroup.mountpoint"]; ok {
t.Fatalf("unexpected attribute found, %s", a)
}
}
8 changes: 8 additions & 0 deletions client/fingerprint/cgroup_universal.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// +build !linux

package fingerprint

// cgroups only exist on Linux
func FindCgroupMountpointDir() (string, error) {
return "", nil
}
3 changes: 3 additions & 0 deletions client/fingerprint/fingerprint.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const EmptyDuration = time.Duration(0)
// fingerprints available, to provided an ordered iteration
var BuiltinFingerprints = []string{
"arch",
"cgroup",
"consul",
"cpu",
"env_aws",
Expand All @@ -30,6 +31,7 @@ var BuiltinFingerprints = []string{
// which are available, corresponding to a key found in BuiltinFingerprints
var builtinFingerprintMap = map[string]Factory{
"arch": NewArchFingerprint,
"cgroup": NewCGroupFingerprint,
"consul": NewConsulFingerprint,
"cpu": NewCPUFingerprint,
"env_aws": NewEnvAWSFingerprint,
Expand All @@ -41,6 +43,7 @@ var builtinFingerprintMap = map[string]Factory{
}

// NewFingerprint is used to instantiate and return a new fingerprint
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Accidental new line?


// given the name and a logger
func NewFingerprint(name string, logger *log.Logger) (Fingerprint, error) {
// Lookup the factory function
Expand Down