/
task_container_metadata_handler.go
109 lines (94 loc) · 4.77 KB
/
task_container_metadata_handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// 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 v2
import (
"encoding/json"
"fmt"
"net/http"
"github.com/aws/amazon-ecs-agent/agent/engine/dockerstate"
"github.com/aws/amazon-ecs-agent/ecs-agent/api/ecs"
"github.com/aws/amazon-ecs-agent/ecs-agent/tmds/handlers/utils"
"github.com/cihub/seelog"
)
const (
// metadataContainerIDMuxName is the key that's used in gorilla/mux to get the container ID
// for container metadata.
metadataContainerIDMuxName = "metadataContainerIDMuxName"
// TaskMetadataPath specifies the relative URI path for serving task metadata.
TaskMetadataPath = "/v2/metadata"
// TaskWithTagsMetadataPath specifies the relative URI path for serving task metadata with Container Instance and Task Tags.
TaskWithTagsMetadataPath = "/v2/metadataWithTags"
// TaskMetadataPathWithSlash specifies the relative URI path for serving task metadata.
TaskMetadataPathWithSlash = TaskMetadataPath + "/"
// TaskWithTagsMetadataPath specifies the relative URI path for serving task metadata with Container Instance and Task Tags.
TaskWithTagsMetadataPathWithSlash = TaskWithTagsMetadataPath + "/"
)
// ContainerMetadataPath specifies the relative URI path for serving container metadata.
var ContainerMetadataPath = TaskMetadataPathWithSlash + utils.ConstructMuxVar(metadataContainerIDMuxName, utils.AnythingButEmptyRegEx)
// TaskContainerMetadataHandler returns the handler method for handling task and container metadata requests.
func TaskContainerMetadataHandler(state dockerstate.TaskEngineState, ecsClient ecs.ECSClient, cluster, az, containerInstanceArn string, propagateTags bool) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
taskARN, err := getTaskARNByRequest(r, state)
if err != nil {
responseJSON, err := json.Marshal(
fmt.Sprintf("Unable to get task arn from request: %s", err.Error()))
if e := utils.WriteResponseIfMarshalError(w, err); e != nil {
return
}
utils.WriteJSONToResponse(w, http.StatusBadRequest, responseJSON, utils.RequestTypeTaskMetadata)
return
}
if containerID, ok := utils.GetMuxValueFromRequest(r, metadataContainerIDMuxName); ok {
seelog.Infof("V2 task/container metadata handler: writing response for container '%s'", containerID)
WriteContainerMetadataResponse(w, containerID, state)
return
}
seelog.Infof("V2 task/container metadata handler: writing response for task '%s'", taskARN)
WriteTaskMetadataResponse(w, taskARN, cluster, state, ecsClient, az, containerInstanceArn, propagateTags)
}
}
// WriteContainerMetadataResponse writes the container metadata to response writer.
func WriteContainerMetadataResponse(w http.ResponseWriter, containerID string, state dockerstate.TaskEngineState) {
containerResponse, err := NewContainerResponseFromState(containerID, state, false)
if err != nil {
errResponseJSON, err := json.Marshal("Unable to generate metadata for container '" + containerID + "'")
if e := utils.WriteResponseIfMarshalError(w, err); e != nil {
return
}
utils.WriteJSONToResponse(w, http.StatusBadRequest, errResponseJSON, utils.RequestTypeContainerMetadata)
return
}
responseJSON, err := json.Marshal(containerResponse)
if e := utils.WriteResponseIfMarshalError(w, err); e != nil {
return
}
utils.WriteJSONToResponse(w, http.StatusOK, responseJSON, utils.RequestTypeContainerMetadata)
}
// WriteTaskMetadataResponse writes the task metadata to response writer.
func WriteTaskMetadataResponse(w http.ResponseWriter, taskARN string, cluster string, state dockerstate.TaskEngineState, ecsClient ecs.ECSClient, az, containerInstanceArn string, propagateTags bool) {
// Generate a response for the task
taskResponse, err := NewTaskResponse(taskARN, state, ecsClient, cluster, az, containerInstanceArn, propagateTags, false)
if err != nil {
errResponseJSON, err := json.Marshal("Unable to generate metadata for task: '" + taskARN + "'")
if e := utils.WriteResponseIfMarshalError(w, err); e != nil {
return
}
utils.WriteJSONToResponse(w, http.StatusBadRequest, errResponseJSON, utils.RequestTypeTaskMetadata)
return
}
responseJSON, err := json.Marshal(taskResponse)
if e := utils.WriteResponseIfMarshalError(w, err); e != nil {
return
}
utils.WriteJSONToResponse(w, http.StatusOK, responseJSON, utils.RequestTypeTaskMetadata)
}