github.com/anuvu/nomad@v0.8.7-atom1/client/fingerprint/cgroup_linux.go (about)

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