From c152cb7bdf685469a4ec70c75e230d9305f94f8c Mon Sep 17 00:00:00 2001
From: Pierre Souchay
Date: Sat, 21 Apr 2018 17:34:29 +0200
Subject: [PATCH] Added Missing Service Meta synchronization and field
---
agent/agent_endpoint.go | 13 +++++++++++--
agent/agent_endpoint_test.go | 8 ++++++--
agent/consul/acl.go | 1 +
api/agent.go | 1 +
api/agent_test.go | 5 ++++-
5 files changed, 23 insertions(+), 5 deletions(-)
diff --git a/agent/agent_endpoint.go b/agent/agent_endpoint.go
index 0e2b4b4803..0fa02b9738 100644
--- a/agent/agent_endpoint.go
+++ b/agent/agent_endpoint.go
@@ -131,9 +131,18 @@ func (s *HTTPServer) AgentServices(resp http.ResponseWriter, req *http.Request)
// Use empty list instead of nil
for id, s := range services {
- if s.Tags == nil {
+ if s.Tags == nil || s.Meta == nil {
clone := *s
- clone.Tags = make([]string, 0)
+ if s.Tags == nil {
+ clone.Tags = make([]string, 0)
+ } else {
+ clone.Tags = s.Tags
+ }
+ if s.Meta == nil {
+ clone.Meta = make(map[string]string)
+ } else {
+ clone.Meta = s.Meta
+ }
services[id] = &clone
}
}
diff --git a/agent/agent_endpoint_test.go b/agent/agent_endpoint_test.go
index 940304b548..c6e4f05324 100644
--- a/agent/agent_endpoint_test.go
+++ b/agent/agent_endpoint_test.go
@@ -1204,6 +1204,7 @@ func TestAgent_RegisterService(t *testing.T) {
args := &structs.ServiceDefinition{
Name: "test",
+ Meta: map[string]string{"hello": "world"},
Tags: []string{"master"},
Port: 8000,
Check: structs.CheckType{
@@ -1232,6 +1233,9 @@ func TestAgent_RegisterService(t *testing.T) {
if _, ok := a.State.Services()["test"]; !ok {
t.Fatalf("missing test service")
}
+ if val := a.State.Service("test").Meta["hello"]; val != "world" {
+ t.Fatalf("Missing meta: %v", a.State.Service("test").Meta)
+ }
// Ensure we have a check mapping
checks := a.State.Checks()
@@ -1254,7 +1258,7 @@ func TestAgent_RegisterService_TranslateKeys(t *testing.T) {
a := NewTestAgent(t.Name(), "")
defer a.Shutdown()
- json := `{"name":"test", "port":8000, "enable_tag_override": true}`
+ json := `{"name":"test", "port":8000, "enable_tag_override": true, "meta": {"some": "meta"}}`
req, _ := http.NewRequest("PUT", "/v1/agent/service/register", strings.NewReader(json))
obj, err := a.srv.AgentRegisterService(nil, req)
@@ -1264,10 +1268,10 @@ func TestAgent_RegisterService_TranslateKeys(t *testing.T) {
if obj != nil {
t.Fatalf("bad: %v", obj)
}
-
svc := &structs.NodeService{
ID: "test",
Service: "test",
+ Meta: map[string]string{"some": "meta"},
Port: 8000,
EnableTagOverride: true,
}
diff --git a/agent/consul/acl.go b/agent/consul/acl.go
index bea2a5617d..eb14e1dabc 100644
--- a/agent/consul/acl.go
+++ b/agent/consul/acl.go
@@ -678,6 +678,7 @@ func vetRegisterWithACL(rule acl.ACL, subj *structs.RegisterRequest,
ID: subj.Service.ID,
Service: subj.Service.Service,
Tags: subj.Service.Tags,
+ Meta: subj.Service.Meta,
Address: subj.Service.Address,
Port: subj.Service.Port,
EnableTagOverride: subj.Service.EnableTagOverride,
diff --git a/api/agent.go b/api/agent.go
index b42baed41d..772948ad1e 100644
--- a/api/agent.go
+++ b/api/agent.go
@@ -23,6 +23,7 @@ type AgentService struct {
ID string
Service string
Tags []string
+ Meta map[string]string
Port int
Address string
EnableTagOverride bool
diff --git a/api/agent_test.go b/api/agent_test.go
index e5ccdb0ef2..a05c4faf1a 100644
--- a/api/agent_test.go
+++ b/api/agent_test.go
@@ -73,7 +73,7 @@ func TestAPI_AgentReload(t *testing.T) {
agent := c.Agent()
// Update the config file with a service definition
- config := `{"service":{"name":"redis", "port":1234}}`
+ config := `{"service":{"name":"redis", "port":1234, "Meta": {"some": "meta"}}}`
err = ioutil.WriteFile(configFile.Name(), []byte(config), 0644)
if err != nil {
t.Fatalf("err: %v", err)
@@ -95,6 +95,9 @@ func TestAPI_AgentReload(t *testing.T) {
if service.Port != 1234 {
t.Fatalf("bad: %v", service.Port)
}
+ if service.Meta["some"] != "meta" {
+ t.Fatalf("Missing metadata some:=meta in %v", service)
+ }
}
func TestAPI_AgentMembersOpts(t *testing.T) {