From d73f079d0fe32e1184236c68bccb7b95b8e7691a Mon Sep 17 00:00:00 2001 From: Paul Banks Date: Wed, 18 Apr 2018 21:48:58 +0100 Subject: [PATCH] Add X-Consul-ContentHash header; implement removing all proxies; add load/unload test. --- agent/agent.go | 6 +++++- agent/agent_endpoint.go | 10 ++++++++-- agent/agent_endpoint_test.go | 2 ++ 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/agent/agent.go b/agent/agent.go index 03f7677d00..ce6a26d0c7 100644 --- a/agent/agent.go +++ b/agent/agent.go @@ -2444,7 +2444,11 @@ func (a *Agent) loadProxies(conf *config.RuntimeConfig) error { // unloadProxies will deregister all proxies known to the local agent. func (a *Agent) unloadProxies() error { - // TODO(banks): implement me + for id := range a.State.Proxies() { + if err := a.RemoveProxy(id, false); err != nil { + return fmt.Errorf("Failed deregistering proxy '%s': %s", id, err) + } + } return nil } diff --git a/agent/agent_endpoint.go b/agent/agent_endpoint.go index e7cec596ab..b3e3741a84 100644 --- a/agent/agent_endpoint.go +++ b/agent/agent_endpoint.go @@ -918,7 +918,7 @@ func (s *HTTPServer) AgentConnectProxyConfig(resp http.ResponseWriter, req *http // didn't want to make very general changes right away. hash := req.URL.Query().Get("hash") - return s.agentLocalBlockingQuery(hash, &queryOpts, + return s.agentLocalBlockingQuery(resp, hash, &queryOpts, func(updateCh chan struct{}) (string, interface{}, error) { // Retrieve the proxy specified proxy := s.agent.State.Proxy(id) @@ -972,7 +972,11 @@ func (s *HTTPServer) AgentConnectProxyConfig(resp http.ResponseWriter, req *http type agentLocalBlockingFunc func(updateCh chan struct{}) (string, interface{}, error) -func (s *HTTPServer) agentLocalBlockingQuery(hash string, +// agentLocalBlockingQuery performs a blocking query in a generic way against +// local agent state that has no RPC or raft to back it. It uses `hash` paramter +// instead of an `index`. The resp is needed to write the `X-Consul-ContentHash` +// header back on return no Status nor body content is ever written to it. +func (s *HTTPServer) agentLocalBlockingQuery(resp http.ResponseWriter, hash string, queryOpts *structs.QueryOptions, fn agentLocalBlockingFunc) (interface{}, error) { var timer *time.Timer @@ -1011,6 +1015,8 @@ func (s *HTTPServer) agentLocalBlockingQuery(hash string, break } } + + resp.Header().Set("X-Consul-ContentHash", curHash) return curResp, err } } diff --git a/agent/agent_endpoint_test.go b/agent/agent_endpoint_test.go index 4e73556ecf..b34ac508ae 100644 --- a/agent/agent_endpoint_test.go +++ b/agent/agent_endpoint_test.go @@ -2358,6 +2358,8 @@ func TestAgentConnectProxy(t *testing.T) { } assert.Equal(tt.wantResp, obj) + + assert.Equal(tt.wantResp.ContentHash, resp.Header().Get("X-Consul-ContentHash")) }) } }