-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add HTTP endpoint to reload agent (#312)
add HTTP endpoint to reload agent
- Loading branch information
Showing
11 changed files
with
176 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package http | ||
|
||
import ( | ||
"net/http" | ||
"strings" | ||
) | ||
|
||
// agentSpecificRequest handles the requests for the `/v1/agent/` endpoint and sub-paths. | ||
func (s *Server) agentSpecificRequest(w http.ResponseWriter, r *http.Request) (interface{}, error) { | ||
path := strings.TrimPrefix(r.URL.Path, "/v1/agent") | ||
switch { | ||
case strings.HasSuffix(path, "/reload"): | ||
return s.agentReload(w, r) | ||
default: | ||
return nil, newCodedError(http.StatusNotFound, "") | ||
} | ||
} | ||
|
||
func (s *Server) agentReload(w http.ResponseWriter, r *http.Request) (interface{}, error) { | ||
if r.Method != http.MethodPost && r.Method != http.MethodPut { | ||
return nil, newCodedError(http.StatusMethodNotAllowed, errInvalidMethod) | ||
} | ||
|
||
return s.agent.ReloadAgent(w, r) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package http | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestServer_agentReload(t *testing.T) { | ||
testCases := []struct { | ||
inputReq *http.Request | ||
expectedRespCode int | ||
name string | ||
}{ | ||
{ | ||
inputReq: httptest.NewRequest("PUT", "/v1/agent/reload", nil), | ||
expectedRespCode: 200, | ||
name: "successfully reload", | ||
}, | ||
{ | ||
inputReq: httptest.NewRequest("GET", "/v1/agent/reload", nil), | ||
expectedRespCode: 405, | ||
name: "incorrect request method", | ||
}, | ||
} | ||
|
||
srv, stopSrv := TestServer(t) | ||
defer stopSrv() | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
assert := assert.New(t) | ||
|
||
w := httptest.NewRecorder() | ||
srv.mux.ServeHTTP(w, tc.inputReq) | ||
assert.Equal(tc.expectedRespCode, w.Code) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package http | ||
|
||
import ( | ||
"testing" | ||
|
||
hclog "github.com/hashicorp/go-hclog" | ||
"github.com/hashicorp/nomad-autoscaler/agent" | ||
"github.com/hashicorp/nomad-autoscaler/agent/config" | ||
) | ||
|
||
func TestServer(t *testing.T) (*Server, func()) { | ||
cfg := &config.HTTP{ | ||
BindAddress: "127.0.0.1", | ||
BindPort: 0, // Use next available port. | ||
} | ||
|
||
s, err := NewHTTPServer(cfg, hclog.NewNullLogger(), &agent.MockAgentHTTP{}) | ||
if err != nil { | ||
t.Fatalf("failed to start test server: %v", err) | ||
} | ||
|
||
return s, func() { | ||
s.Stop() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package agent | ||
|
||
import "net/http" | ||
|
||
// The methods in this file implement in the http.AgentHTTP interface. | ||
|
||
func (a *Agent) DisplayMetrics(resp http.ResponseWriter, req *http.Request) (interface{}, error) { | ||
return a.inMemSink.DisplayMetrics(resp, req) | ||
} | ||
|
||
func (a *Agent) ReloadAgent(_ http.ResponseWriter, _ *http.Request) (interface{}, error) { | ||
a.reload() | ||
return nil, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package agent | ||
|
||
import ( | ||
"net/http" | ||
|
||
metrics "github.com/armon/go-metrics" | ||
) | ||
|
||
type MockAgentHTTP struct{} | ||
|
||
func (m *MockAgentHTTP) DisplayMetrics(resp http.ResponseWriter, req *http.Request) (interface{}, error) { | ||
return metrics.MetricsSummary{ | ||
Timestamp: "2020-11-17 00:17:50 +0000 UTC", | ||
Counters: []metrics.SampledValue{}, | ||
Gauges: []metrics.GaugeValue{}, | ||
Points: []metrics.PointValue{}, | ||
Samples: []metrics.SampledValue{}, | ||
}, nil | ||
} | ||
func (m *MockAgentHTTP) ReloadAgent(resp http.ResponseWriter, req *http.Request) (interface{}, error) { | ||
return nil, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters