github.com/smithx10/nomad@v0.9.1-rc1/client/alloc_endpoint.go (about)

     1  package client
     2  
     3  import (
     4  	"time"
     5  
     6  	metrics "github.com/armon/go-metrics"
     7  	"github.com/hashicorp/nomad/acl"
     8  	cstructs "github.com/hashicorp/nomad/client/structs"
     9  	nstructs "github.com/hashicorp/nomad/nomad/structs"
    10  )
    11  
    12  // Allocations endpoint is used for interacting with client allocations
    13  type Allocations struct {
    14  	c *Client
    15  }
    16  
    17  // GarbageCollectAll is used to garbage collect all allocations on a client.
    18  func (a *Allocations) GarbageCollectAll(args *nstructs.NodeSpecificRequest, reply *nstructs.GenericResponse) error {
    19  	defer metrics.MeasureSince([]string{"client", "allocations", "garbage_collect_all"}, time.Now())
    20  
    21  	// Check node write permissions
    22  	if aclObj, err := a.c.ResolveToken(args.AuthToken); err != nil {
    23  		return err
    24  	} else if aclObj != nil && !aclObj.AllowNodeWrite() {
    25  		return nstructs.ErrPermissionDenied
    26  	}
    27  
    28  	a.c.CollectAllAllocs()
    29  	return nil
    30  }
    31  
    32  // GarbageCollect is used to garbage collect an allocation on a client.
    33  func (a *Allocations) GarbageCollect(args *nstructs.AllocSpecificRequest, reply *nstructs.GenericResponse) error {
    34  	defer metrics.MeasureSince([]string{"client", "allocations", "garbage_collect"}, time.Now())
    35  
    36  	// Check submit job permissions
    37  	if aclObj, err := a.c.ResolveToken(args.AuthToken); err != nil {
    38  		return err
    39  	} else if aclObj != nil && !aclObj.AllowNsOp(args.Namespace, acl.NamespaceCapabilitySubmitJob) {
    40  		return nstructs.ErrPermissionDenied
    41  	}
    42  
    43  	if !a.c.CollectAllocation(args.AllocID) {
    44  		// Could not find alloc
    45  		return nstructs.NewErrUnknownAllocation(args.AllocID)
    46  	}
    47  
    48  	return nil
    49  }
    50  
    51  // Stats is used to collect allocation statistics
    52  func (a *Allocations) Stats(args *cstructs.AllocStatsRequest, reply *cstructs.AllocStatsResponse) error {
    53  	defer metrics.MeasureSince([]string{"client", "allocations", "stats"}, time.Now())
    54  
    55  	// Check read job permissions
    56  	if aclObj, err := a.c.ResolveToken(args.AuthToken); err != nil {
    57  		return err
    58  	} else if aclObj != nil && !aclObj.AllowNsOp(args.Namespace, acl.NamespaceCapabilityReadJob) {
    59  		return nstructs.ErrPermissionDenied
    60  	}
    61  
    62  	clientStats := a.c.StatsReporter()
    63  	aStats, err := clientStats.GetAllocStats(args.AllocID)
    64  	if err != nil {
    65  		return err
    66  	}
    67  
    68  	stats, err := aStats.LatestAllocStats(args.Task)
    69  	if err != nil {
    70  		return err
    71  	}
    72  
    73  	reply.Stats = stats
    74  	return nil
    75  }