Merge pull request #3548 from whoward/fix-marathon-1-5

Parse the normalized container.PortMappings presented by the Marathon 1.5.x API
This commit is contained in:
Conor Broderick 2017-12-06 17:25:21 +00:00 committed by GitHub
commit 7417c60071
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 88 additions and 1 deletions

View File

@ -222,7 +222,8 @@ type DockerContainer struct {
// Container describes the runtime an app in running in.
type Container struct {
Docker DockerContainer `json:"docker"`
Docker DockerContainer `json:"docker"`
PortMappings []PortMappings `json:"portMappings"`
}
// PortDefinitions describes which load balancer port you should access to access the service.
@ -344,12 +345,23 @@ func targetsForApp(app *App) []model.LabelSet {
target[model.LabelName(ln)] = model.LabelValue(lv)
}
}
// Prior to Marathon 1.5 the port mappings could be found at the path
// "container.docker.portMappings". When support for Marathon 1.4
// is dropped then this section of code can be removed.
if i < len(app.Container.Docker.PortMappings) {
for ln, lv := range app.Container.Docker.PortMappings[i].Labels {
ln = portMappingLabelPrefix + strutil.SanitizeLabelName(ln)
target[model.LabelName(ln)] = model.LabelValue(lv)
}
}
// In Marathon 1.5.x the container.docker.portMappings object was moved
// to container.portMappings.
if i < len(app.Container.PortMappings) {
for ln, lv := range app.Container.PortMappings[i].Labels {
ln = portMappingLabelPrefix + strutil.SanitizeLabelName(ln)
target[model.LabelName(ln)] = model.LabelValue(lv)
}
}
targets = append(targets, target)
}
}

View File

@ -472,3 +472,78 @@ func TestMarathonSDSendGroupWithoutPortDefinitions(t *testing.T) {
t.Fatal("Did not get a target group.")
}
}
func marathonTestAppListWithContainerPortMappings(labels map[string]string, runningTasks int) *AppList {
var (
task = Task{
ID: "test-task-1",
Host: "mesos-slave1",
Ports: []uint32{31000, 32000},
}
docker = DockerContainer{
Image: "repo/image:tag",
}
container = Container{
Docker: docker,
PortMappings: []PortMappings{
{Labels: labels},
{Labels: make(map[string]string)},
},
}
app = App{
ID: "test-service",
Tasks: []Task{task},
RunningTasks: runningTasks,
Labels: labels,
Container: container,
}
)
return &AppList{
Apps: []App{app},
}
}
func TestMarathonSDSendGroupWithContainerPortMappings(t *testing.T) {
var (
ch = make(chan []*config.TargetGroup, 1)
client = func(client *http.Client, url, token string) (*AppList, error) {
return marathonTestAppListWithContainerPortMappings(marathonValidLabel, 1), nil
}
)
if err := testUpdateServices(client, ch); err != nil {
t.Fatalf("Got error: %s", err)
}
select {
case tgs := <-ch:
tg := tgs[0]
if tg.Source != "test-service" {
t.Fatalf("Wrong target group name: %s", tg.Source)
}
if len(tg.Targets) != 2 {
t.Fatalf("Wrong number of targets: %v", tg.Targets)
}
tgt := tg.Targets[0]
if tgt[model.AddressLabel] != "mesos-slave1:31000" {
t.Fatalf("Wrong target address: %s", tgt[model.AddressLabel])
}
if tgt[model.LabelName(portMappingLabelPrefix+"prometheus")] != "yes" {
t.Fatalf("Wrong first portMappings label from the first port: %s", tgt[model.AddressLabel])
}
if tgt[model.LabelName(portDefinitionLabelPrefix+"prometheus")] != "" {
t.Fatalf("Wrong first portDefinitions label from the first port: %s", tgt[model.AddressLabel])
}
tgt = tg.Targets[1]
if tgt[model.AddressLabel] != "mesos-slave1:32000" {
t.Fatalf("Wrong target address: %s", tgt[model.AddressLabel])
}
if tgt[model.LabelName(portMappingLabelPrefix+"prometheus")] != "" {
t.Fatalf("Wrong portMappings label from the second port: %s", tgt[model.AddressLabel])
}
if tgt[model.LabelName(portDefinitionLabelPrefix+"prometheus")] != "" {
t.Fatalf("Wrong portDefinitions label from the second port: %s", tgt[model.AddressLabel])
}
default:
t.Fatal("Did not get a target group.")
}
}