From a3676fc09748bda472a08574b5afb4b1c50d7e8d Mon Sep 17 00:00:00 2001 From: Alex Dadgar Date: Wed, 26 Oct 2016 12:58:36 -0700 Subject: [PATCH] Small cleanups --- command/agent/agent.go | 95 ++++++++++-------------- command/agent/config.go | 3 - nomad/structs/config/consul.go | 4 + website/source/docs/agent/config.html.md | 8 +- 4 files changed, 48 insertions(+), 62 deletions(-) diff --git a/command/agent/agent.go b/command/agent/agent.go index f1889941898..6deeb234278 100644 --- a/command/agent/agent.go +++ b/command/agent/agent.go @@ -331,15 +331,15 @@ func (a *Agent) setupServer() error { a.server = server // Resolve consul check addresses. Always use advertise address for services - httpCheckAddr, err := a.selectAddr(a.getHTTPAddr, !a.config.ChecksUseAdvertise) + httpCheckAddr, err := a.selectAddr(a.getHTTPAddr, !a.config.Consul.ChecksUseAdvertise) if err != nil { return err } - rpcCheckAddr, err := a.selectAddr(a.getRPCAddr, !a.config.ChecksUseAdvertise) + rpcCheckAddr, err := a.selectAddr(a.getRPCAddr, !a.config.Consul.ChecksUseAdvertise) if err != nil { return err } - serfCheckAddr, err := a.selectAddr(a.getSerfAddr, !a.config.ChecksUseAdvertise) + serfCheckAddr, err := a.selectAddr(a.getSerfAddr, !a.config.Consul.ChecksUseAdvertise) if err != nil { return err } @@ -451,7 +451,7 @@ func (a *Agent) setupClient() error { a.client = client // Resolve the http check address - httpCheckAddr, err := a.selectAddr(a.getHTTPAddr, !a.config.ChecksUseAdvertise) + httpCheckAddr, err := a.selectAddr(a.getHTTPAddr, !a.config.Consul.ChecksUseAdvertise) if err != nil { return err } @@ -485,7 +485,7 @@ func (a *Agent) setupClient() error { // Defines the selector interface type addrSelector func(bool) (*net.TCPAddr, error) -// Choose the right address given a selector, and return it as a PortLabel +// selectAddr returns the right address given a selector, and return it as a PortLabel // preferBind is a weak preference, and will skip 0.0.0.0 func (a *Agent) selectAddr(selector addrSelector, preferBind bool) (string, error) { addr, err := selector(preferBind) @@ -504,75 +504,60 @@ func (a *Agent) selectAddr(selector addrSelector, preferBind bool) (string, erro return address, nil } -// Address selectors -// Resolve Bind address and Advertise address. Skip 0.0.0.0 unless -// we're looking for the Bind address, since that's the only time -// it's useful -func (a *Agent) getHTTPAddr(returnBind bool) (*net.TCPAddr, error) { - var serverAddr string +// getHTTPAddr returns the HTTP address to use based on the clients +// configuration. If bind is true, an address appropriate for binding is +// returned, otherwise an address for advertising is returned. Skip 0.0.0.0 +// unless returning a bind address, since that's the only time it's useful. +func (a *Agent) getHTTPAddr(bind bool) (*net.TCPAddr, error) { advertAddr := a.config.AdvertiseAddrs.HTTP bindAddr := a.config.Addresses.HTTP globalBindAddr := a.config.BindAddr - - if advertAddr != "" && !returnBind { - serverAddr = advertAddr - } else if bindAddr != "" && !(bindAddr == "0.0.0.0" && !returnBind) { - serverAddr = net.JoinHostPort(bindAddr, strconv.Itoa(a.config.Ports.HTTP)) - } else if globalBindAddr != "" && !(globalBindAddr == "0.0.0.0" && !returnBind) { - serverAddr = net.JoinHostPort(globalBindAddr, strconv.Itoa(a.config.Ports.HTTP)) - } else { - serverAddr = net.JoinHostPort("127.0.0.1", strconv.Itoa(a.config.Ports.HTTP)) - } - - addr, err := net.ResolveTCPAddr("tcp", serverAddr) - if err != nil { - return nil, fmt.Errorf("error resolving HTTP addr %+q: %v", serverAddr, err) - } - return addr, nil + port := a.config.Ports.HTTP + return pickAddress(bind, globalBindAddr, advertAddr, bindAddr, port, "HTTP") } -func (a *Agent) getRPCAddr(returnBind bool) (*net.TCPAddr, error) { - var serverAddr string +// getRPCAddr returns the HTTP address to use based on the clients +// configuration. If bind is true, an address appropriate for binding is +// returned, otherwise an address for advertising is returned. Skip 0.0.0.0 +// unless returning a bind address, since that's the only time it's useful. +func (a *Agent) getRPCAddr(bind bool) (*net.TCPAddr, error) { advertAddr := a.config.AdvertiseAddrs.RPC bindAddr := a.config.Addresses.RPC globalBindAddr := a.config.BindAddr - - if advertAddr != "" && !returnBind { - serverAddr = advertAddr - } else if bindAddr != "" && !(bindAddr == "0.0.0.0" && !returnBind) { - serverAddr = net.JoinHostPort(bindAddr, strconv.Itoa(a.config.Ports.RPC)) - } else if globalBindAddr != "" && !(globalBindAddr == "0.0.0.0" && !returnBind) { - serverAddr = net.JoinHostPort(globalBindAddr, strconv.Itoa(a.config.Ports.RPC)) - } else { - serverAddr = net.JoinHostPort("127.0.0.1", strconv.Itoa(a.config.Ports.RPC)) - } - - addr, err := net.ResolveTCPAddr("tcp", serverAddr) - if err != nil { - return nil, fmt.Errorf("error resolving RPC addr %+q: %v", serverAddr, err) - } - return addr, nil + port := a.config.Ports.RPC + return pickAddress(bind, globalBindAddr, advertAddr, bindAddr, port, "RPC") } -func (a *Agent) getSerfAddr(returnBind bool) (*net.TCPAddr, error) { - var serverAddr string +// getSerfAddr returns the Serf address to use based on the clients +// configuration. If bind is true, an address appropriate for binding is +// returned, otherwise an address for advertising is returned. Skip 0.0.0.0 +// unless returning a bind address, since that's the only time it's useful. +func (a *Agent) getSerfAddr(bind bool) (*net.TCPAddr, error) { advertAddr := a.config.AdvertiseAddrs.Serf bindAddr := a.config.Addresses.Serf globalBindAddr := a.config.BindAddr + port := a.config.Ports.Serf + return pickAddress(bind, globalBindAddr, advertAddr, bindAddr, port, "RPC") +} - if advertAddr != "" && !returnBind { - serverAddr = advertAddr - } else if bindAddr != "" && !(bindAddr == "0.0.0.0" && !returnBind) { - serverAddr = net.JoinHostPort(bindAddr, strconv.Itoa(a.config.Ports.Serf)) - } else if globalBindAddr != "" && !(globalBindAddr == "0.0.0.0" && !returnBind) { - serverAddr = net.JoinHostPort(globalBindAddr, strconv.Itoa(a.config.Ports.Serf)) +// pickAddress is a shared helper to pick the address to either bind to or +// advertise. +func pickAddress(bind bool, globalBindAddr, advertiseAddr, bindAddr string, port int, service string) (*net.TCPAddr, error) { + portConverted := strconv.Itoa(port) + var serverAddr string + if advertiseAddr != "" && !bind { + serverAddr = advertiseAddr + } else if bindAddr != "" && !(bindAddr == "0.0.0.0" && !bind) { + serverAddr = net.JoinHostPort(bindAddr, portConverted) + } else if globalBindAddr != "" && !(globalBindAddr == "0.0.0.0" && !bind) { + serverAddr = net.JoinHostPort(globalBindAddr, portConverted) } else { - serverAddr = net.JoinHostPort("127.0.0.1", strconv.Itoa(a.config.Ports.Serf)) + serverAddr = net.JoinHostPort("127.0.0.1", portConverted) } addr, err := net.ResolveTCPAddr("tcp", serverAddr) if err != nil { - return nil, fmt.Errorf("error resolving Serf addr %+q: %v", serverAddr, err) + return nil, fmt.Errorf("error resolving %s addr %+q: %v", service, serverAddr, err) } return addr, nil } diff --git a/command/agent/config.go b/command/agent/config.go index be4459e336f..91e7f2018b8 100644 --- a/command/agent/config.go +++ b/command/agent/config.go @@ -51,9 +51,6 @@ type Config struct { // AdvertiseAddrs is used to control the addresses we advertise. AdvertiseAddrs *AdvertiseAddrs `mapstructure:"advertise"` - // Specifies that consul checks should use Advertise address instead of bind - ChecksUseAdvertise bool `mapstructure:"checks_use_advertise"` - // Client has our client related settings Client *ClientConfig `mapstructure:"client"` diff --git a/nomad/structs/config/consul.go b/nomad/structs/config/consul.go index 86d70bf47c1..d20e5e419b2 100644 --- a/nomad/structs/config/consul.go +++ b/nomad/structs/config/consul.go @@ -33,6 +33,10 @@ type ConsulConfig struct { // services with Consul. AutoAdvertise bool `mapstructure:"auto_advertise"` + // ChecksUseAdvertise specifies that Consul checks should use advertise + // address instead of bind address + ChecksUseAdvertise bool `mapstructure:"checks_use_advertise"` + // Addr is the address of the local Consul agent Addr string `mapstructure:"address"` diff --git a/website/source/docs/agent/config.html.md b/website/source/docs/agent/config.html.md index 27d60f66d87..f0fe59f22f0 100644 --- a/website/source/docs/agent/config.html.md +++ b/website/source/docs/agent/config.html.md @@ -176,10 +176,6 @@ nodes, unless otherwise specified: reachable from all server nodes. It is not required that clients can reach this address. -* `checks_use_advertise`: By default, Nomad will use the configured bind address - as the target for its consul checks. This boolean option allows you to request - that the advertise address be used instead. - * `consul`: The `consul` configuration block changes how Nomad interacts with Consul. Nomad can automatically advertise Nomad services via Consul, and can automatically bootstrap itself using Consul. For more details see the [`consul` @@ -302,6 +298,10 @@ integration and are entirely optional. services, each tagged appropriately with either `http` or `rpc` tag. Nomad Servers also advertise a `serf` tagged service. Defaults to `true`. + * `checks_use_advertise`: By default, Nomad will use the configured bind + address as the target for its consul checks. This boolean option allows you + to request that the advertise address be used instead. + * `server_auto_join`: Servers will automatically discover and join other Nomad Servers by searching for the Consul service name defined in the `server_service_name` option. This search only happens if the Server does