-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package fingerprint | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"time" | ||
|
||
client "github.com/hashicorp/nomad/client/config" | ||
"github.com/hashicorp/nomad/nomad/structs" | ||
) | ||
|
||
const ( | ||
cgroupAvailable = "available" | ||
cgroupUnavailable = "unavailable" | ||
interval = 15 | ||
) | ||
|
||
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() | ||
} | ||
|
||
// NewCGroupFingerprint returns a new cgroup fingerprinter | ||
func NewCGroupFingerprint(logger *log.Logger) Fingerprint { | ||
f := &CGroupFingerprint{ | ||
logger: logger, | ||
lastState: cgroupUnavailable, | ||
mountPointDetector: &DefaultMountPointDetector{}, | ||
} | ||
return f | ||
} | ||
|
||
// Fingerprint tries to find a valid cgroup moint point | ||
func (f *CGroupFingerprint) Fingerprint(cfg *client.Config, node *structs.Node) (bool, error) { | ||
mount, err := f.mountPointDetector.MountPoint() | ||
if err != nil { | ||
f.clearCGroupAttributes(node) | ||
return false, fmt.Errorf("Failed to discover cgroup mount point: %s", err) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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["unique.cgroup.mountpoint"] = mount | ||
|
||
if f.lastState == cgroupUnavailable { | ||
f.logger.Printf("[INFO] fingerprint.cgroups: cgroups are available") | ||
} | ||
f.lastState = cgroupAvailable | ||
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, "unique.cgroup.mountpoint") | ||
} | ||
|
||
// Periodic determines the interval at which the periodic fingerprinter will run. | ||
func (f *CGroupFingerprint) Periodic() (bool, time.Duration) { | ||
return true, interval * time.Second | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// +build linux | ||
|
||
package fingerprint | ||
|
||
import ( | ||
"github.com/opencontainers/runc/libcontainer/cgroups" | ||
) | ||
|
||
// FindCgroupMountpointDir is used to find the cgroup mount point on a Linux | ||
// system. | ||
func FindCgroupMountpointDir() (string, error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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["unique.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, "unique.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["unique.cgroup.mountpoint"]; ok { | ||
t.Fatalf("unexpected attribute found, %s", a) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// +build !linux | ||
|
||
package fingerprint | ||
|
||
// FindCgroupMountpointDir returns an empty path on non-Linux systems | ||
func FindCgroupMountpointDir() (string, error) { | ||
return "", nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.