Skip to content

Commit

Permalink
Merge pull request #14 from cristian98149/13-namespace-filtering
Browse files Browse the repository at this point in the history
feat: added namespace filtering
  • Loading branch information
cristian98149 authored Jan 14, 2025
2 parents a8be7d7 + 2778763 commit 94e8d21
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
2 changes: 1 addition & 1 deletion chart/Chart.yaml
Original file line number Diff line number Diff line change
@@ -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"
5 changes: 5 additions & 0 deletions chart/templates/cronjob.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 5 additions & 0 deletions chart/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 18 additions & 1 deletion src/main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pandas as pd
import os
from kubernetes import client, config
from tabulate import tabulate

Expand All @@ -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'])
Expand Down

0 comments on commit 94e8d21

Please sign in to comment.