2019-10-15 19:14:25 +00:00
|
|
|
package nomad
|
|
|
|
|
|
|
|
import (
|
2019-11-01 14:33:28 +00:00
|
|
|
"bytes"
|
2019-10-15 19:14:25 +00:00
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net"
|
2019-11-01 14:33:28 +00:00
|
|
|
"time"
|
2019-10-15 19:14:25 +00:00
|
|
|
|
|
|
|
log "github.com/hashicorp/go-hclog"
|
2019-11-01 14:33:28 +00:00
|
|
|
sframer "github.com/hashicorp/nomad/client/lib/streamframer"
|
2019-10-15 19:14:25 +00:00
|
|
|
cstructs "github.com/hashicorp/nomad/client/structs"
|
2020-07-02 13:51:25 +00:00
|
|
|
"github.com/hashicorp/nomad/command/agent/host"
|
2019-10-15 19:14:25 +00:00
|
|
|
"github.com/hashicorp/nomad/command/agent/monitor"
|
2019-12-19 16:41:55 +00:00
|
|
|
"github.com/hashicorp/nomad/command/agent/pprof"
|
2019-10-15 19:14:25 +00:00
|
|
|
"github.com/hashicorp/nomad/helper"
|
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
|
2020-03-18 11:19:04 +00:00
|
|
|
"github.com/hashicorp/go-msgpack/codec"
|
2019-10-15 19:14:25 +00:00
|
|
|
)
|
|
|
|
|
2019-10-30 13:28:24 +00:00
|
|
|
type Agent struct {
|
2019-10-15 19:14:25 +00:00
|
|
|
srv *Server
|
|
|
|
}
|
|
|
|
|
2019-12-10 20:18:57 +00:00
|
|
|
func (a *Agent) register() {
|
|
|
|
a.srv.streamingRpcs.Register("Agent.Monitor", a.monitor)
|
2019-10-15 19:14:25 +00:00
|
|
|
}
|
|
|
|
|
2019-12-12 20:10:32 +00:00
|
|
|
func (a *Agent) Profile(args *structs.AgentPprofRequest, reply *structs.AgentPprofResponse) error {
|
2019-12-19 18:32:23 +00:00
|
|
|
// Check ACL for agent write
|
|
|
|
aclObj, err := a.srv.ResolveToken(args.AuthToken)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if aclObj != nil && !aclObj.AllowAgentWrite() {
|
|
|
|
return structs.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
2019-12-13 16:14:28 +00:00
|
|
|
// Forward to different region if necessary
|
|
|
|
// this would typically be done in a.srv.forward() but since
|
|
|
|
// we are targeting a specific server, not just the leader
|
|
|
|
// we must manually handle region forwarding here.
|
2019-12-12 21:52:13 +00:00
|
|
|
region := args.RequestRegion()
|
|
|
|
if region == "" {
|
|
|
|
return fmt.Errorf("missing target RPC")
|
|
|
|
}
|
|
|
|
|
|
|
|
if region != a.srv.config.Region {
|
|
|
|
// Mark that we are forwarding
|
|
|
|
args.SetForwarded()
|
|
|
|
return a.srv.forwardRegion(region, "Agent.Profile", args, reply)
|
|
|
|
}
|
|
|
|
|
2019-12-09 15:55:43 +00:00
|
|
|
// Targeting a node, forward request to node
|
|
|
|
if args.NodeID != "" {
|
2019-12-10 20:18:57 +00:00
|
|
|
return a.forwardProfileClient(args, reply)
|
2019-12-09 15:55:43 +00:00
|
|
|
}
|
|
|
|
|
2019-12-12 16:56:07 +00:00
|
|
|
// Handle serverID not equal to ours
|
|
|
|
if args.ServerID != "" {
|
2019-12-13 16:14:28 +00:00
|
|
|
serverToFwd, err := a.forwardFor(args.ServerID, region)
|
2019-12-12 16:56:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if serverToFwd != nil {
|
|
|
|
return a.srv.forwardServer(serverToFwd, "Agent.Profile", args, reply)
|
|
|
|
}
|
2019-12-09 15:55:43 +00:00
|
|
|
}
|
|
|
|
|
2019-12-13 18:41:55 +00:00
|
|
|
// If ACLs are disabled, EnableDebug must be enabled
|
2019-12-19 18:32:23 +00:00
|
|
|
if aclObj == nil && !a.srv.config.EnableDebug {
|
|
|
|
return structs.ErrPermissionDenied
|
2019-12-13 18:41:55 +00:00
|
|
|
}
|
|
|
|
|
2019-12-10 20:18:57 +00:00
|
|
|
// Process the request on this server
|
2019-12-09 15:55:43 +00:00
|
|
|
var resp []byte
|
2019-12-11 19:58:41 +00:00
|
|
|
var headers map[string]string
|
2019-12-09 15:55:43 +00:00
|
|
|
|
2019-12-12 20:10:32 +00:00
|
|
|
// Determine which profile to run and generate profile.
|
|
|
|
// Blocks for args.Seconds
|
2019-12-11 19:58:41 +00:00
|
|
|
// Our RPC endpoints currently don't support context
|
2019-12-16 18:42:18 +00:00
|
|
|
// or request cancellation so using server shutdownCtx as a
|
|
|
|
// best effort.
|
2019-12-09 15:55:43 +00:00
|
|
|
switch args.ReqType {
|
2019-12-19 16:41:55 +00:00
|
|
|
case pprof.CPUReq:
|
|
|
|
resp, headers, err = pprof.CPUProfile(a.srv.shutdownCtx, args.Seconds)
|
|
|
|
case pprof.CmdReq:
|
|
|
|
resp, headers, err = pprof.Cmdline()
|
|
|
|
case pprof.LookupReq:
|
|
|
|
resp, headers, err = pprof.Profile(args.Profile, args.Debug, args.GC)
|
|
|
|
case pprof.TraceReq:
|
|
|
|
resp, headers, err = pprof.Trace(a.srv.shutdownCtx, args.Seconds)
|
2019-12-09 15:55:43 +00:00
|
|
|
default:
|
|
|
|
err = structs.NewErrRPCCoded(404, "Unknown profile request type")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2019-12-19 16:41:55 +00:00
|
|
|
if pprof.IsErrProfileNotFound(err) {
|
2019-12-09 15:55:43 +00:00
|
|
|
return structs.NewErrRPCCoded(404, err.Error())
|
|
|
|
}
|
|
|
|
return structs.NewErrRPCCoded(500, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy profile response to reply
|
|
|
|
reply.Payload = resp
|
2019-12-11 19:58:41 +00:00
|
|
|
reply.HTTPHeaders = headers
|
2019-12-12 20:10:32 +00:00
|
|
|
reply.AgentID = a.srv.serf.LocalMember().Name
|
2019-12-09 15:55:43 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-12-10 20:18:57 +00:00
|
|
|
func (a *Agent) monitor(conn io.ReadWriteCloser) {
|
2019-10-15 19:14:25 +00:00
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
// Decode args
|
|
|
|
var args cstructs.MonitorRequest
|
|
|
|
decoder := codec.NewDecoder(conn, structs.MsgpackHandle)
|
|
|
|
encoder := codec.NewEncoder(conn, structs.MsgpackHandle)
|
|
|
|
|
|
|
|
if err := decoder.Decode(&args); err != nil {
|
|
|
|
handleStreamResultError(err, helper.Int64ToPtr(500), encoder)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-11-04 14:04:47 +00:00
|
|
|
// Check agent read permissions
|
2019-12-10 20:18:57 +00:00
|
|
|
if aclObj, err := a.srv.ResolveToken(args.AuthToken); err != nil {
|
2019-10-15 19:14:25 +00:00
|
|
|
handleStreamResultError(err, nil, encoder)
|
|
|
|
return
|
2019-10-25 18:25:19 +00:00
|
|
|
} else if aclObj != nil && !aclObj.AllowAgentRead() {
|
|
|
|
handleStreamResultError(structs.ErrPermissionDenied, helper.Int64ToPtr(403), encoder)
|
2019-10-15 19:14:25 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-10-30 13:36:39 +00:00
|
|
|
logLevel := log.LevelFromString(args.LogLevel)
|
2019-10-15 19:14:25 +00:00
|
|
|
if args.LogLevel == "" {
|
|
|
|
logLevel = log.LevelFromString("INFO")
|
|
|
|
}
|
|
|
|
|
|
|
|
if logLevel == log.NoLevel {
|
|
|
|
handleStreamResultError(errors.New("Unknown log level"), helper.Int64ToPtr(400), encoder)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-11-07 13:33:53 +00:00
|
|
|
// Targeting a node, forward request to node
|
2019-11-13 14:32:18 +00:00
|
|
|
if args.NodeID != "" {
|
2019-12-10 20:18:57 +00:00
|
|
|
a.forwardMonitorClient(conn, args, encoder, decoder)
|
2019-11-07 13:33:53 +00:00
|
|
|
// forwarded request has ended, return
|
|
|
|
return
|
2019-10-15 19:14:25 +00:00
|
|
|
}
|
|
|
|
|
2020-01-29 16:22:43 +00:00
|
|
|
region := args.RequestRegion()
|
|
|
|
if region == "" {
|
|
|
|
handleStreamResultError(fmt.Errorf("missing target RPC"), helper.Int64ToPtr(400), encoder)
|
|
|
|
return
|
2019-11-13 14:32:18 +00:00
|
|
|
}
|
2020-01-29 16:22:43 +00:00
|
|
|
if region != a.srv.config.Region {
|
|
|
|
// Mark that we are forwarding
|
|
|
|
args.SetForwarded()
|
2019-11-12 19:22:51 +00:00
|
|
|
}
|
|
|
|
|
2020-01-29 16:22:43 +00:00
|
|
|
// Try to forward request to remote region/server
|
|
|
|
if args.ServerID != "" {
|
|
|
|
serverToFwd, err := a.forwardFor(args.ServerID, region)
|
|
|
|
if err != nil {
|
|
|
|
handleStreamResultError(err, helper.Int64ToPtr(400), encoder)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if serverToFwd != nil {
|
|
|
|
a.forwardMonitorServer(conn, serverToFwd, args, encoder, decoder)
|
|
|
|
return
|
|
|
|
}
|
2019-11-12 21:23:39 +00:00
|
|
|
}
|
|
|
|
|
2020-01-29 16:22:43 +00:00
|
|
|
// NodeID was empty, ServerID was equal to this server, monitor this server
|
2019-10-15 19:14:25 +00:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
2019-12-10 20:18:57 +00:00
|
|
|
monitor := monitor.New(512, a.srv.logger, &log.LoggerOptions{
|
2019-10-15 19:14:25 +00:00
|
|
|
Level: logLevel,
|
2019-10-24 16:47:46 +00:00
|
|
|
JSONFormat: args.LogJSON,
|
2019-10-15 19:14:25 +00:00
|
|
|
})
|
|
|
|
|
2019-11-01 14:33:28 +00:00
|
|
|
frames := make(chan *sframer.StreamFrame, 32)
|
|
|
|
errCh := make(chan error)
|
|
|
|
var buf bytes.Buffer
|
|
|
|
frameCodec := codec.NewEncoder(&buf, structs.JsonHandle)
|
|
|
|
|
|
|
|
framer := sframer.NewStreamFramer(frames, 1*time.Second, 200*time.Millisecond, 1024)
|
|
|
|
framer.Run()
|
|
|
|
defer framer.Destroy()
|
|
|
|
|
|
|
|
// goroutine to detect remote side closing
|
2019-10-15 19:14:25 +00:00
|
|
|
go func() {
|
|
|
|
if _, err := conn.Read(nil); err != nil {
|
2019-11-01 14:33:28 +00:00
|
|
|
// One end of the pipe explicitly closed, exit
|
2019-10-15 19:14:25 +00:00
|
|
|
cancel()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2019-11-05 14:16:51 +00:00
|
|
|
logCh := monitor.Start()
|
|
|
|
defer monitor.Stop()
|
2019-11-01 14:33:28 +00:00
|
|
|
initialOffset := int64(0)
|
|
|
|
|
|
|
|
// receive logs and build frames
|
|
|
|
go func() {
|
|
|
|
defer framer.Destroy()
|
|
|
|
LOOP:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case log := <-logCh:
|
|
|
|
if err := framer.Send("", "log", log, initialOffset); err != nil {
|
|
|
|
select {
|
|
|
|
case errCh <- err:
|
|
|
|
case <-ctx.Done():
|
|
|
|
}
|
|
|
|
break LOOP
|
|
|
|
}
|
|
|
|
case <-ctx.Done():
|
|
|
|
break LOOP
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2019-10-15 19:14:25 +00:00
|
|
|
|
|
|
|
var streamErr error
|
|
|
|
OUTER:
|
|
|
|
for {
|
|
|
|
select {
|
2019-11-01 14:33:28 +00:00
|
|
|
case frame, ok := <-frames:
|
|
|
|
if !ok {
|
|
|
|
// frame may have been closed when an error
|
|
|
|
// occurred. Check once more for an error.
|
|
|
|
select {
|
|
|
|
case streamErr = <-errCh:
|
|
|
|
// There was a pending error!
|
|
|
|
default:
|
|
|
|
// No error, continue on
|
|
|
|
}
|
|
|
|
|
|
|
|
break OUTER
|
|
|
|
}
|
|
|
|
|
2019-10-15 19:14:25 +00:00
|
|
|
var resp cstructs.StreamErrWrapper
|
2019-11-01 14:33:28 +00:00
|
|
|
if args.PlainText {
|
|
|
|
resp.Payload = frame.Data
|
|
|
|
} else {
|
|
|
|
if err := frameCodec.Encode(frame); err != nil {
|
|
|
|
streamErr = err
|
|
|
|
break OUTER
|
|
|
|
}
|
|
|
|
|
|
|
|
resp.Payload = buf.Bytes()
|
|
|
|
buf.Reset()
|
|
|
|
}
|
|
|
|
|
2019-10-15 19:14:25 +00:00
|
|
|
if err := encoder.Encode(resp); err != nil {
|
|
|
|
streamErr = err
|
|
|
|
break OUTER
|
|
|
|
}
|
|
|
|
encoder.Reset(conn)
|
|
|
|
case <-ctx.Done():
|
|
|
|
break OUTER
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if streamErr != nil {
|
2019-11-04 19:32:53 +00:00
|
|
|
handleStreamResultError(streamErr, helper.Int64ToPtr(500), encoder)
|
|
|
|
return
|
2019-10-15 19:14:25 +00:00
|
|
|
}
|
|
|
|
}
|
2019-11-04 14:25:19 +00:00
|
|
|
|
2019-12-16 18:42:18 +00:00
|
|
|
// forwardFor returns a serverParts for a request to be forwarded to.
|
|
|
|
// A response of nil, nil indicates that the current server is equal to the
|
|
|
|
// serverID and region so the request should not be forwarded.
|
2019-12-13 16:14:28 +00:00
|
|
|
func (a *Agent) forwardFor(serverID, region string) (*serverParts, error) {
|
2019-12-12 16:56:07 +00:00
|
|
|
var target *serverParts
|
2020-01-29 16:22:43 +00:00
|
|
|
var err error
|
2019-12-12 16:56:07 +00:00
|
|
|
|
|
|
|
if serverID == "leader" {
|
|
|
|
isLeader, remoteLeader := a.srv.getLeader()
|
|
|
|
if !isLeader && remoteLeader != nil {
|
|
|
|
target = remoteLeader
|
|
|
|
} else if !isLeader && remoteLeader == nil {
|
|
|
|
return nil, structs.ErrNoLeader
|
|
|
|
} else if isLeader {
|
|
|
|
// This server is current leader do not forward
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
} else {
|
2020-01-29 16:22:43 +00:00
|
|
|
target, err = a.srv.getServer(region, serverID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-12-12 16:56:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unable to find a server
|
|
|
|
if target == nil {
|
|
|
|
return nil, fmt.Errorf("unknown nomad server %s", serverID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ServerID is this current server,
|
|
|
|
// No need to forward request
|
2019-12-12 21:52:13 +00:00
|
|
|
if target.Name == a.srv.LocalMember().Name {
|
2019-12-12 16:56:07 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return target, nil
|
|
|
|
}
|
|
|
|
|
2019-12-10 20:18:57 +00:00
|
|
|
func (a *Agent) forwardMonitorClient(conn io.ReadWriteCloser, args cstructs.MonitorRequest, encoder *codec.Encoder, decoder *codec.Decoder) {
|
2020-07-02 13:51:25 +00:00
|
|
|
// Get the Connection to the client either by fowarding to another server
|
|
|
|
// or creating direct stream
|
2019-11-04 14:25:19 +00:00
|
|
|
|
2020-07-02 13:51:25 +00:00
|
|
|
state, srv, err := a.findClientConn(args.NodeID)
|
2019-11-04 14:25:19 +00:00
|
|
|
if err != nil {
|
|
|
|
handleStreamResultError(err, helper.Int64ToPtr(500), encoder)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var clientConn net.Conn
|
2020-07-02 13:51:25 +00:00
|
|
|
|
|
|
|
if state == nil {
|
2019-12-10 20:18:57 +00:00
|
|
|
conn, err := a.srv.streamingRpc(srv, "Agent.Monitor")
|
2019-11-04 14:25:19 +00:00
|
|
|
if err != nil {
|
|
|
|
handleStreamResultError(err, nil, encoder)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
clientConn = conn
|
|
|
|
} else {
|
|
|
|
stream, err := NodeStreamingRpc(state.Session, "Agent.Monitor")
|
|
|
|
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)
|
|
|
|
return
|
|
|
|
}
|
2019-11-12 19:22:51 +00:00
|
|
|
|
2020-01-29 16:22:43 +00:00
|
|
|
func (a *Agent) forwardMonitorServer(conn io.ReadWriteCloser, server *serverParts, args cstructs.MonitorRequest, encoder *codec.Encoder, decoder *codec.Decoder) {
|
2019-11-12 21:23:39 +00:00
|
|
|
// empty ServerID to prevent forwarding loop
|
|
|
|
args.ServerID = ""
|
|
|
|
|
2020-01-29 16:22:43 +00:00
|
|
|
serverConn, err := a.srv.streamingRpc(server, "Agent.Monitor")
|
2019-11-13 14:32:18 +00:00
|
|
|
if err != nil {
|
|
|
|
handleStreamResultError(err, helper.Int64ToPtr(500), encoder)
|
|
|
|
return
|
|
|
|
}
|
2019-11-12 21:23:39 +00:00
|
|
|
defer serverConn.Close()
|
|
|
|
|
|
|
|
// Send the Request
|
|
|
|
outEncoder := codec.NewEncoder(serverConn, structs.MsgpackHandle)
|
|
|
|
if err := outEncoder.Encode(args); err != nil {
|
2019-11-13 14:32:18 +00:00
|
|
|
handleStreamResultError(err, helper.Int64ToPtr(500), encoder)
|
2019-11-12 21:23:39 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
structs.Bridge(conn, serverConn)
|
|
|
|
return
|
|
|
|
}
|
2019-12-12 20:10:32 +00:00
|
|
|
func (a *Agent) forwardProfileClient(args *structs.AgentPprofRequest, reply *structs.AgentPprofResponse) error {
|
2020-07-02 13:51:25 +00:00
|
|
|
state, srv, err := a.findClientConn(args.NodeID)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if srv != nil {
|
|
|
|
return a.srv.forwardServer(srv, "Agent.Profile", args, reply)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NodeRpc
|
|
|
|
rpcErr := NodeRpc(state.Session, "Agent.Profile", args, reply)
|
|
|
|
if rpcErr != nil {
|
|
|
|
return rpcErr
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Host returns data about the agent's host system for the `debug` command.
|
|
|
|
func (a *Agent) Host(args *structs.HostDataRequest, reply *structs.HostDataResponse) error {
|
|
|
|
|
|
|
|
aclObj, err := a.srv.ResolveToken(args.AuthToken)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if (aclObj != nil && !aclObj.AllowAgentRead()) ||
|
|
|
|
(aclObj == nil && !a.srv.config.EnableDebug) {
|
|
|
|
return structs.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
|
|
|
// Forward to different region if necessary
|
|
|
|
// this would typically be done in a.srv.forward() but since
|
|
|
|
// we are targeting a specific server, not just the leader
|
|
|
|
// we must manually handle region forwarding here.
|
|
|
|
region := args.RequestRegion()
|
|
|
|
if region == "" {
|
|
|
|
return fmt.Errorf("missing target RPC")
|
|
|
|
}
|
|
|
|
|
|
|
|
if region != a.srv.config.Region {
|
|
|
|
// Mark that we are forwarding
|
|
|
|
args.SetForwarded()
|
|
|
|
return a.srv.forwardRegion(region, "Agent.Host", args, reply)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Targeting a client node, forward request to node
|
|
|
|
if args.NodeID != "" {
|
|
|
|
client, srv, err := a.findClientConn(args.NodeID)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if srv != nil {
|
|
|
|
return a.srv.forwardServer(srv, "Agent.Host", args, reply)
|
|
|
|
}
|
|
|
|
|
|
|
|
return NodeRpc(client.Session, "Agent.Host", args, reply)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle serverID not equal to ours
|
|
|
|
if args.ServerID != "" {
|
|
|
|
srv, err := a.forwardFor(args.ServerID, region)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if srv != nil {
|
|
|
|
return a.srv.forwardServer(srv, "Agent.Host", args, reply)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := host.MakeHostData()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
reply.AgentID = a.srv.serf.LocalMember().Name
|
|
|
|
reply.HostData = data
|
|
|
|
return nil
|
|
|
|
}
|
2019-12-09 15:55:43 +00:00
|
|
|
|
2020-07-02 13:51:25 +00:00
|
|
|
// findClientConn is a helper that returns a connection to the client node or, if the client
|
|
|
|
// is connected to a different server, a serverParts describing the server to which the
|
|
|
|
// client bound RPC should be forwarded.
|
|
|
|
func (a *Agent) findClientConn(nodeID string) (*nodeConnState, *serverParts, error) {
|
2019-12-10 20:18:57 +00:00
|
|
|
snap, err := a.srv.State().Snapshot()
|
2019-12-09 15:55:43 +00:00
|
|
|
if err != nil {
|
2020-07-02 13:51:25 +00:00
|
|
|
return nil, nil, structs.NewErrRPCCoded(500, err.Error())
|
2019-12-09 15:55:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
node, err := snap.NodeByID(nil, nodeID)
|
|
|
|
if err != nil {
|
2020-07-02 13:51:25 +00:00
|
|
|
return nil, nil, structs.NewErrRPCCoded(500, err.Error())
|
2019-12-09 15:55:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if node == nil {
|
|
|
|
err := fmt.Errorf("Unknown node %q", nodeID)
|
2020-07-02 13:51:25 +00:00
|
|
|
return nil, nil, structs.NewErrRPCCoded(404, err.Error())
|
2019-12-09 15:55:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := nodeSupportsRpc(node); err != nil {
|
2020-07-02 13:51:25 +00:00
|
|
|
return nil, nil, structs.NewErrRPCCoded(400, err.Error())
|
2019-12-09 15:55:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get the Connection to the client either by fowarding to another server
|
|
|
|
// or creating direct stream
|
2019-12-10 20:18:57 +00:00
|
|
|
state, ok := a.srv.getNodeConn(nodeID)
|
2020-07-02 13:51:25 +00:00
|
|
|
if ok {
|
|
|
|
return state, nil, nil
|
|
|
|
}
|
2019-12-09 15:55:43 +00:00
|
|
|
|
2020-07-02 13:51:25 +00:00
|
|
|
// Determine the server that has a connection to the node
|
|
|
|
srv, err := a.srv.serverWithNodeConn(nodeID, a.srv.Region())
|
|
|
|
if err != nil {
|
|
|
|
code := 500
|
|
|
|
if structs.IsErrNoNodeConn(err) {
|
|
|
|
code = 404
|
2019-12-09 15:55:43 +00:00
|
|
|
}
|
2020-07-02 13:51:25 +00:00
|
|
|
return nil, nil, structs.NewErrRPCCoded(code, err.Error())
|
2019-12-09 15:55:43 +00:00
|
|
|
}
|
|
|
|
|
2020-07-02 13:51:25 +00:00
|
|
|
return nil, srv, nil
|
2019-12-09 15:55:43 +00:00
|
|
|
}
|