diff --git a/chart/Chart.yaml b/chart/Chart.yaml index ae1b981..6086387 100644 --- a/chart/Chart.yaml +++ b/chart/Chart.yaml @@ -1,5 +1,5 @@ apiVersion: v2 name: kcmsu-chart description: A Helm chart that deploys a CronJob and RBAC for kcmsu. -version: 0.0.2 +version: 0.0.3 appVersion: "latest" diff --git a/chart/templates/cronjob.yaml b/chart/templates/cronjob.yaml index 9533203..2b50303 100644 --- a/chart/templates/cronjob.yaml +++ b/chart/templates/cronjob.yaml @@ -13,4 +13,9 @@ spec: - name: cronjob image: "{{ .Values.image.repository }}:{{ default .Chart.AppVersion .Values.image.tag }}" imagePullPolicy: {{ .Values.image.pullPolicy }} + {{- if .Values.namespaces }} + env: + - name: NAMESPACES + value: {{ join "," .Values.namespaces }} + {{- end }} restartPolicy: OnFailure diff --git a/chart/values.yaml b/chart/values.yaml index d7afe87..daf3f59 100644 --- a/chart/values.yaml +++ b/chart/values.yaml @@ -7,6 +7,11 @@ cronjob: schedule: "*/1 * * * *" # Cron schedule for job execution jobName: kcmsu +# Namespaces to be scanned by the tool +# namespaces: +# - kube-system +# - default + serviceAccount: create: true name: kcmsu diff --git a/src/main.py b/src/main.py index cb886b9..07f3dc1 100644 --- a/src/main.py +++ b/src/main.py @@ -1,4 +1,5 @@ import pandas as pd +import os from kubernetes import client, config from tabulate import tabulate @@ -9,7 +10,23 @@ # config.load_kube_config() # when running locally api = client.CoreV1Api() - selected_ns = list_ns(api).items + namespaces = os.getenv("NAMESPACES", []) + selected_ns = [] + cluster_namespaces = {} + + for ns in list_ns(api).items: + 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.") + + if not namespaces or not selected_ns: + print("INFO: Namespace list is empty. Checking all namespaces.") + selected_ns = list_ns(api).items df = pd.DataFrame( columns=['Namespace', 'Kind', 'Name', 'UsedCount', 'UsedAs', 'UsedBy'])