github.com/bigcommerce/nomad@v0.9.3-bc/client/fingerprint/cgroup_linux.go (about)

     1  // +build linux
     2  
     3  package fingerprint
     4  
     5  import (
     6  	"fmt"
     7  
     8  	"github.com/opencontainers/runc/libcontainer/cgroups"
     9  )
    10  
    11  const (
    12  	cgroupAvailable = "available"
    13  )
    14  
    15  // FindCgroupMountpointDir is used to find the cgroup mount point on a Linux
    16  // system.
    17  func FindCgroupMountpointDir() (string, error) {
    18  	mount, err := cgroups.FindCgroupMountpointDir()
    19  	if err != nil {
    20  		switch e := err.(type) {
    21  		case *cgroups.NotFoundError:
    22  			// It's okay if the mount point is not discovered
    23  			return "", nil
    24  		default:
    25  			// All other errors are passed back as is
    26  			return "", e
    27  		}
    28  	}
    29  	return mount, nil
    30  }
    31  
    32  // Fingerprint tries to find a valid cgroup mount point
    33  func (f *CGroupFingerprint) Fingerprint(req *FingerprintRequest, resp *FingerprintResponse) error {
    34  	mount, err := f.mountPointDetector.MountPoint()
    35  	if err != nil {
    36  		f.clearCGroupAttributes(resp)
    37  		return fmt.Errorf("Failed to discover cgroup mount point: %s", err)
    38  	}
    39  
    40  	// Check if a cgroup mount point was found
    41  	if mount == "" {
    42  
    43  		f.clearCGroupAttributes(resp)
    44  
    45  		if f.lastState == cgroupAvailable {
    46  			f.logger.Info("cgroups are unavailable")
    47  		}
    48  		f.lastState = cgroupUnavailable
    49  		return nil
    50  	}
    51  
    52  	resp.AddAttribute("unique.cgroup.mountpoint", mount)
    53  	resp.Detected = true
    54  
    55  	if f.lastState == cgroupUnavailable {
    56  		f.logger.Info("cgroups are available")
    57  	}
    58  	f.lastState = cgroupAvailable
    59  	return nil
    60  }