Skip to content

Commit 9b31d52

Browse files
teutat3slucwillems
andcommitted
Support service meta data
TritonDataCenter#592 TritonDataCenter#593 All credits and thanks to Luc Willems Co-authored-by: Luc Willems <[email protected]>
1 parent ea436c7 commit 9b31d52

File tree

7 files changed

+65
-6
lines changed

7 files changed

+65
-6
lines changed

config/config_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ func TestValidConfigJobs(t *testing.T) {
3333
assert.Equal(job0.Port, 8080, "config for job0.Port")
3434
assert.Equal(job0.Exec, "/bin/serviceA", "config for job0.Exec")
3535
assert.Equal(job0.Tags, []string{"tag1", "tag2"}, "config for job0.Tags")
36+
assert.Equal(job0.Meta, map[string]string{"keyA": "A"}, "config for job0.Meta")
3637
assert.Equal(job0.Restarts, nil, "config for job1.Restarts")
3738

3839
job1 := cfg.Jobs[1]

config/testdata/test.json5

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@
1818
interval: 19,
1919
ttl: 30,
2020
},
21-
tags: ["tag1","tag2"]
21+
tags: ["tag1","tag2"],
22+
meta: {
23+
keyA: "A",
24+
}
2225
},
2326
{
2427
name: "serviceB",

discovery/consul_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ func TestWithConsul(t *testing.T) {
8484
t.Run("TestConsulReregister", testConsulReregister(testServer))
8585
t.Run("TestConsulCheckForChanges", testConsulCheckForChanges(testServer))
8686
t.Run("TestConsulEnableTagOverride", testConsulEnableTagOverride(testServer))
87+
t.Run("testConsulTagsMeta", testConsulTagsMeta(testServer))
8788
}
8889

8990
func testConsulTTLPass(testServer *TestServer) func(*testing.T) {
@@ -146,6 +147,39 @@ func testConsulReregister(testServer *TestServer) func(*testing.T) {
146147
}
147148
}
148149

150+
func contains(s []string, str string) bool {
151+
for _, v := range s {
152+
if v == str {
153+
return true
154+
}
155+
}
156+
157+
return false
158+
}
159+
160+
func testConsulTagsMeta(testServer *TestServer) func(*testing.T) {
161+
return func(t *testing.T) {
162+
consul, _ := NewConsul(testServer.HTTPAddr)
163+
name := "TestConsulReregister"
164+
service := generateServiceDefinition(name, consul)
165+
id := service.ID
166+
167+
service.SendHeartbeat() // force registration and 1st heartbeat
168+
services, _ := consul.Agent().Services()
169+
svc := services[id]
170+
if !contains(svc.Tags, "a") || !contains(svc.Tags, "b") {
171+
t.Fatalf("first tag must containt a & b but is %s", svc.Tags)
172+
}
173+
if svc.Meta["keyA"] != "A" {
174+
t.Fatalf("first meta must containt keyA:A but is %s", svc.Meta["keyA"])
175+
}
176+
if svc.Meta["keyB"] != "B" {
177+
t.Fatalf("first meta must containt keyB:B but is %s", svc.Meta["keyB"])
178+
}
179+
180+
}
181+
}
182+
149183
func testConsulCheckForChanges(testServer *TestServer) func(*testing.T) {
150184
return func(t *testing.T) {
151185
backend := "TestConsulCheckForChanges"
@@ -211,5 +245,10 @@ func generateServiceDefinition(serviceName string, consul *Consul) *ServiceDefin
211245
TTL: 5,
212246
Port: 9000,
213247
Consul: consul,
248+
Tags: []string{"a", "b"},
249+
Meta: map[string]string{
250+
"keyA": "A",
251+
"keyB": "B",
252+
},
214253
}
215254
}

discovery/service.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ type ServiceDefinition struct {
1515
Port int
1616
TTL int
1717
Tags []string
18+
Meta map[string]string
1819
InitialStatus string
1920
IPAddress string
2021
EnableTagOverride bool
@@ -93,6 +94,7 @@ func (service *ServiceDefinition) registerService(status string) error {
9394
ID: service.ID,
9495
Name: service.Name,
9596
Tags: service.Tags,
97+
Meta: service.Meta,
9698
Port: service.Port,
9799
Address: service.IPAddress,
98100
EnableTagOverride: service.EnableTagOverride,

docs/30-configuration/32-configuration-file.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ The following is a completed example of the JSON5 file configuration schema, wit
6060
"app",
6161
"prod"
6262
],
63+
meta: {
64+
keyA: "A",
65+
keyB: "B",
66+
},
6367
interfaces: [
6468
"eth0",
6569
"eth1[1]",

docs/30-configuration/34-jobs.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ jobs: [
8181
"app",
8282
"prod"
8383
],
84+
meta: {
85+
keyA: "A",
86+
keyB: "B",
87+
},
8488
interfaces: [
8589
"eth0",
8690
"eth1[1]",
@@ -241,6 +245,10 @@ The `initial_status` field is optional and specifies which status to immediately
241245

242246
The `tags` field is an optional array of tags to be used when the job is registered as a service in Consul. Other containers can use these tags in `watches` to filter a service by tag.
243247

248+
##### `meta`
249+
250+
The `meta` field is an optional map key/value to be used when the job is registered as a service in Consul. Key names must be valid JSON5/Ecmascript identifierNames or be quoted and follow consul limitation , practical this means only [a-zA-Z0-9_-] can be used in key names and key names with '-' must be quoted
251+
244252
##### `interfaces`
245253

246254
The `interfaces` field is an optional single or array of interface specifications. If given, the IP of the service will be obtained from the first interface specification that matches. (Default value is `["eth0:inet"]`). The value that ContainerPilot uses for the IP address of the interface will be set as an environment variable with the name `CONTAINERPILOT_{JOB}_IP`. See the [environment variables](./32-configuration-file.md#environment-variables) section.

jobs/config.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,12 @@ type Config struct {
2525
GID int `mapstructure:"gid"`
2626

2727
// service discovery
28-
Port int `mapstructure:"port"`
29-
InitialStatus string `mapstructure:"initial_status"`
30-
Interfaces interface{} `mapstructure:"interfaces"`
31-
Tags []string `mapstructure:"tags"`
32-
ConsulExtras *ConsulExtras `mapstructure:"consul"`
28+
Port int `mapstructure:"port"`
29+
InitialStatus string `mapstructure:"initial_status"`
30+
Interfaces interface{} `mapstructure:"interfaces"`
31+
Tags []string `mapstructure:"tags"`
32+
Meta map[string]string `mapstructure:"meta"`
33+
ConsulExtras *ConsulExtras `mapstructure:"consul"`
3334
serviceDefinition *discovery.ServiceDefinition
3435

3536
// health checking
@@ -434,6 +435,7 @@ func (cfg *Config) addDiscoveryConfig(disc discovery.Backend) error {
434435
Port: cfg.Port,
435436
TTL: cfg.ttl,
436437
Tags: cfg.Tags,
438+
Meta: cfg.Meta,
437439
InitialStatus: cfg.InitialStatus,
438440
IPAddress: ipAddress,
439441
DeregisterCriticalServiceAfter: deregAfter,

0 commit comments

Comments
 (0)