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"
15-
1613def helm_template (
1714 helm_args : Dict ,
1815 helm_chart_path : Optional [str ] = "helm_chart" ,
@@ -146,6 +143,53 @@ def process_run_and_check(args, **kwargs):
146143 logger .error (f"output: { exc .output } " )
147144 raise
148145
146+ def oci_helm_registry_login (helm_registry : str , region : str ):
147+ logger .info (f"Attempting to log into ECR registry: { helm_registry } , using helm registry login." )
148+
149+ aws_command = ["aws" , "ecr" , "get-login-password" , "--region" , region ]
150+
151+ # as we can see the password is being provided by stdin, that would mean we will have to
152+ # pipe the aws_command (it figures out the password) into helm_command.
153+ helm_command = ["helm" , "registry" , "login" , "--username" , "AWS" , "--password-stdin" , helm_registry ]
154+
155+ try :
156+ logger .info ("Starting AWS ECR credential retrieval." )
157+ aws_proc = subprocess .Popen (
158+ aws_command , stdout = subprocess .PIPE , stderr = subprocess .PIPE , text = True # Treat input/output as text strings
159+ )
160+
161+ logger .info ("Starting Helm registry login." )
162+ helm_proc = subprocess .Popen (
163+ helm_command , stdin = aws_proc .stdout , stdout = subprocess .PIPE , stderr = subprocess .PIPE , text = True
164+ )
165+
166+ # Close the stdout stream of aws_proc in the parent process
167+ # to prevent resource leakage (only needed if you plan to do more processing)
168+ aws_proc .stdout .close ()
169+
170+ # Wait for the Helm command (helm_proc) to finish and capture its output
171+ helm_stdout , helm_stderr = helm_proc .communicate ()
172+
173+ # Wait for the AWS process to finish as well
174+ aws_proc .wait ()
175+
176+ if aws_proc .returncode != 0 :
177+ _ , aws_stderr = aws_proc .communicate ()
178+ raise Exception (f"aws command to get password failed. Error: { aws_stderr } " )
179+
180+ if helm_proc .returncode == 0 :
181+ logger .info ("Login to helm registry was successful." )
182+ logger .info (helm_stdout .strip ())
183+ else :
184+ raise Exception (
185+ f"Login to helm registry failed, Exit code: { helm_proc .returncode } , Error: { helm_stderr .strip ()} "
186+ )
187+
188+ except FileNotFoundError as e :
189+ # This catches errors if 'aws' or 'helm' are not in the PATH
190+ raise Exception (f"Command not found. Please ensure '{ e .filename } ' is installed and in your system's PATH." )
191+ except Exception as e :
192+ raise Exception (f"An unexpected error occurred: { e } ." )
149193
150194def helm_upgrade (
151195 name : str ,
@@ -168,10 +212,14 @@ def helm_upgrade(
168212 apply_crds_from_chart (chart_dir )
169213
170214 # 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 } " )
215+ try :
216+ registry , repository , region = oci_chart_info ()
217+
218+ oci_helm_registry_login (registry , region )
219+ except Exception as e :
220+ raise Exception (f"Failed logging in to the helm registry { registry } . Error: { e } " )
173221
174- chart_dir = f"oci://{ HELM_ECR_REGISTRY } /dev/helm-charts/mongodb-kubernetes "
222+ chart_uri = f"oci://{ registry } / { repository } "
175223 command_args = _create_helm_args (helm_args , helm_options )
176224 args = [
177225 "helm" ,
@@ -181,89 +229,28 @@ def helm_upgrade(
181229 * command_args ,
182230 name ,
183231 ]
184- custom_operator_version = "0.0.0+68e3eec04df1df00072e1bb2"
232+
185233 if custom_operator_version :
186234 args .append (f"--version={ custom_operator_version } " )
235+ else :
236+ published_chart_version = os .environ .get ("OPERATOR_VERSION" )
237+ if not published_chart_version :
238+ logger .info ("OPERATOR_VERSION env var is not set" )
239+ args .append (f"--version=0.0.0+{ published_chart_version } " )
187240
188- args .append (chart_dir )
241+ args .append (chart_uri )
189242
190243 command = " " .join (args )
191244 process_run_and_check (command , check = True , capture_output = True , shell = True )
192245
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- ]
246+ def oci_chart_info ():
247+ registry = os .environ .get ("OCI_HELM_REGISTRY" )
248+ repository = os .environ .get ("OCI_HELM_REPOSITORY" )
249+ region = os .environ .get ("OCI_HELM_REGION" )
215250
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
251+ print (f"oci chart details in test image is registry { registry } , repo { repository } , region { region } " )
259252
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
253+ return registry , f"{ repository } /mongodb-kubernetes" , region
267254
268255def apply_crds_from_chart (chart_dir : str ):
269256 crd_files = glob .glob (os .path .join (chart_dir , "crds" , "*.yaml" ))
0 commit comments