1919Default StorageClass and PersistentVolumeClaims, to be found under /Standards/scs-0211-v1-kaas-default-storage-class.md
2020
2121"""
22-
2322import getopt
24- import sys
25- import time
23+ import inspect
2624import json
2725import logging
26+ import logging .config
2827import os
29- import inspect
28+ import sys
29+ import time
3030
31+ from helper import (SCSTestException , gen_sonobuoy_result_file ,
32+ initialize_logging , print_usage , setup_k8s_client )
3133from kubernetes import client
3234from kubernetes .client .rest import ApiException
33- from helper import gen_sonobuoy_result_file
34- from helper import SCSTestException
35- from helper import initialize_logging
36- from helper import print_usage
37- from helper import setup_k8s_client
38-
39- import logging .config
4035
4136logger = logging .getLogger ("k8s-default-storage-class-check" )
4237
@@ -197,11 +192,16 @@ def __init__(self, kubeconfig):
197192 self .kubeconfig = kubeconfig
198193
199194 def prepare (self ):
195+ """
196+ Sets up k8s client in preparation of the testing.
197+ Checks whether the test pod and/or pvc already exists in the test namespace.
198+ If so, it returns True indecating cleanup has to be executed first.
199+ """
200200 try :
201201 logger .debug ("setup_k8s_client(kubeconfig)" )
202202 self .k8s_core_api , self .k8s_storage_api = setup_k8s_client (self .kubeconfig )
203203 except Exception as exception_message :
204- logger .info (f"L{ inspect .currentframe ().f_lineno } { exception_message } " )
204+ logger .error (f"L{ inspect .currentframe ().f_lineno } { exception_message } " )
205205 self .return_message = f"{ exception_message } "
206206 self .return_code = 1
207207
@@ -225,7 +225,7 @@ def prepare(self):
225225 return True
226226 return False
227227 except ApiException as e :
228- logger .debug (f"Error preparing Environment: { e } " )
228+ logger .error (f"Error preparing Environment: { e } " )
229229 return False
230230
231231 def clean (self ):
@@ -236,14 +236,14 @@ def clean(self):
236236 self .pod_name , self .namespace
237237 )
238238 except :
239- logger .debug (f"The pod { self .pod_name } couldn't be deleted." , exc_info = True )
239+ logger .error (f"The pod { self .pod_name } couldn't be deleted." , exc_info = True )
240240 try :
241241 logger .debug (f"delete pvc:{ self .pvc_name } " )
242242 api_response = self .k8s_core_api .delete_namespaced_persistent_volume_claim (
243243 self .pvc_name , self .namespace
244244 )
245245 except :
246- logger .debug (f"The PVC { self .pvc_name } couldn't be deleted." , exc_info = True )
246+ logger .error (f"The PVC { self .pvc_name } couldn't be deleted." , exc_info = True )
247247 return api_response
248248
249249 def __enter__ (self ):
@@ -268,18 +268,18 @@ def __exit__(self, exc_type, exc_value, traceback):
268268 if isinstance (exc_value , SCSTestException ):
269269 self .return_message = exc_value .args [0 ]
270270 self .return_code = exc_value .return_code
271- print (f"SCSTestException occurred with return_code: { self .return_code } " )
271+ logger . debug (f"SCSTestException occurred with return_code: { self .return_code } " )
272272 else :
273273 # No specific exception, handle normally
274- print (f"Exiting the context with return_code: { self .return_code } " )
274+ logger . debug (f"Exiting the context with return_code: { self .return_code } " )
275275 logger .debug (f"return_code:{ self .return_code } { self .return_message } " )
276276
277277 gen_sonobuoy_result_file (
278278 self .return_code , self .return_message , os .path .basename (__file__ )
279279 )
280- print (f"Exiting the context { self .k8s_core_api } " )
280+ logger . debug (f"Exiting the context { self .k8s_core_api } " )
281281 if exc_type :
282- logger .debug (f"An exception occurred: { exc_value } " )
282+ logger .error (f"An exception occurred: { exc_value } " )
283283 # Return True if the exception should be suppressed, otherwise False
284284 return False
285285
@@ -289,7 +289,7 @@ def main(argv):
289289 try :
290290 opts , args = getopt .gnu_getopt (argv , "k:hd:" , ["kubeconfig=" , "help" , "debug" ])
291291 except getopt .GetoptError as exc :
292- logger .debug (f"{ exc } " , file = sys .stderr )
292+ logger .error (f"{ exc } " , file = sys .stderr )
293293 print_usage ()
294294 return 1
295295
@@ -324,14 +324,14 @@ def main(argv):
324324 k8s_core_api = env .k8s_core_api
325325
326326 try :
327- logger .info ("check_default_storageclass()" )
327+ logger .debug ("check_default_storageclass()" )
328328 default_class_name = check_default_storageclass (env .k8s_storage_api )
329329 except SCSTestException as test_exception :
330- logger .info (f"L{ inspect .currentframe ().f_lineno } { test_exception } " )
330+ logger .error (f"L{ inspect .currentframe ().f_lineno } { test_exception } " )
331331 env .return_message = f"{ test_exception } "
332332 env .return_code = test_exception .return_code
333333 except Exception as exception_message :
334- logger .info (f"L{ inspect .currentframe ().f_lineno } { exception_message } " )
334+ logger .error (f"L{ inspect .currentframe ().f_lineno } { exception_message } " )
335335 env .return_message = f"{ exception_message } "
336336 env .return_code = 1
337337
@@ -359,7 +359,7 @@ def main(argv):
359359 k8s_core_api
360360 )
361361 except SCSTestException as test_exception :
362- logger .info (f"L{ inspect .currentframe ().f_lineno } { test_exception } " )
362+ logger .error (f"L{ inspect .currentframe ().f_lineno } { test_exception } " )
363363 env .return_message = f"{ test_exception } "
364364 env .return_code = test_exception .return_code
365365 except Exception as exception_message :
0 commit comments