Skip to content
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

v4 task and container stats handler #2370

Merged
merged 1 commit into from
Feb 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions agent/handlers/v3/container_association_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ const (
var (
// Container associations endpoint: /v3/<v3 endpoint id>/<association type>
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/<v3 endpoint id>/<association type>/<association name>
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/<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
Expand Down
2 changes: 1 addition & 1 deletion agent/handlers/v3/container_metadata_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion agent/handlers/v3/container_stats_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
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 @@ -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")
}
Expand All @@ -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")
}
Expand Down
6 changes: 3 additions & 3 deletions agent/handlers/v3/task_metadata_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion agent/handlers/v3/task_stats_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions agent/handlers/v4/container_metadata_handler.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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) {
Expand Down
53 changes: 53 additions & 0 deletions agent/handlers/v4/container_stats_handler.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
2 changes: 1 addition & 1 deletion agent/handlers/v4/response.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion agent/handlers/v4/response_test.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down
9 changes: 3 additions & 6 deletions agent/handlers/v4/task_metadata_handler.go
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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) {
Expand Down
43 changes: 43 additions & 0 deletions agent/handlers/v4/task_stats_handler.go
Original file line number Diff line number Diff line change
@@ -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)
}
}