2020-01-16 20:12:52 +00:00
|
|
|
package nomad
|
|
|
|
|
|
|
|
import (
|
2020-09-09 22:30:40 +00:00
|
|
|
"strings"
|
2020-01-16 20:12:52 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
metrics "github.com/armon/go-metrics"
|
|
|
|
log "github.com/hashicorp/go-hclog"
|
|
|
|
memdb "github.com/hashicorp/go-memdb"
|
|
|
|
|
2020-03-23 01:01:36 +00:00
|
|
|
"github.com/hashicorp/nomad/acl"
|
2020-09-09 22:30:40 +00:00
|
|
|
"github.com/hashicorp/nomad/helper"
|
2020-01-16 20:12:52 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/state"
|
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Scaling endpoint is used for listing and retrieving scaling policies
|
|
|
|
type Scaling struct {
|
|
|
|
srv *Server
|
|
|
|
logger log.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListPolicies is used to list the policies
|
2020-09-09 22:30:40 +00:00
|
|
|
func (p *Scaling) ListPolicies(args *structs.ScalingPolicyListRequest, reply *structs.ScalingPolicyListResponse) error {
|
2020-01-16 20:12:52 +00:00
|
|
|
|
2020-09-09 22:30:40 +00:00
|
|
|
if done, err := p.srv.forward("Scaling.ListPolicies", args, args, reply); done {
|
2020-01-16 20:12:52 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer metrics.MeasureSince([]string{"nomad", "scaling", "list_policies"}, time.Now())
|
|
|
|
|
2020-09-09 22:30:40 +00:00
|
|
|
if args.RequestNamespace() == structs.AllNamespacesSentinel {
|
|
|
|
return p.listAllNamespaces(args, reply)
|
|
|
|
}
|
|
|
|
|
|
|
|
if aclObj, err := p.srv.ResolveToken(args.AuthToken); err != nil {
|
2020-03-23 01:01:36 +00:00
|
|
|
return err
|
|
|
|
} else if aclObj != nil {
|
|
|
|
hasListScalingPolicies := aclObj.AllowNsOp(args.RequestNamespace(), acl.NamespaceCapabilityListScalingPolicies)
|
|
|
|
hasListAndReadJobs := aclObj.AllowNsOp(args.RequestNamespace(), acl.NamespaceCapabilityListJobs) &&
|
|
|
|
aclObj.AllowNsOp(args.RequestNamespace(), acl.NamespaceCapabilityReadJob)
|
|
|
|
if !(hasListScalingPolicies || hasListAndReadJobs) {
|
|
|
|
return structs.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
}
|
2020-01-16 20:12:52 +00:00
|
|
|
|
|
|
|
// Setup the blocking query
|
|
|
|
opts := blockingOptions{
|
|
|
|
queryOpts: &args.QueryOptions,
|
|
|
|
queryMeta: &reply.QueryMeta,
|
|
|
|
run: func(ws memdb.WatchSet, state *state.StateStore) error {
|
|
|
|
// Iterate over all the policies
|
2020-09-09 22:30:40 +00:00
|
|
|
var err error
|
|
|
|
var iter memdb.ResultIterator
|
|
|
|
if prefix := args.QueryOptions.Prefix; prefix != "" {
|
|
|
|
iter, err = state.ScalingPoliciesByIDPrefix(ws, args.RequestNamespace(), prefix)
|
|
|
|
} else if job := args.Job; job != "" {
|
|
|
|
iter, err = state.ScalingPoliciesByJob(ws, args.RequestNamespace(), job)
|
|
|
|
} else {
|
|
|
|
iter, err = state.ScalingPoliciesByNamespace(ws, args.Namespace, args.Type)
|
|
|
|
}
|
|
|
|
|
2020-01-16 20:12:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert all the policies to a list stub
|
|
|
|
reply.Policies = nil
|
2020-09-09 22:30:40 +00:00
|
|
|
for raw := iter.Next(); raw != nil; raw = iter.Next() {
|
2020-01-16 20:12:52 +00:00
|
|
|
policy := raw.(*structs.ScalingPolicy)
|
|
|
|
reply.Policies = append(reply.Policies, policy.Stub())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use the last index that affected the policy table
|
|
|
|
index, err := state.Index("scaling_policy")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-09-09 22:30:40 +00:00
|
|
|
// Don't return index zero, otherwise a blocking query cannot be used.
|
2020-01-16 20:12:52 +00:00
|
|
|
if index == 0 {
|
|
|
|
index = 1
|
|
|
|
}
|
|
|
|
reply.Index = index
|
|
|
|
return nil
|
|
|
|
}}
|
2020-09-09 22:30:40 +00:00
|
|
|
return p.srv.blockingRPC(&opts)
|
2020-01-16 20:12:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetPolicy is used to get a specific policy
|
2020-09-09 22:30:40 +00:00
|
|
|
func (p *Scaling) GetPolicy(args *structs.ScalingPolicySpecificRequest,
|
2020-01-16 20:12:52 +00:00
|
|
|
reply *structs.SingleScalingPolicyResponse) error {
|
|
|
|
|
2020-09-09 22:30:40 +00:00
|
|
|
if done, err := p.srv.forward("Scaling.GetPolicy", args, args, reply); done {
|
2020-01-16 20:12:52 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer metrics.MeasureSince([]string{"nomad", "scaling", "get_policy"}, time.Now())
|
|
|
|
|
2020-03-23 01:01:36 +00:00
|
|
|
// Check for list-job permissions
|
2020-09-09 22:30:40 +00:00
|
|
|
if aclObj, err := p.srv.ResolveToken(args.AuthToken); err != nil {
|
2020-03-23 01:01:36 +00:00
|
|
|
return err
|
|
|
|
} else if aclObj != nil {
|
|
|
|
hasReadScalingPolicy := aclObj.AllowNsOp(args.RequestNamespace(), acl.NamespaceCapabilityReadScalingPolicy)
|
|
|
|
hasListAndReadJobs := aclObj.AllowNsOp(args.RequestNamespace(), acl.NamespaceCapabilityListJobs) &&
|
|
|
|
aclObj.AllowNsOp(args.RequestNamespace(), acl.NamespaceCapabilityReadJob)
|
|
|
|
if !(hasReadScalingPolicy || hasListAndReadJobs) {
|
|
|
|
return structs.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
}
|
2020-01-16 20:12:52 +00:00
|
|
|
|
|
|
|
// Setup the blocking query
|
|
|
|
opts := blockingOptions{
|
|
|
|
queryOpts: &args.QueryOptions,
|
|
|
|
queryMeta: &reply.QueryMeta,
|
|
|
|
run: func(ws memdb.WatchSet, state *state.StateStore) error {
|
|
|
|
// Iterate over all the policies
|
|
|
|
p, err := state.ScalingPolicyByID(ws, args.ID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
reply.Policy = p
|
|
|
|
|
|
|
|
// Use the last index that affected the policy table
|
|
|
|
index, err := state.Index("scaling_policy")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure we never set the index to zero, otherwise a blocking query cannot be used.
|
|
|
|
// We floor the index at one, since realistically the first write must have a higher index.
|
|
|
|
if index == 0 {
|
|
|
|
index = 1
|
|
|
|
}
|
|
|
|
reply.Index = index
|
|
|
|
return nil
|
|
|
|
}}
|
2020-09-09 22:30:40 +00:00
|
|
|
return p.srv.blockingRPC(&opts)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (j *Scaling) listAllNamespaces(args *structs.ScalingPolicyListRequest, reply *structs.ScalingPolicyListResponse) error {
|
|
|
|
// Check for list-job permissions
|
|
|
|
aclObj, err := j.srv.ResolveToken(args.AuthToken)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
prefix := args.QueryOptions.Prefix
|
|
|
|
allow := func(ns string) bool {
|
|
|
|
return aclObj.AllowNsOp(ns, acl.NamespaceCapabilityListScalingPolicies) ||
|
|
|
|
(aclObj.AllowNsOp(ns, acl.NamespaceCapabilityListJobs) && aclObj.AllowNsOp(ns, acl.NamespaceCapabilityReadJob))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup the blocking query
|
|
|
|
opts := blockingOptions{
|
|
|
|
queryOpts: &args.QueryOptions,
|
|
|
|
queryMeta: &reply.QueryMeta,
|
|
|
|
run: func(ws memdb.WatchSet, state *state.StateStore) error {
|
|
|
|
// check if user has permission to all namespaces
|
|
|
|
allowedNSes, err := allowedNSes(aclObj, state, allow)
|
|
|
|
if err == structs.ErrPermissionDenied {
|
|
|
|
// return empty if token isn't authorized for any namespace
|
|
|
|
reply.Policies = []*structs.ScalingPolicyListStub{}
|
|
|
|
return nil
|
|
|
|
} else if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Capture all the policies
|
|
|
|
var iter memdb.ResultIterator
|
|
|
|
if args.Type != "" {
|
|
|
|
iter, err = state.ScalingPoliciesByTypePrefix(ws, args.Type)
|
|
|
|
} else {
|
|
|
|
iter, err = state.ScalingPolicies(ws)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var policies []*structs.ScalingPolicyListStub
|
|
|
|
for raw := iter.Next(); raw != nil; raw = iter.Next() {
|
|
|
|
policy := raw.(*structs.ScalingPolicy)
|
|
|
|
if allowedNSes != nil && !allowedNSes[policy.Target[structs.ScalingTargetNamespace]] {
|
|
|
|
// not permitted to this name namespace
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if prefix != "" && !strings.HasPrefix(policy.ID, prefix) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
policies = append(policies, policy.Stub())
|
|
|
|
}
|
|
|
|
reply.Policies = policies
|
|
|
|
|
|
|
|
// Use the last index that affected the policies table or summary
|
|
|
|
index, err := state.Index("scaling_policy")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
reply.Index = helper.Uint64Max(1, index)
|
|
|
|
|
|
|
|
// Set the query response
|
|
|
|
j.srv.setQueryMeta(&reply.QueryMeta)
|
|
|
|
return nil
|
|
|
|
}}
|
|
|
|
return j.srv.blockingRPC(&opts)
|
2020-01-16 20:12:52 +00:00
|
|
|
}
|