Skip to content
This repository has been archived by the owner on Sep 26, 2018. It is now read-only.

Commit

Permalink
Add fallback to hostname when service.Port does not contain the IP
Browse files Browse the repository at this point in the history
  • Loading branch information
mihaitodor committed May 19, 2017
1 parent a6236aa commit 1062fec
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
16 changes: 15 additions & 1 deletion provider/sidecar.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"os"
"path/filepath"
Expand Down Expand Up @@ -270,8 +271,21 @@ func (provider *Sidecar) makeBackends(sidecarStates *catalog.ServicesState) map[
if len(svc.Ports) > 1 {
name = fmt.Sprintf("%s_%d", svc.Hostname, port.Port)
}

host := port.IP
if host == "" {
ipAddr, err := net.LookupIP(svc.Hostname)
if err != nil {
log.Errorf("Error resolving IP address for host '%s': %s", svc.Hostname, err)
host = svc.Hostname

} else {
host = ipAddr[0].String()
}
}

backend.Servers[name] = types.Server{
URL: fmt.Sprintf("http://%s:%d", port.IP, port.Port),
URL: fmt.Sprintf("http://%s:%d", host, port.Port),
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions provider/sidecar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ func TestSidecar(t *testing.T) {
Type: "udp",
Port: 9000,
ServicePort: 9000,
IP: "127.0.0.1",
},
},
},
Expand Down Expand Up @@ -142,7 +141,9 @@ func TestSidecar(t *testing.T) {

// A server can have multiple ports exposed
So(backs["api"].Servers["another-aws-host_8000"].URL, ShouldEqual, "http://127.0.0.1:8000")
So(backs["api"].Servers["another-aws-host_9000"].URL, ShouldEqual, "http://127.0.0.1:9000")
// Fall back to the hostname if the service.Port does not contain the IP address
// and the hostname IP address can't be resolved
So(backs["api"].Servers["another-aws-host_9000"].URL, ShouldEqual, "http://another-aws-host:9000")

// Don't add servers for services that are not alive
So(backs["sso"].Servers, ShouldBeEmpty)
Expand Down

0 comments on commit 1062fec

Please sign in to comment.