Skip to content

Commit

Permalink
Merge pull request #2376 from cyastella/task_sever_setup
Browse files Browse the repository at this point in the history
container associations handler and task server setup
  • Loading branch information
cyastella authored Feb 28, 2020
2 parents 5856981 + e716058 commit 89f43d8
Show file tree
Hide file tree
Showing 7 changed files with 204 additions and 11 deletions.
3 changes: 3 additions & 0 deletions agent/handlers/task_server_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,9 @@ func v4HandlersSetup(muxRouter *mux.Router,
muxRouter.HandleFunc(v4.TaskWithTagsMetadataPath, v4.TaskMetadataHandler(state, ecsClient, cluster, availabilityZone, containerInstanceArn, true))
muxRouter.HandleFunc(v4.ContainerStatsPath, v4.ContainerStatsHandler(state, statsEngine))
muxRouter.HandleFunc(v4.TaskStatsPath, v4.TaskStatsHandler(state, statsEngine))
muxRouter.HandleFunc(v4.ContainerAssociationsPath, v4.ContainerAssociationsHandler(state))
muxRouter.HandleFunc(v4.ContainerAssociationPathWithSlash, v4.ContainerAssociationHandler(state))
muxRouter.HandleFunc(v4.ContainerAssociationPath, v4.ContainerAssociationHandler(state))
}

// ServeTaskHTTPEndpoint serves task/container metadata, task/container stats, and IAM Role Credentials
Expand Down
53 changes: 53 additions & 0 deletions agent/handlers/task_server_setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1385,6 +1385,59 @@ func TestV4ContainerStats(t *testing.T) {
assert.Equal(t, dockerStats.NumProcs, statsFromResult.NumProcs)
}

func TestV4ContainerAssociations(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

state := mock_dockerstate.NewMockTaskEngineState(ctrl)
auditLog := mock_audit.NewMockAuditLogger(ctrl)
statsEngine := mock_stats.NewMockEngine(ctrl)
ecsClient := mock_api.NewMockECSClient(ctrl)

gomock.InOrder(
state.EXPECT().DockerIDByV3EndpointID(v3EndpointID).Return(containerID, true),
state.EXPECT().TaskARNByV3EndpointID(v3EndpointID).Return(taskARN, true),
state.EXPECT().ContainerByID(containerID).Return(dockerContainer, true),
state.EXPECT().TaskByArn(taskARN).Return(task, true),
)
server := taskServerSetup(credentials.NewManager(), auditLog, state, ecsClient, clusterName, statsEngine,
config.DefaultTaskMetadataSteadyStateRate, config.DefaultTaskMetadataBurstRate, "", containerInstanceArn)
recorder := httptest.NewRecorder()
req, _ := http.NewRequest("GET", v4BasePath+v3EndpointID+"/associations/"+associationType, nil)
server.Handler.ServeHTTP(recorder, req)
res, err := ioutil.ReadAll(recorder.Body)
assert.NoError(t, err)
assert.Equal(t, http.StatusOK, recorder.Code)

var associationsResponse v3.AssociationsResponse
err = json.Unmarshal(res, &associationsResponse)
assert.NoError(t, err)
assert.Equal(t, expectedAssociationsResponse, associationsResponse)
}

func TestV4ContainerAssociation(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

state := mock_dockerstate.NewMockTaskEngineState(ctrl)
auditLog := mock_audit.NewMockAuditLogger(ctrl)
statsEngine := mock_stats.NewMockEngine(ctrl)
ecsClient := mock_api.NewMockECSClient(ctrl)

gomock.InOrder(
state.EXPECT().TaskARNByV3EndpointID(v3EndpointID).Return(taskARN, true),
state.EXPECT().TaskByArn(taskARN).Return(task, true),
)
server := taskServerSetup(credentials.NewManager(), auditLog, state, ecsClient, clusterName, statsEngine, config.DefaultTaskMetadataSteadyStateRate, config.DefaultTaskMetadataBurstRate, "", containerInstanceArn)
recorder := httptest.NewRecorder()
req, _ := http.NewRequest("GET", v4BasePath+v3EndpointID+"/associations/"+associationType+"/"+associationName, nil)
server.Handler.ServeHTTP(recorder, req)
res, err := ioutil.ReadAll(recorder.Body)
assert.NoError(t, err)
assert.Equal(t, http.StatusOK, recorder.Code)
assert.Equal(t, expectedAssociationResponse, string(res))
}

func TestTaskHTTPEndpoint301Redirect(t *testing.T) {
testPathsMap := map[string]string{
"http://127.0.0.1/v3///task/": "http://127.0.0.1/v3/task/",
Expand Down
10 changes: 5 additions & 5 deletions agent/handlers/v3/container_association_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func ContainerAssociationsHandler(state dockerstate.TaskEngineState) func(http.R
return
}

associationType, err := getAssociationTypeByRequest(r)
associationType, err := GetAssociationTypeByRequest(r)
if err != nil {
responseJSON, _ := json.Marshal(
fmt.Sprintf("V3 container associations handler: %s", err.Error()))
Expand All @@ -88,15 +88,15 @@ func ContainerAssociationHandler(state dockerstate.TaskEngineState) func(http.Re
return
}

associationType, err := getAssociationTypeByRequest(r)
associationType, err := GetAssociationTypeByRequest(r)
if err != nil {
responseJSON, _ := json.Marshal(
fmt.Sprintf("V3 container associations handler: %s", err.Error()))
utils.WriteJSONToResponse(w, http.StatusBadRequest, responseJSON, utils.RequestTypeContainerAssociation)
return
}

associationName, err := getAssociationNameByRequest(r)
associationName, err := GetAssociationNameByRequest(r)
if err != nil {
responseJSON, _ := json.Marshal(
fmt.Sprintf("V3 container associations handler: %s", err.Error()))
Expand All @@ -111,7 +111,7 @@ func ContainerAssociationHandler(state dockerstate.TaskEngineState) func(http.Re
}

func writeContainerAssociationsResponse(w http.ResponseWriter, containerID, taskARN, associationType string, state dockerstate.TaskEngineState) {
associationsResponse, err := newAssociationsResponse(containerID, taskARN, associationType, state)
associationsResponse, err := NewAssociationsResponse(containerID, taskARN, associationType, state)
if err != nil {
errResponseJSON, _ := json.Marshal(fmt.Sprintf("Unable to write container associations response: %s", err.Error()))
utils.WriteJSONToResponse(w, http.StatusBadRequest, errResponseJSON, utils.RequestTypeContainerAssociations)
Expand All @@ -123,7 +123,7 @@ func writeContainerAssociationsResponse(w http.ResponseWriter, containerID, task
}

func writeContainerAssociationResponse(w http.ResponseWriter, taskARN, associationType, associationName string, state dockerstate.TaskEngineState) {
associationResponse, err := newAssociationResponse(taskARN, associationType, associationName, state)
associationResponse, err := NewAssociationResponse(taskARN, associationType, associationName, state)
if err != nil {
errResponseJSON, _ := json.Marshal(fmt.Sprintf("Unable to write container association response: %s", err.Error()))
utils.WriteJSONToResponse(w, http.StatusBadRequest, errResponseJSON, utils.RequestTypeContainerAssociation)
Expand Down
4 changes: 2 additions & 2 deletions agent/handlers/v3/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func GetContainerIDByRequest(r *http.Request, state dockerstate.TaskEngineState)
return dockerID, nil
}

func getAssociationTypeByRequest(r *http.Request) (string, error) {
func GetAssociationTypeByRequest(r *http.Request) (string, error) {
associationType, ok := utils.GetMuxValueFromRequest(r, associationTypeMuxName)
if !ok {
return "", errors.New("unable to get association type from request")
Expand All @@ -60,7 +60,7 @@ func getAssociationTypeByRequest(r *http.Request) (string, error) {
return associationType, nil
}

func getAssociationNameByRequest(r *http.Request) (string, error) {
func GetAssociationNameByRequest(r *http.Request) (string, error) {
associationType, ok := utils.GetMuxValueFromRequest(r, associationNameMuxName)
if !ok {
return "", errors.New("unable to get association name from request")
Expand Down
4 changes: 2 additions & 2 deletions agent/handlers/v3/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type AssociationsResponse struct {
Associations []string `json:"Associations"`
}

func newAssociationsResponse(containerID, taskARN, associationType string, state dockerstate.TaskEngineState) (*AssociationsResponse, error) {
func NewAssociationsResponse(containerID, taskARN, associationType string, state dockerstate.TaskEngineState) (*AssociationsResponse, error) {
dockerContainer, ok := state.ContainerByID(containerID)
if !ok {
return nil, errors.Errorf("unable to get container name from docker id: %s", containerID)
Expand All @@ -47,7 +47,7 @@ func newAssociationsResponse(containerID, taskARN, associationType string, state
// don't do any decoding base on the encoding, because the only encoding that cp currently sends us is 'identity';
// we don't explicitly model the value field as a struct because we don't want to let agent's implementation depends
// on the payload format of the association (i.e. eia device for now)
func newAssociationResponse(taskARN, associationType, associationName string, state dockerstate.TaskEngineState) (string, error) {
func NewAssociationResponse(taskARN, associationType, associationName string, state dockerstate.TaskEngineState) (string, error) {
task, ok := state.TaskByArn(taskARN)
if !ok {
return "", errors.Errorf("unable to get task from task arn: %s", taskARN)
Expand Down
4 changes: 2 additions & 2 deletions agent/handlers/v3/response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func TestContainerAssociationsResponse(t *testing.T) {
state.EXPECT().TaskByArn(taskARN).Return(task, true),
)

associationsResponse, err := newAssociationsResponse(dockerID, taskARN, associationType, state)
associationsResponse, err := NewAssociationsResponse(dockerID, taskARN, associationType, state)
assert.NoError(t, err)

associationsResponseJSON, err := json.Marshal(associationsResponse)
Expand All @@ -97,7 +97,7 @@ func TestContainerAssociationResponse(t *testing.T) {

state.EXPECT().TaskByArn(taskARN).Return(task, true)

associationResponse, err := newAssociationResponse(taskARN, associationType, associationName, state)
associationResponse, err := NewAssociationResponse(taskARN, associationType, associationName, state)
assert.NoError(t, err)

// the response is expected to be the same as the association value
Expand Down
137 changes: 137 additions & 0 deletions agent/handlers/v4/container_association_handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may
// not use this file except in compliance with the License. A copy of the
// License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file is distributed
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the License for the specific language governing
// permissions and limitations under the License.

package v4

import (
"encoding/json"
"fmt"
"net/http"

v3 "github.com/aws/amazon-ecs-agent/agent/handlers/v3"

"github.com/aws/amazon-ecs-agent/agent/engine/dockerstate"
"github.com/aws/amazon-ecs-agent/agent/handlers/utils"
"github.com/cihub/seelog"
)

const (
// associationTypeMuxName is the key that's used in gorilla/mux to get the association type.
associationTypeMuxName = "associationTypeMuxName"
// associationNameMuxName is the key that's used in gorilla/mux to get the association name.
associationNameMuxName = "associationNameMuxName"
)

var (
// Container associations endpoint: /v4/<v3 endpoint id>/<association type>
ContainerAssociationsPath = fmt.Sprintf("/v4/%s/associations/%s",
utils.ConstructMuxVar(v3.V3EndpointIDMuxName, utils.AnythingButSlashRegEx),
utils.ConstructMuxVar(associationTypeMuxName, utils.AnythingButSlashRegEx))
// Container association endpoint: /v4/<v3 endpoint id>/<association type>/<association name>
ContainerAssociationPath = fmt.Sprintf("/v4/%s/associations/%s/%s",
utils.ConstructMuxVar(v3.V3EndpointIDMuxName, utils.AnythingButSlashRegEx),
utils.ConstructMuxVar(associationTypeMuxName, utils.AnythingButSlashRegEx),
utils.ConstructMuxVar(associationNameMuxName, utils.AnythingButEmptyRegEx))
// Treat "/v4/<v3 endpoint id>/<association type>/" as a container association endpoint with empty association name (therefore invalid), to be consistent with similar situation in credentials endpoint and v3 metadata endpoint
ContainerAssociationPathWithSlash = ContainerAssociationsPath + "/"
)

// ContainerAssociationHandler returns the handler method for handling container associations requests.
func ContainerAssociationsHandler(state dockerstate.TaskEngineState) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
containerID, err := v3.GetContainerIDByRequest(r, state)
if err != nil {
responseJSON, _ := json.Marshal(
fmt.Sprintf("V4 container associations handler: unable to get container id from request: %s", err.Error()))
utils.WriteJSONToResponse(w, http.StatusBadRequest, responseJSON, utils.RequestTypeContainerAssociations)
return
}

taskARN, err := v3.GetTaskARNByRequest(r, state)
if err != nil {
responseJSON, _ := json.Marshal(
fmt.Sprintf("V4 container associations handler: unable to get task arn from request: %s", err.Error()))
utils.WriteJSONToResponse(w, http.StatusBadRequest, responseJSON, utils.RequestTypeContainerAssociations)
return
}

associationType, err := v3.GetAssociationTypeByRequest(r)
if err != nil {
responseJSON, _ := json.Marshal(
fmt.Sprintf("V4 container associations handler: %s", err.Error()))
utils.WriteJSONToResponse(w, http.StatusBadRequest, responseJSON, utils.RequestTypeContainerAssociations)
return
}

seelog.Infof("V4 container associations handler: writing response for container '%s' for association type %s", containerID, associationType)

writeContainerAssociationsResponse(w, containerID, taskARN, associationType, state)
}
}

// ContainerAssociationHandler returns the handler method for handling container association requests.
func ContainerAssociationHandler(state dockerstate.TaskEngineState) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
taskARN, err := v3.GetTaskARNByRequest(r, state)
if err != nil {
responseJSON, _ := json.Marshal(
fmt.Sprintf("V4 container associations handler: unable to get task arn from request: %s", err.Error()))
utils.WriteJSONToResponse(w, http.StatusBadRequest, responseJSON, utils.RequestTypeContainerAssociation)
return
}

associationType, err := v3.GetAssociationTypeByRequest(r)
if err != nil {
responseJSON, _ := json.Marshal(
fmt.Sprintf("V4 container associations handler: %s", err.Error()))
utils.WriteJSONToResponse(w, http.StatusBadRequest, responseJSON, utils.RequestTypeContainerAssociation)
return
}

associationName, err := v3.GetAssociationNameByRequest(r)
if err != nil {
responseJSON, _ := json.Marshal(
fmt.Sprintf("V4 container associations handler: %s", err.Error()))
utils.WriteJSONToResponse(w, http.StatusBadRequest, responseJSON, utils.RequestTypeContainerAssociation)
return
}

seelog.Infof("V4 container association handler: writing response for association '%s' of type %s", associationName, associationType)

writeContainerAssociationResponse(w, taskARN, associationType, associationName, state)
}
}

func writeContainerAssociationsResponse(w http.ResponseWriter, containerID, taskARN, associationType string, state dockerstate.TaskEngineState) {
associationsResponse, err := v3.NewAssociationsResponse(containerID, taskARN, associationType, state)
if err != nil {
errResponseJSON, _ := json.Marshal(fmt.Sprintf("Unable to write container associations response: %s", err.Error()))
utils.WriteJSONToResponse(w, http.StatusBadRequest, errResponseJSON, utils.RequestTypeContainerAssociations)
return
}

responseJSON, _ := json.Marshal(associationsResponse)
utils.WriteJSONToResponse(w, http.StatusOK, responseJSON, utils.RequestTypeContainerAssociations)
}

func writeContainerAssociationResponse(w http.ResponseWriter, taskARN, associationType, associationName string, state dockerstate.TaskEngineState) {
associationResponse, err := v3.NewAssociationResponse(taskARN, associationType, associationName, state)
if err != nil {
errResponseJSON, _ := json.Marshal(fmt.Sprintf("Unable to write container association response: %s", err.Error()))
utils.WriteJSONToResponse(w, http.StatusBadRequest, errResponseJSON, utils.RequestTypeContainerAssociation)
return
}

// associationResponse is assumed to be a valid json string; see comments on newAssociationResponse method for details
utils.WriteJSONToResponse(w, http.StatusOK, []byte(associationResponse), utils.RequestTypeContainerAssociation)
}

0 comments on commit 89f43d8

Please sign in to comment.