Incorporate review comments

This commit is contained in:
Jan-Eric Hellenberg
2021-05-18 15:03:52 +02:00
parent 0590f31200
commit fe2ec4df35
13 changed files with 177 additions and 83 deletions

View File

@ -31,7 +31,7 @@ func NewRouter(runnerManager runner.Manager, environmentManager environment.Mana
return router
}
// configureV1Router configures a given router with the routes of version 1 of our API.
// configureV1Router configures a given router with the routes of version 1 of the Poseidon API.
func configureV1Router(router *mux.Router, runnerManager runner.Manager, environmentManager environment.Manager) {
v1 := router.PathPrefix(RouteBase).Subrouter()
v1.HandleFunc(RouteHealth, Health).Methods(http.MethodGet)

View File

@ -56,6 +56,7 @@ func TestNewRouterV1WithAuthenticationEnabled(t *testing.T) {
})
t.Run("protected route is not accessible", func(t *testing.T) {
// request an available API route that should be guarded by authentication (which one, in particular, does not matter here)
request, err := http.NewRequest(http.MethodPost, "/api/v1/runners", nil)
if err != nil {
t.Fatal(err)

View File

@ -13,12 +13,24 @@ type ExecutionRequest struct {
Environment map[string]string
}
// ExecutionEnvironmentRequest is the expected json structure of the request body for the create execution environment function.
// nolint:unused,structcheck
type ExecutionEnvironmentRequest struct {
prewarmingPoolSize uint
cpuLimit uint
memoryLimit uint
image string
networkAccess bool
exposedPorts []uint16
}
// RunnerResponse is the expected response when providing a runner.
type RunnerResponse struct {
Id string `json:"runnerId"`
}
// FileCreation is the expected json structure of the request body for the copy files route.
// TODO: specify content of the struct
type FileCreation struct{}
// WebsocketResponse is the expected response when creating an execution for a runner.

View File

@ -5,7 +5,7 @@ import (
)
// Health handles the health route.
// It tries to respond that the server is alive.
// It responds that the server is alive.
// If it is not, the response won't reach the client.
func Health(writer http.ResponseWriter, _ *http.Request) {
writer.WriteHeader(http.StatusNoContent)

View File

@ -30,7 +30,7 @@ func (r *RunnerController) ConfigureRoutes(router *mux.Router) {
r.runnerRouter = runnersRouter.PathPrefix(fmt.Sprintf("/{%s}", RunnerIdKey)).Subrouter()
r.runnerRouter.Use(r.findRunnerMiddleware)
r.runnerRouter.HandleFunc(ExecutePath, r.execute).Methods(http.MethodPost).Name(ExecutePath)
r.runnerRouter.HandleFunc(WebsocketPath, connectToRunner).Methods(http.MethodGet).Name(WebsocketPath)
r.runnerRouter.HandleFunc(WebsocketPath, r.connectToRunner).Methods(http.MethodGet).Name(WebsocketPath)
r.runnerRouter.HandleFunc("", r.delete).Methods(http.MethodDelete).Name(DeleteRoute)
}
@ -43,7 +43,7 @@ func (r *RunnerController) provide(writer http.ResponseWriter, request *http.Req
return
}
environmentId := runner.EnvironmentId(runnerRequest.ExecutionEnvironmentId)
nextRunner, err := r.manager.Use(environmentId)
nextRunner, err := r.manager.Claim(environmentId)
if err != nil {
if err == runner.ErrUnknownExecutionEnvironment {
writeNotFound(writer, err)
@ -101,14 +101,13 @@ func (r *RunnerController) execute(writer http.ResponseWriter, request *http.Req
// and adds the runner to the context of the request.
func (r *RunnerController) findRunnerMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
// Find runner
runnerId := mux.Vars(request)[RunnerIdKey]
r, err := r.manager.Get(runnerId)
targetRunner, err := r.manager.Get(runnerId)
if err != nil {
writeNotFound(writer, err)
return
}
ctx := runner.NewContext(request.Context(), r.(runner.Runner))
ctx := runner.NewContext(request.Context(), targetRunner.(runner.Runner))
requestWithRunner := request.WithContext(ctx)
next.ServeHTTP(writer, requestWithRunner)
})
@ -123,9 +122,9 @@ func (r *RunnerController) delete(writer http.ResponseWriter, request *http.Requ
if err != nil {
if err == runner.ErrUnknownExecutionEnvironment {
writeNotFound(writer, err)
} else {
writeInternalServerError(writer, err, dto.ErrorNomadInternalServerError)
}
writeInternalServerError(writer, err, dto.ErrorNomadInternalServerError)
return
}

View File

@ -9,16 +9,16 @@ import (
)
// connectToRunner is the endpoint for websocket connections.
func connectToRunner(writer http.ResponseWriter, request *http.Request) {
r, _ := runner.FromContext(request.Context())
func (r *RunnerController) connectToRunner(writer http.ResponseWriter, request *http.Request) {
targetRunner, _ := runner.FromContext(request.Context())
executionId := runner.ExecutionId(request.URL.Query().Get(ExecutionIdKey))
_, ok := r.Execution(executionId)
_, ok := targetRunner.Execution(executionId)
if !ok {
writeNotFound(writer, errors.New("executionId does not exist"))
return
}
log.
WithField("runnerId", r.Id()).
WithField("runnerId", targetRunner.Id()).
WithField("executionId", executionId).
Info("Running execution")
connUpgrader := websocket.Upgrader{}