Skip to content

Commit

Permalink
Small cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
dadgar committed Oct 27, 2016
1 parent ba55b1a commit a3676fc
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 62 deletions.
95 changes: 40 additions & 55 deletions command/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
Expand All @@ -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
}
Expand Down
3 changes: 0 additions & 3 deletions command/agent/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`

Expand Down
4 changes: 4 additions & 0 deletions nomad/structs/config/consul.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`

Expand Down
8 changes: 4 additions & 4 deletions website/source/docs/agent/config.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit a3676fc

Please sign in to comment.