From 4090a6fec226d508cd798e8bb47aff4fc84eb0c5 Mon Sep 17 00:00:00 2001 From: RupakVeerla <54211182+RupakVeerla@users.noreply.github.com> Date: Thu, 29 Sep 2022 11:40:29 +0530 Subject: [PATCH 1/2] Add container option in waitjob read namespace pod log --- contents/job-wait.py | 36 +++++++++++++++++++----------------- plugin.yaml | 6 ++++++ 2 files changed, 25 insertions(+), 17 deletions(-) diff --git a/contents/job-wait.py b/contents/job-wait.py index 6a4543f..34ef3bb 100644 --- a/contents/job-wait.py +++ b/contents/job-wait.py @@ -26,18 +26,19 @@ def wait(): retries = int(environ.get("RD_CONFIG_RETRIES")) sleep = float(environ.get("RD_CONFIG_SLEEP")) show_log = environ.get("RD_CONFIG_SHOW_LOG") == "true" + container = environ.get("RD_CONFIG_CONTAINER") # Poll for completion if retries retries_count = 0 completed = False - while True: common.connect() - #validate retries + # validate retries if retries_count != 0: - log.warning("An error occurred - retries: {0}".format(retries_count)) + log.warning( + "An error occurred - retries: {0}".format(retries_count)) retries_count = retries_count + 1 if retries_count > retries: @@ -46,13 +47,13 @@ def wait(): if show_log and not completed: log.debug("Searching for pod associated with job") - + start_time = time.time() - timeout = 300 #Revisar si este tiempo es suficiente para pods que no logran ser creados + timeout = 300 # Revisar si este tiempo es suficiente para pods que no logran ser creados while True: core_v1 = client.CoreV1Api() try: - #get available pod + # get available pod pod_list = core_v1.list_namespaced_pod( namespace, label_selector="job-name==" + name @@ -60,7 +61,7 @@ def wait(): first_item = pod_list.items[0] pod_name = first_item.metadata.name - #try get available log + # try get available log core_v1.read_namespaced_pod_log(name=pod_name, namespace=namespace) break @@ -73,19 +74,21 @@ def wait(): time.sleep(15) if timeout and time.time() - start_time > timeout: # pragma: no cover raise TimeoutError - + log.info("Fetching logs from pod: {0}".format(pod_name)) - + if retries_count == 1: - log.info("========================== job log start ==========================") + log.info( + "========================== job log start ==========================") w = watch.Watch() for line in w.stream(core_v1.read_namespaced_pod_log, - name=pod_name, - namespace=namespace): + name=pod_name, + namespace=namespace, + container=container): print(line.encode('ascii', 'ignore')) - #check status job + # check status job batch_v1 = client.BatchV1Api() api_response = batch_v1.read_namespaced_job_status( @@ -105,13 +108,13 @@ def wait(): if completed: if show_log: - log.info("=========================== job log end ===========================") + log.info( + "=========================== job log end ===========================") break log.info("Waiting for job completion") time.sleep(sleep) - if api_response.status.succeeded: log.info("Job succeeded") sys.exit(0) @@ -124,13 +127,12 @@ def wait(): sys.exit(1) - def main(): if environ.get("RD_CONFIG_DEBUG") == "true": log.setLevel(logging.DEBUG) log.debug("Log level configured for DEBUG") - #common.connect() + # common.connect() wait() diff --git a/plugin.yaml b/plugin.yaml index 77fdfa7..01dc598 100644 --- a/plugin.yaml +++ b/plugin.yaml @@ -1819,6 +1819,12 @@ providers: required: false renderingOptions: groupName: Authentication + - name: container + type: String + title: "Container" + default: "" + description: "Get logs from this container" + required: false - name: url type: String title: "Cluster URL" From af0368545555a630a2bdecb28f8476b5b86806de Mon Sep 17 00:00:00 2001 From: RupakVeerla <54211182+RupakVeerla@users.noreply.github.com> Date: Mon, 3 Oct 2022 21:26:28 +0530 Subject: [PATCH 2/2] Add container in while loop --- contents/job-wait.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/contents/job-wait.py b/contents/job-wait.py index 34ef3bb..0ded87f 100644 --- a/contents/job-wait.py +++ b/contents/job-wait.py @@ -63,7 +63,7 @@ def wait(): # try get available log core_v1.read_namespaced_pod_log(name=pod_name, - namespace=namespace) + namespace=namespace, container=container) break except ApiException as ex: log.warning("Pod is not ready, status: %d", ex.status) @@ -84,8 +84,7 @@ def wait(): w = watch.Watch() for line in w.stream(core_v1.read_namespaced_pod_log, name=pod_name, - namespace=namespace, - container=container): + namespace=namespace, container=container): print(line.encode('ascii', 'ignore')) # check status job