11import os
22import shlex
3+ import shutil
34
45from pytest_testconfig import config as py_config
56from pytest import Item
67from pyhelper_utils .shell import run_command
78from simple_logger .logger import get_logger
89from utilities .exceptions import InvalidArgumentsError
9- from utilities .infra import get_rhods_csv_version , get_oc_image_info , generate_openshift_pull_secret_file
10+ from utilities .infra import get_rhods_operator_installed_csv
1011
1112BASE_DIRECTORY_NAME = "must-gather-collected"
1213BASE_RESULTS_DIR = "/home/odh/opendatahub-tests/"
@@ -132,33 +133,32 @@ def run_must_gather(
132133 return run_command (command = shlex .split (must_gather_command ), check = False )[1 ]
133134
134135
135- def get_must_gather_image_info (architecture : str = "linux/amd64" ) -> str :
136- try :
137- csv_version = get_rhods_csv_version ()
138- if csv_version :
139- must_gather_image_manifest = f"quay.io/modh/must-gather:rhoai-{ csv_version .major } .{ csv_version .minor } "
140- pull_secret = generate_openshift_pull_secret_file ()
141- image_info = get_oc_image_info (
142- image = must_gather_image_manifest , architecture = architecture , pull_secret = pull_secret
143- )
144- return f"quay.io/modh/must-gather@{ image_info ['digest' ]} "
145- else :
146- LOGGER .warning (
147- "No RHAOI CSV found. Potentially ODH cluster and must-gather collection is not "
148- "relevant for this cluster"
149- )
150- return ""
151- except Exception as exec :
152- raise RuntimeError (f"Failed to retrieve must-gather image info: { str (exec )} " ) from exec
136+ def get_must_gather_image_info () -> str :
137+ csv_object = get_rhods_operator_installed_csv ()
138+ if not csv_object :
139+ return ""
140+ must_gather_image = [
141+ image ["image" ] for image in csv_object .instance .spec .relatedImages if "odh-must-gather" in image ["image" ]
142+ ]
143+ if not must_gather_image :
144+ LOGGER .warning (
145+ "No RHAOI CSV found. Potentially ODH cluster and must-gather collection is not relevant for this cluster"
146+ )
147+ return ""
148+ return must_gather_image [0 ]
153149
154150
155151def collect_rhoai_must_gather (
156- target_dir : str , since : int , save_collection_output : bool = True , architecture : str = "linux/amd64"
157- ) -> str :
152+ base_file_name : str ,
153+ target_dir : str ,
154+ since : int ,
155+ save_collection_output : bool = True ,
156+ ) -> None :
158157 """
159158 Collect must-gather data for RHOAI cluster.
160159
161160 Args:
161+ base_file_name: (str): Base file name for must-gather compressed file
162162 target_dir (str): Directory to store the must-gather output
163163 since (int): Time in seconds to collect logs from
164164 save_collection_output (bool, optional): Whether to save must-gather command output. Defaults to True.
@@ -167,13 +167,22 @@ def collect_rhoai_must_gather(
167167 Returns:
168168 str: Path to the must-gather output directory, or empty string if collection is skipped
169169 """
170- must_gather_image = get_must_gather_image_info (architecture = architecture )
170+ must_gather_image = get_must_gather_image_info ()
171171 if must_gather_image :
172172 output = run_must_gather (image_url = must_gather_image , target_dir = target_dir , since = f"{ since } s" )
173173 if save_collection_output :
174174 with open (os .path .join (target_dir , "output.log" ), "w" ) as _file :
175175 _file .write (output )
176- return get_must_gather_output_dir (must_gather_path = target_dir )
176+ # get must gather directory to archive
177+ path = get_must_gather_output_dir (must_gather_path = target_dir )
178+ # archive the folder and get the zip file's name
179+ file_name = shutil .make_archive (base_name = base_file_name , format = "zip" , base_dir = path )
180+ # remove the folder that was archived
181+ shutil .rmtree (path = path , ignore_errors = True )
182+ # copy back the archived file to the same path
183+ dest_file = os .path .join (target_dir , file_name )
184+ shutil .copy (src = file_name , dst = dest_file )
185+ LOGGER .info (f"{ dest_file } is collected successfully" )
186+ os .unlink (file_name )
177187 else :
178- LOGGER .warning ("Must-gather collection would be skipped." )
179- return ""
188+ LOGGER .error ("No must-gather image is found from the csv. Must-gather collection would be skipped." )
0 commit comments