Skip to content

Commit

Permalink
Merge pull request #30 from Nitro/relistan/allow-disabling-ips
Browse files Browse the repository at this point in the history
Adds a configuration setting to force the old haproxy.cfg behavior
  • Loading branch information
relistan committed Apr 18, 2017
2 parents b44d199 + 3010ba1 commit 70c2fd8
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 4 deletions.
1 change: 1 addition & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type HAproxyConfig struct {
Disable bool `toml:"disable"`
User string `toml:"user"`
Group string `toml:"group"`
UseHostnames bool `toml:"use_hostnames"`
}

type ServicesConfig struct {
Expand Down
5 changes: 5 additions & 0 deletions docker/s6/services/sidecar.svc/run
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ if [[ -n "$BIND_IP" ]]; then
sed -i.bak 's~^bind_ip *=.*$~bind_ip = "'"$BIND_IP"'"~' sidecar.toml
fi

# If we're disabling IP addresses in the HAproxy configs
if [[ -n "$USE_HOSTNAMES" ]]; then
sed -i.bak 's~^use_hostnames *=.*$~use_hostnames = "'"$USE_HOSTNAMES"'"~' sidecar.toml
fi

BIND_IP=`grep bind_ip sidecar.toml | grep -o "[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*"`

# If there's a BIND_IP and we don't already have it, add the
Expand Down
1 change: 1 addition & 0 deletions docker/sidecar.docker.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ config_file = "/etc/haproxy.cfg"
pid_file = "/var/run/haproxy.pid"
user = "haproxy"
group = "haproxy"
use_hostnames = false

[listeners]
urls = [ "http://192.168.168.168:7778/update" ]
11 changes: 9 additions & 2 deletions haproxy/haproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type HAproxy struct {
PidFile string `toml:"pid_file"`
User string `toml:"user"`
Group string `toml:"group"`
UseHostnames bool `toml:"use_hostnames"`
eventChannel chan catalog.ChangeEvent
}

Expand Down Expand Up @@ -100,7 +101,13 @@ func findPortForService(svcPort string, svc *service.Service) string {
}

// Find the matching IP address when given a ServicePort
func findIpForService(svcPort string, svc *service.Service) string {
func (h *HAproxy) findIpForService(svcPort string, svc *service.Service) string {
// We can turn off using IP addresses in the config, which is sometimes
// necessary (e.g. w/Docker for Mac).
if h.UseHostnames {
return svc.Hostname
}

matchPort, err := strconv.ParseInt(svcPort, 10, 64)
if err != nil {
log.Errorf("Invalid value from template ('%s') can't parse as int64: %s", svcPort, err.Error())
Expand Down Expand Up @@ -151,7 +158,7 @@ func (h *HAproxy) WriteConfig(state *catalog.ServicesState, output io.Writer) er
return ports[k]
},
"portFor": findPortForService,
"ipFor": findIpForService,
"ipFor": h.findIpForService,
"bindIP": func() string { return h.BindIP },
"sanitizeName": sanitizeName,
}
Expand Down
20 changes: 18 additions & 2 deletions haproxy/haproxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,22 @@ func Test_HAproxy(t *testing.T) {
So(result["some-svc"], ShouldEqual, "tcp")
})

Convey("findIpForService() returns hostnames when UseHostnames is set", func() {
proxy.UseHostnames = true
svc := services[0]
result := proxy.findIpForService("8080", &svc)

So(result, ShouldEqual, "indomitable")
})

Convey("findIpForService() returns IP addresses when UseHostnames is false", func() {
proxy.UseHostnames = false
svc := services[0]
result := proxy.findIpForService("8080", &svc)

So(result, ShouldEqual, "127.0.0.1")
})

Convey("servicesWithPorts() groups services by name and port", func() {
badSvc := service.Service{
ID: "0000bad00000",
Expand Down Expand Up @@ -192,11 +208,11 @@ func Test_HAproxy(t *testing.T) {
Convey("Reload() returns an error when it fails", func() {
proxy.ReloadCmd = "sh -c 'exit 1'"
err := proxy.Reload()
So(err.Error(), ShouldEqual, "exit status 1")
So(err.Error(), ShouldContainSubstring, "exit status 1")

proxy.ReloadCmd = "yomomma"
err = proxy.Reload()
So(err.Error(), ShouldEqual, "exit status 127")
So(err.Error(), ShouldContainSubstring, "exit status 127")
})

Convey("WriteAndReload() bubbles up errors on failure", func() {
Expand Down
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ func configureHAproxy(config Config) *haproxy.HAproxy {
proxy.Group = config.HAproxy.Group
}

proxy.UseHostnames = config.HAproxy.UseHostnames

return proxy
}

Expand Down
6 changes: 6 additions & 0 deletions sidecar.example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ template_file = "views/haproxy.cfg"
config_file = "/etc/haproxy.cfg"
pid_file = "/var/run/haproxy.pid"

# This setting will force HAproxy configs to be written
# using hostnames rather than IP addresses. This can be
# required when using Docker for Mac, for example, or
# in a situation where there is some address remapping.
use_hostnames = false

# URLs can be subscribed to Sidecar state change events.
# Each URL in the list will be posted to with a copy
# of the event, which contains the service that changed
Expand Down

0 comments on commit 70c2fd8

Please sign in to comment.