From 8db2e3bd5844cd7dcc7b0c80a3eb1168f6e78575 Mon Sep 17 00:00:00 2001 From: Armon Dadgar Date: Thu, 3 Apr 2014 12:12:23 -0700 Subject: [PATCH] agent: Support multiple tags per service registration --- command/agent/agent_endpoint_test.go | 4 ++-- command/agent/agent_test.go | 2 +- command/agent/catalog_endpoint_test.go | 4 ++-- command/agent/config_test.go | 4 ++-- command/agent/dns_test.go | 18 +++++++++--------- command/agent/local_test.go | 8 ++++---- command/agent/structs.go | 4 ++-- command/agent/util.go | 10 ++++++++++ 8 files changed, 32 insertions(+), 22 deletions(-) diff --git a/command/agent/agent_endpoint_test.go b/command/agent/agent_endpoint_test.go index 14a07ea2cb..088e42ee5a 100644 --- a/command/agent/agent_endpoint_test.go +++ b/command/agent/agent_endpoint_test.go @@ -19,7 +19,7 @@ func TestHTTPAgentServices(t *testing.T) { srv1 := &structs.NodeService{ ID: "mysql", Service: "mysql", - Tag: "master", + Tags: []string{"master"}, Port: 5000, } srv.agent.state.AddService(srv1) @@ -394,7 +394,7 @@ func TestHTTPAgentRegisterService(t *testing.T) { } args := &ServiceDefinition{ Name: "test", - Tag: "master", + Tags: []string{"master"}, Port: 8000, Check: CheckType{ TTL: 15 * time.Second, diff --git a/command/agent/agent_test.go b/command/agent/agent_test.go index 8dde02690b..7d59d420e2 100644 --- a/command/agent/agent_test.go +++ b/command/agent/agent_test.go @@ -108,7 +108,7 @@ func TestAgent_AddService(t *testing.T) { srv := &structs.NodeService{ ID: "redis", Service: "redis", - Tag: "foo", + Tags: []string{"foo"}, Port: 8000, } chk := &CheckType{TTL: time.Minute} diff --git a/command/agent/catalog_endpoint_test.go b/command/agent/catalog_endpoint_test.go index 03928aaf93..573ddf8863 100644 --- a/command/agent/catalog_endpoint_test.go +++ b/command/agent/catalog_endpoint_test.go @@ -249,7 +249,7 @@ func TestCatalogServiceNodes(t *testing.T) { Address: "127.0.0.1", Service: &structs.NodeService{ Service: "api", - Tag: "a", + Tags: []string{"a"}, }, } var out struct{} @@ -293,7 +293,7 @@ func TestCatalogNodeServices(t *testing.T) { Address: "127.0.0.1", Service: &structs.NodeService{ Service: "api", - Tag: "a", + Tags: []string{"a"}, }, } var out struct{} diff --git a/command/agent/config_test.go b/command/agent/config_test.go index 878e61a0e7..d6d3f6eaa2 100644 --- a/command/agent/config_test.go +++ b/command/agent/config_test.go @@ -197,7 +197,7 @@ func TestDecodeConfig(t *testing.T) { func TestDecodeConfig_Service(t *testing.T) { // Basics - input := `{"service": {"id": "red1", "name": "redis", "tag": "master", "port":8000, "check": {"script": "/bin/check_redis", "interval": "10s", "ttl": "15s" }}}` + input := `{"service": {"id": "red1", "name": "redis", "tags": ["master"], "port":8000, "check": {"script": "/bin/check_redis", "interval": "10s", "ttl": "15s" }}}` config, err := DecodeConfig(bytes.NewReader([]byte(input))) if err != nil { t.Fatalf("err: %s", err) @@ -216,7 +216,7 @@ func TestDecodeConfig_Service(t *testing.T) { t.Fatalf("bad: %v", serv) } - if serv.Tag != "master" { + if !strContains(serv.Tags, "master") { t.Fatalf("bad: %v", serv) } diff --git a/command/agent/dns_test.go b/command/agent/dns_test.go index 68592923d1..e065ea4a46 100644 --- a/command/agent/dns_test.go +++ b/command/agent/dns_test.go @@ -219,7 +219,7 @@ func TestDNS_ServiceLookup(t *testing.T) { Address: "127.0.0.1", Service: &structs.NodeService{ Service: "db", - Tag: "master", + Tags: []string{"master"}, Port: 12345, }, } @@ -279,7 +279,7 @@ func TestDNS_ServiceLookup_Dedup(t *testing.T) { Address: "127.0.0.1", Service: &structs.NodeService{ Service: "db", - Tag: "master", + Tags: []string{"master"}, Port: 12345, }, } @@ -295,7 +295,7 @@ func TestDNS_ServiceLookup_Dedup(t *testing.T) { Service: &structs.NodeService{ ID: "db2", Service: "db", - Tag: "slave", + Tags: []string{"slave"}, Port: 12345, }, } @@ -310,7 +310,7 @@ func TestDNS_ServiceLookup_Dedup(t *testing.T) { Service: &structs.NodeService{ ID: "db3", Service: "db", - Tag: "slave", + Tags: []string{"slave"}, Port: 12346, }, } @@ -355,7 +355,7 @@ func TestDNS_ServiceLookup_Dedup_SRV(t *testing.T) { Address: "127.0.0.1", Service: &structs.NodeService{ Service: "db", - Tag: "master", + Tags: []string{"master"}, Port: 12345, }, } @@ -371,7 +371,7 @@ func TestDNS_ServiceLookup_Dedup_SRV(t *testing.T) { Service: &structs.NodeService{ ID: "db2", Service: "db", - Tag: "slave", + Tags: []string{"slave"}, Port: 12345, }, } @@ -386,7 +386,7 @@ func TestDNS_ServiceLookup_Dedup_SRV(t *testing.T) { Service: &structs.NodeService{ ID: "db3", Service: "db", - Tag: "slave", + Tags: []string{"slave"}, Port: 12346, }, } @@ -482,7 +482,7 @@ func TestDNS_ServiceLookup_FilterCritical(t *testing.T) { Address: "127.0.0.1", Service: &structs.NodeService{ Service: "db", - Tag: "master", + Tags: []string{"master"}, Port: 12345, }, Check: &structs.HealthCheck{ @@ -502,7 +502,7 @@ func TestDNS_ServiceLookup_FilterCritical(t *testing.T) { Address: "127.0.0.2", Service: &structs.NodeService{ Service: "db", - Tag: "master", + Tags: []string{"master"}, Port: 12345, }, Check: &structs.HealthCheck{ diff --git a/command/agent/local_test.go b/command/agent/local_test.go index be6bdbbebb..a84c1a2929 100644 --- a/command/agent/local_test.go +++ b/command/agent/local_test.go @@ -29,7 +29,7 @@ func TestAgentAntiEntropy_Services(t *testing.T) { srv1 := &structs.NodeService{ ID: "mysql", Service: "mysql", - Tag: "master", + Tags: []string{"master"}, Port: 5000, } agent.state.AddService(srv1) @@ -42,7 +42,7 @@ func TestAgentAntiEntropy_Services(t *testing.T) { srv2 := &structs.NodeService{ ID: "redis", Service: "redis", - Tag: "", + Tags: nil, Port: 8000, } agent.state.AddService(srv2) @@ -59,7 +59,7 @@ func TestAgentAntiEntropy_Services(t *testing.T) { srv3 := &structs.NodeService{ ID: "web", Service: "web", - Tag: "", + Tags: nil, Port: 80, } agent.state.AddService(srv3) @@ -68,7 +68,7 @@ func TestAgentAntiEntropy_Services(t *testing.T) { srv4 := &structs.NodeService{ ID: "lb", Service: "lb", - Tag: "", + Tags: nil, Port: 443, } args.Service = srv4 diff --git a/command/agent/structs.go b/command/agent/structs.go index 876ce4f8d8..2370c3936a 100644 --- a/command/agent/structs.go +++ b/command/agent/structs.go @@ -8,7 +8,7 @@ import ( type ServiceDefinition struct { ID string Name string - Tag string + Tags []string Port int Check CheckType } @@ -17,7 +17,7 @@ func (s *ServiceDefinition) NodeService() *structs.NodeService { ns := &structs.NodeService{ ID: s.ID, Service: s.Name, - Tag: s.Tag, + Tags: s.Tags, Port: s.Port, } if ns.ID == "" && ns.Service != "" { diff --git a/command/agent/util.go b/command/agent/util.go index f6ce7747e9..8f6103a804 100644 --- a/command/agent/util.go +++ b/command/agent/util.go @@ -29,3 +29,13 @@ func aeScale(interval time.Duration, n int) time.Duration { func randomStagger(intv time.Duration) time.Duration { return time.Duration(uint64(rand.Int63()) % uint64(intv)) } + +// strContains checks if a list contains a string +func strContains(l []string, s string) bool { + for _, v := range l { + if v == s { + return true + } + } + return false +}