Merge pull request #400 from hashicorp/f-service-filter

agent: Register "consul" service in agent layer
This commit is contained in:
Armon Dadgar 2014-10-15 16:48:57 -07:00
commit e720a09aa4
5 changed files with 34 additions and 8 deletions

View File

@ -114,6 +114,14 @@ func Create(config *Config, logOutput io.Writer) (*Agent, error) {
if config.Server { if config.Server {
err = agent.setupServer() err = agent.setupServer()
agent.state.SetIface(agent.server) agent.state.SetIface(agent.server)
// Automatically register the "consul" service on server nodes
consulService := structs.NodeService{
Service: consul.ConsulServiceName,
ID: consul.ConsulServiceID,
Port: agent.config.Ports.Server,
}
agent.state.AddService(&consulService)
} else { } else {
err = agent.setupClient() err = agent.setupClient()
agent.state.SetIface(agent.client) agent.state.SetIface(agent.client)
@ -452,6 +460,13 @@ func (a *Agent) AddService(service *structs.NodeService, chkType *CheckType) err
// RemoveService is used to remove a service entry. // RemoveService is used to remove a service entry.
// The agent will make a best effort to ensure it is deregistered // The agent will make a best effort to ensure it is deregistered
func (a *Agent) RemoveService(serviceID string) error { func (a *Agent) RemoveService(serviceID string) error {
// Protect "consul" service from deletion by a user
if a.server != nil && serviceID == consul.ConsulServiceID {
return fmt.Errorf(
"Deregistering the %s service is not allowed",
consul.ConsulServiceID)
}
// Remove service immeidately // Remove service immeidately
a.state.RemoveService(serviceID) a.state.RemoveService(serviceID)

View File

@ -31,7 +31,7 @@ func TestHTTPAgentServices(t *testing.T) {
t.Fatalf("Err: %v", err) t.Fatalf("Err: %v", err)
} }
val := obj.(map[string]*structs.NodeService) val := obj.(map[string]*structs.NodeService)
if len(val) != 1 { if len(val) != 2 {
t.Fatalf("bad services: %v", obj) t.Fatalf("bad services: %v", obj)
} }
if val["mysql"].Port != 5000 { if val["mysql"].Port != 5000 {

View File

@ -146,6 +146,11 @@ func TestAgent_RemoveService(t *testing.T) {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
// Remove the consul service
if err := agent.RemoveService("consul"); err == nil {
t.Fatalf("should have errored")
}
srv := &structs.NodeService{ srv := &structs.NodeService{
ID: "redis", ID: "redis",
Service: "redis", Service: "redis",
@ -315,3 +320,14 @@ func TestAgent_UpdateCheck(t *testing.T) {
t.Fatalf("bad: %v", status) t.Fatalf("bad: %v", status)
} }
} }
func TestAgent_ConsulService(t *testing.T) {
dir, agent := makeAgent(t, nextConfig())
defer os.RemoveAll(dir)
defer agent.Shutdown()
services := agent.state.Services()
if _, ok := services[consul.ConsulServiceID]; !ok {
t.Fatalf("%s service should be registered", consul.ConsulServiceID)
}
}

View File

@ -314,11 +314,6 @@ func (l *localState) setSyncState() error {
// If we don't have the service locally, deregister it // If we don't have the service locally, deregister it
existing, ok := l.services[id] existing, ok := l.services[id]
if !ok { if !ok {
// The Consul service is created automatically, and
// does not need to be registered
if id == consul.ConsulServiceID && l.config.Server {
continue
}
l.serviceStatus[id] = syncStatus{remoteDelete: true} l.serviceStatus[id] = syncStatus{remoteDelete: true}
continue continue
} }

View File

@ -119,10 +119,10 @@ func TestAgentAntiEntropy_Services(t *testing.T) {
} }
// Check the local state // Check the local state
if len(agent.state.services) != 3 { if len(agent.state.services) != 4 {
t.Fatalf("bad: %v", agent.state.services) t.Fatalf("bad: %v", agent.state.services)
} }
if len(agent.state.serviceStatus) != 3 { if len(agent.state.serviceStatus) != 4 {
t.Fatalf("bad: %v", agent.state.serviceStatus) t.Fatalf("bad: %v", agent.state.serviceStatus)
} }
for name, status := range agent.state.serviceStatus { for name, status := range agent.state.serviceStatus {