diff --git a/agent/handlers/v3/container_association_handler.go b/agent/handlers/v3/container_association_handler.go index 0a00c95879b..7d7d7b3fadb 100644 --- a/agent/handlers/v3/container_association_handler.go +++ b/agent/handlers/v3/container_association_handler.go @@ -33,11 +33,11 @@ const ( var ( // Container associations endpoint: /v3// ContainerAssociationsPath = fmt.Sprintf("/v3/%s/associations/%s", - utils.ConstructMuxVar(v3EndpointIDMuxName, utils.AnythingButSlashRegEx), + utils.ConstructMuxVar(V3EndpointIDMuxName, utils.AnythingButSlashRegEx), utils.ConstructMuxVar(associationTypeMuxName, utils.AnythingButSlashRegEx)) // Container association endpoint: /v3/// ContainerAssociationPath = fmt.Sprintf("/v3/%s/associations/%s/%s", - utils.ConstructMuxVar(v3EndpointIDMuxName, utils.AnythingButSlashRegEx), + utils.ConstructMuxVar(V3EndpointIDMuxName, utils.AnythingButSlashRegEx), utils.ConstructMuxVar(associationTypeMuxName, utils.AnythingButSlashRegEx), utils.ConstructMuxVar(associationNameMuxName, utils.AnythingButEmptyRegEx)) // Treat "/v3///" as a container association endpoint with empty association name (therefore invalid), to be consistent with similar situation in credentials endpoint and v3 metadata endpoint diff --git a/agent/handlers/v3/container_metadata_handler.go b/agent/handlers/v3/container_metadata_handler.go index f2f4bd78290..1dc6516e8d7 100644 --- a/agent/handlers/v3/container_metadata_handler.go +++ b/agent/handlers/v3/container_metadata_handler.go @@ -27,7 +27,7 @@ import ( ) // ContainerMetadataPath specifies the relative URI path for serving container metadata. -var ContainerMetadataPath = "/v3/" + utils.ConstructMuxVar(v3EndpointIDMuxName, utils.AnythingButSlashRegEx) +var ContainerMetadataPath = "/v3/" + utils.ConstructMuxVar(V3EndpointIDMuxName, utils.AnythingButSlashRegEx) // ContainerMetadataHandler returns the handler method for handling container metadata requests. func ContainerMetadataHandler(state dockerstate.TaskEngineState) func(http.ResponseWriter, *http.Request) { diff --git a/agent/handlers/v3/container_stats_handler.go b/agent/handlers/v3/container_stats_handler.go index d03dff66d7d..e1f19c78816 100644 --- a/agent/handlers/v3/container_stats_handler.go +++ b/agent/handlers/v3/container_stats_handler.go @@ -26,7 +26,7 @@ import ( ) // ContainerStatsPath specifies the relative URI path for serving container stats. -var ContainerStatsPath = "/v3/" + utils.ConstructMuxVar(v3EndpointIDMuxName, utils.AnythingButSlashRegEx) + "/stats" +var ContainerStatsPath = "/v3/" + utils.ConstructMuxVar(V3EndpointIDMuxName, utils.AnythingButSlashRegEx) + "/stats" // ContainerStatsHandler returns the handler method for handling container stats requests. func ContainerStatsHandler(state dockerstate.TaskEngineState, statsEngine stats.Engine) func(http.ResponseWriter, *http.Request) { diff --git a/agent/handlers/v3/helper.go b/agent/handlers/v3/helper.go index 2f9ec73fcf5..e7b0d07de26 100644 --- a/agent/handlers/v3/helper.go +++ b/agent/handlers/v3/helper.go @@ -22,7 +22,7 @@ import ( ) func GetTaskARNByRequest(r *http.Request, state dockerstate.TaskEngineState) (string, error) { - v3EndpointID, ok := utils.GetMuxValueFromRequest(r, v3EndpointIDMuxName) + v3EndpointID, ok := utils.GetMuxValueFromRequest(r, V3EndpointIDMuxName) if !ok { return "", errors.New("unable to get v3 endpoint ID from request") } @@ -37,7 +37,7 @@ func GetTaskARNByRequest(r *http.Request, state dockerstate.TaskEngineState) (st } func GetContainerIDByRequest(r *http.Request, state dockerstate.TaskEngineState) (string, error) { - v3EndpointID, ok := utils.GetMuxValueFromRequest(r, v3EndpointIDMuxName) + v3EndpointID, ok := utils.GetMuxValueFromRequest(r, V3EndpointIDMuxName) if !ok { return "", errors.New("unable to get v3 endpoint ID from request") } diff --git a/agent/handlers/v3/task_metadata_handler.go b/agent/handlers/v3/task_metadata_handler.go index 0acdd5f0088..f1af857f0cc 100644 --- a/agent/handlers/v3/task_metadata_handler.go +++ b/agent/handlers/v3/task_metadata_handler.go @@ -26,14 +26,14 @@ import ( ) // v3EndpointIDMuxName is the key that's used in gorilla/mux to get the v3 endpoint ID. -const v3EndpointIDMuxName = "v3EndpointIDMuxName" +const V3EndpointIDMuxName = "v3EndpointIDMuxName" // TaskMetadataPath specifies the relative URI path for serving task metadata. -var TaskMetadataPath = "/v3/" + utils.ConstructMuxVar(v3EndpointIDMuxName, utils.AnythingButSlashRegEx) + "/task" +var TaskMetadataPath = "/v3/" + utils.ConstructMuxVar(V3EndpointIDMuxName, utils.AnythingButSlashRegEx) + "/task" // TaskWithTagsMetadataPath specifies the relative URI path for serving task metdata // with Container Instance and Task Tags retrieved through the ECS API -var TaskWithTagsMetadataPath = "/v3/" + utils.ConstructMuxVar(v3EndpointIDMuxName, utils.AnythingButSlashRegEx) + "/taskWithTags" +var TaskWithTagsMetadataPath = "/v3/" + utils.ConstructMuxVar(V3EndpointIDMuxName, utils.AnythingButSlashRegEx) + "/taskWithTags" // TaskMetadataHandler returns the handler method for handling task metadata requests. func TaskMetadataHandler(state dockerstate.TaskEngineState, ecsClient api.ECSClient, cluster, az, containerInstanceArn string, propagateTags bool) func(http.ResponseWriter, *http.Request) { diff --git a/agent/handlers/v3/task_stats_handler.go b/agent/handlers/v3/task_stats_handler.go index b40f16b22d4..6543096c0b9 100644 --- a/agent/handlers/v3/task_stats_handler.go +++ b/agent/handlers/v3/task_stats_handler.go @@ -26,7 +26,7 @@ import ( ) // TaskStatsPath specifies the relative URI path for serving task stats. -var TaskStatsPath = "/v3/" + utils.ConstructMuxVar(v3EndpointIDMuxName, utils.AnythingButSlashRegEx) + "/task/stats" +var TaskStatsPath = "/v3/" + utils.ConstructMuxVar(V3EndpointIDMuxName, utils.AnythingButSlashRegEx) + "/task/stats" // TaskStatsHandler returns the handler method for handling task stats requests. func TaskStatsHandler(state dockerstate.TaskEngineState, statsEngine stats.Engine) func(http.ResponseWriter, *http.Request) { diff --git a/agent/handlers/v4/container_metadata_handler.go b/agent/handlers/v4/container_metadata_handler.go index 23265585056..94796b4df49 100644 --- a/agent/handlers/v4/container_metadata_handler.go +++ b/agent/handlers/v4/container_metadata_handler.go @@ -1,4 +1,4 @@ -// Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. +// 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 @@ -27,7 +27,7 @@ import ( ) // ContainerMetadataPath specifies the relative URI path for serving container metadata. -var ContainerMetadataPath = "/v4/" + utils.ConstructMuxVar(v4EndpointIDMuxName, utils.AnythingButSlashRegEx) +var ContainerMetadataPath = "/v4/" + utils.ConstructMuxVar(v3.V3EndpointIDMuxName, utils.AnythingButSlashRegEx) // ContainerMetadataHandler returns the handler method for handling container metadata requests. func ContainerMetadataHandler(state dockerstate.TaskEngineState) func(http.ResponseWriter, *http.Request) { diff --git a/agent/handlers/v4/container_stats_handler.go b/agent/handlers/v4/container_stats_handler.go new file mode 100644 index 00000000000..aacb4d18a75 --- /dev/null +++ b/agent/handlers/v4/container_stats_handler.go @@ -0,0 +1,53 @@ +// 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" + + "github.com/aws/amazon-ecs-agent/agent/engine/dockerstate" + "github.com/aws/amazon-ecs-agent/agent/handlers/utils" + v2 "github.com/aws/amazon-ecs-agent/agent/handlers/v2" + v3 "github.com/aws/amazon-ecs-agent/agent/handlers/v3" + "github.com/aws/amazon-ecs-agent/agent/stats" + "github.com/cihub/seelog" +) + +// ContainerStatsPath specifies the relative URI path for serving container stats. +var ContainerStatsPath = "/v4/" + utils.ConstructMuxVar(v3.V3EndpointIDMuxName, utils.AnythingButSlashRegEx) + "/stats" + +// ContainerStatsHandler returns the handler method for handling container stats requests. +func ContainerStatsHandler(state dockerstate.TaskEngineState, statsEngine stats.Engine) func(w http.ResponseWriter, r *http.Request) { + return func(w http.ResponseWriter, r *http.Request) { + taskArn, err := v3.GetTaskARNByRequest(r, state) + if err != nil { + errResponseJSON, _ := json.Marshal(fmt.Sprintf("V4 container handler: unable to get task arn from request: %s", err.Error())) + utils.WriteJSONToResponse(w, http.StatusBadRequest, errResponseJSON, utils.RequestTypeTaskStats) + return + } + + containerID, err := v3.GetContainerIDByRequest(r, state) + if err != nil { + responseJSON, _ := json.Marshal(fmt.Sprintf("V4 container stats handler: unable to get container ID from request: %s", err.Error())) + utils.WriteJSONToResponse(w, http.StatusBadRequest, responseJSON, utils.RequestTypeContainerStats) + return + } + + seelog.Infof("V4 container stats handler: writing response for container '%s'", containerID) + // v4 handler shares the same container states response format with v2 handler. + v2.WriteContainerStatsResponse(w, taskArn, containerID, statsEngine) + } +} diff --git a/agent/handlers/v4/response.go b/agent/handlers/v4/response.go index e86e0124b90..5e4b4116a17 100644 --- a/agent/handlers/v4/response.go +++ b/agent/handlers/v4/response.go @@ -1,4 +1,4 @@ -// Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. +// 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 diff --git a/agent/handlers/v4/response_test.go b/agent/handlers/v4/response_test.go index ba8301bf737..c88c38a1b8b 100644 --- a/agent/handlers/v4/response_test.go +++ b/agent/handlers/v4/response_test.go @@ -1,6 +1,6 @@ // +build unit -// Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. +// 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 diff --git a/agent/handlers/v4/task_metadata_handler.go b/agent/handlers/v4/task_metadata_handler.go index 1a1f0c74389..f7a5448a9a3 100644 --- a/agent/handlers/v4/task_metadata_handler.go +++ b/agent/handlers/v4/task_metadata_handler.go @@ -1,4 +1,4 @@ -// Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. +// 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 @@ -25,15 +25,12 @@ import ( "github.com/cihub/seelog" ) -// v4EndpointIDMuxName is the key that's used in gorilla/mux to get the v4 endpoint ID. -const v4EndpointIDMuxName = "v4EndpointIDMuxName" - // TaskMetadataPath specifies the relative URI path for serving task metadata. -var TaskMetadataPath = "/v4/" + utils.ConstructMuxVar(v4EndpointIDMuxName, utils.AnythingButSlashRegEx) + "/task" +var TaskMetadataPath = "/v4/" + utils.ConstructMuxVar(v3.V3EndpointIDMuxName, utils.AnythingButSlashRegEx) + "/task" // TaskWithTagsMetadataPath specifies the relative URI path for serving task metdata // with Container Instance and Task Tags retrieved through the ECS API -var TaskWithTagsMetadataPath = "/v4/" + utils.ConstructMuxVar(v4EndpointIDMuxName, utils.AnythingButSlashRegEx) + "/taskWithTags" +var TaskWithTagsMetadataPath = "/v4/" + utils.ConstructMuxVar(v3.V3EndpointIDMuxName, utils.AnythingButSlashRegEx) + "/taskWithTags" // TaskMetadataHandler returns the handler method for handling task metadata requests. func TaskMetadataHandler(state dockerstate.TaskEngineState, ecsClient api.ECSClient, cluster, az, containerInstanceArn string, propagateTags bool) func(http.ResponseWriter, *http.Request) { diff --git a/agent/handlers/v4/task_stats_handler.go b/agent/handlers/v4/task_stats_handler.go new file mode 100644 index 00000000000..9d3eec08d0d --- /dev/null +++ b/agent/handlers/v4/task_stats_handler.go @@ -0,0 +1,43 @@ +// 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" + + "github.com/aws/amazon-ecs-agent/agent/engine/dockerstate" + "github.com/aws/amazon-ecs-agent/agent/handlers/utils" + v2 "github.com/aws/amazon-ecs-agent/agent/handlers/v2" + v3 "github.com/aws/amazon-ecs-agent/agent/handlers/v3" + "github.com/aws/amazon-ecs-agent/agent/stats" + "github.com/cihub/seelog" +) + +var TaskStatsPath = "/v4/" + utils.ConstructMuxVar(v3.V3EndpointIDMuxName, utils.AnythingButSlashRegEx) + "/task/stats" + +func TaskStatsHandler(state dockerstate.TaskEngineState, statsEngine stats.Engine) func(http.ResponseWriter, *http.Request) { + return func(w http.ResponseWriter, r *http.Request) { + taskArn, err := v3.GetTaskARNByRequest(r, state) + if err != nil { + errResponseJSON, _ := json.Marshal(fmt.Sprintf("V4 task stats handler: unable to get task arn from request: %s", err.Error())) + utils.WriteJSONToResponse(w, http.StatusBadRequest, errResponseJSON, utils.RequestTypeTaskStats) + return + } + seelog.Infof("V4 tasks stats handler: writing response for task '%s'", taskArn) + // v4 handler shares with the same task response format with v2 handler + v2.WriteTaskStatsResponse(w, taskArn, state, statsEngine) + } +}