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 },