forked from aws/amazon-ecs-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
v1_handlers.go
243 lines (215 loc) · 8.08 KB
/
v1_handlers.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
// Copyright 2014-2017 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 handlers deals with the agent introspection api.
package handlers
import (
"encoding/json"
"net/http"
"strconv"
"sync"
"time"
"github.com/aws/amazon-ecs-agent/agent/api"
"github.com/aws/amazon-ecs-agent/agent/config"
"github.com/aws/amazon-ecs-agent/agent/engine"
"github.com/aws/amazon-ecs-agent/agent/engine/dockerstate"
"github.com/aws/amazon-ecs-agent/agent/handlers/types/v1"
"github.com/aws/amazon-ecs-agent/agent/logger"
"github.com/aws/amazon-ecs-agent/agent/utils"
"github.com/aws/amazon-ecs-agent/agent/version"
)
var log = logger.ForModule("Handlers")
const (
dockerIdQueryField = "dockerid"
taskArnQueryField = "taskarn"
dockerShortIdLen = 12
)
type rootResponse struct {
AvailableCommands []string
}
// ValueFromRequest returns the value of a field in the http request. The boolean value is
// set to true if the field exists in the query.
func ValueFromRequest(r *http.Request, field string) (string, bool) {
values := r.URL.Query()
_, exists := values[field]
return values.Get(field), exists
}
func metadataV1RequestHandlerMaker(containerInstanceArn *string, cfg *config.Config) func(http.ResponseWriter, *http.Request) {
resp := &v1.MetadataResponse{
Cluster: cfg.Cluster,
ContainerInstanceArn: containerInstanceArn,
Version: version.String(),
}
responseJSON, _ := json.Marshal(resp)
return func(w http.ResponseWriter, r *http.Request) {
w.Write(responseJSON)
}
}
func newTaskResponse(task *api.Task, containerMap map[string]*api.DockerContainer) *v1.TaskResponse {
containers := []v1.ContainerResponse{}
for containerName, container := range containerMap {
if container.Container.IsInternal() {
continue
}
containers = append(containers, v1.ContainerResponse{DockerId: container.DockerID, DockerName: container.DockerName, Name: containerName})
}
knownStatus := task.GetKnownStatus()
knownBackendStatus := knownStatus.BackendStatus()
desiredStatusInAgent := task.GetDesiredStatus()
desiredStatus := desiredStatusInAgent.BackendStatus()
if (knownBackendStatus == "STOPPED" && desiredStatus != "STOPPED") || (knownBackendStatus == "RUNNING" && desiredStatus == "PENDING") {
desiredStatus = ""
}
return &v1.TaskResponse{
Arn: task.Arn,
DesiredStatus: desiredStatus,
KnownStatus: knownBackendStatus,
Family: task.Family,
Version: task.Version,
Containers: containers,
}
}
func newTasksResponse(state dockerstate.TaskEngineState) *v1.TasksResponse {
allTasks := state.AllTasks()
taskResponses := make([]*v1.TaskResponse, len(allTasks))
for ndx, task := range allTasks {
containerMap, _ := state.ContainerMapByArn(task.Arn)
taskResponses[ndx] = newTaskResponse(task, containerMap)
}
return &v1.TasksResponse{Tasks: taskResponses}
}
// Creates JSON response and sets the http status code for the task queried.
func createTaskJSONResponse(task *api.Task, found bool, resourceId string, state dockerstate.TaskEngineState) ([]byte, int) {
var responseJSON []byte
status := http.StatusOK
if found {
containerMap, _ := state.ContainerMapByArn(task.Arn)
responseJSON, _ = json.Marshal(newTaskResponse(task, containerMap))
} else {
log.Warn("Could not find requested resource: " + resourceId)
responseJSON, _ = json.Marshal(&v1.TaskResponse{})
status = http.StatusNotFound
}
return responseJSON, status
}
// Creates response for the 'v1/tasks' API. Lists all tasks if the request
// doesn't contain any fields. Returns a Task if either of 'dockerid' or
// 'taskarn' are specified in the request.
func tasksV1RequestHandlerMaker(taskEngine DockerStateResolver) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
var responseJSON []byte
dockerTaskEngineState := taskEngine.State()
dockerId, dockerIdExists := ValueFromRequest(r, dockerIdQueryField)
taskArn, taskArnExists := ValueFromRequest(r, taskArnQueryField)
var status int
if dockerIdExists && taskArnExists {
log.Info("Request contains both ", dockerIdQueryField, " and ", taskArnQueryField, ". Expect at most one of these.")
w.WriteHeader(http.StatusBadRequest)
w.Write(responseJSON)
return
}
if dockerIdExists {
// Create TaskResponse for the docker id in the query.
var task *api.Task
var found bool
if len(dockerId) > dockerShortIdLen {
task, found = dockerTaskEngineState.TaskByID(dockerId)
} else {
tasks, _ := dockerTaskEngineState.TaskByShortID(dockerId)
if len(tasks) == 0 {
task = nil
found = false
} else if len(tasks) == 1 {
task = tasks[0]
found = true
} else {
log.Info("Multiple tasks found for requested dockerId: " + dockerId)
w.WriteHeader(http.StatusBadRequest)
w.Write(responseJSON)
return
}
}
responseJSON, status = createTaskJSONResponse(task, found, dockerId, dockerTaskEngineState)
w.WriteHeader(status)
} else if taskArnExists {
// Create TaskResponse for the task arn in the query.
task, found := dockerTaskEngineState.TaskByArn(taskArn)
responseJSON, status = createTaskJSONResponse(task, found, taskArn, dockerTaskEngineState)
w.WriteHeader(status)
} else {
// List all tasks.
responseJSON, _ = json.Marshal(newTasksResponse(dockerTaskEngineState))
}
w.Write(responseJSON)
}
}
var licenseProvider = utils.NewLicenseProvider()
func licenseHandler(w http.ResponseWriter, h *http.Request) {
text, err := licenseProvider.GetText()
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
} else {
w.Write([]byte(text))
}
}
func setupServer(containerInstanceArn *string, taskEngine DockerStateResolver, cfg *config.Config) *http.Server {
serverFunctions := map[string]func(w http.ResponseWriter, r *http.Request){
"/v1/metadata": metadataV1RequestHandlerMaker(containerInstanceArn, cfg),
"/v1/tasks": tasksV1RequestHandlerMaker(taskEngine),
"/license": licenseHandler,
}
paths := make([]string, 0, len(serverFunctions))
for path := range serverFunctions {
paths = append(paths, path)
}
availableCommands := &rootResponse{paths}
// Autogenerated list of the above serverFunctions paths
availableCommandResponse, _ := json.Marshal(&availableCommands)
defaultHandler := func(w http.ResponseWriter, r *http.Request) {
w.Write(availableCommandResponse)
}
serverMux := http.NewServeMux()
serverMux.HandleFunc("/", defaultHandler)
for key, fn := range serverFunctions {
serverMux.HandleFunc(key, fn)
}
// Log all requests and then pass through to serverMux
loggingServeMux := http.NewServeMux()
loggingServeMux.Handle("/", LoggingHandler{serverMux})
server := &http.Server{
Addr: ":" + strconv.Itoa(config.AgentIntrospectionPort),
Handler: loggingServeMux,
ReadTimeout: 5 * time.Second,
WriteTimeout: 5 * time.Second,
}
return server
}
// ServeHttp serves information about this agent / containerInstance and tasks
// running on it.
func ServeHttp(containerInstanceArn *string, taskEngine engine.TaskEngine, cfg *config.Config) {
// Is this the right level to type assert, assuming we'd abstract multiple taskengines here?
// Revisit if we ever add another type..
dockerTaskEngine := taskEngine.(*engine.DockerTaskEngine)
server := setupServer(containerInstanceArn, dockerTaskEngine, cfg)
for {
once := sync.Once{}
utils.RetryWithBackoff(utils.NewSimpleBackoff(time.Second, time.Minute, 0.2, 2), func() error {
// TODO, make this cancellable and use the passed in context; for
// now, not critical if this gets interrupted
err := server.ListenAndServe()
once.Do(func() {
log.Error("Error running http api", "err", err)
})
return err
})
}
}