2018-02-06 00:16:20 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
metrics "github.com/armon/go-metrics"
|
|
|
|
"github.com/hashicorp/nomad/acl"
|
|
|
|
cstructs "github.com/hashicorp/nomad/client/structs"
|
|
|
|
nstructs "github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Allocations endpoint is used for interacting with client allocations
|
|
|
|
type Allocations struct {
|
|
|
|
c *Client
|
|
|
|
}
|
|
|
|
|
|
|
|
// GarbageCollectAll is used to garbage collect all allocations on a client.
|
2018-02-06 01:20:42 +00:00
|
|
|
func (a *Allocations) GarbageCollectAll(args *nstructs.NodeSpecificRequest, reply *nstructs.GenericResponse) error {
|
2018-02-06 00:16:20 +00:00
|
|
|
defer metrics.MeasureSince([]string{"client", "allocations", "garbage_collect_all"}, time.Now())
|
|
|
|
|
2018-02-13 23:50:51 +00:00
|
|
|
// Check node write permissions
|
2018-02-06 00:16:20 +00:00
|
|
|
if aclObj, err := a.c.ResolveToken(args.AuthToken); err != nil {
|
|
|
|
return err
|
|
|
|
} else if aclObj != nil && !aclObj.AllowNodeWrite() {
|
|
|
|
return nstructs.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
|
|
|
a.c.CollectAllAllocs()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GarbageCollect is used to garbage collect an allocation on a client.
|
|
|
|
func (a *Allocations) GarbageCollect(args *nstructs.AllocSpecificRequest, reply *nstructs.GenericResponse) error {
|
|
|
|
defer metrics.MeasureSince([]string{"client", "allocations", "garbage_collect"}, time.Now())
|
|
|
|
|
2018-02-13 23:50:51 +00:00
|
|
|
// Check submit job permissions
|
2018-02-06 00:16:20 +00:00
|
|
|
if aclObj, err := a.c.ResolveToken(args.AuthToken); err != nil {
|
|
|
|
return err
|
|
|
|
} else if aclObj != nil && !aclObj.AllowNsOp(args.Namespace, acl.NamespaceCapabilitySubmitJob) {
|
|
|
|
return nstructs.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
|
|
|
if !a.c.CollectAllocation(args.AllocID) {
|
|
|
|
// Could not find alloc
|
2018-02-13 23:50:51 +00:00
|
|
|
return nstructs.NewErrUnknownAllocation(args.AllocID)
|
2018-02-06 00:16:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stats is used to collect allocation statistics
|
|
|
|
func (a *Allocations) Stats(args *cstructs.AllocStatsRequest, reply *cstructs.AllocStatsResponse) error {
|
|
|
|
defer metrics.MeasureSince([]string{"client", "allocations", "stats"}, time.Now())
|
|
|
|
|
2018-02-13 23:50:51 +00:00
|
|
|
// Check read job permissions
|
2018-02-06 00:16:20 +00:00
|
|
|
if aclObj, err := a.c.ResolveToken(args.AuthToken); err != nil {
|
|
|
|
return err
|
|
|
|
} else if aclObj != nil && !aclObj.AllowNsOp(args.Namespace, acl.NamespaceCapabilityReadJob) {
|
|
|
|
return nstructs.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
|
|
|
clientStats := a.c.StatsReporter()
|
|
|
|
aStats, err := clientStats.GetAllocStats(args.AllocID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
stats, err := aStats.LatestAllocStats(args.Task)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
reply.Stats = stats
|
|
|
|
return nil
|
|
|
|
}
|