44from pytest_testconfig import config as py_config
55from pytest import Item
66from pyhelper_utils .shell import run_command
7-
8- from utilities .exceptions import InvalidArguments
7+ from simple_logger . logger import get_logger
8+ from utilities .exceptions import InvalidArgumentsError
99from utilities .infra import get_rhods_csv_version , get_oc_image_info , generate_openshift_pull_secret_file
1010
1111BASE_DIRECTORY_NAME = "must-gather-collected"
12+ BASE_RESULTS_DIR = "/home/odh/opendatahub-tests/"
13+ LOGGER = get_logger (name = __name__ )
1214
1315
1416def get_base_dir () -> str :
15- if os .path .exists ("/home/odh/opendatahub-tests/" ):
17+ if os .path .exists (BASE_RESULTS_DIR ):
1618 # we are running from jenkins.
17- return "/home/odh/opendatahub-tests/ results"
19+ return os . path . join ( BASE_RESULTS_DIR , " results")
1820 else :
1921 # this is local run
2022 return ""
2123
2224
2325def set_must_gather_collector_values () -> dict [str , str ]:
2426 py_config ["must_gather_collector" ] = {
25- "must_gather_base_directory" : f" { get_base_dir ()} { BASE_DIRECTORY_NAME } " ,
27+ "must_gather_base_directory" : os . path . join ( get_base_dir (), BASE_DIRECTORY_NAME ) ,
2628 }
2729 return py_config ["must_gather_collector" ]
2830
@@ -81,8 +83,22 @@ def run_must_gather(
8183 component_name : str = "" ,
8284 namespaces_dict : dict [str , str ] | None = None ,
8385) -> str :
86+ """
87+ Process the arguments to build must-gather command and run the same
88+
89+ Args:
90+ image_url (str): must-gather image url
91+ target_dir (str): must-gather target directory
92+ since (str): duration in seconds for must-gather log collection
93+ component_name (str): must-gather component name
94+ namespaces_dict (dict[str, str] | None): namespaces dict for extra data collection from different component
95+ namespaces
96+
97+ Returns:
98+ str: must-gather output
99+ """
84100 if component_name and namespaces_dict :
85- raise InvalidArguments ("component name and namespaces can't be passed together" )
101+ raise InvalidArgumentsError ("component name and namespaces can't be passed together" )
86102
87103 must_gather_command = "oc adm must-gather"
88104 if target_dir :
@@ -96,41 +112,56 @@ def run_must_gather(
96112 elif namespaces_dict :
97113 namespace_str = ""
98114 if namespaces_dict .get ("operator" ):
99- namespace_str += f"export OPERATOR_NAMESPACE={ namespaces_dict ['operator' ]} ;"
115+ namespace_str += f"export OPERATOR_NAMESPACE={ shlex . quote ( namespaces_dict ['operator' ]) } ;"
100116 if namespaces_dict .get ("notebooks" ):
101- namespace_str += f"export NOTEBOOKS_NAMESPACE={ namespaces_dict ['notebooks' ]} ;"
117+ namespace_str += f"export NOTEBOOKS_NAMESPACE={ shlex . quote ( namespaces_dict ['notebooks' ]) } ;"
102118 if namespaces_dict .get ("monitoring" ):
103- namespace_str += f"export MONITORING_NAMESPACE={ namespaces_dict ['monitoring' ]} ;"
119+ namespace_str += f"export MONITORING_NAMESPACE={ shlex . quote ( namespaces_dict ['monitoring' ]) } ;"
104120 if namespaces_dict .get ("application" ):
105- namespace_str += f"export APPLICATIONS_NAMESPACE={ namespaces_dict ['application' ]} ;"
121+ namespace_str += f"export APPLICATIONS_NAMESPACE={ shlex . quote ( namespaces_dict ['application' ]) } ;"
106122 if namespaces_dict .get ("model_registries" ):
107- namespace_str += f"export MODEL_REGISTRIES_NAMESPACE={ namespaces_dict ['model_registries' ]} ;"
123+ namespace_str += f"export MODEL_REGISTRIES_NAMESPACE={ shlex . quote ( namespaces_dict ['model_registries' ]) } ;"
108124 if namespaces_dict .get ("ossm" ):
109- namespace_str += f"export OSSM_NS={ namespaces_dict ['ossm' ]} ;"
125+ namespace_str += f"export OSSM_NS={ shlex . quote ( namespaces_dict ['ossm' ]) } ;"
110126 if namespaces_dict .get ("knative" ):
111- namespace_str += f"export KNATIVE_NS={ namespaces_dict ['knative' ]} ;"
127+ namespace_str += f"export KNATIVE_NS={ shlex . quote ( namespaces_dict ['knative' ]) } ;"
112128 if namespaces_dict .get ("auth" ):
113- namespace_str += f"export AUTH_NS={ namespaces_dict ['auth' ]} ;"
114- must_gather_command += " /usr/bin/gather"
129+ namespace_str += f"export AUTH_NS={ shlex . quote ( namespaces_dict ['auth' ]) } ;"
130+ must_gather_command += f" -- ' { namespace_str } /usr/bin/gather' "
115131
116132 return run_command (command = shlex .split (must_gather_command ), check = False )[1 ]
117133
118134
119135def get_must_gather_image_info (architecture : str = "linux/amd64" ) -> str :
120- csv_version = get_rhods_csv_version ()
121- must_gather_image_manifest = f"quay.io/modh/must-gather:rhoai-{ csv_version .major } .{ csv_version .minor } "
122- image_info = get_oc_image_info (
123- image = must_gather_image_manifest , architecture = architecture , pull_secret = generate_openshift_pull_secret_file ()
124- )
125- return f"quay.io/modh/must-gather@{ image_info ['digest' ]} "
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
126153
127154
128155def collect_rhoai_must_gather (
129156 target_dir : str , since : int , save_collection_output : bool = True , architecture : str = "linux/amd64"
130157) -> str :
131158 must_gather_image = get_must_gather_image_info (architecture = architecture )
132- output = run_must_gather (image_url = must_gather_image , target_dir = target_dir , since = f"{ since } s" )
133- if save_collection_output :
134- with open (os .path .join (target_dir , "output.log" ), "w" ) as _file :
135- _file .write (output )
136- return get_must_gather_output_dir (must_gather_path = target_dir )
159+ if must_gather_image :
160+ output = run_must_gather (image_url = must_gather_image , target_dir = target_dir , since = f"{ since } s" )
161+ if save_collection_output :
162+ with open (os .path .join (target_dir , "output.log" ), "w" ) as _file :
163+ _file .write (output )
164+ return get_must_gather_output_dir (must_gather_path = target_dir )
165+ else :
166+ LOGGER .warning ("Must-gather collection would be skipped." )
167+ return ""
0 commit comments