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

Use service server IPs from Sidecar #11

Merged
merged 8 commits into from
May 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion glide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -180,4 +180,10 @@ import:
- package: github.com/jtolds/gls
version: 9a4a02dbe491bef4bab3c24fd9f3087d6c4c6690
- package: github.com/Nitro/sidecar
version: 2f5d357f0dce6d8f30acb1c5c281ba5a7075d4f7
version: 56cb7f1a269721cf7d9bbb0f4cc490913e0e7a7b
- package: github.com/pquerna/ffjson
version: f60b597ded7e7542b36fe20999dfd48166abd5ae
subpackages:
- ffjson
- fflib/v1
- fflib/v1/internal
29 changes: 19 additions & 10 deletions provider/sidecar.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package provider

import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -266,17 +266,26 @@ func (provider *Sidecar) makeBackends(sidecarStates *catalog.ServicesState) map[
}

if svc.IsAlive() {
for i := 0; i < len(svc.Ports); i++ {
ipAddr, err := net.LookupIP(svc.Hostname)
if err != nil {
log.Errorln("Error resolving Ip address, ", err)
backend.Servers[svc.Hostname] = types.Server{
URL: "http://" + svc.Hostname + ":" + strconv.FormatInt(svc.Ports[i].Port, 10),
for _, port := range svc.Ports {
var host string
parsedIP := net.ParseIP(port.IP)
if parsedIP == nil {
// Try to resolve the hostname if we don't get a valid IP from Sidecar
ipAddr, err := net.LookupIP(svc.Hostname)
if err != nil {
log.Errorf("Error resolving IP address for host '%s': %s", svc.Hostname, err)
continue
} else {
host = ipAddr[0].String()
}
} else {
backend.Servers[svc.Hostname] = types.Server{
URL: "http://" + ipAddr[0].String() + ":" + strconv.FormatInt(svc.Ports[i].Port, 10),
}
host = parsedIP.String()
}

// A service can expose multiple ports on the same host
name := fmt.Sprintf("%s_%d", svc.Hostname, port.Port)
backend.Servers[name] = types.Server{
URL: fmt.Sprintf("http://%s:%d", host, port.Port),
}
}
}
Expand Down
129 changes: 107 additions & 22 deletions provider/sidecar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@ func TestSidecar(t *testing.T) {
Hostname: "some-aws-host",
Status: 0,
Ports: []service.Port{
service.Port{
{
Type: "tcp",
Port: 8000,
ServicePort: 8000,
IP: "127.0.0.1",
},
},
},
Expand Down Expand Up @@ -87,27 +88,108 @@ func TestSidecar(t *testing.T) {
Endpoint: "http://some.dummy.service",
}

dummyState.AddServiceEntry(
service.Service{
ID: "008",
Name: "api",
Hostname: "another-aws-host",
Status: 1,
},
)
Convey("and add servers for services with a single port exposed", func() {
states, err := prov.fetchState()
So(err, ShouldBeNil)

states, err := prov.fetchState()
So(err, ShouldBeNil)
backs := prov.makeBackends(states)

So(backs, ShouldContainKey, "web")

So(backs["web"].LoadBalancer.Method, ShouldEqual, "wrr")
So(backs["web"].Servers["some-aws-host_8000"].URL, ShouldEqual, "http://127.0.0.1:8000")
})

backs := prov.makeBackends(states)
Convey("and add servers for services with multiple ports exposed", func() {
dummyState.AddServiceEntry(
service.Service{
ID: "008",
Name: "api",
Hostname: "another-aws-host",
Status: 0,
Ports: []service.Port{
{
Type: "tcp",
Port: 8000,
ServicePort: 8000,
IP: "127.0.0.1",
},
{
Type: "udp",
Port: 9000,
ServicePort: 9000,
IP: "127.0.0.1",
},
},
},
)

So(backs["web"].LoadBalancer.Method, ShouldEqual, "wrr")
So(backs["web"].Servers["some-aws-host"].URL, ShouldEqual, "http://some-aws-host:8000")
So(backs["api"].Servers["another-aws-host"], ShouldBeZeroValue)
states, err := prov.fetchState()
So(err, ShouldBeNil)

So(backs["web"].MaxConn, ShouldBeNil)
So(backs["api"].MaxConn, ShouldBeNil)
backs := prov.makeBackends(states)

So(backs, ShouldContainKey, "api")

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")
})

Convey("and not add servers for which the IP cannot be obtained", func() {
dummyState.AddServiceEntry(
service.Service{
ID: "008",
Name: "api",
Hostname: "another-aws-host",
Status: 0,
Ports: []service.Port{
{
Type: "tcp",
Port: 8000,
ServicePort: 8000,
},
},
},
)

states, err := prov.fetchState()
So(err, ShouldBeNil)

backs := prov.makeBackends(states)

So(backs, ShouldContainKey, "api")

// Don't add the server if the service.Port does not contain the IP address
// and the hostname IP address can't be resolved
So(backs["api"].Servers, ShouldNotContainKey, "another-aws-host_8000")
})

Convey("and not add servers for services that are not alive", func() {
dummyState.AddServiceEntry(
service.Service{
ID: "009",
Name: "sso",
Hostname: "yet-another-aws-host",
Status: 1,
Ports: []service.Port{
{
Type: "tcp",
Port: 8000,
ServicePort: 8000,
IP: "127.0.0.1",
},
},
},
)

states, err := prov.fetchState()
So(err, ShouldBeNil)

backs := prov.makeBackends(states)

So(backs, ShouldContainKey, "sso")
So(backs["sso"].Servers, ShouldBeEmpty)
})
})

Convey("construct config", func() {
Expand All @@ -123,7 +205,7 @@ func TestSidecar(t *testing.T) {
ID: "008",
Name: "sso",
Hostname: "yet-another-aws-host",
Status: 1,
Status: 0,
},
)

Expand All @@ -142,6 +224,8 @@ func TestSidecar(t *testing.T) {
Convey("and set maxconn values", func() {
So(config.Backends["web"].MaxConn.Amount, ShouldEqual, 10)
So(config.Backends["web"].MaxConn.ExtractorFunc, ShouldEqual, "client.ip")

// Don't add any connection limits if a backend is not assigned to any frontend
So(config.Backends["sso"].MaxConn, ShouldBeNil)
})

Expand Down Expand Up @@ -212,7 +296,7 @@ func TestSidecar(t *testing.T) {
So(err, ShouldBeNil)
So(configMsg.ProviderName, ShouldEqual, "sidecar")
So(configMsg.Configuration.Frontends["web"].Routes["test_1"].Rule, ShouldEqual, "Host: some-aws-host")
So(configMsg.Configuration.Backends["web"].Servers["some-aws-host"].URL, ShouldEqual, "http://some-aws-host:8000")
So(configMsg.Configuration.Backends["web"].Servers["some-aws-host_8000"].URL, ShouldEqual, "http://127.0.0.1:8000")
})

Convey("run Provide() in watcher mode", func(c C) {
Expand Down Expand Up @@ -261,7 +345,7 @@ func TestSidecar(t *testing.T) {

So(configMsg.ProviderName, ShouldEqual, "sidecar")
So(configMsg.Configuration.Frontends["web"].Routes["test_1"].Rule, ShouldEqual, "Host: some-aws-host")
So(configMsg.Configuration.Backends["web"].Servers["some-aws-host"].URL, ShouldEqual, "http://some-aws-host:8000")
So(configMsg.Configuration.Backends["web"].Servers["some-aws-host_8000"].URL, ShouldEqual, "http://127.0.0.1:8000")

dummyState.AddServiceEntry(
service.Service{
Expand All @@ -270,10 +354,11 @@ func TestSidecar(t *testing.T) {
Hostname: "another-aws-host",
Status: 0,
Ports: []service.Port{
service.Port{
{
Type: "tcp",
Port: 9000,
ServicePort: 9000,
IP: "169.254.1.1",
},
},
},
Expand All @@ -284,7 +369,7 @@ func TestSidecar(t *testing.T) {
configMsg = <-configMsgChan

So(configMsg.Configuration.Backends, ShouldContainKey, "api")
So(configMsg.Configuration.Backends["api"].Servers["another-aws-host"].URL, ShouldEqual, "http://another-aws-host:9000")
So(configMsg.Configuration.Backends["api"].Servers["another-aws-host_9000"].URL, ShouldEqual, "http://169.254.1.1:9000")

})

Expand Down
7 changes: 4 additions & 3 deletions vendor/github.com/Nitro/sidecar/catalog/services_state.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading