Skip to content

Commit

Permalink
Merge pull request #17 from cristian98149/15-add-logging
Browse files Browse the repository at this point in the history
feat(logging): replaced print with logging
  • Loading branch information
cristian98149 authored Jan 24, 2025
2 parents 21cf613 + b2cb9f7 commit 4d28195
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
16 changes: 11 additions & 5 deletions src/main.py
Original file line number Diff line number Diff line change
@@ -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(
Expand Down
14 changes: 9 additions & 5 deletions src/utils/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import logging

from kubernetes.client.rest import ApiException


Expand All @@ -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):
Expand All @@ -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):
Expand All @@ -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)

Expand Down

0 comments on commit 4d28195

Please sign in to comment.