2018-02-06 01:20:42 +00:00
|
|
|
package nomad
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2019-04-28 21:25:27 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net"
|
2018-02-06 01:20:42 +00:00
|
|
|
"time"
|
|
|
|
|
2022-12-01 15:05:15 +00:00
|
|
|
"github.com/armon/go-metrics"
|
|
|
|
"github.com/hashicorp/go-hclog"
|
2020-03-18 11:27:32 +00:00
|
|
|
"github.com/hashicorp/go-msgpack/codec"
|
2022-12-01 15:05:15 +00:00
|
|
|
|
2022-06-07 14:18:19 +00:00
|
|
|
"github.com/hashicorp/nomad/acl"
|
2018-02-06 01:20:42 +00:00
|
|
|
cstructs "github.com/hashicorp/nomad/client/structs"
|
2022-08-17 16:26:34 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/pointer"
|
2018-02-06 01:20:42 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
)
|
|
|
|
|
2022-06-07 14:18:19 +00:00
|
|
|
// ClientAllocations is used to forward RPC requests to the targeted Nomad client's
|
2018-02-06 01:20:42 +00:00
|
|
|
// Allocation endpoint.
|
|
|
|
type ClientAllocations struct {
|
2018-09-15 23:23:13 +00:00
|
|
|
srv *Server
|
2022-12-01 15:05:15 +00:00
|
|
|
logger hclog.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewClientAllocationsEndpoint(srv *Server) *ClientAllocations {
|
|
|
|
return &ClientAllocations{srv: srv, logger: srv.logger.Named("client_allocs")}
|
2018-02-06 01:20:42 +00:00
|
|
|
}
|
|
|
|
|
2019-04-28 21:25:27 +00:00
|
|
|
func (a *ClientAllocations) register() {
|
|
|
|
a.srv.streamingRpcs.Register("Allocations.Exec", a.exec)
|
|
|
|
}
|
|
|
|
|
2018-02-06 01:20:42 +00:00
|
|
|
// GarbageCollectAll is used to garbage collect all allocations on a client.
|
|
|
|
func (a *ClientAllocations) GarbageCollectAll(args *structs.NodeSpecificRequest, reply *structs.GenericResponse) error {
|
|
|
|
// We only allow stale reads since the only potentially stale information is
|
2019-04-01 12:56:02 +00:00
|
|
|
// the Node registration and the cost is fairly high for adding another hop
|
2018-02-06 01:20:42 +00:00
|
|
|
// in the forwarding chain.
|
|
|
|
args.QueryOptions.AllowStale = true
|
|
|
|
|
2023-01-25 19:33:06 +00:00
|
|
|
authErr := a.srv.Authenticate(nil, args)
|
|
|
|
|
2018-02-06 01:20:42 +00:00
|
|
|
// Potentially forward to a different region.
|
|
|
|
if done, err := a.srv.forward("ClientAllocations.GarbageCollectAll", args, args, reply); done {
|
|
|
|
return err
|
|
|
|
}
|
2023-01-25 21:37:24 +00:00
|
|
|
a.srv.MeasureRPCRate("client_allocations", structs.RateMetricWrite, args)
|
2023-01-25 19:33:06 +00:00
|
|
|
if authErr != nil {
|
|
|
|
return structs.ErrPermissionDenied
|
|
|
|
}
|
2018-02-06 01:20:42 +00:00
|
|
|
defer metrics.MeasureSince([]string{"nomad", "client_allocations", "garbage_collect_all"}, time.Now())
|
|
|
|
|
|
|
|
// Check node read permissions
|
2023-01-25 19:33:06 +00:00
|
|
|
if aclObj, err := a.srv.ResolveACL(args); err != nil {
|
2018-02-06 01:20:42 +00:00
|
|
|
return err
|
|
|
|
} else if aclObj != nil && !aclObj.AllowNodeWrite() {
|
|
|
|
return structs.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify the arguments.
|
|
|
|
if args.NodeID == "" {
|
|
|
|
return errors.New("missing NodeID")
|
|
|
|
}
|
|
|
|
|
2018-04-06 18:33:08 +00:00
|
|
|
// Make sure Node is valid and new enough to support RPC
|
|
|
|
snap, err := a.srv.State().Snapshot()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-04-09 17:17:39 +00:00
|
|
|
_, err = getNodeForRpc(snap, args.NodeID)
|
2018-04-06 18:33:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-02-06 01:20:42 +00:00
|
|
|
// Get the connection to the client
|
|
|
|
state, ok := a.srv.getNodeConn(args.NodeID)
|
|
|
|
if !ok {
|
2018-04-06 18:33:08 +00:00
|
|
|
return findNodeConnAndForward(a.srv, args.NodeID, "ClientAllocations.GarbageCollectAll", args, reply)
|
2018-02-06 01:20:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make the RPC
|
|
|
|
return NodeRpc(state.Session, "Allocations.GarbageCollectAll", args, reply)
|
|
|
|
}
|
|
|
|
|
2019-04-03 10:46:15 +00:00
|
|
|
// Signal is used to send a signal to an allocation on a client.
|
|
|
|
func (a *ClientAllocations) Signal(args *structs.AllocSignalRequest, reply *structs.GenericResponse) error {
|
|
|
|
// 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 := a.srv.Authenticate(nil, args)
|
|
|
|
|
2019-04-03 10:46:15 +00:00
|
|
|
// Potentially forward to a different region.
|
|
|
|
if done, err := a.srv.forward("ClientAllocations.Signal", args, args, reply); done {
|
|
|
|
return err
|
|
|
|
}
|
2023-01-25 21:37:24 +00:00
|
|
|
a.srv.MeasureRPCRate("client_allocations", structs.RateMetricWrite, args)
|
2023-01-25 19:33:06 +00:00
|
|
|
if authErr != nil {
|
|
|
|
return structs.ErrPermissionDenied
|
|
|
|
}
|
2019-04-03 10:46:15 +00:00
|
|
|
defer metrics.MeasureSince([]string{"nomad", "client_allocations", "signal"}, time.Now())
|
|
|
|
|
|
|
|
// Verify the arguments.
|
|
|
|
if args.AllocID == "" {
|
|
|
|
return errors.New("missing AllocID")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find the allocation
|
|
|
|
snap, err := a.srv.State().Snapshot()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-10-01 20:06:24 +00:00
|
|
|
alloc, err := getAlloc(snap, args.AllocID)
|
2019-04-03 10:46:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-10-01 20:06:24 +00:00
|
|
|
// Check namespace alloc-lifecycle permission.
|
2023-01-25 19:33:06 +00:00
|
|
|
if aclObj, err := a.srv.ResolveACL(args); err != nil {
|
2019-10-01 20:06:24 +00:00
|
|
|
return err
|
|
|
|
} else if aclObj != nil && !aclObj.AllowNsOp(alloc.Namespace, acl.NamespaceCapabilityAllocLifecycle) {
|
|
|
|
return structs.ErrPermissionDenied
|
2019-04-03 10:46:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure Node is valid and new enough to support RPC
|
|
|
|
_, err = getNodeForRpc(snap, alloc.NodeID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the connection to the client
|
|
|
|
state, ok := a.srv.getNodeConn(alloc.NodeID)
|
|
|
|
if !ok {
|
|
|
|
return findNodeConnAndForward(a.srv, alloc.NodeID, "ClientAllocations.Signal", args, reply)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make the RPC
|
|
|
|
return NodeRpc(state.Session, "Allocations.Signal", args, reply)
|
|
|
|
}
|
|
|
|
|
2018-02-06 01:20:42 +00:00
|
|
|
// GarbageCollect is used to garbage collect an allocation on a client.
|
|
|
|
func (a *ClientAllocations) GarbageCollect(args *structs.AllocSpecificRequest, reply *structs.GenericResponse) error {
|
|
|
|
// We only allow stale reads since the only potentially stale information is
|
2019-04-01 12:56:02 +00:00
|
|
|
// the Node registration and the cost is fairly high for adding another hop
|
2018-02-06 01:20:42 +00:00
|
|
|
// in the forwarding chain.
|
|
|
|
args.QueryOptions.AllowStale = true
|
|
|
|
|
2023-01-25 19:33:06 +00:00
|
|
|
authErr := a.srv.Authenticate(nil, args)
|
|
|
|
|
2018-02-06 01:20:42 +00:00
|
|
|
// Potentially forward to a different region.
|
|
|
|
if done, err := a.srv.forward("ClientAllocations.GarbageCollect", args, args, reply); done {
|
|
|
|
return err
|
|
|
|
}
|
2023-01-25 21:37:24 +00:00
|
|
|
a.srv.MeasureRPCRate("client_allocations", structs.RateMetricWrite, args)
|
2023-01-25 19:33:06 +00:00
|
|
|
if authErr != nil {
|
|
|
|
return structs.ErrPermissionDenied
|
|
|
|
}
|
2018-02-06 01:20:42 +00:00
|
|
|
defer metrics.MeasureSince([]string{"nomad", "client_allocations", "garbage_collect"}, time.Now())
|
|
|
|
|
|
|
|
// Verify the arguments.
|
|
|
|
if args.AllocID == "" {
|
|
|
|
return errors.New("missing AllocID")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find the allocation
|
|
|
|
snap, err := a.srv.State().Snapshot()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-10-01 20:06:24 +00:00
|
|
|
alloc, err := getAlloc(snap, args.AllocID)
|
2018-02-06 01:20:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-10-01 20:06:24 +00:00
|
|
|
// Check namespace submit-job permission.
|
2023-01-25 19:33:06 +00:00
|
|
|
if aclObj, err := a.srv.ResolveACL(args); err != nil {
|
2019-10-01 20:06:24 +00:00
|
|
|
return err
|
|
|
|
} else if aclObj != nil && !aclObj.AllowNsOp(alloc.Namespace, acl.NamespaceCapabilitySubmitJob) {
|
|
|
|
return structs.ErrPermissionDenied
|
2018-02-06 01:20:42 +00:00
|
|
|
}
|
|
|
|
|
2018-04-06 18:33:08 +00:00
|
|
|
// Make sure Node is valid and new enough to support RPC
|
2018-04-09 17:17:39 +00:00
|
|
|
_, err = getNodeForRpc(snap, alloc.NodeID)
|
2018-04-06 18:33:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-02-06 01:20:42 +00:00
|
|
|
// Get the connection to the client
|
|
|
|
state, ok := a.srv.getNodeConn(alloc.NodeID)
|
|
|
|
if !ok {
|
2018-04-06 18:33:08 +00:00
|
|
|
return findNodeConnAndForward(a.srv, alloc.NodeID, "ClientAllocations.GarbageCollect", args, reply)
|
2018-02-06 01:20:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make the RPC
|
2018-02-06 18:53:00 +00:00
|
|
|
return NodeRpc(state.Session, "Allocations.GarbageCollect", args, reply)
|
2018-02-06 01:20:42 +00:00
|
|
|
}
|
|
|
|
|
2019-04-01 12:56:02 +00:00
|
|
|
// Restart is used to trigger a restart of an allocation or a subtask on a client.
|
|
|
|
func (a *ClientAllocations) Restart(args *structs.AllocRestartRequest, reply *structs.GenericResponse) error {
|
|
|
|
// We only allow stale reads since the only potentially stale information is
|
|
|
|
// the Node registration and the cost is fairly high for adding another hop
|
|
|
|
// in the forwarding chain.
|
|
|
|
args.QueryOptions.AllowStale = true
|
|
|
|
|
2023-01-25 19:33:06 +00:00
|
|
|
authErr := a.srv.Authenticate(nil, args)
|
|
|
|
|
2019-04-01 12:56:02 +00:00
|
|
|
// Potentially forward to a different region.
|
|
|
|
if done, err := a.srv.forward("ClientAllocations.Restart", args, args, reply); done {
|
|
|
|
return err
|
|
|
|
}
|
2023-01-25 21:37:24 +00:00
|
|
|
a.srv.MeasureRPCRate("client_allocations", structs.RateMetricWrite, args)
|
2023-01-25 19:33:06 +00:00
|
|
|
if authErr != nil {
|
|
|
|
return structs.ErrPermissionDenied
|
|
|
|
}
|
2019-04-01 12:56:02 +00:00
|
|
|
defer metrics.MeasureSince([]string{"nomad", "client_allocations", "restart"}, time.Now())
|
|
|
|
|
|
|
|
// Find the allocation
|
|
|
|
snap, err := a.srv.State().Snapshot()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-10-01 20:06:24 +00:00
|
|
|
alloc, err := getAlloc(snap, args.AllocID)
|
2019-04-01 12:56:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-10-01 20:06:24 +00:00
|
|
|
// Check for namespace alloc-lifecycle permissions.
|
2023-01-25 19:33:06 +00:00
|
|
|
if aclObj, err := a.srv.ResolveACL(args); err != nil {
|
2019-10-01 20:06:24 +00:00
|
|
|
return err
|
|
|
|
} else if aclObj != nil && !aclObj.AllowNsOp(alloc.Namespace, acl.NamespaceCapabilityAllocLifecycle) {
|
|
|
|
return structs.ErrPermissionDenied
|
2019-04-01 12:56:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure Node is valid and new enough to support RPC
|
|
|
|
_, err = getNodeForRpc(snap, alloc.NodeID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the connection to the client
|
|
|
|
state, ok := a.srv.getNodeConn(alloc.NodeID)
|
|
|
|
if !ok {
|
|
|
|
return findNodeConnAndForward(a.srv, alloc.NodeID, "ClientAllocations.Restart", args, reply)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make the RPC
|
|
|
|
return NodeRpc(state.Session, "Allocations.Restart", args, reply)
|
|
|
|
}
|
|
|
|
|
2018-02-06 01:20:42 +00:00
|
|
|
// Stats is used to collect allocation statistics
|
|
|
|
func (a *ClientAllocations) Stats(args *cstructs.AllocStatsRequest, reply *cstructs.AllocStatsResponse) error {
|
|
|
|
// We only allow stale reads since the only potentially stale information is
|
2019-04-01 12:56:02 +00:00
|
|
|
// the Node registration and the cost is fairly high for adding another hop
|
2018-02-06 01:20:42 +00:00
|
|
|
// in the forwarding chain.
|
|
|
|
args.QueryOptions.AllowStale = true
|
|
|
|
|
2023-01-25 19:33:06 +00:00
|
|
|
authErr := a.srv.Authenticate(nil, args)
|
|
|
|
|
2018-02-06 01:20:42 +00:00
|
|
|
// Potentially forward to a different region.
|
|
|
|
if done, err := a.srv.forward("ClientAllocations.Stats", args, args, reply); done {
|
|
|
|
return err
|
|
|
|
}
|
2023-01-25 21:37:24 +00:00
|
|
|
a.srv.MeasureRPCRate("client_allocations", structs.RateMetricRead, args)
|
2023-01-25 19:33:06 +00:00
|
|
|
if authErr != nil {
|
|
|
|
return structs.ErrPermissionDenied
|
|
|
|
}
|
2018-02-06 01:20:42 +00:00
|
|
|
defer metrics.MeasureSince([]string{"nomad", "client_allocations", "stats"}, time.Now())
|
|
|
|
|
|
|
|
// Find the allocation
|
|
|
|
snap, err := a.srv.State().Snapshot()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-10-01 20:06:24 +00:00
|
|
|
alloc, err := getAlloc(snap, args.AllocID)
|
2018-02-06 01:20:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-10-01 20:06:24 +00:00
|
|
|
// Check for namespace read-job permissions.
|
2023-01-25 19:33:06 +00:00
|
|
|
if aclObj, err := a.srv.ResolveACL(args); err != nil {
|
2019-10-01 20:06:24 +00:00
|
|
|
return err
|
|
|
|
} else if aclObj != nil && !aclObj.AllowNsOp(alloc.Namespace, acl.NamespaceCapabilityReadJob) {
|
|
|
|
return structs.ErrPermissionDenied
|
2018-02-06 01:20:42 +00:00
|
|
|
}
|
|
|
|
|
2018-04-06 18:33:08 +00:00
|
|
|
// Make sure Node is valid and new enough to support RPC
|
2018-04-09 17:17:39 +00:00
|
|
|
_, err = getNodeForRpc(snap, alloc.NodeID)
|
2018-04-06 18:33:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-02-06 01:20:42 +00:00
|
|
|
// Get the connection to the client
|
|
|
|
state, ok := a.srv.getNodeConn(alloc.NodeID)
|
|
|
|
if !ok {
|
2018-04-06 18:33:08 +00:00
|
|
|
return findNodeConnAndForward(a.srv, alloc.NodeID, "ClientAllocations.Stats", args, reply)
|
2018-02-06 01:20:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make the RPC
|
|
|
|
return NodeRpc(state.Session, "Allocations.Stats", args, reply)
|
|
|
|
}
|
2019-04-28 21:25:27 +00:00
|
|
|
|
2022-09-08 14:55:23 +00:00
|
|
|
// Checks is the server implementation of the allocation checks RPC. The
|
|
|
|
// ultimate response is provided by the node running the allocation. This RPC
|
|
|
|
// is needed to handle queries which hit the server agent API directly, or via
|
|
|
|
// another node which is not running the allocation.
|
|
|
|
func (a *ClientAllocations) Checks(args *cstructs.AllocChecksRequest, reply *cstructs.AllocChecksResponse) error {
|
|
|
|
|
|
|
|
// We only allow stale reads since the only potentially stale information
|
|
|
|
// is the Node registration and the cost is fairly high for adding another
|
|
|
|
// hop in the forwarding chain.
|
|
|
|
args.QueryOptions.AllowStale = true
|
|
|
|
|
2023-01-25 19:33:06 +00:00
|
|
|
authErr := a.srv.Authenticate(nil, args)
|
|
|
|
|
2022-09-08 14:55:23 +00:00
|
|
|
// Potentially forward to a different region.
|
|
|
|
if done, err := a.srv.forward("ClientAllocations.Checks", args, args, reply); done {
|
|
|
|
return err
|
|
|
|
}
|
2023-01-25 21:37:24 +00:00
|
|
|
a.srv.MeasureRPCRate("client_allocations", structs.RateMetricRead, args)
|
2023-01-25 19:33:06 +00:00
|
|
|
if authErr != nil {
|
|
|
|
return structs.ErrPermissionDenied
|
|
|
|
}
|
2022-09-08 14:55:23 +00:00
|
|
|
defer metrics.MeasureSince([]string{"nomad", "client_allocations", "checks"}, time.Now())
|
|
|
|
|
|
|
|
// Grab the state snapshot, as we need this to perform lookups for a number
|
|
|
|
// of objects, all things being well.
|
|
|
|
snap, err := a.srv.State().Snapshot()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the full allocation object, so we have information such as the
|
|
|
|
// namespace and node ID.
|
|
|
|
alloc, err := getAlloc(snap, args.AllocID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for namespace read-job permissions.
|
2023-01-25 19:33:06 +00:00
|
|
|
if aclObj, err := a.srv.ResolveACL(args); err != nil {
|
2022-09-08 14:55:23 +00:00
|
|
|
return err
|
|
|
|
} else if aclObj != nil && !aclObj.AllowNsOp(alloc.Namespace, acl.NamespaceCapabilityReadJob) {
|
|
|
|
return structs.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure Node is valid and new enough to support RPC.
|
|
|
|
if _, err = getNodeForRpc(snap, alloc.NodeID); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the connection to the client.
|
|
|
|
state, ok := a.srv.getNodeConn(alloc.NodeID)
|
|
|
|
if !ok {
|
|
|
|
return findNodeConnAndForward(a.srv, alloc.NodeID, "ClientAllocations.Checks", args, reply)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make the RPC
|
|
|
|
return NodeRpc(state.Session, "Allocations.Checks", args, reply)
|
|
|
|
}
|
|
|
|
|
2019-04-28 21:25:27 +00:00
|
|
|
// exec is used to execute command in a running task
|
|
|
|
func (a *ClientAllocations) exec(conn io.ReadWriteCloser) {
|
|
|
|
defer conn.Close()
|
|
|
|
defer metrics.MeasureSince([]string{"nomad", "alloc", "exec"}, time.Now())
|
|
|
|
|
|
|
|
// Decode the arguments
|
|
|
|
var args cstructs.AllocExecRequest
|
|
|
|
decoder := codec.NewDecoder(conn, structs.MsgpackHandle)
|
|
|
|
encoder := codec.NewEncoder(conn, structs.MsgpackHandle)
|
|
|
|
|
|
|
|
if err := decoder.Decode(&args); err != nil {
|
2022-08-17 16:26:34 +00:00
|
|
|
handleStreamResultError(err, pointer.Of(int64(500)), encoder)
|
2019-04-28 21:25:27 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-01-25 19:33:06 +00:00
|
|
|
authErr := a.srv.Authenticate(nil, &args)
|
|
|
|
|
2019-04-28 21:25:27 +00:00
|
|
|
// Check if we need to forward to a different region
|
|
|
|
if r := args.RequestRegion(); r != a.srv.Region() {
|
|
|
|
forwardRegionStreamingRpc(a.srv, conn, encoder, &args, "Allocations.Exec",
|
|
|
|
args.AllocID, &args.QueryOptions)
|
|
|
|
return
|
|
|
|
}
|
2023-01-25 21:37:24 +00:00
|
|
|
a.srv.MeasureRPCRate("client_allocations", structs.RateMetricWrite, &args)
|
2023-01-25 19:33:06 +00:00
|
|
|
if authErr != nil {
|
|
|
|
handleStreamResultError(structs.ErrPermissionDenied, nil, encoder)
|
|
|
|
return
|
|
|
|
}
|
2019-04-28 21:25:27 +00:00
|
|
|
|
|
|
|
// Verify the arguments.
|
|
|
|
if args.AllocID == "" {
|
2022-08-17 16:26:34 +00:00
|
|
|
handleStreamResultError(errors.New("missing AllocID"), pointer.Of(int64(400)), encoder)
|
2019-04-28 21:25:27 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Retrieve the allocation
|
|
|
|
snap, err := a.srv.State().Snapshot()
|
|
|
|
if err != nil {
|
|
|
|
handleStreamResultError(err, nil, encoder)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-10-01 20:06:24 +00:00
|
|
|
alloc, err := getAlloc(snap, args.AllocID)
|
|
|
|
if structs.IsErrUnknownAllocation(err) {
|
2022-08-17 16:26:34 +00:00
|
|
|
handleStreamResultError(err, pointer.Of(int64(404)), encoder)
|
2019-10-01 20:06:24 +00:00
|
|
|
return
|
|
|
|
}
|
2019-04-28 21:25:27 +00:00
|
|
|
if err != nil {
|
|
|
|
handleStreamResultError(err, nil, encoder)
|
|
|
|
return
|
|
|
|
}
|
2019-10-01 20:06:24 +00:00
|
|
|
|
|
|
|
// Check node read permissions
|
2023-01-25 19:33:06 +00:00
|
|
|
if aclObj, err := a.srv.ResolveACL(&args); err != nil {
|
2019-10-01 20:06:24 +00:00
|
|
|
handleStreamResultError(err, nil, encoder)
|
|
|
|
return
|
|
|
|
} else if aclObj != nil && !aclObj.AllowNsOp(alloc.Namespace, acl.NamespaceCapabilityAllocExec) {
|
|
|
|
// client ultimately checks if AllocNodeExec is required
|
|
|
|
handleStreamResultError(structs.ErrPermissionDenied, nil, encoder)
|
2019-04-28 21:25:27 +00:00
|
|
|
return
|
|
|
|
}
|
2019-10-01 20:06:24 +00:00
|
|
|
|
2019-04-28 21:25:27 +00:00
|
|
|
nodeID := alloc.NodeID
|
|
|
|
|
|
|
|
// Make sure Node is valid and new enough to support RPC
|
|
|
|
node, err := snap.NodeByID(nil, nodeID)
|
|
|
|
if err != nil {
|
2022-08-17 16:26:34 +00:00
|
|
|
handleStreamResultError(err, pointer.Of(int64(500)), encoder)
|
2019-04-28 21:25:27 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if node == nil {
|
|
|
|
err := fmt.Errorf("Unknown node %q", nodeID)
|
2022-08-17 16:26:34 +00:00
|
|
|
handleStreamResultError(err, pointer.Of(int64(400)), encoder)
|
2019-04-28 21:25:27 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := nodeSupportsRpc(node); err != nil {
|
2022-08-17 16:26:34 +00:00
|
|
|
handleStreamResultError(err, pointer.Of(int64(400)), encoder)
|
2019-04-28 21:25:27 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the connection to the client either by forwarding to another server
|
|
|
|
// or creating a direct stream
|
|
|
|
var clientConn net.Conn
|
|
|
|
state, ok := a.srv.getNodeConn(nodeID)
|
|
|
|
if !ok {
|
|
|
|
// Determine the Server that has a connection to the node.
|
|
|
|
srv, err := a.srv.serverWithNodeConn(nodeID, a.srv.Region())
|
|
|
|
if err != nil {
|
|
|
|
var code *int64
|
|
|
|
if structs.IsErrNoNodeConn(err) {
|
2022-08-17 16:26:34 +00:00
|
|
|
code = pointer.Of(int64(404))
|
2019-04-28 21:25:27 +00:00
|
|
|
}
|
|
|
|
handleStreamResultError(err, code, encoder)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get a connection to the server
|
|
|
|
conn, err := a.srv.streamingRpc(srv, "Allocations.Exec")
|
|
|
|
if err != nil {
|
|
|
|
handleStreamResultError(err, nil, encoder)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
clientConn = conn
|
|
|
|
} else {
|
|
|
|
stream, err := NodeStreamingRpc(state.Session, "Allocations.Exec")
|
|
|
|
if err != nil {
|
|
|
|
handleStreamResultError(err, nil, encoder)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
clientConn = stream
|
|
|
|
}
|
|
|
|
defer clientConn.Close()
|
|
|
|
|
|
|
|
// Send the request.
|
|
|
|
outEncoder := codec.NewEncoder(clientConn, structs.MsgpackHandle)
|
|
|
|
if err := outEncoder.Encode(args); err != nil {
|
|
|
|
handleStreamResultError(err, nil, encoder)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
structs.Bridge(conn, clientConn)
|
|
|
|
}
|