From b2cb9f7858cc2f907b9be951dfbae87278d3b535 Mon Sep 17 00:00:00 2001 From: Cristian Bonanno Date: Sat, 25 Jan 2025 00:42:27 +0100 Subject: [PATCH] feat(logging): replaced print with logging Signed-off-by: Cristian Bonanno --- src/main.py | 16 +++++++++++----- src/utils/utils.py | 14 +++++++++----- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/main.py b/src/main.py index 07f3dc1..6fd9591 100644 --- a/src/main.py +++ b/src/main.py @@ -1,31 +1,37 @@ -import pandas as pd +import logging import os + +import pandas as pd from kubernetes import client, config from tabulate import tabulate from utils.utils import list_cm, list_ns, list_secret, usage if __name__ == "__main__": + logging.basicConfig(format='%(asctime)s %(levelname)-8s %(message)s', + level=logging.INFO, + datefmt='%Y-%m-%d %H:%M:%S') + config.load_incluster_config() # when running in a POD # config.load_kube_config() # when running locally api = client.CoreV1Api() namespaces = os.getenv("NAMESPACES", []) selected_ns = [] - cluster_namespaces = {} + cluster_namespaces = {} for ns in list_ns(api).items: - cluster_namespaces.update({ ns.metadata.name : ns }) + cluster_namespaces.update({ns.metadata.name: ns}) if namespaces: for ns in namespaces.split(","): if ns in cluster_namespaces: selected_ns.append(cluster_namespaces[ns]) else: - print(f"WARNING: Namespace {ns} not found. Skipping it.") + logging.warning(f"Namespace {ns} not found. Skipping it.") if not namespaces or not selected_ns: - print("INFO: Namespace list is empty. Checking all namespaces.") + logging.info("Namespace list is empty. Checking all namespaces.") selected_ns = list_ns(api).items df = pd.DataFrame( diff --git a/src/utils/utils.py b/src/utils/utils.py index 5ce36fd..6430866 100644 --- a/src/utils/utils.py +++ b/src/utils/utils.py @@ -1,3 +1,5 @@ +import logging + from kubernetes.client.rest import ApiException @@ -6,7 +8,8 @@ def list_ns(api): api_response = api.list_namespace(pretty=True) return api_response except ApiException as e: - print("Exception when calling CoreV1Api->list_namespace: %s\n" % e) + logging.error("Exception when calling CoreV1Api->list_namespace: %s\n" % + e) def list_cm(api, ns): @@ -24,8 +27,9 @@ def list_secret(api, ns): api_response = api.list_namespaced_secret(namespace=ns, pretty=True) return api_response except ApiException as e: - print("Exception when calling CoreV1Api->list_namespaced_secret: %s\n" % - e) + logging.error( + "Exception when calling CoreV1Api->list_namespaced_secret: %s\n" % + e) def list_pod(api, ns): @@ -34,9 +38,9 @@ def list_pod(api, ns): return api_response except ApiException as e: if "404" in str(e): - print("No POD found in ns %s\n" % ns) + logging.error("No POD found in ns %s\n" % ns) else: - print( + logging.error( "Exception when calling CoreV1Api->list_namespaced_pod: %s\n" % e)