2222import logging
2323
2424from base64 import b64decode , b64encode
25- from .validation import is_empty_arg
2625
2726from openserverless .config .app_config import AppConfig
2827from openserverless .error .config_exception import ConfigException
3433
3534
3635def _join_host_port (host , port ):
36+ """
37+ Join host and port into a URL format.
38+ >>> _join_host_port("localhost", "8080")
39+ 'localhost:8080'
40+ >>> _join_host_port("localhost", 8080)
41+ 'localhost:8080'
42+ >>> _join_host_port("localhost", "80")
43+ 'localhost:80'
44+ >>> _join_host_port("localhost", "abcd")
45+ Traceback (most recent call last):
46+ ...
47+ ValueError: Port must be numeric
48+
49+ """
3750 template = "%s:%s"
51+ try :
52+ port_int = int (port )
53+ port = str (port_int )
54+ except (ValueError , TypeError ):
55+ raise ValueError ("Port must be numeric" )
56+
3857 host_requires_bracketing = ":" in host or "%" in host
3958 if host_requires_bracketing :
4059 template = "[%s]:%s"
@@ -54,6 +73,13 @@ def _parse_b64(self, encoded_str):
5473 Decode b64 encoded string
5574 param: encoded_str a Base64 encoded string
5675 return: decoded string
76+ >>> oa = KubeApiClient()
77+ >>> oa._parse_b64("aGVsbG8gd29ybGQ=")
78+ 'hello world'
79+ >>> oa._parse_b64("aGVsbG8gd29ybGQ")
80+ Traceback (most recent call last):
81+ ...
82+ openserverless.error.config_exception.ConfigException: Could not decode base64 encoded value
5783 """
5884 try :
5985 return b64decode (encoded_str ).decode ()
@@ -254,7 +280,13 @@ def get_config_map(self, cm_name, namespace="nuvolaris"):
254280 return None
255281
256282 def post_config_map (self , cm_name , file_or_dir , namespace = "nuvolaris" ):
257-
283+ """
284+ Create a ConfigMap from a file or directory.
285+ :param cm_name: Name of the ConfigMap.
286+ :param file_or_dir: Path to the file or directory containing the data.
287+ :param namespace: Namespace where the ConfigMap will be created.
288+ :return: The created ConfigMap or None if failed.
289+ """
258290 if not os .path .exists (file_or_dir ):
259291 raise ConfigException (f"File or directory { file_or_dir } does not exist." )
260292
@@ -301,6 +333,12 @@ def post_config_map(self, cm_name, file_or_dir, namespace="nuvolaris"):
301333 return None
302334
303335 def delete_config_map (self , cm_name , namespace = "nuvolaris" ):
336+ """
337+ Delete a ConfigMap by name.
338+ :param cm_name: Name of the ConfigMap to delete.
339+ :param namespace: Namespace where the ConfigMap is located.
340+ :return: True if deletion was successful, False otherwise.
341+ """
304342 url = f"{ self .host } /api/v1/namespaces/{ namespace } /configmaps/{ cm_name } "
305343 headers = {"Authorization" : self .token }
306344
@@ -416,8 +454,14 @@ def delete_secret(self, secret_name, namespace="nuvolaris"):
416454 logging .error (f"delete_secret { ex } " )
417455 return False
418456
419- # --- CREA JOB ---
420- def post_job (self , job_name , job_manifest , namespace = "nuvolaris" ):
457+ def post_job (self , job_name , job_manifest , namespace = "nuvolaris" ):
458+ """
459+ Create a Kubernetes job.
460+ :param job_name: Name of the job.
461+ :param job_manifest: Dictionary containing the job manifest.
462+ :param namespace: Namespace where the job will be created.
463+ :return: The created job or None if failed.
464+ """
421465 url = f"{ self .host } /apis/batch/v1/namespaces/{ namespace } /jobs"
422466 headers = {"Authorization" : self .token }
423467 try :
@@ -437,8 +481,13 @@ def post_job(self, job_name, job_manifest, namespace="nuvolaris"):
437481 logging .error (f"post_job { ex } " )
438482 return None
439483
440- # --- OTTIENI POD ---
441484 def get_pod_by_job_name (self , job_name , namespace = "nuvolaris" ):
485+ """
486+ Get the pod name associated with a job by its name.
487+ :param job_name: Name of the job.
488+ :param namespace: Namespace where the job is located.
489+ :return: The pod name if found, None otherwise.
490+ """
442491 url = f"{ self .host } /api/v1/namespaces/{ namespace } /pods"
443492 headers = {"Authorization" : self .token }
444493 try :
@@ -466,17 +515,26 @@ def get_pod_by_job_name(self, job_name, namespace="nuvolaris"):
466515 logging .error (f"get_pod_by_job_name { ex } " )
467516 return None
468517
469- # --- LEGGI LOG POD ---
470518 def stream_pod_logs (self , pod_name , namespace = "nuvolaris" ):
519+ """
520+ Stream logs from a specific pod.
521+ :param pod_name: Name of the pod to stream logs from.
522+ :param namespace: Namespace where the pod is located.
523+ """
471524 url = f"{ self .host } /api/v1/namespaces/{ namespace } /pods/{ pod_name } /log?follow=true"
472525 headers = {"Authorization" : self .token }
473526 with req .get (url , headers = headers , verify = self .ssl_ca_cert , stream = True ) as r :
474527 for line in r .iter_lines ():
475528 if line :
476529 print (line .decode ())
477530
478- # --- CHECK STATUS JOB ---
479531 def check_job_status (self , job_name , namespace = "nuvolaris" ):
532+ """
533+ Check the status of a job by its name.
534+ :param job_name: Name of the job to check.
535+ :param namespace: Namespace where the job is located.
536+ :return: True if the job has succeeded, False otherwise.
537+ """
480538 url = f"{ self .host } /apis/batch/v1/namespaces/{ namespace } /jobs/{ job_name } "
481539 headers = {"Authorization" : self .token }
482540 try :
0 commit comments