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

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