2018-01-11 23:58:59 +00:00
|
|
|
package nomad
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
metrics "github.com/armon/go-metrics"
|
2018-09-15 23:23:13 +00:00
|
|
|
log "github.com/hashicorp/go-hclog"
|
2018-01-11 23:58:59 +00:00
|
|
|
nstructs "github.com/hashicorp/nomad/nomad/structs"
|
2018-09-15 23:23:13 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/client/structs"
|
2018-01-11 23:58:59 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// ClientStats is used to forward RPC requests to the targed Nomad client's
|
|
|
|
// ClientStats endpoint.
|
|
|
|
type ClientStats struct {
|
2018-09-15 23:23:13 +00:00
|
|
|
srv *Server
|
|
|
|
logger log.Logger
|
2018-01-11 23:58:59 +00:00
|
|
|
}
|
|
|
|
|
2022-12-01 15:05:15 +00:00
|
|
|
func NewClientStatsEndpoint(srv *Server) *ClientStats {
|
|
|
|
return &ClientStats{srv: srv, logger: srv.logger.Named("client_stats")}
|
|
|
|
}
|
|
|
|
|
2018-02-06 01:20:42 +00:00
|
|
|
func (s *ClientStats) Stats(args *nstructs.NodeSpecificRequest, reply *structs.ClientStatsResponse) error {
|
2023-01-25 19:33:06 +00:00
|
|
|
|
2018-01-11 23:58:59 +00:00
|
|
|
// We only allow stale reads since the only potentially stale information is
|
|
|
|
// the Node registration and the cost is fairly high for adding another hope
|
|
|
|
// in the forwarding chain.
|
|
|
|
args.QueryOptions.AllowStale = true
|
2023-01-25 19:33:06 +00:00
|
|
|
authErr := s.srv.Authenticate(nil, args)
|
2018-01-11 23:58:59 +00:00
|
|
|
|
|
|
|
// Potentially forward to a different region.
|
|
|
|
if done, err := s.srv.forward("ClientStats.Stats", args, args, reply); done {
|
|
|
|
return err
|
|
|
|
}
|
2023-01-25 21:37:24 +00:00
|
|
|
s.srv.MeasureRPCRate("client_stats", nstructs.RateMetricRead, args)
|
2023-01-25 19:33:06 +00:00
|
|
|
if authErr != nil {
|
|
|
|
return nstructs.ErrPermissionDenied
|
|
|
|
}
|
2018-01-11 23:58:59 +00:00
|
|
|
defer metrics.MeasureSince([]string{"nomad", "client_stats", "stats"}, time.Now())
|
|
|
|
|
|
|
|
// Check node read permissions
|
2023-01-25 19:33:06 +00:00
|
|
|
if aclObj, err := s.srv.ResolveACL(args); err != nil {
|
2018-01-11 23:58:59 +00:00
|
|
|
return err
|
|
|
|
} else if aclObj != nil && !aclObj.AllowNodeRead() {
|
|
|
|
return nstructs.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
2023-02-07 22:42:25 +00:00
|
|
|
return s.srv.forwardClientRPC("ClientStats.Stats", args.NodeID, args, reply)
|
2018-01-11 23:58:59 +00:00
|
|
|
}
|