Skip to content

Commit 60e5ff6

Browse files
harshadbhatiadaschaa
authored andcommitted
fix(eks): revert shell=True and allow public ecr to work (aws#20724)
This fixes the change made by the following PR. aws#19778 `shell=True` caused regression observed in the following issue: [20402](aws#20402) The code should now allow Public and Private AWS ECR repositories to work with oci prefix. ---- ### All Submissions: * [X] Have you followed the guidelines in our [Contributing guide?](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) ### Adding new Unconventional Dependencies: * [ ] This PR adds new unconventional dependencies following the process described [here](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md/#adding-new-unconventional-dependencies) ### New Features * [ ] Have you added the new feature to an [integration test](https://github.com/aws/aws-cdk/blob/main/INTEGRATION_TESTS.md)? * [ ] Did you use `yarn integ` to deploy the infrastructure and generate the snapshot (i.e. `yarn integ` without `--dry-run`)? No *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 33c2da1 commit 60e5ff6

File tree

3 files changed

+1325
-1324
lines changed

3 files changed

+1325
-1324
lines changed

Diff for: packages/@aws-cdk/aws-eks/lib/kubectl-handler/helm/__init__.py

+16-15
Original file line numberDiff line numberDiff line change
@@ -94,20 +94,30 @@ def helm_handler(event, context):
9494

9595

9696
def get_oci_cmd(repository, version):
97-
97+
# Generates OCI command based on pattern. Public ECR vs Private ECR are treated differently.
9898
cmnd = []
99-
pattern = '\d+.dkr.ecr.[a-z]+-[a-z]+-\d.amazonaws.com'
99+
private_ecr_pattern = '\d+.dkr.ecr.[a-z]+-[a-z]+-\d.amazonaws.com'
100+
public_ecr = 'public.ecr.aws'
100101

101102
registry = repository.rsplit('/', 1)[0].replace('oci://', '')
102103

103-
if re.fullmatch(pattern, registry) is not None:
104+
if re.fullmatch(private_ecr_pattern, registry) is not None:
105+
logger.info("Found AWS private repository")
104106
region = registry.replace('.amazonaws.com', '').split('.')[-1]
105107
cmnd = [
106108
f"aws ecr get-login-password --region {region} | " \
107109
f"helm registry login --username AWS --password-stdin {registry}; helm pull {repository} --version {version} --untar"
108110
]
111+
elif registry.startswith(public_ecr):
112+
logger.info("Found AWS public repository, will use default region as deployment")
113+
region = os.environ.get('AWS_REGION', 'us-east-1')
114+
115+
cmnd = [
116+
f"aws ecr-public get-login-password --region {region} | " \
117+
f"helm registry login --username AWS --password-stdin {public_ecr}; helm pull {repository} --version {version} --untar"
118+
]
109119
else:
110-
logger.info("Non AWS OCI repository found")
120+
logger.error("OCI repository format not recognized, falling back to helm pull")
111121
cmnd = ['helm', 'pull', repository, '--version', version, '--untar']
112122

113123
return cmnd
@@ -122,8 +132,7 @@ def get_chart_from_oci(tmpdir, release, repository = None, version = None):
122132
while retry > 0:
123133
try:
124134
logger.info(cmnd)
125-
env = get_env_with_oci_flag()
126-
output = subprocess.check_output(cmnd, stderr=subprocess.STDOUT, cwd=tmpdir, env=env)
135+
output = subprocess.check_output(cmnd, stderr=subprocess.STDOUT, cwd=tmpdir, shell=True)
127136
logger.info(output)
128137

129138
return os.path.join(tmpdir, release)
@@ -137,13 +146,6 @@ def get_chart_from_oci(tmpdir, release, repository = None, version = None):
137146
raise Exception(f'Operation failed after {maxAttempts} attempts: {output}')
138147

139148

140-
def get_env_with_oci_flag():
141-
env = os.environ.copy()
142-
env['HELM_EXPERIMENTAL_OCI'] = '1'
143-
144-
return env
145-
146-
147149
def helm(verb, release, chart = None, repo = None, file = None, namespace = None, version = None, wait = False, timeout = None, create_namespace = None):
148150
import subprocess
149151

@@ -172,8 +174,7 @@ def helm(verb, release, chart = None, repo = None, file = None, namespace = None
172174
retry = maxAttempts
173175
while retry > 0:
174176
try:
175-
env = get_env_with_oci_flag()
176-
output = subprocess.check_output(cmnd, stderr=subprocess.STDOUT, cwd=outdir, env=env)
177+
output = subprocess.check_output(cmnd, stderr=subprocess.STDOUT, cwd=outdir)
177178
logger.info(output)
178179
return
179180
except subprocess.CalledProcessError as exc:

0 commit comments

Comments
 (0)