Skip to content

Commit

Permalink
Merge pull request #237 from Willbern/will-macvlan
Browse files Browse the repository at this point in the history
add ipAddress to support docker network like macvlan
  • Loading branch information
Jing Dong committed Jun 29, 2017
2 parents 7e7071d + b45d07c commit 31cb7bf
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 4 deletions.
40 changes: 38 additions & 2 deletions services/marathon/marathon.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,26 @@ type App struct {
Env map[string]string
Labels map[string]string
SplitId []string
IpAddress AppIpAddress `json:"ipAddress"`
}

type AppIpAddress struct {
Discovery Discovery `json:"discovery"`
}

type Discovery struct {
Ports []Port `json:"ports"`
}

type Port struct {
Number int `json:"number"`
Name string `json:"name"`
Protocol string `json:"protocol"`
}

type TaskIpAddress struct {
IpAddress string `json:"ipAddress"`
Protocol string `json:"protocol"`
}

type AppList []App
Expand Down Expand Up @@ -79,6 +99,7 @@ type marathonTask struct {
StartedAt string
StagedAt string
Version string
IpAddresses []TaskIpAddress `json:"IpAddresses"`
HealthCheckResults []HealthCheckResult
}

Expand Down Expand Up @@ -110,6 +131,7 @@ type marathonApp struct {
Tasks marathonTaskList `json:"tasks"`
ReadinessChecks []marathonReadinessCheck `json:"readinessChecks"`
ReadinessCheckResults []readinessCheckResult `json:"readinessCheckResults"`
IpAddress AppIpAddress `json:"ipAddress"`
}

type marathonHealthCheck struct {
Expand Down Expand Up @@ -216,6 +238,7 @@ func createApps(marathonApps []marathonApp) AppList {
Env: mApp.Env,
Labels: mApp.Labels,
SplitId: strings.Split(appId, "/"),
IpAddress: mApp.IpAddress,
}

app.HealthChecks = make([]HealthCheck, 0, len(mApp.HealthChecks))
Expand All @@ -236,11 +259,24 @@ func createApps(marathonApps []marathonApp) AppList {
// build Tasks for this App
tasks := []Task{}
for _, mTask := range mApp.Tasks {
var host string
var port int
if len(mTask.Ports) > 0 {
host = mTask.Host
port = mTask.Ports[0]
}
if len(app.IpAddress.Discovery.Ports) > 0 {
if len(mTask.IpAddresses) > 0 {
host = mTask.IpAddresses[0].IpAddress
port = app.IpAddress.Discovery.Ports[0].Number
}
}

if host != "" && port != 0 {
t := Task{
Id: mTask.Id,
Host: mTask.Host,
Port: mTask.Ports[0],
Host: host,
Port: port,
Ports: mTask.Ports,
Alive: calculateTaskHealth(mTask.HealthCheckResults, mApp.HealthChecks),
State: mTask.State,
Expand Down
45 changes: 43 additions & 2 deletions services/marathon/marathon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,12 @@ func TestFetchApps(t *testing.T) {
"id": "/app2WithSlash",
"tasks": [
{
"host":"localhost",
"id": "task2",
"ports": [8002]
},
{
"host":"localhost",
"id": "task1",
"ports": [8001]
}
Expand All @@ -202,14 +204,48 @@ func TestFetchApps(t *testing.T) {
"id": "app1WithoutSlash",
"tasks": [
{
"host":"localhost",
"id": "task1",
"ports": [8001]
},
{
"host":"localhost",
"id": "task2",
"ports": [8002]
}
]
},
{
"id": "app3WithIpAddress",
"ipAddress": {
"discovery":{
"ports": [
{
"number": 80
}
]
}
},
"tasks": [
{
"host":"localhost",
"ipAddresses":[
{
"ipAddress": "127.0.0.1"
}
],
"id": "task1"
},
{
"host":"localhost",
"ipAddresses":[
{
"ipAddress": "127.0.0.1"
}
],
"id": "task2"
}
]
}
]
}`)
Expand Down Expand Up @@ -237,8 +273,13 @@ func TestFetchApps(t *testing.T) {
}
assertFetchedApp(t, 2, "/app2WithSlash", apps[1])

if len(apps) > 2 {
t.Fatalf("got %d apps, want 2", len(apps))
if len(apps) < 3 {
t.Fatal("missing third app")
}
assertFetchedApp(t, 3, "/app3WithIpAddress", apps[2])

if len(apps) > 3 {
t.Fatalf("got %d apps, want 3", len(apps))
}
}

Expand Down

0 comments on commit 31cb7bf

Please sign in to comment.