From cb7541c7af9af5408639f0670a704c3d6d2a93ed Mon Sep 17 00:00:00 2001 From: Armon Dadgar Date: Mon, 20 Jan 2014 15:00:52 -1000 Subject: [PATCH] Exposing the agent checks and services over HTTP endpoints --- command/agent/agent_endpoint.go | 9 +++++++-- command/agent/http.go | 1 + command/agent/http_api.md | 4 ++-- command/agent/local.go | 26 ++++++++++++++++++++++++++ 4 files changed, 36 insertions(+), 4 deletions(-) diff --git a/command/agent/agent_endpoint.go b/command/agent/agent_endpoint.go index 09966fcc84..eb4d8589dd 100644 --- a/command/agent/agent_endpoint.go +++ b/command/agent/agent_endpoint.go @@ -6,8 +6,13 @@ import ( ) func (s *HTTPServer) AgentServices(resp http.ResponseWriter, req *http.Request) (interface{}, error) { - // TODO - return nil, nil + services := s.agent.Services() + return services, nil +} + +func (s *HTTPServer) AgentChecks(resp http.ResponseWriter, req *http.Request) (interface{}, error) { + checks := s.agent.Checks() + return checks, nil } func (s *HTTPServer) AgentMembers(resp http.ResponseWriter, req *http.Request) (interface{}, error) { diff --git a/command/agent/http.go b/command/agent/http.go index bedb1efeac..415fbffeaa 100644 --- a/command/agent/http.go +++ b/command/agent/http.go @@ -71,6 +71,7 @@ func (s *HTTPServer) registerHandlers() { s.mux.HandleFunc("/v1/health/service/", s.wrap(s.HealthServiceNodes)) s.mux.HandleFunc("/v1/agent/services", s.wrap(s.AgentServices)) + s.mux.HandleFunc("/v1/agent/checks", s.wrap(s.AgentChecks)) s.mux.HandleFunc("/v1/agent/members", s.wrap(s.AgentMembers)) s.mux.HandleFunc("/v1/agent/join/", s.wrap(s.AgentJoin)) s.mux.HandleFunc("/v1/agent/force-leave/", s.wrap(s.AgentForceLeave)) diff --git a/command/agent/http_api.md b/command/agent/http_api.md index 1cb25871c8..73f66fbe7f 100644 --- a/command/agent/http_api.md +++ b/command/agent/http_api.md @@ -25,8 +25,8 @@ The current URLs supported are: * /v1/status/leader : Returns the current Raft leader * /v1/status/peers : Returns the current Raft peer set -* /v1/agent/health: Returns the health info from the local agent (future) -* /v1/agent/services : Returns the services local agent is attempting to register +* /v1/agent/checks: Returns the checks the local agent is managing +* /v1/agent/services : Returns the services local agent is managing * /v1/agent/members : Returns the members as seen by the local serf agent * /v1/agent/join/ : Instructs the local agent to join a node * /v1/agent/force-leave/: Instructs the agent to force a node into the left state diff --git a/command/agent/local.go b/command/agent/local.go index 76e220657d..12c745e00a 100644 --- a/command/agent/local.go +++ b/command/agent/local.go @@ -89,6 +89,19 @@ func (a *Agent) RemoveService(serviceID string) { a.state.changeMade() } +// Services returns the locally registered services that the +// agent is aware of and are being kept in sync with the server +func (a *Agent) Services() map[string]*structs.NodeService { + services := make(map[string]*structs.NodeService) + a.state.Lock() + defer a.state.Unlock() + + for name, serv := range a.state.services { + services[name] = serv + } + return services +} + // AddCheck is used to add a health check to the local state. // This entry is persistent and the agent will make a best effort to // ensure it is registered @@ -133,6 +146,19 @@ func (a *Agent) UpdateCheck(checkID, status string) { a.state.changeMade() } +// Checks returns the locally registered checks that the +// agent is aware of and are being kept in sync with the server +func (a *Agent) Checks() map[string]*structs.HealthCheck { + checks := make(map[string]*structs.HealthCheck) + a.state.Lock() + defer a.state.Unlock() + + for name, check := range a.state.checks { + checks[name] = check + } + return checks +} + // antiEntropy is a long running method used to perform anti-entropy // between local and remote state. func (a *Agent) antiEntropy() {