Skip to content

Commit 05ae702

Browse files
authored
Merge pull request #183 from CircleCI-Public/implement-dockerLabels
feat: implement container-docker-label-updates
2 parents a00014f + 85ec791 commit 05ae702

File tree

6 files changed

+87
-9
lines changed

6 files changed

+87
-9
lines changed

src/commands/update-service.yml

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,17 +71,26 @@ parameters:
7171
default: ''
7272
container-secret-updates:
7373
description: >
74-
Use this to update or set the values of secrets variables that will be
75-
defined for the containers.
76-
(Existing secrets variables not included in this parameter will not be
77-
removed)
74+
Use this to update or set the values of secret variables that will be defined for the containers.
75+
(Existing secrets variables not included in this parameter will not beremoved)
7876
7977
Expected format:
8078
container=<container-name>,name=<env-var-name>,valueFrom=<env-var-value>,container=...,name=...,valueFrom=...,
8179
8280
Values should not contain commas.
8381
type: string
8482
default: ''
83+
container-docker-label-updates:
84+
description: >
85+
Use this to update or set the values of docker label variables that will be defined for the containers.
86+
(Existing secrets variables not included in this parameter will not be removed)
87+
88+
Expected format:
89+
container=<container-name>,<key>=<env-var-name>,<key>=<env-var-value>,container=...,<key>=...,<key>=...,
90+
91+
Values should not contain commas.
92+
type: string
93+
default: ''
8594
force-new-deployment:
8695
description: |
8796
Whether to force a new deployment of the service.
@@ -185,6 +194,7 @@ steps:
185194
container-image-name-updates: << parameters.container-image-name-updates >>
186195
container-env-var-updates: << parameters.container-env-var-updates >>
187196
container-secret-updates: << parameters.container-secret-updates >>
197+
container-docker-label-updates: << parameters.container-docker-label-updates >>
188198
profile-name: << parameters.profile-name >>
189199
- when:
190200
condition: << parameters.skip-task-definition-registration >>

src/commands/update-task-definition.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,17 @@ parameters:
4545
Values should not contain commas.
4646
type: string
4747
default: ''
48+
container-docker-label-updates:
49+
description: >
50+
Use this to update or set the values of docker label variables that will be defined for the containers.
51+
(Existing secrets variables not included in this parameter will not be removed)
52+
53+
Expected format:
54+
container=<container-name>,<key>=<env-var-name>,<key>=<env-var-value>,container=...,<key>=...,<key>=...,
55+
56+
Values should not contain commas.
57+
type: string
58+
default: ''
4859
profile-name:
4960
description: AWS profile name to be configured.
5061
type: string
@@ -66,6 +77,7 @@ steps:
6677
ECS_PARAM_PROFILE_NAME: <<parameters.profile-name>>
6778
ECS_PARAM_PREVIOUS_REVISION_NUMBER: <<parameters.previous-revision-number>>
6879
ECS_PARAM_CONTAINER_SECRET_UPDATES: <<parameters.container-secret-updates>>
80+
ECS_PARAM_CONTAINER_DOCKER_LABEL_UPDATES: << parameters.container-docker-label-updates >>
6981
- run:
7082
name: Register new task definition
7183
command: <<include(scripts/register-new-task-def.sh)>>

src/jobs/deploy-service-update.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,17 @@ parameters:
126126
Values should not contain commas.
127127
type: string
128128
default: ''
129+
container-docker-label-updates:
130+
description: >
131+
Use this to update or set the values of docker label variables that will be defined for the containers.
132+
(Existing secrets variables not included in this parameter will not be removed)
133+
134+
Expected format:
135+
container=<container-name>,<key>=<env-var-name>,<key>=<env-var-value>,container=...,<key>=...,<key>=...,
136+
137+
Values should not contain commas.
138+
type: string
139+
default: ''
129140
force-new-deployment:
130141
description: |
131142
Whether to force a new deployment of the service.
@@ -242,6 +253,7 @@ steps:
242253
container-image-name-updates: << parameters.container-image-name-updates >>
243254
container-env-var-updates: << parameters.container-env-var-updates >>
244255
container-secret-updates: << parameters.container-secret-updates >>
256+
container-docker-label-updates: << parameters.container-docker-label-updates >>
245257
force-new-deployment: << parameters.force-new-deployment >>
246258
verify-revision-is-deployed: << parameters.verify-revision-is-deployed >>
247259
max-poll-attempts: << parameters.max-poll-attempts >>

src/jobs/update-task-definition.yml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,17 @@ parameters:
7878
Values should not contain commas.
7979
type: string
8080
default: ''
81+
container-docker-label-updates:
82+
description: >
83+
Use this to update or set the values of docker label variables that will be defined for the containers.
84+
(Existing secrets variables not included in this parameter will not be removed)
85+
86+
Expected format:
87+
container=<container-name>,<key>=<env-var-name>,<key>=<env-var-value>,container=...,<key>=...,<key>=...,
88+
89+
Values should not contain commas.
90+
type: string
91+
default: ''
8192
deploy-scheduled-task:
8293
description: >
8394
Set this parameter to true to deploy updated task definition to a scheduled task rule.
@@ -96,8 +107,9 @@ steps:
96107
family: << parameters.family >>
97108
container-image-name-updates: << parameters.container-image-name-updates >>
98109
container-env-var-updates: << parameters.container-env-var-updates >>
99-
profile-name: << parameters.profile-name >>
100110
container-secret-updates: << parameters.container-secret-updates >>
111+
container-docker-label-updates: << parameters.container-docker-label-updates >>
112+
profile-name: << parameters.profile-name >>
101113
- when:
102114
condition: <<parameters.deploy-scheduled-task>>
103115
steps:

src/scripts/get-prev-task.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ ECS_PARAM_CONTAINER_IMAGE_NAME_UPDATES=$(eval echo "$ECS_PARAM_CONTAINER_IMAGE_N
66
ECS_PARAM_CONTAINER_ENV_VAR_UPDATES=$(eval echo "$ECS_PARAM_CONTAINER_ENV_VAR_UPDATES")
77
ECS_PARAM_PROFILE_NAME=$(eval echo "$ECS_PARAM_PROFILE_NAME")
88
ECS_PARAM_CONTAINER_SECRET_UPDATES=$(eval echo "$ECS_PARAM_CONTAINER_SECRET_UPDATES")
9+
ECS_PARAM_CONTAINER_DOCKER_LABEL_UPDATES=$(eval echo "$ECS_PARAM_CONTAINER_DOCKER_LABEL_UPDATES")
910

1011
if [ -n "${ECS_PARAM_PROFILE_NAME}" ]; then
1112
set -- "$@" --profile "${ECS_PARAM_PROFILE_NAME}"
@@ -29,7 +30,7 @@ cat <<< "$ECS_SCRIPT_UPDATE_CONTAINER_DEFS" > "$UPDATE_CONTAINER_DEFS_SCRIPT_FIL
2930

3031

3132
# Prepare container definitions
32-
CONTAINER_DEFS=$(python "$UPDATE_CONTAINER_DEFS_SCRIPT_FILE" "$PREVIOUS_TASK_DEFINITION" "$ECS_PARAM_CONTAINER_IMAGE_NAME_UPDATES" "$ECS_PARAM_CONTAINER_ENV_VAR_UPDATES" "$ECS_PARAM_CONTAINER_SECRET_UPDATES")
33+
CONTAINER_DEFS=$(python "$UPDATE_CONTAINER_DEFS_SCRIPT_FILE" "$PREVIOUS_TASK_DEFINITION" "$ECS_PARAM_CONTAINER_IMAGE_NAME_UPDATES" "$ECS_PARAM_CONTAINER_ENV_VAR_UPDATES" "$ECS_PARAM_CONTAINER_SECRET_UPDATES" "$ECS_PARAM_CONTAINER_DOCKER_LABEL_UPDATES")
3334

3435
# Escape single quotes from environment variables for BASH_ENV
3536

src/scripts/update_container_defs.py

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22
import sys
33
import json
44

5-
65
# shellcheck disable=SC1036 # Hold-over from previous iteration.
76
def run(previous_task_definition, container_image_name_updates,
8-
container_env_var_updates, container_secret_updates):
7+
container_env_var_updates, container_secret_updates, container_docker_label_updates):
98
try:
109
definition = json.loads(previous_task_definition)
1110
container_definitions = definition['taskDefinition']['containerDefinitions']
@@ -61,6 +60,38 @@ def run(previous_task_definition, container_image_name_updates,
6160
except:
6261
raise Exception('Environment variable update parameter could not be processed; please check parameter value: ' + container_env_var_updates)
6362

63+
# Expected format: container=...,string=...,string=...,container=...,string=...,string=
64+
65+
try:
66+
docker_label_kv_pairs = container_docker_label_updates.split(',')
67+
for index, kv_pair in enumerate(docker_label_kv_pairs):
68+
kv = kv_pair.split('=')
69+
key = kv[0].strip()
70+
71+
if key == 'container':
72+
container_name = kv[1].strip()
73+
docker_label_kv = docker_label_kv_pairs[index+1].split('=')
74+
docker_label_key = docker_label_kv[0].strip()
75+
docker_label_value = docker_label_kv[1].strip()
76+
container_entry = container_map.get(container_name)
77+
if container_entry is None:
78+
raise ValueError('The container ' + container_name + ' is not defined in the existing task definition')
79+
container_index = container_entry['index']
80+
docker_label_entry = container_entry['environment_map'].get(docker_label_key)
81+
if docker_label_entry is None:
82+
# The existing container definition does not contain environment variables
83+
if container_definitions[container_index].get('dockerLabels') is None:
84+
container_definitions[container_index]['dockerLabels'] = {}
85+
# This env var does not exist in the existing container definition
86+
container_definitions[container_index]['dockerLabels'][docker_label_key] = docker_label_value
87+
else:
88+
docker_label_index = docker_label_entry['index']
89+
container_definitions[container_index]['dockerLabels'][docker_label_index][docker_label_key] = docker_label_value
90+
except ValueError as value_error:
91+
raise value_error
92+
except:
93+
raise Exception('Docker label update parameter could not be processed; please check parameter value: ' + container_docker_label_updates)
94+
6495
# Expected format: container=...,name=...,valueFrom=...,container=...,name=...,valueFrom=...
6596

6697
try:
@@ -143,7 +174,7 @@ def run(previous_task_definition, container_image_name_updates,
143174

144175
if __name__ == '__main__':
145176
try:
146-
print(run(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4]))
177+
print(run(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4], sys.argv[5]))
147178
except Exception as e:
148179
sys.stderr.write(str(e) + "\n")
149180
exit(1)

0 commit comments

Comments
 (0)