Skip to content
This repository has been archived by the owner on Oct 23, 2024. It is now read-only.

Commit

Permalink
Added logic to read NetworkInfo from /state.json.
Browse files Browse the repository at this point in the history
  • Loading branch information
ConnorDoyle committed Sep 16, 2015
1 parent 3003c57 commit f853919
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 4 deletions.
32 changes: 28 additions & 4 deletions records/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,22 @@ type Label struct {

// Status holds a task status as defined in the /state.json Mesos HTTP endpoint.
type Status struct {
Timestamp float64 `json:"timestamp"`
State string `json:"state"`
Labels []Label `json:"labels,omitempty"`
Timestamp float64 `json:"timestamp"`
State string `json:"state"`
Labels []Label `json:"labels,omitempty"`
ContainerStatus ContainerStatus `json:"container_status"`
}

// ContainerStatus holds container metadata as defined in the /state.json
// Mesos HTTP endpoint.
type ContainerStatus struct {
NetworkInfos []NetworkInfo `json:"network_infos"`
}

// NetworkInfo holds a network address resolution as defined in the
// /state.json Mesos HTTP endpoint.
type NetworkInfo struct {
IpAddress string `json:"ip_address"`
}

// Task holds a task as defined in the /state.json Mesos HTTP endpoint.
Expand Down Expand Up @@ -103,7 +116,18 @@ func (t *Task) containerIP(src string) string {
continue
}

// find the latest docker-inspect label
// first try to extract the address from the NetworkInfo structure
if len(status.ContainerStatus.NetworkInfos) > 0 {
// TODO(CD): handle multiple NetworkInfo objects
// TODO(CD): only create A records if the address is IPv4
// TODO(CD): create AAAA records if the address is IPv6
latestContainerIP = status.ContainerStatus.NetworkInfos[0].IpAddress
latestTimestamp = status.Timestamp
break
}

// next, fall back to the docker-inspect label
// TODO(CD): deprecate and then remove this address discovery method
for _, label := range status.Labels {
if label.Key == ipLabel {
latestContainerIP = label.Value
Expand Down
60 changes: 60 additions & 0 deletions records/state/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,63 @@ func TestPID_UnmarshalJSON(t *testing.T) {
}
}
}

func TestTask_containerIP(t *testing.T) {
makeTask := func(networkInfoAddress string, label Label) Task {
labels := make([]Label, 0, 1)
if label.Key != "" {
labels = append(labels, label)
}

var containerStatus ContainerStatus
if networkInfoAddress != "" {
containerStatus = ContainerStatus{
NetworkInfos: []NetworkInfo{
NetworkInfo{
IpAddress: networkInfoAddress,
},
},
}
}

return Task{
State: "TASK_RUNNING",
Statuses: []Status{
Status{
Timestamp: 1.0,
State: "TASK_RUNNING",
Labels: labels,
ContainerStatus: containerStatus,
},
},
}
}

// Verify IP extraction from NetworkInfo
task := makeTask("1.2.3.4", Label{})
if task.containerIP("mesos") != "1.2.3.4" {
t.Errorf("Failed to extract IP from NetworkInfo")
}

// Verify IP extraction from NetworkInfo takes precedence over
// labels
task = makeTask("1.2.3.4",
Label{Key: ipLabels["mesos"], Value: "2.4.6.8"})
if task.containerIP("mesos") != "1.2.3.4" {
t.Errorf("Failed to extract IP from NetworkInfo when label also supplied")
}

// Verify IP extraction from the Mesos label without NetworkInfo
task = makeTask("",
Label{Key: ipLabels["mesos"], Value: "1.2.3.4"})
if task.containerIP("mesos") != "1.2.3.4" {
t.Errorf("Failed to extract IP from Mesos label")
}

// Verify IP extraction from the Docker label without NetworkInfo
task = makeTask("",
Label{Key: ipLabels["docker"], Value: "1.2.3.4"})
if task.containerIP("docker") != "1.2.3.4" {
t.Errorf("Failed to extract IP from Docker label")
}
}

0 comments on commit f853919

Please sign in to comment.