From d2ab3deacfb0aa1983fd04c6cee05e354be28580 Mon Sep 17 00:00:00 2001
From: Pierre Souchay
Date: Wed, 18 Apr 2018 22:18:58 +0200
Subject: [PATCH] =?UTF-8?q?[BUGFIX]=C2=A0Added=20Service=20Meta=20support?=
=?UTF-8?q?=20in=20configuration=20files?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Fixes https://github.com/hashicorp/consul/issues/4045
Was not added by mistake in https://github.com/hashicorp/consul/pull/3881
---
agent/config/builder.go | 7 +++++++
agent/config/config.go | 1 +
agent/config/runtime_test.go | 8 +++++---
3 files changed, 13 insertions(+), 3 deletions(-)
diff --git a/agent/config/builder.go b/agent/config/builder.go
index e9cb19394c..7b99b49602 100644
--- a/agent/config/builder.go
+++ b/agent/config/builder.go
@@ -997,11 +997,18 @@ func (b *Builder) serviceVal(v *ServiceDefinition) *structs.ServiceDefinition {
checks = append(checks, b.checkVal(v.Check).CheckType())
}
+ meta := make(map[string]string)
+ if err := structs.ValidateMetadata(v.Meta, false); err != nil {
+ b.err = multierror.Append(fmt.Errorf("invalid meta for service %v: %v", v.Name, err))
+ } else {
+ meta = v.Meta
+ }
return &structs.ServiceDefinition{
ID: b.stringVal(v.ID),
Name: b.stringVal(v.Name),
Tags: v.Tags,
Address: b.stringVal(v.Address),
+ Meta: meta,
Port: b.intVal(v.Port),
Token: b.stringVal(v.Token),
EnableTagOverride: b.boolVal(v.EnableTagOverride),
diff --git a/agent/config/config.go b/agent/config/config.go
index 2a14abc6b8..6b8ba113a5 100644
--- a/agent/config/config.go
+++ b/agent/config/config.go
@@ -319,6 +319,7 @@ type ServiceDefinition struct {
Name *string `json:"name,omitempty" hcl:"name" mapstructure:"name"`
Tags []string `json:"tags,omitempty" hcl:"tags" mapstructure:"tags"`
Address *string `json:"address,omitempty" hcl:"address" mapstructure:"address"`
+ Meta map[string]string `json:"node_meta,omitempty" hcl:"meta" mapstructure:"meta"`
Port *int `json:"port,omitempty" hcl:"port" mapstructure:"port"`
Check *CheckDefinition `json:"check,omitempty" hcl:"check" mapstructure:"check"`
Checks []CheckDefinition `json:"checks,omitempty" hcl:"checks" mapstructure:"checks"`
diff --git a/agent/config/runtime_test.go b/agent/config/runtime_test.go
index da62e0339c..511ebc4a80 100644
--- a/agent/config/runtime_test.go
+++ b/agent/config/runtime_test.go
@@ -49,6 +49,8 @@ func TestConfigFlagsAndEdgecases(t *testing.T) {
dataDir := testutil.TempDir(t, "consul")
defer os.RemoveAll(dataDir)
+ metaVal := make(map[string]string)
+ metaVal["my"] = "value"
tests := []configTest{
// ------------------------------------------------------------
// cmd line flags
@@ -1923,16 +1925,16 @@ func TestConfigFlagsAndEdgecases(t *testing.T) {
},
json: []string{
`{ "service": { "name": "a", "port": 80 } }`,
- `{ "service": { "name": "b", "port": 90 } }`,
+ `{ "service": { "name": "b", "port": 90, "meta": {"my": "value"} } }`,
},
hcl: []string{
`service = { name = "a" port = 80 }`,
- `service = { name = "b" port = 90 }`,
+ `service = { name = "b" port = 90 meta={my="value"}}`,
},
patch: func(rt *RuntimeConfig) {
rt.Services = []*structs.ServiceDefinition{
&structs.ServiceDefinition{Name: "a", Port: 80},
- &structs.ServiceDefinition{Name: "b", Port: 90},
+ &structs.ServiceDefinition{Name: "b", Port: 90, Meta: metaVal},
}
rt.DataDir = dataDir
},