1010
1111logger = test_logger .get_test_logger (__name__ )
1212
13+ HELM_REGISTRY_AWS_REGION = "us-east-1"
14+ HELM_ECR_REGISTRY = "268558157000.dkr.ecr.us-east-1.amazonaws.com"
1315
1416def helm_template (
1517 helm_args : Dict ,
@@ -162,8 +164,14 @@ def helm_upgrade(
162164 chart_dir = helm_chart_path if helm_override_path else _helm_chart_dir (helm_chart_path )
163165
164166 if apply_crds_first :
167+ # right now tests image has the entire helm_chart directory, maybe we should just copy the CRDs
165168 apply_crds_from_chart (chart_dir )
166169
170+ # login to helm registry because we are going to install published helm chart
171+ if not helm_registry_login ():
172+ raise Exception (f"Failed logging in to the helm registry { HELM_ECR_REGISTRY } " )
173+
174+ chart_dir = f"oci://{ HELM_ECR_REGISTRY } /dev/helm-charts/mongodb-kubernetes"
167175 command_args = _create_helm_args (helm_args , helm_options )
168176 args = [
169177 "helm" ,
@@ -173,7 +181,7 @@ def helm_upgrade(
173181 * command_args ,
174182 name ,
175183 ]
176-
184+ custom_operator_version = "0.0.0+68e3eec04df1df00072e1bb2"
177185 if custom_operator_version :
178186 args .append (f"--version={ custom_operator_version } " )
179187
@@ -182,6 +190,80 @@ def helm_upgrade(
182190 command = " " .join (args )
183191 process_run_and_check (command , check = True , capture_output = True , shell = True )
184192
193+ def helm_registry_login ():
194+ logger .info (f"Attempting to log into ECR registry: { HELM_ECR_REGISTRY } , using helm registry login." )
195+
196+ aws_command = [
197+ "aws" ,
198+ "ecr" ,
199+ "get-login-password" ,
200+ "--region" ,
201+ HELM_REGISTRY_AWS_REGION
202+ ]
203+
204+ # as we can see the password is being provided by stdin, that would mean we will have to
205+ # pipe the aws_command (it figures out the password) into helm_command.
206+ helm_command = [
207+ "helm" ,
208+ "registry" ,
209+ "login" ,
210+ "--username" ,
211+ "AWS" ,
212+ "--password-stdin" ,
213+ HELM_ECR_REGISTRY
214+ ]
215+
216+ try :
217+ logger .info ("Starting AWS ECR credential retrieval." )
218+ aws_proc = subprocess .Popen (
219+ aws_command ,
220+ stdout = subprocess .PIPE ,
221+ stderr = subprocess .PIPE ,
222+ text = True # Treat input/output as text strings
223+ )
224+
225+ logger .info ("Starting Helm registry login." )
226+ helm_proc = subprocess .Popen (
227+ helm_command ,
228+ stdin = aws_proc .stdout ,
229+ stdout = subprocess .PIPE ,
230+ stderr = subprocess .PIPE ,
231+ text = True
232+ )
233+
234+ # Close the stdout stream of aws_proc in the parent process
235+ # to prevent resource leakage (only needed if you plan to do more processing)
236+ aws_proc .stdout .close ()
237+
238+ # Wait for the Helm command (helm_proc) to finish and capture its output
239+ helm_stdout , helm_stderr = helm_proc .communicate ()
240+
241+ # Wait for the AWS process to finish as well
242+ aws_proc .wait ()
243+
244+ if aws_proc .returncode != 0 :
245+ logger .error (f"aws command to get password failed, (Exit Code { aws_proc .returncode } )." )
246+ # We captured AWS stderr directly, so print it
247+ _ , aws_stderr = aws_proc .communicate ()
248+ logger .error (aws_stderr )
249+ return False
250+
251+ if helm_proc .returncode == 0 :
252+ logger .info ("Login to helm registry was successful." )
253+ logger .info (helm_stdout .strip ())
254+ return True
255+ else :
256+ logger .error (f"Login to helm registry failed, (Exit Code { helm_proc .returncode } )." )
257+ logger .error (helm_stderr .strip ())
258+ return False
259+
260+ except FileNotFoundError as e :
261+ # This catches errors if 'aws' or 'helm' are not in the PATH
262+ logger .error (f"Command not found. Please ensure '{ e .filename } ' is installed and in your system's PATH." )
263+ return False
264+ except Exception as e :
265+ logger .error (f"An unexpected error occurred: { e } ." )
266+ return False
185267
186268def apply_crds_from_chart (chart_dir : str ):
187269 crd_files = glob .glob (os .path .join (chart_dir , "crds" , "*.yaml" ))
0 commit comments