Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support for multiple tags #26

Merged
merged 8 commits into from
Apr 3, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions command/agent/agent_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion command/agent/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
4 changes: 2 additions & 2 deletions command/agent/catalog_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down Expand Up @@ -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{}
Expand Down
8 changes: 8 additions & 0 deletions command/agent/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,14 @@ func DecodeServiceDefinition(raw interface{}) (*ServiceDefinition, error) {
if !ok {
goto AFTER_FIX
}

// If no 'tags', handle the deprecated 'tag' value.
if _, ok := rawMap["tags"]; !ok {
if tag, ok := rawMap["tag"]; ok {
rawMap["tags"] = []interface{}{tag}
}
}

sub, ok = rawMap["check"]
if !ok {
goto AFTER_FIX
Expand Down
4 changes: 2 additions & 2 deletions command/agent/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}

Expand Down
18 changes: 9 additions & 9 deletions command/agent/dns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
}
Expand Down Expand Up @@ -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,
},
}
Expand All @@ -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,
},
}
Expand All @@ -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,
},
}
Expand Down Expand Up @@ -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,
},
}
Expand All @@ -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,
},
}
Expand All @@ -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,
},
}
Expand Down Expand Up @@ -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{
Expand All @@ -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{
Expand Down
8 changes: 4 additions & 4 deletions command/agent/local_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions command/agent/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
type ServiceDefinition struct {
ID string
Name string
Tag string
Tags []string
Port int
Check CheckType
}
Expand All @@ -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 != "" {
Expand Down
10 changes: 10 additions & 0 deletions command/agent/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
24 changes: 12 additions & 12 deletions consul/catalog_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestCatalogRegister(t *testing.T) {
Address: "127.0.0.1",
Service: &structs.NodeService{
Service: "db",
Tag: "master",
Tags: []string{"master"},
Port: 8000,
},
}
Expand Down Expand Up @@ -79,7 +79,7 @@ func TestCatalogRegister_ForwardLeader(t *testing.T) {
Address: "127.0.0.1",
Service: &structs.NodeService{
Service: "db",
Tag: "master",
Tags: []string{"master"},
Port: 8000,
},
}
Expand Down Expand Up @@ -116,7 +116,7 @@ func TestCatalogRegister_ForwardDC(t *testing.T) {
Address: "127.0.0.1",
Service: &structs.NodeService{
Service: "db",
Tag: "master",
Tags: []string{"master"},
Port: 8000,
},
}
Expand Down Expand Up @@ -277,7 +277,7 @@ func TestCatalogListServices(t *testing.T) {

// Just add a node
s1.fsm.State().EnsureNode(1, structs.Node{"foo", "127.0.0.1"})
s1.fsm.State().EnsureService(2, "foo", &structs.NodeService{"db", "db", "primary", 5000})
s1.fsm.State().EnsureService(2, "foo", &structs.NodeService{"db", "db", []string{"primary"}, 5000})

if err := client.Call("Catalog.ListServices", &args, &out); err != nil {
t.Fatalf("err: %v", err)
Expand All @@ -287,7 +287,7 @@ func TestCatalogListServices(t *testing.T) {
t.Fatalf("bad: %v", out)
}
// Consul service should auto-register
if len(out.Services["consul"]) != 1 {
if _, ok := out.Services["consul"]; !ok {
t.Fatalf("bad: %v", out)
}
if len(out.Services["db"]) != 1 {
Expand Down Expand Up @@ -327,7 +327,7 @@ func TestCatalogListServices_Blocking(t *testing.T) {
go func() {
time.Sleep(100 * time.Millisecond)
s1.fsm.State().EnsureNode(1, structs.Node{"foo", "127.0.0.1"})
s1.fsm.State().EnsureService(2, "foo", &structs.NodeService{"db", "db", "primary", 5000})
s1.fsm.State().EnsureService(2, "foo", &structs.NodeService{"db", "db", []string{"primary"}, 5000})
}()

// Re-run the query
Expand Down Expand Up @@ -418,7 +418,7 @@ func TestCatalogListServiceNodes(t *testing.T) {

// Just add a node
s1.fsm.State().EnsureNode(1, structs.Node{"foo", "127.0.0.1"})
s1.fsm.State().EnsureService(2, "foo", &structs.NodeService{"db", "db", "primary", 5000})
s1.fsm.State().EnsureService(2, "foo", &structs.NodeService{"db", "db", []string{"primary"}, 5000})

if err := client.Call("Catalog.ServiceNodes", &args, &out); err != nil {
t.Fatalf("err: %v", err)
Expand Down Expand Up @@ -462,8 +462,8 @@ func TestCatalogNodeServices(t *testing.T) {

// Just add a node
s1.fsm.State().EnsureNode(1, structs.Node{"foo", "127.0.0.1"})
s1.fsm.State().EnsureService(2, "foo", &structs.NodeService{"db", "db", "primary", 5000})
s1.fsm.State().EnsureService(3, "foo", &structs.NodeService{"web", "web", "", 80})
s1.fsm.State().EnsureService(2, "foo", &structs.NodeService{"db", "db", []string{"primary"}, 5000})
s1.fsm.State().EnsureService(3, "foo", &structs.NodeService{"web", "web", nil, 80})

if err := client.Call("Catalog.NodeServices", &args, &out); err != nil {
t.Fatalf("err: %v", err)
Expand All @@ -476,10 +476,10 @@ func TestCatalogNodeServices(t *testing.T) {
t.Fatalf("bad: %v", out)
}
services := out.NodeServices.Services
if services["db"].Tag != "primary" || services["db"].Port != 5000 {
if !strContains(services["db"].Tags, "primary") || services["db"].Port != 5000 {
t.Fatalf("bad: %v", out)
}
if services["web"].Tag != "" || services["web"].Port != 80 {
if services["web"].Tags != nil || services["web"].Port != 80 {
t.Fatalf("bad: %v", out)
}
}
Expand All @@ -498,7 +498,7 @@ func TestCatalogRegister_FailedCase1(t *testing.T) {
Address: "127.0.0.2",
Service: &structs.NodeService{
Service: "web",
Tag: "",
Tags: nil,
Port: 8000,
},
}
Expand Down
16 changes: 8 additions & 8 deletions consul/fsm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func TestFSM_RegisterNode_Service(t *testing.T) {
Service: &structs.NodeService{
ID: "db",
Service: "db",
Tag: "master",
Tags: []string{"master"},
Port: 8000,
},
Check: &structs.HealthCheck{
Expand Down Expand Up @@ -138,7 +138,7 @@ func TestFSM_DeregisterService(t *testing.T) {
Service: &structs.NodeService{
ID: "db",
Service: "db",
Tag: "master",
Tags: []string{"master"},
Port: 8000,
},
}
Expand Down Expand Up @@ -248,7 +248,7 @@ func TestFSM_DeregisterNode(t *testing.T) {
Service: &structs.NodeService{
ID: "db",
Service: "db",
Tag: "master",
Tags: []string{"master"},
Port: 8000,
},
Check: &structs.HealthCheck{
Expand Down Expand Up @@ -311,10 +311,10 @@ func TestFSM_SnapshotRestore(t *testing.T) {
// Add some state
fsm.state.EnsureNode(1, structs.Node{"foo", "127.0.0.1"})
fsm.state.EnsureNode(2, structs.Node{"baz", "127.0.0.2"})
fsm.state.EnsureService(3, "foo", &structs.NodeService{"web", "web", "", 80})
fsm.state.EnsureService(4, "foo", &structs.NodeService{"db", "db", "primary", 5000})
fsm.state.EnsureService(5, "baz", &structs.NodeService{"web", "web", "", 80})
fsm.state.EnsureService(6, "baz", &structs.NodeService{"db", "db", "secondary", 5000})
fsm.state.EnsureService(3, "foo", &structs.NodeService{"web", "web", nil, 80})
fsm.state.EnsureService(4, "foo", &structs.NodeService{"db", "db", []string{"primary"}, 5000})
fsm.state.EnsureService(5, "baz", &structs.NodeService{"web", "web", nil, 80})
fsm.state.EnsureService(6, "baz", &structs.NodeService{"db", "db", []string{"secondary"}, 5000})
fsm.state.EnsureCheck(7, &structs.HealthCheck{
Node: "foo",
CheckID: "web",
Expand Down Expand Up @@ -363,7 +363,7 @@ func TestFSM_SnapshotRestore(t *testing.T) {
if len(fooSrv.Services) != 2 {
t.Fatalf("Bad: %v", fooSrv)
}
if fooSrv.Services["db"].Tag != "primary" {
if !strContains(fooSrv.Services["db"].Tags, "primary") {
t.Fatalf("Bad: %v", fooSrv)
}
if fooSrv.Services["db"].Port != 5000 {
Expand Down
Loading