Skip to content

Commit 85f89e3

Browse files
authored
Merge pull request #300 from allegro/multiple-names-under-one-port
Handle same port multiple service registration
2 parents 43df4c3 + 8d6e502 commit 85f89e3

File tree

5 files changed

+81
-7
lines changed

5 files changed

+81
-7
lines changed

.goxc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"deb-source"
99
],
1010
"BuildConstraints": "linux,!arm darwin",
11-
"PackageVersion": "1.5.2",
11+
"PackageVersion": "1.5.3",
1212
"TaskSettings": {
1313
"bintray": {
1414
"downloadspage": "bintray.md",

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ dist: trusty
33
sudo: false
44

55
go:
6-
- 1.x
6+
- 1.13
77
before_install:
88
- gem install fpm
99
- go get github.com/mattn/goveralls

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ $(TEST_TARGETS):
5252

5353
check-deps: deps
5454
@which golangci-lint > /dev/null || \
55-
(go get -u github.com/golangci/golangci-lint/cmd/golangci-lint)
55+
(GO111MODULE=on go get -v github.com/golangci/golangci-lint/cmd/golangci-lint@v1.25.0)
5656

57-
check: check-deps $(SOURCES)
57+
check: check-deps $(SOURCES) test
5858
golangci-lint run --config=golangcilinter.yaml ./...
5959

6060
format:

apps/app.go

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
// Only Marathon apps with this label will be registered in Consul
1212
const MarathonConsulLabel = "consul"
13+
const MarathonConsulTagValue = "tag"
1314

1415
type HealthCheck struct {
1516
Path string `json:"path"`
@@ -147,7 +148,7 @@ func marathonAppNameToServiceName(name string, nameSeparator string) string {
147148
func labelsToTags(labels map[string]string, tagPlaceholderMapping map[string]string) []string {
148149
tags := make([]string, 0, len(labels))
149150
for key, value := range labels {
150-
if value == "tag" {
151+
if value == MarathonConsulTagValue {
151152
tags = append(tags, resolvePlaceholders(key, tagPlaceholderMapping))
152153
}
153154
}
@@ -206,14 +207,45 @@ func (app App) extractIndexedPortDefinitions() []indexedPortDefinition {
206207
func (app App) filterConsulDefinitions(all []indexedPortDefinition) []indexedPortDefinition {
207208
var consulDefinitions []indexedPortDefinition
208209
for _, d := range all {
209-
if _, ok := d.Labels[MarathonConsulLabel]; ok {
210-
consulDefinitions = append(consulDefinitions, d)
210+
if labelName, ok := d.Labels[MarathonConsulLabel]; ok {
211+
multipleDefinitions := strings.Split(labelName, ",")
212+
213+
for _, name := range multipleDefinitions {
214+
labels := extractLabelsForService(name, d.Labels)
215+
consulDefinitions = append(consulDefinitions, indexedPortDefinition{
216+
Index: d.Index,
217+
Labels: labels,
218+
Name: name,
219+
})
220+
}
211221
}
212222
}
213223

214224
return consulDefinitions
215225
}
216226

227+
func extractLabelsForService(serviceName string, labels map[string]string) map[string]string {
228+
newLabels := make(map[string]string)
229+
230+
for key, value := range labels {
231+
valueAndSelector := strings.Split(value, ":")
232+
if len(valueAndSelector) > 1 {
233+
extractedValue := valueAndSelector[0]
234+
serviceSelector := valueAndSelector[1]
235+
236+
if extractedValue == MarathonConsulTagValue && serviceSelector == serviceName {
237+
newLabels[key] = extractedValue
238+
}
239+
} else if key == MarathonConsulLabel {
240+
newLabels[key] = serviceName
241+
} else {
242+
newLabels[key] = value
243+
}
244+
}
245+
246+
return newLabels
247+
}
248+
217249
// Deprecated: Allows for backward compatibility with Marathons' network API
218250
// PortDefinitions are deprecated in favor of Marathons' new PortMappings
219251
// see https://github.com/mesosphere/marathon/pull/5391

consul/consul_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,48 @@ func TestRegisterServices_CustomServiceName(t *testing.T) {
483483
assert.Equal(t, "myCustomServiceName", services[0].Name)
484484
}
485485

486+
func TestRegisterServices_MultipleRegistrationsWithSamePort(t *testing.T) {
487+
t.Parallel()
488+
server := CreateTestServer(t)
489+
defer server.Stop()
490+
491+
consul := ClientAtServer(server)
492+
consul.config.Tag = "marathon"
493+
494+
// given
495+
app := utils.ConsulApp("serviceA", 1)
496+
app.PortDefinitions = []apps.PortDefinition{
497+
{
498+
Labels: map[string]string{"consul": "first-name,second-name", "first-tag": "tag:first-name", "second-tag": "tag:second-name"},
499+
},
500+
}
501+
app.Tasks[0].Host = server.Config.Bind
502+
app.Tasks[0].Ports = []int{8080}
503+
app.Labels["common-tag"] = "tag"
504+
505+
// when
506+
err := consul.Register(&app.Tasks[0], app)
507+
508+
// then
509+
assert.NoError(t, err)
510+
511+
// when
512+
services, _ := consul.GetAllServices()
513+
514+
// then
515+
assert.Len(t, services, 2)
516+
517+
first, found := findServiceByName("first-name", services)
518+
assert.True(t, found, "first-name not found in services")
519+
second, found := findServiceByName("second-name", services)
520+
assert.True(t, found, "second-name not found in services")
521+
522+
assert.Equal(t, "first-name", first.Name)
523+
assert.Equal(t, []string{"marathon", "first-tag", "common-tag", "marathon-task:serviceA.0"}, first.Tags)
524+
assert.Equal(t, "second-name", second.Name)
525+
assert.Equal(t, []string{"marathon", "second-tag", "common-tag", "marathon-task:serviceA.0"}, second.Tags)
526+
}
527+
486528
func TestRegisterServices_MultipleRegistrations(t *testing.T) {
487529
t.Parallel()
488530
server := CreateTestServer(t)

0 commit comments

Comments
 (0)