agent: auto-register the consul service on server nodes

This commit is contained in:
Ryan Uber 2014-10-14 15:05:41 -07:00
parent b804e1ab52
commit 4576b4eb29
3 changed files with 33 additions and 5 deletions

View File

@ -14,6 +14,9 @@ import (
"github.com/hashicorp/serf/serf" "github.com/hashicorp/serf/serf"
) )
// The internal service ID of the consul service
const internalServiceID = "consul"
/* /*
The agent is the long running process that is run on every machine. The agent is the long running process that is run on every machine.
It exposes an RPC interface that is used by the CLI to control the It exposes an RPC interface that is used by the CLI to control the
@ -114,6 +117,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: internalServiceID,
ID: internalServiceID,
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 +463,12 @@ 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 == internalServiceID {
return fmt.Errorf(
"Deregistering the %s service is not allowed", internalServiceID)
}
// Remove service immeidately // Remove service immeidately
a.state.RemoveService(serviceID) a.state.RemoveService(serviceID)

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[internalServiceID]; !ok {
t.Fatalf("%s service should be registered", internalServiceID)
}
}

View File

@ -157,11 +157,6 @@ func (a *Agent) shouldProcessUserEvent(msg *UserEvent) bool {
} }
if msg.ServiceFilter != "" { if msg.ServiceFilter != "" {
// Handle "consul" service on server nodes
if a.server != nil && msg.ServiceFilter == "consul" {
return true
}
re, err := regexp.Compile(msg.ServiceFilter) re, err := regexp.Compile(msg.ServiceFilter)
if err != nil { if err != nil {
a.logger.Printf("[ERR] agent: Failed to parse service filter '%s' for event '%s': %v", a.logger.Printf("[ERR] agent: Failed to parse service filter '%s' for event '%s': %v",