diff --git a/CHANGELOG.md b/CHANGELOG.md index 1b5fcd7117e..64db8227437 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -32,6 +32,7 @@ BUG FIXES: [[GH-4234](https://github.com/hashicorp/nomad/issues/4234)] * driver/docker: Fix docker credential helper support [[GH-4266](https://github.com/hashicorp/nomad/issues/4266)] * driver/docker: Fix panic when docker client configuration options are invalid [[GH-4303](https://github.com/hashicorp/nomad/issues/4303)] + * rpc: Fix RPC tunneling when running both client/server on one machine [[GH-4317](https://github.com/hashicorp/nomad/issues/4317)] ## 0.8.3 (April 27, 2018) diff --git a/command/agent/agent.go b/command/agent/agent.go index 2eddcad021c..34453a04d7e 100644 --- a/command/agent/agent.go +++ b/command/agent/agent.go @@ -314,9 +314,24 @@ func (a *Agent) clientConfig() (*clientconfig.Config, error) { if conf == nil { conf = clientconfig.DefaultConfig() } + + // If we are running a server, append both its bind and advertise address so + // we are able to at least talk to the local server even if that isn't + // configured explicitly. This handles both running server and client on one + // host and -dev mode. + conf.Servers = a.config.Client.Servers if a.server != nil { - conf.RPCHandler = a.server + if a.config.AdvertiseAddrs == nil || a.config.AdvertiseAddrs.RPC == "" { + return nil, fmt.Errorf("AdvertiseAddrs is nil or empty") + } else if a.config.normalizedAddrs == nil || a.config.normalizedAddrs.RPC == "" { + return nil, fmt.Errorf("normalizedAddrs is nil or empty") + } + + conf.Servers = append(conf.Servers, + a.config.normalizedAddrs.RPC, + a.config.AdvertiseAddrs.RPC) } + conf.LogOutput = a.logOutput conf.LogLevel = a.config.LogLevel conf.DevMode = a.config.DevMode @@ -333,7 +348,6 @@ func (a *Agent) clientConfig() (*clientconfig.Config, error) { if a.config.Client.AllocDir != "" { conf.AllocDir = a.config.Client.AllocDir } - conf.Servers = a.config.Client.Servers if a.config.Client.NetworkInterface != "" { conf.NetworkInterface = a.config.Client.NetworkInterface } diff --git a/command/agent/agent_test.go b/command/agent/agent_test.go index 2926231239c..89c556808e6 100644 --- a/command/agent/agent_test.go +++ b/command/agent/agent_test.go @@ -9,7 +9,9 @@ import ( "testing" "time" + cstructs "github.com/hashicorp/nomad/client/structs" "github.com/hashicorp/nomad/helper" + "github.com/hashicorp/nomad/nomad/structs" sconfig "github.com/hashicorp/nomad/nomad/structs/config" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -1057,3 +1059,24 @@ func TestServer_ShouldReload_ShouldHandleMultipleChanges(t *testing.T) { require.False(shouldReloadRPC) } } + +func TestAgent_ProxyRPC_Dev(t *testing.T) { + t.Parallel() + agent := NewTestAgent(t, t.Name(), nil) + defer agent.Shutdown() + + id := agent.client.NodeID() + req := &structs.NodeSpecificRequest{ + NodeID: id, + QueryOptions: structs.QueryOptions{ + Region: agent.server.Region(), + }, + } + + time.Sleep(100 * time.Millisecond) + + var resp cstructs.ClientStatsResponse + if err := agent.RPC("ClientStats.Stats", req, &resp); err != nil { + t.Fatalf("err: %v", err) + } +} diff --git a/nomad/client_rpc.go b/nomad/client_rpc.go index 5a3498de7bf..1493348ebd3 100644 --- a/nomad/client_rpc.go +++ b/nomad/client_rpc.go @@ -31,6 +31,9 @@ func (s *Server) getNodeConn(nodeID string) (*nodeConnState, bool) { s.nodeConnsLock.RLock() defer s.nodeConnsLock.RUnlock() conns, ok := s.nodeConns[nodeID] + if !ok { + return nil, false + } // Return the latest conn var state *nodeConnState