-
Notifications
You must be signed in to change notification settings - Fork 619
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add TMDS initialization functionality to ecs-agent module #3660
Conversation
d27aa95
to
54042a1
Compare
54042a1
to
ff85e19
Compare
} | ||
|
||
// Define a reqeuest rate limiter | ||
limiter := tollbooth. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Server setup here is taken from agent module
amazon-ecs-agent/agent/handlers/task_server_setup.go
Lines 81 to 100 in 0202ad2
limiter := tollbooth.NewLimiter(float64(steadyStateRate), nil) | |
limiter.SetOnLimitReached(handlersutils.LimitReachedHandler(auditLogger)) | |
limiter.SetBurst(burstRate) | |
// Log all requests and then pass through to muxRouter. | |
loggingMuxRouter := mux.NewRouter() | |
// rootPath is a path for any traffic to this endpoint, "root" mux name will not be used. | |
rootPath := "/" + handlersutils.ConstructMuxVar("root", handlersutils.AnythingRegEx) | |
loggingMuxRouter.Handle(rootPath, tollbooth.LimitHandler( | |
limiter, NewLoggingHandler(muxRouter))) | |
loggingMuxRouter.SkipClean(false) | |
server := http.Server{ | |
Addr: "127.0.0.1:" + strconv.Itoa(config.AgentCredentialsPort), | |
Handler: loggingMuxRouter, | |
ReadTimeout: readTimeout, | |
WriteTimeout: writeTimeout, | |
} |
} | ||
|
||
// LimitReachedHandler logs the throttled request in the credentials audit log | ||
func limitReachedHandler(auditLogger audit.AuditLogger) func(http.ResponseWriter, *http.Request) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This handler is taken from agent module
amazon-ecs-agent/agent/handlers/utils/helpers.go
Lines 128 to 135 in 0202ad2
func LimitReachedHandler(auditLogger audit.AuditLogger) func(http.ResponseWriter, *http.Request) { | |
return func(w http.ResponseWriter, r *http.Request) { | |
logRequest := request.LogRequest{ | |
Request: r, | |
} | |
auditLogger.Log(logRequest, http.StatusTooManyRequests, "") | |
} | |
} |
"github.com/cihub/seelog" | ||
) | ||
|
||
// LoggingHandler is used to log all requests for an endpoint. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is taken from agent module.
amazon-ecs-agent/agent/handlers/logging_handler.go
Lines 22 to 34 in 0202ad2
// LoggingHandler is used to log all requests for an endpoint. | |
type LoggingHandler struct{ h http.Handler } | |
// NewLoggingHandler creates a new LoggingHandler object. | |
func NewLoggingHandler(handler http.Handler) LoggingHandler { | |
return LoggingHandler{h: handler} | |
} | |
// ServeHTTP logs the method and remote address of the request. | |
func (lh LoggingHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { | |
seelog.Debug("Handling http request", "method", r.Method, "from", r.RemoteAddr) | |
lh.h.ServeHTTP(w, r) | |
} |
} | ||
|
||
// Create a new HTTP Task Metadata Server (TMDS) | ||
func NewServer(auditLogger audit.AuditLogger, options ...ConfigOpt) (*http.Server, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can see how this function will be consumed by agent module in the draft PR below.
ecs-agent/tmds/server_integ_test.go
Outdated
if err == nil { | ||
return nil // server is up now | ||
} | ||
time.Sleep(100 * time.Millisecond) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit/q: probably not very significant here.. should this sleep be before Get call? I think we may always want to sleep at least once, and the last sleep (i==9) is redundant if we put the sleep here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point. I will move the sleep to before the HTTP call!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated in d7da34f
Summary
Add TMDS initialization functionality to
ecs-agent
module. The server read/write timeout, request rate limits, listen address, and router need to be provided by the consumer. The server is configured with a request rate limiter and logging middleware just like the TMDS inagent
module.agent
module will be updated in a separate PR to consume this new functionality for initializing TMDS.Also making integration test make targets run integration tests of
ecs-agent
module.Implementation details
tmds
package toecs-agent
module and addNewServer()
function to it. The function takes an instance ofAuditLogger
and an arbitrary number of configuration options and initializes anet/http
server for TMDS. Configuration options must include a listen address and a router for the server.NewServer()
function creates request rate limiter and logger middlewares and adds them to the server it initializes and returns the server.make run-integ-tests
target to run integration tests forecs-agent
module too.Testing
Not a part of this PR but I replaced the current TMDS initialization in
agent
module with the new TMDS initialization fromecs-agent
module and built Agent from source. I then sent some requests to TMDS which were successful. More comprehensive tests will be performed for the next CR that will include changes I used for the testing.Added integration tests for server write timeout and rate limiter.
New tests cover the changes: yes
Description for the changelog
Add TMDS initialization functionality to ecs-agent module
Licensing
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.