open-nomad/nomad/system_endpoint.go
Tim Gross f61f801e77
provide RPCContext to all RPC handlers (#15430)
Upcoming work to instrument the rate of RPC requests by consumer (and eventually
rate limit) requires that we thread the `RPCContext` through all RPC
handlers so that we can access the underlying connection. This changeset adds
the context to everywhere we intend to initially support it and intentionally
excludes streaming RPCs and client RPCs.

To improve the ergonomics of adding the context everywhere its needed and to
clarify the requirements of dynamic vs static handlers, I've also done a good
bit of refactoring here:

* canonicalized the RPC handler fields so they're as close to identical as
  possible without introducing unused fields (i.e. I didn't add loggers if the
  handler doesn't use them already).
* canonicalized the imports in the handler files.
* added a `NewExampleEndpoint` function for each handler that ensures we're
  constructing the handlers with the required arguments.
* reordered the registration in server.go to match the order of the files (to
  make it easier to see if we've missed one), and added a bunch of commentary
  there as to what the difference between static and dynamic handlers is.
2022-12-01 10:05:15 -05:00

67 lines
1.9 KiB
Go

package nomad
import (
"fmt"
"github.com/hashicorp/go-hclog"
"github.com/hashicorp/nomad/nomad/structs"
)
// System endpoint is used to call invoke system tasks.
type System struct {
srv *Server
ctx *RPCContext
logger hclog.Logger
}
func NewSystemEndpoint(srv *Server, ctx *RPCContext) *System {
return &System{srv: srv, ctx: ctx, logger: srv.logger.Named("system")}
}
// 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 {
if done, err := s.srv.forward("System.GarbageCollect", args, args, reply); done {
return err
}
// Check management level permissions
if acl, err := s.srv.ResolveToken(args.AuthToken); err != nil {
return err
} else if acl != nil && !acl.IsManagement() {
return structs.ErrPermissionDenied
}
// Get the states current index
snapshotIndex, err := s.srv.fsm.State().LatestIndex()
if err != nil {
return fmt.Errorf("failed to determine state store's index: %v", err)
}
s.srv.evalBroker.Enqueue(s.srv.coreJobEval(structs.CoreJobForceGC, snapshotIndex))
return nil
}
// ReconcileJobSummaries reconciles the summaries of all the jobs in the state
// store
func (s *System) ReconcileJobSummaries(args *structs.GenericRequest, reply *structs.GenericResponse) error {
if done, err := s.srv.forward("System.ReconcileJobSummaries", args, args, reply); done {
return err
}
// Check management level permissions
if acl, err := s.srv.ResolveToken(args.AuthToken); err != nil {
return err
} else if acl != nil && !acl.IsManagement() {
return structs.ErrPermissionDenied
}
_, index, err := s.srv.raftApply(structs.ReconcileJobSummariesRequestType, args)
if err != nil {
return fmt.Errorf("reconciliation of job summaries failed: %v", err)
}
reply.Index = index
return nil
}