Skip to content

Commit 5d362cc

Browse files
authored
CDRIVER-5971 Use Amazon ECR to obtain OCI images in EVG (#2058)
* Address Earthly 0.8 compatibility warnings * Workaround repository metalink retrieval errors on CentOS * Update release instructions to use AWS ECR instead of Artifactory * Use `$default_search_registry` to avoid forcing use of Amazon ECR * Also migrate sbom task from Artifactory to Amazon ECR
1 parent c2c72f5 commit 5d362cc

File tree

8 files changed

+220
-115
lines changed

8 files changed

+220
-115
lines changed

.evergreen/config_generator/components/earthly.py

Lines changed: 49 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,18 @@
55
from typing import Iterable, Literal, Mapping, NamedTuple, TypeVar
66

77
from shrub.v3.evg_build_variant import BuildVariant
8-
from shrub.v3.evg_command import BuiltInCommand, EvgCommandType, subprocess_exec
8+
from shrub.v3.evg_command import (
9+
BuiltInCommand,
10+
EvgCommandType,
11+
KeyValueParam,
12+
ec2_assume_role,
13+
expansions_update,
14+
subprocess_exec,
15+
)
916
from shrub.v3.evg_task import EvgTask, EvgTaskRef
1017

18+
from config_generator.etc.function import Function
19+
1120
from ..etc.utils import all_possible
1221

1322
T = TypeVar("T")
@@ -38,7 +47,7 @@
3847
"Valid options for the SASL configuration parameter"
3948
TLSOption = Literal["OpenSSL", "off"]
4049
"Options for the TLS backend configuration parameter (AKA 'ENABLE_SSL')"
41-
CxxVersion = Literal["none"] # TODO: Once CXX-3103 is released, add latest C++ release tag.
50+
CxxVersion = Literal["none"] # TODO: Once CXX-3103 is released, add latest C++ release tag.
4251
"C++ driver refs that are under CI test"
4352

4453
# A separator character, since we cannot use whitespace
@@ -136,6 +145,34 @@ def suffix(self) -> str:
136145
return _SEPARATOR.join(f"{k}={v}" for k, v in self._asdict().items())
137146

138147

148+
# Authenticate with DevProd-provided Amazon ECR instance to use as pull-through cache for DockerHub.
149+
class DockerLoginAmazonECR(Function):
150+
name = 'docker-login-amazon-ecr'
151+
commands = [
152+
# Avoid inadvertently using a pre-existing and potentially conflicting Docker config.
153+
expansions_update(updates=[KeyValueParam(key='DOCKER_CONFIG', value='${workdir}/.docker')]),
154+
ec2_assume_role(role_arn="arn:aws:iam::901841024863:role/ecr-role-evergreen-ro"),
155+
subprocess_exec(
156+
binary="bash",
157+
command_type=EvgCommandType.SETUP,
158+
include_expansions_in_env=[
159+
"AWS_ACCESS_KEY_ID",
160+
"AWS_SECRET_ACCESS_KEY",
161+
"AWS_SESSION_TOKEN",
162+
"DOCKER_CONFIG",
163+
],
164+
args=[
165+
"-c",
166+
'aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin 901841024863.dkr.ecr.us-east-1.amazonaws.com',
167+
],
168+
),
169+
]
170+
171+
@classmethod
172+
def call(cls, **kwargs):
173+
return cls.default_call(**kwargs)
174+
175+
139176
def task_filter(env: EarthlyVariant, conf: Configuration) -> bool:
140177
"""
141178
Control which tasks are actually defined by matching on the platform and
@@ -170,11 +207,16 @@ def earthly_exec(
170207
return subprocess_exec(
171208
"./tools/earthly.sh",
172209
args=[
210+
# Use Amazon ECR as pull-through cache for DockerHub to avoid rate limits.
211+
"--buildkit-image=901841024863.dkr.ecr.us-east-1.amazonaws.com/dockerhub/earthly/buildkitd:v0.8.3",
173212
*(f"--secret={k}" for k in (secrets or ())),
174213
f"+{target}",
214+
# Use Amazon ECR as pull-through cache for DockerHub to avoid rate limits.
215+
"--default_search_registry=901841024863.dkr.ecr.us-east-1.amazonaws.com/dockerhub",
175216
*(f"--{arg}={val}" for arg, val in (args or {}).items()),
176217
],
177218
command_type=EvgCommandType(kind),
219+
include_expansions_in_env=["DOCKER_CONFIG"],
178220
env=env if env else None,
179221
working_dir="mongoc",
180222
)
@@ -209,15 +251,7 @@ def earthly_task(
209251
return EvgTask(
210252
name=name,
211253
commands=[
212-
# Ensure subsequent Docker commands are authenticated.
213-
subprocess_exec(
214-
binary="bash",
215-
command_type=EvgCommandType.SETUP,
216-
args=[
217-
"-c",
218-
r'docker login -u "${artifactory_username}" --password-stdin artifactory.corp.mongodb.com <<<"${artifactory_password}"',
219-
],
220-
),
254+
DockerLoginAmazonECR.call(),
221255
# First, just build the "env-warmup" which will prepare the build environment.
222256
# This won't generate any output, but allows EVG to track it as a separate build step
223257
# for timing and logging purposes. The subequent build step will cache-hit the
@@ -249,6 +283,10 @@ def earthly_task(
249283
]
250284

251285

286+
def functions():
287+
return DockerLoginAmazonECR.defn()
288+
289+
252290
def tasks() -> Iterable[EvgTask]:
253291
for conf in all_possible(Configuration):
254292
# test-example is a target in all configurations

.evergreen/config_generator/components/sbom.py

Lines changed: 47 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,17 @@
33
from config_generator.etc.utils import bash_exec
44

55
from shrub.v3.evg_build_variant import BuildVariant
6-
from shrub.v3.evg_command import BuiltInCommand, EvgCommandType, expansions_update, s3_put
6+
from shrub.v3.evg_command import (
7+
BuiltInCommand,
8+
EvgCommandType,
9+
KeyValueParam,
10+
ec2_assume_role,
11+
expansions_update,
12+
s3_put,
13+
)
714
from shrub.v3.evg_task import EvgTask, EvgTaskRef
815

916
from pydantic import ConfigDict
10-
from typing import Optional
1117

1218

1319
TAG = 'sbom'
@@ -18,56 +24,58 @@ class CustomCommand(BuiltInCommand):
1824
model_config = ConfigDict(arbitrary_types_allowed=True)
1925

2026

21-
def ec2_assume_role(
22-
role_arn: Optional[str] = None,
23-
policy: Optional[str] = None,
24-
duration_seconds: Optional[int] = None,
25-
command_type: Optional[EvgCommandType] = None,
26-
) -> CustomCommand:
27-
return CustomCommand(
28-
command="ec2.assume_role",
29-
params={
30-
"role_arn": role_arn,
31-
"policy": policy,
32-
"duration_seconds": duration_seconds,
33-
},
34-
type=command_type,
35-
)
36-
37-
3827
class SBOM(Function):
3928
name = 'sbom'
4029
commands = [
41-
ec2_assume_role(
42-
command_type=EvgCommandType.SETUP,
43-
role_arn='${kondukto_role_arn}',
44-
),
45-
bash_exec(
46-
command_type=EvgCommandType.SETUP,
47-
include_expansions_in_env=[
48-
'AWS_ACCESS_KEY_ID',
49-
'AWS_SECRET_ACCESS_KEY',
50-
'AWS_SESSION_TOKEN',
51-
],
52-
script='''\
30+
# Authenticate with Kondukto.
31+
*[
32+
ec2_assume_role(
33+
command_type=EvgCommandType.SETUP,
34+
role_arn='${kondukto_role_arn}',
35+
),
36+
bash_exec(
37+
command_type=EvgCommandType.SETUP,
38+
include_expansions_in_env=[
39+
'AWS_ACCESS_KEY_ID',
40+
'AWS_SECRET_ACCESS_KEY',
41+
'AWS_SESSION_TOKEN',
42+
],
43+
script='''\
5344
set -o errexit
5445
set -o pipefail
5546
kondukto_token="$(aws secretsmanager get-secret-value --secret-id "kondukto-token" --region "us-east-1" --query 'SecretString' --output text)"
5647
printf "KONDUKTO_TOKEN: %s\\n" "$kondukto_token" >|expansions.kondukto.yml
5748
''',
58-
),
59-
expansions_update(
60-
command_type=EvgCommandType.SETUP,
61-
file='expansions.kondukto.yml',
62-
),
49+
),
50+
expansions_update(
51+
command_type=EvgCommandType.SETUP,
52+
file='expansions.kondukto.yml',
53+
),
54+
],
55+
# Authenticate with Amazon ECR.
56+
*[
57+
# Avoid inadvertently using a pre-existing and potentially conflicting Podman config.
58+
# Note: podman understands and uses DOCKER_CONFIG despite the name.
59+
expansions_update(updates=[KeyValueParam(key='DOCKER_CONFIG', value='${workdir}/.docker')]),
60+
ec2_assume_role(role_arn="arn:aws:iam::901841024863:role/ecr-role-evergreen-ro"),
61+
bash_exec(
62+
command_type=EvgCommandType.SETUP,
63+
include_expansions_in_env=[
64+
"AWS_ACCESS_KEY_ID",
65+
"AWS_SECRET_ACCESS_KEY",
66+
"AWS_SESSION_TOKEN",
67+
"DOCKER_CONFIG",
68+
],
69+
script='aws ecr get-login-password --region us-east-1 | podman login --username AWS --password-stdin 901841024863.dkr.ecr.us-east-1.amazonaws.com',
70+
),
71+
],
6372
bash_exec(
6473
command_type=EvgCommandType.TEST,
6574
working_dir='mongoc',
6675
include_expansions_in_env=[
67-
'artifactory_password',
68-
'artifactory_username',
6976
'branch_name',
70-
'KONDUKTO_TOKEN',
77+
'DOCKER_CONFIG',
78+
"KONDUKTO_TOKEN",
7179
],
7280
script='.evergreen/scripts/sbom.sh',
7381
),

.evergreen/generated_configs/functions.yml

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,26 @@ functions:
175175
args:
176176
- -c
177177
- EXTRA_CONFIGURE_FLAGS="-DENABLE_PIC=ON ${EXTRA_CONFIGURE_FLAGS}" .evergreen/scripts/compile.sh
178+
docker-login-amazon-ecr:
179+
- command: expansions.update
180+
params:
181+
updates:
182+
- { key: DOCKER_CONFIG, value: "${workdir}/.docker" }
183+
- command: ec2.assume_role
184+
params:
185+
role_arn: arn:aws:iam::901841024863:role/ecr-role-evergreen-ro
186+
- command: subprocess.exec
187+
type: setup
188+
params:
189+
binary: bash
190+
include_expansions_in_env:
191+
- AWS_ACCESS_KEY_ID
192+
- AWS_SECRET_ACCESS_KEY
193+
- AWS_SESSION_TOKEN
194+
- DOCKER_CONFIG
195+
args:
196+
- -c
197+
- aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin 901841024863.dkr.ecr.us-east-1.amazonaws.com
178198
fetch-build:
179199
- command: subprocess.exec
180200
type: setup
@@ -506,15 +526,33 @@ functions:
506526
type: setup
507527
params:
508528
file: expansions.kondukto.yml
529+
- command: expansions.update
530+
params:
531+
updates:
532+
- { key: DOCKER_CONFIG, value: "${workdir}/.docker" }
533+
- command: ec2.assume_role
534+
params:
535+
role_arn: arn:aws:iam::901841024863:role/ecr-role-evergreen-ro
536+
- command: subprocess.exec
537+
type: setup
538+
params:
539+
binary: bash
540+
include_expansions_in_env:
541+
- AWS_ACCESS_KEY_ID
542+
- AWS_SECRET_ACCESS_KEY
543+
- AWS_SESSION_TOKEN
544+
- DOCKER_CONFIG
545+
args:
546+
- -c
547+
- aws ecr get-login-password --region us-east-1 | podman login --username AWS --password-stdin 901841024863.dkr.ecr.us-east-1.amazonaws.com
509548
- command: subprocess.exec
510549
type: test
511550
params:
512551
binary: bash
513552
working_dir: mongoc
514553
include_expansions_in_env:
515-
- artifactory_password
516-
- artifactory_username
517554
- branch_name
555+
- DOCKER_CONFIG
518556
- KONDUKTO_TOKEN
519557
args:
520558
- -c

0 commit comments

Comments
 (0)