github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/client/fingerprint/cgroup.go (about)

     1  package fingerprint
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/hashicorp/go-hclog"
     7  	"github.com/hashicorp/nomad/client/lib/cgutil"
     8  )
     9  
    10  const (
    11  	cgroupUnavailable = "unavailable" // "available" is over in cgroup_linux
    12  
    13  	cgroupMountPointAttribute = "unique.cgroup.mountpoint"
    14  	cgroupVersionAttribute    = "unique.cgroup.version"
    15  
    16  	cgroupDetectInterval = 15 * time.Second
    17  )
    18  
    19  type CGroupFingerprint struct {
    20  	logger             hclog.Logger
    21  	lastState          string
    22  	mountPointDetector MountPointDetector
    23  	versionDetector    CgroupVersionDetector
    24  }
    25  
    26  // MountPointDetector isolates calls to the cgroup library.
    27  //
    28  // This facilitates testing where we can implement fake mount points to test
    29  // various code paths.
    30  type MountPointDetector interface {
    31  	// MountPoint returns a cgroup mount-point.
    32  	//
    33  	// In v1, this is one arbitrary subsystem (e.g. /sys/fs/cgroup/cpu).
    34  	//
    35  	// In v2, this is the actual root mount point (i.e. /sys/fs/cgroup).
    36  	MountPoint() (string, error)
    37  }
    38  
    39  // DefaultMountPointDetector implements the interface detector which calls the cgroups
    40  // library directly
    41  type DefaultMountPointDetector struct {
    42  }
    43  
    44  // MountPoint calls out to the default cgroup library.
    45  func (*DefaultMountPointDetector) MountPoint() (string, error) {
    46  	return cgutil.FindCgroupMountpointDir()
    47  }
    48  
    49  // CgroupVersionDetector isolates calls to the cgroup library.
    50  type CgroupVersionDetector interface {
    51  	// CgroupVersion returns v1 or v2 depending on the cgroups version in use.
    52  	CgroupVersion() string
    53  }
    54  
    55  // DefaultCgroupVersionDetector implements the version detector which calls the
    56  // cgroups library directly.
    57  type DefaultCgroupVersionDetector struct {
    58  }
    59  
    60  func (*DefaultCgroupVersionDetector) CgroupVersion() string {
    61  	if cgutil.UseV2 {
    62  		return "v2"
    63  	}
    64  	return "v1"
    65  }
    66  
    67  // NewCGroupFingerprint returns a new cgroup fingerprinter
    68  func NewCGroupFingerprint(logger hclog.Logger) Fingerprint {
    69  	return &CGroupFingerprint{
    70  		logger:             logger.Named("cgroup"),
    71  		lastState:          cgroupUnavailable,
    72  		mountPointDetector: new(DefaultMountPointDetector),
    73  		versionDetector:    new(DefaultCgroupVersionDetector),
    74  	}
    75  }
    76  
    77  // clearCGroupAttributes clears any node attributes related to cgroups that might
    78  // have been set in a previous fingerprint run.
    79  func (f *CGroupFingerprint) clearCGroupAttributes(r *FingerprintResponse) {
    80  	r.RemoveAttribute(cgroupMountPointAttribute)
    81  	r.RemoveAttribute(cgroupVersionAttribute)
    82  }
    83  
    84  // Periodic determines the interval at which the periodic fingerprinter will run.
    85  func (f *CGroupFingerprint) Periodic() (bool, time.Duration) {
    86  	return true, cgroupDetectInterval
    87  }