forked from aws/amazon-ecs-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.go
309 lines (277 loc) · 11.4 KB
/
parse.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
// Copyright 2018 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 config
import (
"encoding/json"
"fmt"
"io"
"os"
"strconv"
"strings"
"time"
"github.com/aws/amazon-ecs-agent/agent/dockerclient"
"github.com/aws/amazon-ecs-agent/agent/utils"
"github.com/cihub/seelog"
cnitypes "github.com/containernetworking/cni/pkg/types"
)
func parseCheckpoint(dataDir string) bool {
var checkPoint bool
if dataDir != "" {
// if we have a directory to checkpoint to, default it to be on
checkPoint = utils.ParseBool(os.Getenv("ECS_CHECKPOINT"), true)
} else {
// if the directory is not set, default to checkpointing off for
// backwards compatibility
checkPoint = utils.ParseBool(os.Getenv("ECS_CHECKPOINT"), false)
}
return checkPoint
}
func parseReservedPorts(env string) []uint16 {
// Format: json array, e.g. [1,2,3]
reservedPortEnv := os.Getenv(env)
portDecoder := json.NewDecoder(strings.NewReader(reservedPortEnv))
var reservedPorts []uint16
err := portDecoder.Decode(&reservedPorts)
// EOF means the string was blank as opposed to UnexepctedEof which means an
// invalid parse
// Blank is not a warning; we have sane defaults
if err != io.EOF && err != nil {
err := fmt.Errorf("Invalid format for \"%s\" environment variable; expected a JSON array like [1,2,3]. err %v", env, err)
seelog.Warn(err)
}
return reservedPorts
}
func parseDockerStopTimeout() time.Duration {
var dockerStopTimeout time.Duration
parsedStopTimeout := parseEnvVariableDuration("ECS_CONTAINER_STOP_TIMEOUT")
if parsedStopTimeout >= minimumDockerStopTimeout {
dockerStopTimeout = parsedStopTimeout
// if the ECS_CONTAINER_STOP_TIMEOUT is invalid or empty, then the parsedStopTimeout
// will be 0, in this case we should return a 0,
// because the DockerStopTimeout will merge with the DefaultDockerStopTimeout,
// only when the DockerStopTimeout is empty
} else if parsedStopTimeout != 0 {
// if the configured ECS_CONTAINER_STOP_TIMEOUT is smaller than minimumDockerStopTimeout,
// DockerStopTimeout will be set to minimumDockerStopTimeout
// if the ECS_CONTAINER_STOP_TIMEOUT is 0, empty or an invalid value, then DockerStopTimeout
// will be set to defaultDockerStopTimeout during the config merge operation
dockerStopTimeout = minimumDockerStopTimeout
seelog.Warnf("Discarded invalid value for docker stop timeout, parsed as: %v", parsedStopTimeout)
}
return dockerStopTimeout
}
func parseContainerStartTimeout() time.Duration {
var containerStartTimeout time.Duration
parsedStartTimeout := parseEnvVariableDuration("ECS_CONTAINER_START_TIMEOUT")
if parsedStartTimeout >= minimumContainerStartTimeout {
containerStartTimeout = parsedStartTimeout
// do the parsedStartTimeout != 0 check for the same reason as in getDockerStopTimeout()
} else if parsedStartTimeout != 0 {
containerStartTimeout = minimumContainerStartTimeout
seelog.Warnf("Discarded invalid value for container start timeout, parsed as: %v", parsedStartTimeout)
}
return containerStartTimeout
}
func parseImagePullInactivityTimeout() time.Duration {
var imagePullInactivityTimeout time.Duration
parsedImagePullInactivityTimeout := parseEnvVariableDuration("ECS_IMAGE_PULL_INACTIVITY_TIMEOUT")
if parsedImagePullInactivityTimeout >= minimumImagePullInactivityTimeout {
imagePullInactivityTimeout = parsedImagePullInactivityTimeout
// do the parsedStartTimeout != 0 check for the same reason as in getDockerStopTimeout()
} else if parsedImagePullInactivityTimeout != 0 {
imagePullInactivityTimeout = minimumImagePullInactivityTimeout
seelog.Warnf("Discarded invalid value for image pull inactivity timeout, parsed as: %v", parsedImagePullInactivityTimeout)
}
return imagePullInactivityTimeout
}
func parseAvailableLoggingDrivers() []dockerclient.LoggingDriver {
availableLoggingDriversEnv := os.Getenv("ECS_AVAILABLE_LOGGING_DRIVERS")
loggingDriverDecoder := json.NewDecoder(strings.NewReader(availableLoggingDriversEnv))
var availableLoggingDrivers []dockerclient.LoggingDriver
err := loggingDriverDecoder.Decode(&availableLoggingDrivers)
// EOF means the string was blank as opposed to UnexpectedEof which means an
// invalid parse
// Blank is not a warning; we have sane defaults
if err != io.EOF && err != nil {
err := fmt.Errorf("Invalid format for \"ECS_AVAILABLE_LOGGING_DRIVERS\" environment variable; expected a JSON array like [\"json-file\",\"syslog\"]. err %v", err)
seelog.Warn(err)
}
return availableLoggingDrivers
}
func parseNumImagesToDeletePerCycle() int {
numImagesToDeletePerCycleEnvVal := os.Getenv("ECS_NUM_IMAGES_DELETE_PER_CYCLE")
numImagesToDeletePerCycle, err := strconv.Atoi(numImagesToDeletePerCycleEnvVal)
if numImagesToDeletePerCycleEnvVal != "" && err != nil {
seelog.Warnf("Invalid format for \"ECS_NUM_IMAGES_DELETE_PER_CYCLE\", expected an integer. err %v", err)
}
return numImagesToDeletePerCycle
}
func parseNumNonECSContainersToDeletePerCycle() int {
numNonEcsContainersToDeletePerCycleEnvVal := os.Getenv("NONECS_NUM_CONTAINERS_DELETE_PER_CYCLE")
numNonEcsContainersToDeletePerCycle, err := strconv.Atoi(numNonEcsContainersToDeletePerCycleEnvVal)
if numNonEcsContainersToDeletePerCycleEnvVal != "" && err != nil {
seelog.Warnf("Invalid format for \"NONECS_NUM_CONTAINERS_DELETE_PER_CYCLE\", expected an integer. err %v", err)
}
return numNonEcsContainersToDeletePerCycle
}
func parseImagePullBehavior() ImagePullBehaviorType {
ImagePullBehaviorString := os.Getenv("ECS_IMAGE_PULL_BEHAVIOR")
switch ImagePullBehaviorString {
case "always":
return ImagePullAlwaysBehavior
case "once":
return ImagePullOnceBehavior
case "prefer-cached":
return ImagePullPreferCachedBehavior
default:
// Use the default image pull behavior when ECS_IMAGE_PULL_BEHAVIOR is
// "default" or not valid
return ImagePullDefaultBehavior
}
}
func parseInstanceAttributes(errs []error) (map[string]string, []error) {
var instanceAttributes map[string]string
instanceAttributesEnv := os.Getenv("ECS_INSTANCE_ATTRIBUTES")
err := json.Unmarshal([]byte(instanceAttributesEnv), &instanceAttributes)
if instanceAttributesEnv != "" {
if err != nil {
wrappedErr := fmt.Errorf("Invalid format for ECS_INSTANCE_ATTRIBUTES. Expected a json hash: %v", err)
seelog.Error(wrappedErr)
errs = append(errs, wrappedErr)
}
}
for attributeKey, attributeValue := range instanceAttributes {
seelog.Debugf("Setting instance attribute %v: %v", attributeKey, attributeValue)
}
return instanceAttributes, errs
}
func parseAdditionalLocalRoutes(errs []error) ([]cnitypes.IPNet, []error) {
var additionalLocalRoutes []cnitypes.IPNet
additionalLocalRoutesEnv := os.Getenv("ECS_AWSVPC_ADDITIONAL_LOCAL_ROUTES")
if additionalLocalRoutesEnv != "" {
err := json.Unmarshal([]byte(additionalLocalRoutesEnv), &additionalLocalRoutes)
if err != nil {
seelog.Errorf("Invalid format for ECS_AWSVPC_ADDITIONAL_LOCAL_ROUTES, expected a json array of CIDRs: %v", err)
errs = append(errs, err)
}
}
return additionalLocalRoutes, errs
}
func parseTaskCPUMemLimitEnabled() Conditional {
var taskCPUMemLimitEnabled Conditional
taskCPUMemLimitConfigString := os.Getenv("ECS_ENABLE_TASK_CPU_MEM_LIMIT")
// We only want to set taskCPUMemLimit if it is explicitly set to true or false.
// We can do this by checking against the ParseBool default
if taskCPUMemLimitConfigString != "" {
if utils.ParseBool(taskCPUMemLimitConfigString, false) {
taskCPUMemLimitEnabled = ExplicitlyEnabled
} else {
taskCPUMemLimitEnabled = ExplicitlyDisabled
}
}
return taskCPUMemLimitEnabled
}
func parseTaskMetadataThrottles() (int, int) {
var steadyStateRate, burstRate int
rpsLimitEnvVal := os.Getenv("ECS_TASK_METADATA_RPS_LIMIT")
if rpsLimitEnvVal == "" {
seelog.Debug("Environment variable empty: ECS_TASK_METADATA_RPS_LIMIT")
return 0, 0
}
rpsLimitSplits := strings.Split(rpsLimitEnvVal, ",")
if len(rpsLimitSplits) != 2 {
seelog.Warn(`Invalid format for "ECS_TASK_METADATA_RPS_LIMIT", expected: "rateLimit,burst"`)
return 0, 0
}
steadyStateRate, err := strconv.Atoi(strings.TrimSpace(rpsLimitSplits[0]))
if err != nil {
seelog.Warnf(`Invalid format for "ECS_TASK_METADATA_RPS_LIMIT", expected integer for steady state rate: %v`, err)
return 0, 0
}
burstRate, err = strconv.Atoi(strings.TrimSpace(rpsLimitSplits[1]))
if err != nil {
seelog.Warnf(`Invalid format for "ECS_TASK_METADATA_RPS_LIMIT", expected integer for burst rate: %v`, err)
return 0, 0
}
return steadyStateRate, burstRate
}
func parseContainerInstanceTags(errs []error) (map[string]string, []error) {
var containerInstanceTags map[string]string
containerInstanceTagsConfigString := os.Getenv("ECS_CONTAINER_INSTANCE_TAGS")
// If duplicate keys exist, the value of the key will be the value of latter key.
err := json.Unmarshal([]byte(containerInstanceTagsConfigString), &containerInstanceTags)
if containerInstanceTagsConfigString != "" {
if err != nil {
wrappedErr := fmt.Errorf("Invalid format for ECS_CONTAINER_INSTANCE_TAGS. Expected a json hash: %v", err)
seelog.Error(wrappedErr)
errs = append(errs, wrappedErr)
}
}
for tagKey, tagValue := range containerInstanceTags {
seelog.Debugf("Setting instance tag %v: %v", tagKey, tagValue)
}
return containerInstanceTags, errs
}
func parseContainerInstancePropagateTagsFrom() ContainerInstancePropagateTagsFromType {
containerInstancePropagateTagsFromString := os.Getenv("ECS_CONTAINER_INSTANCE_PROPAGATE_TAGS_FROM")
switch containerInstancePropagateTagsFromString {
case "ec2_instance":
return ContainerInstancePropagateTagsFromEC2InstanceType
default:
// Use the default "none" type when ECS_CONTAINER_INSTANCE_PROPAGATE_TAGS_FROM is
// "none" or not valid.
return ContainerInstancePropagateTagsFromNoneType
}
}
func parseEnvVariableUint16(envVar string) uint16 {
envVal := os.Getenv(envVar)
var var16 uint16
if envVal != "" {
var64, err := strconv.ParseUint(envVal, 10, 16)
if err != nil {
seelog.Warnf("Invalid format for \""+envVar+"\" environment variable; expected unsigned integer. err %v", err)
} else {
var16 = uint16(var64)
}
}
return var16
}
func parseEnvVariableDuration(envVar string) time.Duration {
var duration time.Duration
envVal := os.Getenv(envVar)
if envVal == "" {
seelog.Debugf("Environment variable empty: %v", envVar)
} else {
var err error
duration, err = time.ParseDuration(envVal)
if err != nil {
seelog.Warnf("Could not parse duration value: %v for Environment Variable %v : %v", envVal, envVar, err)
}
}
return duration
}
func parseImageCleanupExclusionList(envVar string) []string {
imageEnv := os.Getenv(envVar)
var imageCleanupExclusionList []string
if imageEnv == "" {
seelog.Debugf("Environment variable empty: %s", imageEnv)
} else {
imageCleanupExclusionList = strings.Split(imageEnv, ",")
}
// append known cached internal images to imageCleanupExclusionLis
imageCleanupExclusionList = append(imageCleanupExclusionList, CachedImageNameAgentContainer, CachedImageNamePauseContainer)
for _, image := range imageCleanupExclusionList {
seelog.Infof("Image excluded from cleanup: %s", image)
}
return imageCleanupExclusionList
}