2017-06-26 21:23:52 +00:00
|
|
|
package nomad
|
|
|
|
|
|
|
|
import (
|
2017-06-29 05:00:18 +00:00
|
|
|
"fmt"
|
2022-02-16 16:40:30 +00:00
|
|
|
"net/http"
|
2017-06-26 21:23:52 +00:00
|
|
|
"time"
|
|
|
|
|
2022-12-01 15:05:15 +00:00
|
|
|
"github.com/armon/go-metrics"
|
|
|
|
"github.com/hashicorp/go-hclog"
|
|
|
|
"github.com/hashicorp/go-memdb"
|
|
|
|
|
2017-09-11 23:31:37 +00:00
|
|
|
"github.com/hashicorp/nomad/acl"
|
2017-06-26 21:23:52 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/state"
|
2022-03-09 01:54:17 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/state/paginator"
|
2017-06-26 21:23:52 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Deployment endpoint is used for manipulating deployments
|
|
|
|
type Deployment struct {
|
2018-09-15 23:23:13 +00:00
|
|
|
srv *Server
|
2022-12-01 15:05:15 +00:00
|
|
|
ctx *RPCContext
|
|
|
|
logger hclog.Logger
|
|
|
|
}
|
2022-02-02 20:03:18 +00:00
|
|
|
|
2022-12-01 15:05:15 +00:00
|
|
|
func NewDeploymentEndpoint(srv *Server, ctx *RPCContext) *Deployment {
|
|
|
|
return &Deployment{srv: srv, ctx: ctx, logger: srv.logger.Named("deployment")}
|
2017-06-26 21:23:52 +00:00
|
|
|
}
|
|
|
|
|
2017-06-29 05:00:18 +00:00
|
|
|
// GetDeployment is used to request information about a specific deployment
|
|
|
|
func (d *Deployment) GetDeployment(args *structs.DeploymentSpecificRequest,
|
|
|
|
reply *structs.SingleDeploymentResponse) error {
|
2023-01-24 15:52:07 +00:00
|
|
|
|
|
|
|
authErr := d.srv.Authenticate(d.ctx, args)
|
2017-06-29 05:00:18 +00:00
|
|
|
if done, err := d.srv.forward("Deployment.GetDeployment", args, args, reply); done {
|
|
|
|
return err
|
|
|
|
}
|
2023-01-25 21:37:24 +00:00
|
|
|
d.srv.MeasureRPCRate("deployment", structs.RateMetricRead, args)
|
2023-01-24 15:52:07 +00:00
|
|
|
if authErr != nil {
|
|
|
|
return structs.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
2017-06-29 05:00:18 +00:00
|
|
|
defer metrics.MeasureSince([]string{"nomad", "deployment", "get_deployment"}, time.Now())
|
|
|
|
|
2017-09-11 23:31:37 +00:00
|
|
|
// Check namespace read-job permissions
|
2019-10-01 20:06:24 +00:00
|
|
|
allowNsOp := acl.NamespaceValidator(acl.NamespaceCapabilityReadJob)
|
2023-01-25 19:33:06 +00:00
|
|
|
aclObj, err := d.srv.ResolveACL(args)
|
2019-10-01 20:06:24 +00:00
|
|
|
if err != nil {
|
2017-09-11 23:31:37 +00:00
|
|
|
return err
|
2019-10-01 20:06:24 +00:00
|
|
|
} else if !allowNsOp(aclObj, args.RequestNamespace()) {
|
2017-09-11 23:31:37 +00:00
|
|
|
return structs.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
2017-06-29 05:00:18 +00:00
|
|
|
// Setup the blocking query
|
|
|
|
opts := blockingOptions{
|
|
|
|
queryOpts: &args.QueryOptions,
|
|
|
|
queryMeta: &reply.QueryMeta,
|
|
|
|
run: func(ws memdb.WatchSet, state *state.StateStore) error {
|
|
|
|
// Verify the arguments
|
|
|
|
if args.DeploymentID == "" {
|
|
|
|
return fmt.Errorf("missing deployment ID")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Look for the deployment
|
|
|
|
out, err := state.DeploymentByID(ws, args.DeploymentID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-09-08 19:33:55 +00:00
|
|
|
// Re-check namespace in case it differs from request.
|
|
|
|
if out != nil && !allowNsOp(aclObj, out.Namespace) {
|
2020-09-08 21:39:51 +00:00
|
|
|
// hide this deployment, caller is not authorized to view it
|
2020-09-08 19:33:55 +00:00
|
|
|
out = nil
|
|
|
|
}
|
|
|
|
|
2017-06-29 05:00:18 +00:00
|
|
|
// Setup the output
|
|
|
|
reply.Deployment = out
|
|
|
|
if out != nil {
|
|
|
|
reply.Index = out.ModifyIndex
|
|
|
|
} else {
|
|
|
|
// Use the last index that affected the deployments table
|
|
|
|
index, err := state.Index("deployment")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
reply.Index = index
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the query response
|
|
|
|
d.srv.setQueryMeta(&reply.QueryMeta)
|
|
|
|
return nil
|
|
|
|
}}
|
|
|
|
return d.srv.blockingRPC(&opts)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fail is used to force fail a deployment
|
|
|
|
func (d *Deployment) Fail(args *structs.DeploymentFailRequest, reply *structs.DeploymentUpdateResponse) error {
|
2023-01-25 19:33:06 +00:00
|
|
|
|
|
|
|
authErr := d.srv.Authenticate(d.ctx, args)
|
2017-06-29 05:00:18 +00:00
|
|
|
if done, err := d.srv.forward("Deployment.Fail", args, args, reply); done {
|
|
|
|
return err
|
|
|
|
}
|
2023-01-25 21:37:24 +00:00
|
|
|
d.srv.MeasureRPCRate("deployment", structs.RateMetricWrite, args)
|
2023-01-25 19:33:06 +00:00
|
|
|
if authErr != nil {
|
|
|
|
return structs.ErrPermissionDenied
|
|
|
|
}
|
2017-06-29 05:00:18 +00:00
|
|
|
defer metrics.MeasureSince([]string{"nomad", "deployment", "fail"}, time.Now())
|
|
|
|
|
|
|
|
// Validate the arguments
|
|
|
|
if args.DeploymentID == "" {
|
|
|
|
return fmt.Errorf("missing deployment ID")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lookup the deployment
|
|
|
|
snap, err := d.srv.fsm.State().Snapshot()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ws := memdb.NewWatchSet()
|
|
|
|
deploy, err := snap.DeploymentByID(ws, args.DeploymentID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if deploy == nil {
|
|
|
|
return fmt.Errorf("deployment not found")
|
|
|
|
}
|
|
|
|
|
2019-10-01 20:06:24 +00:00
|
|
|
// Check namespace submit-job permissions
|
2023-01-25 19:33:06 +00:00
|
|
|
if aclObj, err := d.srv.ResolveACL(args); err != nil {
|
2019-10-01 20:06:24 +00:00
|
|
|
return err
|
|
|
|
} else if aclObj != nil && !aclObj.AllowNsOp(deploy.Namespace, acl.NamespaceCapabilitySubmitJob) {
|
|
|
|
return structs.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
2017-06-29 05:00:18 +00:00
|
|
|
if !deploy.Active() {
|
2020-06-16 12:10:41 +00:00
|
|
|
return structs.ErrDeploymentTerminalNoFail
|
2017-06-29 05:00:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Call into the deployment watcher
|
|
|
|
return d.srv.deploymentWatcher.FailDeployment(args, reply)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pause is used to pause a deployment
|
|
|
|
func (d *Deployment) Pause(args *structs.DeploymentPauseRequest, reply *structs.DeploymentUpdateResponse) error {
|
2023-01-25 19:33:06 +00:00
|
|
|
authErr := d.srv.Authenticate(d.ctx, args)
|
2017-06-29 05:00:18 +00:00
|
|
|
if done, err := d.srv.forward("Deployment.Pause", args, args, reply); done {
|
|
|
|
return err
|
|
|
|
}
|
2023-01-25 21:37:24 +00:00
|
|
|
d.srv.MeasureRPCRate("deployment", structs.RateMetricWrite, args)
|
2023-01-25 19:33:06 +00:00
|
|
|
if authErr != nil {
|
|
|
|
return structs.ErrPermissionDenied
|
|
|
|
}
|
2017-06-29 05:00:18 +00:00
|
|
|
defer metrics.MeasureSince([]string{"nomad", "deployment", "pause"}, time.Now())
|
|
|
|
|
|
|
|
// Validate the arguments
|
|
|
|
if args.DeploymentID == "" {
|
|
|
|
return fmt.Errorf("missing deployment ID")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lookup the deployment
|
|
|
|
snap, err := d.srv.fsm.State().Snapshot()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ws := memdb.NewWatchSet()
|
|
|
|
deploy, err := snap.DeploymentByID(ws, args.DeploymentID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if deploy == nil {
|
|
|
|
return fmt.Errorf("deployment not found")
|
|
|
|
}
|
|
|
|
|
2019-10-01 20:06:24 +00:00
|
|
|
// Check namespace submit-job permissions
|
2023-01-25 19:33:06 +00:00
|
|
|
if aclObj, err := d.srv.ResolveACL(args); err != nil {
|
2019-10-01 20:06:24 +00:00
|
|
|
return err
|
|
|
|
} else if aclObj != nil && !aclObj.AllowNsOp(deploy.Namespace, acl.NamespaceCapabilitySubmitJob) {
|
|
|
|
return structs.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
2017-06-29 05:00:18 +00:00
|
|
|
if !deploy.Active() {
|
2017-07-19 20:34:24 +00:00
|
|
|
if args.Pause {
|
2020-06-16 12:10:41 +00:00
|
|
|
return structs.ErrDeploymentTerminalNoPause
|
2017-07-19 20:34:24 +00:00
|
|
|
}
|
|
|
|
|
2020-06-16 12:10:41 +00:00
|
|
|
return structs.ErrDeploymentTerminalNoResume
|
2017-06-29 05:00:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Call into the deployment watcher
|
|
|
|
return d.srv.deploymentWatcher.PauseDeployment(args, reply)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Promote is used to promote canaries in a deployment
|
|
|
|
func (d *Deployment) Promote(args *structs.DeploymentPromoteRequest, reply *structs.DeploymentUpdateResponse) error {
|
2023-01-25 19:33:06 +00:00
|
|
|
authErr := d.srv.Authenticate(d.ctx, args)
|
2017-06-29 05:00:18 +00:00
|
|
|
if done, err := d.srv.forward("Deployment.Promote", args, args, reply); done {
|
|
|
|
return err
|
|
|
|
}
|
2023-01-25 21:37:24 +00:00
|
|
|
d.srv.MeasureRPCRate("deployment", structs.RateMetricWrite, args)
|
2023-01-25 19:33:06 +00:00
|
|
|
if authErr != nil {
|
|
|
|
return structs.ErrPermissionDenied
|
|
|
|
}
|
2017-06-29 05:00:18 +00:00
|
|
|
defer metrics.MeasureSince([]string{"nomad", "deployment", "promote"}, time.Now())
|
|
|
|
|
|
|
|
// Validate the arguments
|
|
|
|
if args.DeploymentID == "" {
|
|
|
|
return fmt.Errorf("missing deployment ID")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lookup the deployment
|
|
|
|
snap, err := d.srv.fsm.State().Snapshot()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ws := memdb.NewWatchSet()
|
|
|
|
deploy, err := snap.DeploymentByID(ws, args.DeploymentID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if deploy == nil {
|
|
|
|
return fmt.Errorf("deployment not found")
|
|
|
|
}
|
|
|
|
|
2019-10-01 20:06:24 +00:00
|
|
|
// Check namespace submit-job permissions
|
2023-01-25 19:33:06 +00:00
|
|
|
if aclObj, err := d.srv.ResolveACL(args); err != nil {
|
2019-10-01 20:06:24 +00:00
|
|
|
return err
|
|
|
|
} else if aclObj != nil && !aclObj.AllowNsOp(deploy.Namespace, acl.NamespaceCapabilitySubmitJob) {
|
|
|
|
return structs.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
2017-06-29 05:00:18 +00:00
|
|
|
if !deploy.Active() {
|
2020-06-16 12:10:41 +00:00
|
|
|
return structs.ErrDeploymentTerminalNoPromote
|
2017-06-29 05:00:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Call into the deployment watcher
|
|
|
|
return d.srv.deploymentWatcher.PromoteDeployment(args, reply)
|
|
|
|
}
|
|
|
|
|
2020-06-17 14:25:37 +00:00
|
|
|
// Run is used to start a pending deployment
|
|
|
|
func (d *Deployment) Run(args *structs.DeploymentRunRequest, reply *structs.DeploymentUpdateResponse) error {
|
2023-01-25 19:33:06 +00:00
|
|
|
authErr := d.srv.Authenticate(d.ctx, args)
|
2020-06-17 14:25:37 +00:00
|
|
|
if done, err := d.srv.forward("Deployment.Run", args, args, reply); done {
|
|
|
|
return err
|
|
|
|
}
|
2023-01-25 21:37:24 +00:00
|
|
|
d.srv.MeasureRPCRate("deployment", structs.RateMetricWrite, args)
|
2023-01-25 19:33:06 +00:00
|
|
|
if authErr != nil {
|
|
|
|
return structs.ErrPermissionDenied
|
|
|
|
}
|
2020-06-17 14:25:37 +00:00
|
|
|
defer metrics.MeasureSince([]string{"nomad", "deployment", "run"}, time.Now())
|
|
|
|
|
|
|
|
// Validate the arguments
|
|
|
|
if args.DeploymentID == "" {
|
|
|
|
return fmt.Errorf("missing deployment ID")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lookup the deployment
|
|
|
|
snap, err := d.srv.fsm.State().Snapshot()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ws := memdb.NewWatchSet()
|
|
|
|
deploy, err := snap.DeploymentByID(ws, args.DeploymentID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if deploy == nil {
|
|
|
|
return fmt.Errorf("deployment not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check namespace submit-job permissions
|
2023-01-25 19:33:06 +00:00
|
|
|
if aclObj, err := d.srv.ResolveACL(args); err != nil {
|
2020-06-17 14:25:37 +00:00
|
|
|
return err
|
|
|
|
} else if aclObj != nil && !aclObj.AllowNsOp(deploy.Namespace, acl.NamespaceCapabilitySubmitJob) {
|
|
|
|
return structs.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
|
|
|
if !deploy.Active() {
|
|
|
|
return structs.ErrDeploymentTerminalNoRun
|
|
|
|
}
|
|
|
|
|
|
|
|
// Call into the deployment watcher
|
|
|
|
return d.srv.deploymentWatcher.RunDeployment(args, reply)
|
|
|
|
}
|
|
|
|
|
2020-06-17 15:02:26 +00:00
|
|
|
// Unblock is used to unblock a deployment
|
|
|
|
func (d *Deployment) Unblock(args *structs.DeploymentUnblockRequest, reply *structs.DeploymentUpdateResponse) error {
|
2023-01-25 19:33:06 +00:00
|
|
|
authErr := d.srv.Authenticate(d.ctx, args)
|
2020-06-17 15:02:26 +00:00
|
|
|
if done, err := d.srv.forward("Deployment.Unblock", args, args, reply); done {
|
|
|
|
return err
|
|
|
|
}
|
2023-01-25 21:37:24 +00:00
|
|
|
d.srv.MeasureRPCRate("deployment", structs.RateMetricWrite, args)
|
2023-01-25 19:33:06 +00:00
|
|
|
if authErr != nil {
|
|
|
|
return structs.ErrPermissionDenied
|
|
|
|
}
|
2020-06-17 15:02:26 +00:00
|
|
|
defer metrics.MeasureSince([]string{"nomad", "deployment", "unblock"}, time.Now())
|
|
|
|
|
|
|
|
// Validate the arguments
|
|
|
|
if args.DeploymentID == "" {
|
|
|
|
return fmt.Errorf("missing deployment ID")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lookup the deployment
|
|
|
|
snap, err := d.srv.fsm.State().Snapshot()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ws := memdb.NewWatchSet()
|
|
|
|
deploy, err := snap.DeploymentByID(ws, args.DeploymentID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if deploy == nil {
|
|
|
|
return fmt.Errorf("deployment not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check namespace submit-job permissions
|
2023-01-25 19:33:06 +00:00
|
|
|
if aclObj, err := d.srv.ResolveACL(args); err != nil {
|
2020-06-17 15:02:26 +00:00
|
|
|
return err
|
|
|
|
} else if aclObj != nil && !aclObj.AllowNsOp(deploy.Namespace, acl.NamespaceCapabilitySubmitJob) {
|
|
|
|
return structs.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
|
|
|
if !deploy.Active() {
|
2020-06-17 14:25:37 +00:00
|
|
|
return structs.ErrDeploymentTerminalNoUnblock
|
2020-06-17 15:02:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Call into the deployment watcher
|
|
|
|
return d.srv.deploymentWatcher.UnblockDeployment(args, reply)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cancel is used to cancel a deployment
|
|
|
|
func (d *Deployment) Cancel(args *structs.DeploymentCancelRequest, reply *structs.DeploymentUpdateResponse) error {
|
2023-01-25 19:33:06 +00:00
|
|
|
authErr := d.srv.Authenticate(d.ctx, args)
|
2020-06-17 15:02:26 +00:00
|
|
|
if done, err := d.srv.forward("Deployment.Cancel", args, args, reply); done {
|
|
|
|
return err
|
|
|
|
}
|
2023-01-25 21:37:24 +00:00
|
|
|
d.srv.MeasureRPCRate("deployment", structs.RateMetricWrite, args)
|
2023-01-25 19:33:06 +00:00
|
|
|
if authErr != nil {
|
|
|
|
return structs.ErrPermissionDenied
|
|
|
|
}
|
2020-06-17 15:02:26 +00:00
|
|
|
defer metrics.MeasureSince([]string{"nomad", "deployment", "cancel"}, time.Now())
|
|
|
|
|
|
|
|
// Validate the arguments
|
|
|
|
if args.DeploymentID == "" {
|
|
|
|
return fmt.Errorf("missing deployment ID")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lookup the deployment
|
|
|
|
snap, err := d.srv.fsm.State().Snapshot()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ws := memdb.NewWatchSet()
|
|
|
|
deploy, err := snap.DeploymentByID(ws, args.DeploymentID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if deploy == nil {
|
|
|
|
return fmt.Errorf("deployment not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check namespace submit-job permissions
|
2023-01-25 19:33:06 +00:00
|
|
|
if aclObj, err := d.srv.ResolveACL(args); err != nil {
|
2020-06-17 15:02:26 +00:00
|
|
|
return err
|
|
|
|
} else if aclObj != nil && !aclObj.AllowNsOp(deploy.Namespace, acl.NamespaceCapabilitySubmitJob) {
|
|
|
|
return structs.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
|
|
|
if !deploy.Active() {
|
2020-06-17 14:25:37 +00:00
|
|
|
return structs.ErrDeploymentTerminalNoCancel
|
2020-06-17 15:02:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Call into the deployment watcher
|
|
|
|
return d.srv.deploymentWatcher.CancelDeployment(args, reply)
|
|
|
|
}
|
|
|
|
|
2017-06-29 21:50:47 +00:00
|
|
|
// SetAllocHealth is used to set the health of allocations that are part of the
|
|
|
|
// deployment.
|
2017-06-29 05:00:18 +00:00
|
|
|
func (d *Deployment) SetAllocHealth(args *structs.DeploymentAllocHealthRequest, reply *structs.DeploymentUpdateResponse) error {
|
2023-01-25 19:33:06 +00:00
|
|
|
authErr := d.srv.Authenticate(d.ctx, args)
|
2017-06-29 05:00:18 +00:00
|
|
|
if done, err := d.srv.forward("Deployment.SetAllocHealth", args, args, reply); done {
|
|
|
|
return err
|
|
|
|
}
|
2023-01-25 21:37:24 +00:00
|
|
|
d.srv.MeasureRPCRate("deployment", structs.RateMetricWrite, args)
|
2023-01-25 19:33:06 +00:00
|
|
|
if authErr != nil {
|
|
|
|
return structs.ErrPermissionDenied
|
|
|
|
}
|
2017-06-29 05:00:18 +00:00
|
|
|
defer metrics.MeasureSince([]string{"nomad", "deployment", "set_alloc_health"}, time.Now())
|
|
|
|
|
|
|
|
// Validate the arguments
|
|
|
|
if args.DeploymentID == "" {
|
|
|
|
return fmt.Errorf("missing deployment ID")
|
|
|
|
}
|
|
|
|
|
2017-06-29 21:50:47 +00:00
|
|
|
if len(args.HealthyAllocationIDs)+len(args.UnhealthyAllocationIDs) == 0 {
|
|
|
|
return fmt.Errorf("must specify at least one healthy/unhealthy allocation ID")
|
|
|
|
}
|
|
|
|
|
2017-06-29 05:00:18 +00:00
|
|
|
// Lookup the deployment
|
|
|
|
snap, err := d.srv.fsm.State().Snapshot()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ws := memdb.NewWatchSet()
|
|
|
|
deploy, err := snap.DeploymentByID(ws, args.DeploymentID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if deploy == nil {
|
|
|
|
return fmt.Errorf("deployment not found")
|
|
|
|
}
|
|
|
|
|
2019-10-01 20:06:24 +00:00
|
|
|
// Check namespace submit-job permissions
|
2023-01-25 19:33:06 +00:00
|
|
|
if aclObj, err := d.srv.ResolveACL(args); err != nil {
|
2019-10-01 20:06:24 +00:00
|
|
|
return err
|
|
|
|
} else if aclObj != nil && !aclObj.AllowNsOp(deploy.Namespace, acl.NamespaceCapabilitySubmitJob) {
|
|
|
|
return structs.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
2017-06-29 05:00:18 +00:00
|
|
|
if !deploy.Active() {
|
2020-06-16 12:10:41 +00:00
|
|
|
return structs.ErrDeploymentTerminalNoSetHealth
|
2017-06-29 05:00:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Call into the deployment watcher
|
|
|
|
return d.srv.deploymentWatcher.SetAllocHealth(args, reply)
|
|
|
|
}
|
|
|
|
|
2017-06-27 18:15:07 +00:00
|
|
|
// List returns the list of deployments in the system
|
|
|
|
func (d *Deployment) List(args *structs.DeploymentListRequest, reply *structs.DeploymentListResponse) error {
|
2023-01-25 19:33:06 +00:00
|
|
|
authErr := d.srv.Authenticate(d.ctx, args)
|
2017-06-27 18:15:07 +00:00
|
|
|
if done, err := d.srv.forward("Deployment.List", args, args, reply); done {
|
|
|
|
return err
|
|
|
|
}
|
2023-01-25 21:37:24 +00:00
|
|
|
d.srv.MeasureRPCRate("deployment", structs.RateMetricList, args)
|
2023-01-25 19:33:06 +00:00
|
|
|
if authErr != nil {
|
|
|
|
return structs.ErrPermissionDenied
|
|
|
|
}
|
2017-06-29 05:00:18 +00:00
|
|
|
defer metrics.MeasureSince([]string{"nomad", "deployment", "list"}, time.Now())
|
2017-06-27 18:15:07 +00:00
|
|
|
|
2022-02-10 17:50:34 +00:00
|
|
|
namespace := args.RequestNamespace()
|
|
|
|
|
2019-10-01 20:06:24 +00:00
|
|
|
// Check namespace read-job permissions against request namespace since
|
|
|
|
// results are filtered by request namespace.
|
2023-01-25 19:33:06 +00:00
|
|
|
if aclObj, err := d.srv.ResolveACL(args); err != nil {
|
2017-10-02 22:13:56 +00:00
|
|
|
return err
|
2022-02-10 17:50:34 +00:00
|
|
|
} else if aclObj != nil && !aclObj.AllowNsOp(namespace, acl.NamespaceCapabilityReadJob) {
|
2017-10-02 22:13:56 +00:00
|
|
|
return structs.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
2017-06-27 18:15:07 +00:00
|
|
|
// Setup the blocking query
|
2022-03-09 01:54:17 +00:00
|
|
|
sort := state.SortOption(args.Reverse)
|
2017-06-27 18:15:07 +00:00
|
|
|
opts := blockingOptions{
|
|
|
|
queryOpts: &args.QueryOptions,
|
|
|
|
queryMeta: &reply.QueryMeta,
|
2022-01-03 13:36:02 +00:00
|
|
|
run: func(ws memdb.WatchSet, store *state.StateStore) error {
|
2017-06-27 18:15:07 +00:00
|
|
|
// Capture all the deployments
|
|
|
|
var err error
|
|
|
|
var iter memdb.ResultIterator
|
2022-03-09 01:54:17 +00:00
|
|
|
var opts paginator.StructsTokenizerOptions
|
2022-02-10 17:50:34 +00:00
|
|
|
|
2017-06-27 18:15:07 +00:00
|
|
|
if prefix := args.QueryOptions.Prefix; prefix != "" {
|
2022-03-12 00:44:52 +00:00
|
|
|
iter, err = store.DeploymentsByIDPrefix(ws, namespace, prefix, sort)
|
2022-03-09 01:54:17 +00:00
|
|
|
opts = paginator.StructsTokenizerOptions{
|
|
|
|
WithID: true,
|
|
|
|
}
|
2022-02-10 17:50:34 +00:00
|
|
|
} else if namespace != structs.AllNamespacesSentinel {
|
2022-03-09 01:54:17 +00:00
|
|
|
iter, err = store.DeploymentsByNamespaceOrdered(ws, namespace, sort)
|
|
|
|
opts = paginator.StructsTokenizerOptions{
|
|
|
|
WithCreateIndex: true,
|
|
|
|
WithID: true,
|
|
|
|
}
|
2017-06-27 18:15:07 +00:00
|
|
|
} else {
|
2022-03-09 01:54:17 +00:00
|
|
|
iter, err = store.Deployments(ws, sort)
|
|
|
|
opts = paginator.StructsTokenizerOptions{
|
|
|
|
WithCreateIndex: true,
|
|
|
|
WithID: true,
|
|
|
|
}
|
2017-06-27 18:15:07 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-03-09 01:54:17 +00:00
|
|
|
tokenizer := paginator.NewStructsTokenizer(iter, opts)
|
2022-03-01 20:36:49 +00:00
|
|
|
|
2017-06-27 18:15:07 +00:00
|
|
|
var deploys []*structs.Deployment
|
2022-03-09 01:54:17 +00:00
|
|
|
paginator, err := paginator.NewPaginator(iter, tokenizer, nil, args.QueryOptions,
|
2022-02-16 19:34:36 +00:00
|
|
|
func(raw interface{}) error {
|
2022-01-03 13:36:02 +00:00
|
|
|
deploy := raw.(*structs.Deployment)
|
|
|
|
deploys = append(deploys, deploy)
|
2022-02-16 19:34:36 +00:00
|
|
|
return nil
|
2022-01-03 13:36:02 +00:00
|
|
|
})
|
2022-02-16 16:40:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return structs.NewErrRPCCodedf(
|
|
|
|
http.StatusBadRequest, "failed to create result paginator: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
nextToken, err := paginator.Page()
|
|
|
|
if err != nil {
|
|
|
|
return structs.NewErrRPCCodedf(
|
|
|
|
http.StatusBadRequest, "failed to read result page: %v", err)
|
|
|
|
}
|
2022-01-03 13:36:02 +00:00
|
|
|
|
|
|
|
reply.QueryMeta.NextToken = nextToken
|
2017-06-27 18:15:07 +00:00
|
|
|
reply.Deployments = deploys
|
|
|
|
|
2017-08-31 00:45:32 +00:00
|
|
|
// Use the last index that affected the deployment table
|
2022-01-03 13:36:02 +00:00
|
|
|
index, err := store.Index("deployment")
|
2017-06-27 18:15:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
reply.Index = index
|
|
|
|
|
|
|
|
// Set the query response
|
|
|
|
d.srv.setQueryMeta(&reply.QueryMeta)
|
|
|
|
return nil
|
|
|
|
}}
|
|
|
|
return d.srv.blockingRPC(&opts)
|
|
|
|
}
|
|
|
|
|
2017-06-26 21:23:52 +00:00
|
|
|
// Allocations returns the list of allocations that are a part of the deployment
|
|
|
|
func (d *Deployment) Allocations(args *structs.DeploymentSpecificRequest, reply *structs.AllocListResponse) error {
|
2023-01-25 19:33:06 +00:00
|
|
|
authErr := d.srv.Authenticate(d.ctx, args)
|
2017-06-26 21:23:52 +00:00
|
|
|
if done, err := d.srv.forward("Deployment.Allocations", args, args, reply); done {
|
|
|
|
return err
|
|
|
|
}
|
2023-01-25 21:37:24 +00:00
|
|
|
d.srv.MeasureRPCRate("deployment", structs.RateMetricList, args)
|
2023-01-25 19:33:06 +00:00
|
|
|
if authErr != nil {
|
|
|
|
return structs.ErrPermissionDenied
|
|
|
|
}
|
2017-06-26 21:23:52 +00:00
|
|
|
defer metrics.MeasureSince([]string{"nomad", "deployment", "allocations"}, time.Now())
|
|
|
|
|
2019-10-01 20:06:24 +00:00
|
|
|
// Check namespace read-job permissions against the request namespace.
|
|
|
|
// Must re-check against the alloc namespace when they return to ensure
|
|
|
|
// there's no namespace mismatch.
|
|
|
|
allowNsOp := acl.NamespaceValidator(acl.NamespaceCapabilityReadJob)
|
2023-01-25 19:33:06 +00:00
|
|
|
aclObj, err := d.srv.ResolveACL(args)
|
2019-10-01 20:06:24 +00:00
|
|
|
if err != nil {
|
2017-10-02 22:23:29 +00:00
|
|
|
return err
|
2019-10-01 20:06:24 +00:00
|
|
|
} else if !allowNsOp(aclObj, args.RequestNamespace()) {
|
2017-10-02 22:23:29 +00:00
|
|
|
return structs.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
2017-06-26 21:23:52 +00:00
|
|
|
// Setup the blocking query
|
|
|
|
opts := blockingOptions{
|
|
|
|
queryOpts: &args.QueryOptions,
|
|
|
|
queryMeta: &reply.QueryMeta,
|
|
|
|
run: func(ws memdb.WatchSet, state *state.StateStore) error {
|
|
|
|
// Capture all the allocations
|
|
|
|
allocs, err := state.AllocsByDeployment(ws, args.DeploymentID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-10-01 20:06:24 +00:00
|
|
|
// Deployments do not span namespaces so just check the
|
|
|
|
// first allocs namespace.
|
|
|
|
if len(allocs) > 0 {
|
|
|
|
ns := allocs[0].Namespace
|
|
|
|
if ns != args.RequestNamespace() && !allowNsOp(aclObj, ns) {
|
|
|
|
return structs.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-26 21:23:52 +00:00
|
|
|
stubs := make([]*structs.AllocListStub, 0, len(allocs))
|
|
|
|
for _, alloc := range allocs {
|
2020-10-09 05:21:41 +00:00
|
|
|
stubs = append(stubs, alloc.Stub(nil))
|
2017-06-26 21:23:52 +00:00
|
|
|
}
|
|
|
|
reply.Allocations = stubs
|
|
|
|
|
|
|
|
// Use the last index that affected the jobs table
|
|
|
|
index, err := state.Index("allocs")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
reply.Index = index
|
|
|
|
|
|
|
|
// Set the query response
|
|
|
|
d.srv.setQueryMeta(&reply.QueryMeta)
|
|
|
|
return nil
|
|
|
|
}}
|
|
|
|
return d.srv.blockingRPC(&opts)
|
|
|
|
}
|
2017-06-29 19:32:37 +00:00
|
|
|
|
|
|
|
// Reap is used to cleanup terminal deployments
|
|
|
|
func (d *Deployment) Reap(args *structs.DeploymentDeleteRequest,
|
|
|
|
reply *structs.GenericResponse) error {
|
2022-02-05 01:35:20 +00:00
|
|
|
|
2023-01-26 20:04:25 +00:00
|
|
|
authErr := d.srv.Authenticate(d.ctx, args)
|
|
|
|
|
2022-02-05 01:35:20 +00:00
|
|
|
// Ensure the connection was initiated by another server if TLS is used.
|
|
|
|
err := validateTLSCertificateLevel(d.srv, d.ctx, tlsCertificateLevelServer)
|
|
|
|
if err != nil {
|
2017-06-29 19:32:37 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-02-05 01:35:20 +00:00
|
|
|
if done, err := d.srv.forward("Deployment.Reap", args, args, reply); done {
|
|
|
|
return err
|
2022-02-02 20:03:18 +00:00
|
|
|
}
|
2023-01-26 20:04:25 +00:00
|
|
|
d.srv.MeasureRPCRate("deployment", structs.RateMetricWrite, args)
|
|
|
|
if authErr != nil {
|
|
|
|
return structs.ErrPermissionDenied
|
|
|
|
}
|
2022-02-05 01:35:20 +00:00
|
|
|
defer metrics.MeasureSince([]string{"nomad", "deployment", "reap"}, time.Now())
|
2022-02-02 20:03:18 +00:00
|
|
|
|
2017-06-29 19:32:37 +00:00
|
|
|
// Update via Raft
|
|
|
|
_, index, err := d.srv.raftApply(structs.DeploymentDeleteRequestType, args)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the index
|
|
|
|
reply.Index = index
|
|
|
|
return nil
|
|
|
|
}
|