github.com/hhrutter/nomad@v0.6.0-rc2.0.20170723054333-80c4b03f0705/client/fingerprint/cgroup.go (about)

     1  // +build linux
     2  
     3  package fingerprint
     4  
     5  import (
     6  	"log"
     7  	"time"
     8  
     9  	"github.com/hashicorp/nomad/nomad/structs"
    10  )
    11  
    12  const (
    13  	cgroupAvailable   = "available"
    14  	cgroupUnavailable = "unavailable"
    15  	interval          = 15
    16  )
    17  
    18  type CGroupFingerprint struct {
    19  	logger             *log.Logger
    20  	lastState          string
    21  	mountPointDetector MountPointDetector
    22  }
    23  
    24  // An interface to isolate calls to the cgroup library
    25  // This facilitates testing where we can implement
    26  // fake mount points to test various code paths
    27  type MountPointDetector interface {
    28  	MountPoint() (string, error)
    29  }
    30  
    31  // Implements the interface detector which calls the cgroups library directly
    32  type DefaultMountPointDetector struct {
    33  }
    34  
    35  // Call out to the default cgroup library
    36  func (b *DefaultMountPointDetector) MountPoint() (string, error) {
    37  	return FindCgroupMountpointDir()
    38  }
    39  
    40  // NewCGroupFingerprint returns a new cgroup fingerprinter
    41  func NewCGroupFingerprint(logger *log.Logger) Fingerprint {
    42  	f := &CGroupFingerprint{
    43  		logger:             logger,
    44  		lastState:          cgroupUnavailable,
    45  		mountPointDetector: &DefaultMountPointDetector{},
    46  	}
    47  	return f
    48  }
    49  
    50  // clearCGroupAttributes clears any node attributes related to cgroups that might
    51  // have been set in a previous fingerprint run.
    52  func (f *CGroupFingerprint) clearCGroupAttributes(n *structs.Node) {
    53  	delete(n.Attributes, "unique.cgroup.mountpoint")
    54  }
    55  
    56  // Periodic determines the interval at which the periodic fingerprinter will run.
    57  func (f *CGroupFingerprint) Periodic() (bool, time.Duration) {
    58  	return true, interval * time.Second
    59  }