2023-04-10 15:36:59 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2016-02-20 23:50:41 +00:00
|
|
|
package nomad
|
|
|
|
|
|
|
|
import (
|
2016-06-22 16:04:22 +00:00
|
|
|
"fmt"
|
|
|
|
|
2022-12-01 15:05:15 +00:00
|
|
|
"github.com/hashicorp/go-hclog"
|
2018-09-15 23:23:13 +00:00
|
|
|
|
2016-02-20 23:50:41 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
)
|
|
|
|
|
|
|
|
// System endpoint is used to call invoke system tasks.
|
|
|
|
type System struct {
|
2018-09-15 23:23:13 +00:00
|
|
|
srv *Server
|
2022-12-01 15:05:15 +00:00
|
|
|
ctx *RPCContext
|
|
|
|
logger hclog.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewSystemEndpoint(srv *Server, ctx *RPCContext) *System {
|
|
|
|
return &System{srv: srv, ctx: ctx, logger: srv.logger.Named("system")}
|
2016-02-20 23:50:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GarbageCollect is used to trigger the system to immediately garbage collect nodes, evals
|
|
|
|
// and jobs.
|
|
|
|
func (s *System) GarbageCollect(args *structs.GenericRequest, reply *structs.GenericResponse) error {
|
2023-01-25 19:33:06 +00:00
|
|
|
|
|
|
|
authErr := s.srv.Authenticate(s.ctx, args)
|
2016-02-20 23:50:41 +00:00
|
|
|
if done, err := s.srv.forward("System.GarbageCollect", args, args, reply); done {
|
|
|
|
return err
|
|
|
|
}
|
2023-01-25 21:37:24 +00:00
|
|
|
s.srv.MeasureRPCRate("system", structs.RateMetricWrite, args)
|
2023-01-25 19:33:06 +00:00
|
|
|
if authErr != nil {
|
|
|
|
return structs.ErrPermissionDenied
|
|
|
|
}
|
2016-02-20 23:50:41 +00:00
|
|
|
|
2017-10-10 17:51:38 +00:00
|
|
|
// Check management level permissions
|
2023-01-25 19:33:06 +00:00
|
|
|
if acl, err := s.srv.ResolveACL(args); err != nil {
|
2017-10-10 17:51:38 +00:00
|
|
|
return err
|
|
|
|
} else if acl != nil && !acl.IsManagement() {
|
|
|
|
return structs.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
2016-06-22 16:33:15 +00:00
|
|
|
// Get the states current index
|
|
|
|
snapshotIndex, err := s.srv.fsm.State().LatestIndex()
|
2016-06-22 16:04:22 +00:00
|
|
|
if err != nil {
|
2016-06-22 16:33:15 +00:00
|
|
|
return fmt.Errorf("failed to determine state store's index: %v", err)
|
2016-06-22 16:04:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
s.srv.evalBroker.Enqueue(s.srv.coreJobEval(structs.CoreJobForceGC, snapshotIndex))
|
2016-02-20 23:50:41 +00:00
|
|
|
return nil
|
|
|
|
}
|
2016-08-03 23:08:30 +00:00
|
|
|
|
2021-08-30 09:08:12 +00:00
|
|
|
// ReconcileJobSummaries reconciles the summaries of all the jobs in the state
|
2016-08-03 23:08:30 +00:00
|
|
|
// store
|
|
|
|
func (s *System) ReconcileJobSummaries(args *structs.GenericRequest, reply *structs.GenericResponse) error {
|
2023-01-25 19:33:06 +00:00
|
|
|
|
|
|
|
authErr := s.srv.Authenticate(s.ctx, args)
|
2016-08-03 23:08:30 +00:00
|
|
|
if done, err := s.srv.forward("System.ReconcileJobSummaries", args, args, reply); done {
|
|
|
|
return err
|
|
|
|
}
|
2023-01-25 21:37:24 +00:00
|
|
|
s.srv.MeasureRPCRate("system", structs.RateMetricWrite, args)
|
2023-01-25 19:33:06 +00:00
|
|
|
if authErr != nil {
|
|
|
|
return structs.ErrPermissionDenied
|
|
|
|
}
|
2016-08-03 23:08:30 +00:00
|
|
|
|
2017-10-10 17:51:38 +00:00
|
|
|
// Check management level permissions
|
2023-01-25 19:33:06 +00:00
|
|
|
if acl, err := s.srv.ResolveACL(args); err != nil {
|
2017-10-10 17:51:38 +00:00
|
|
|
return err
|
|
|
|
} else if acl != nil && !acl.IsManagement() {
|
|
|
|
return structs.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
2016-08-03 23:08:30 +00:00
|
|
|
_, index, err := s.srv.raftApply(structs.ReconcileJobSummariesRequestType, args)
|
|
|
|
if err != nil {
|
2016-08-05 21:40:35 +00:00
|
|
|
return fmt.Errorf("reconciliation of job summaries failed: %v", err)
|
2016-08-03 23:08:30 +00:00
|
|
|
}
|
|
|
|
reply.Index = index
|
|
|
|
return nil
|
|
|
|
}
|