Skip to main content

Kubernetes orphaned resources finder and cleaner

Project description

K8s Investigate - Kubernetes Orphaned Resources Finder

A Python-based tool to discover and clean up unused Kubernetes resources in your cluster.

Features

  • 22 resource type scanners: ConfigMaps, Secrets, Services, Deployments, StatefulSets, Pods, Ingresses, PVCs, PVs, Roles, ClusterRoles, RoleBindings, ClusterRoleBindings, ServiceAccounts, HPAs, Jobs, ReplicaSets, DaemonSets, PDBs, NetworkPolicies, StorageClasses, PriorityClasses
  • Multiple output formats: Table (rich), JSON, YAML
  • Label-based filtering: Include/exclude resources by labels
  • Age-based filtering: Find resources older or newer than a threshold
  • Namespace filtering: Scan specific namespaces or exclude namespaces
  • Deletion support: Optionally delete unused resources (with confirmation)
  • Prometheus metrics: Export orphaned resource metrics for monitoring
  • Label override: Mark resources with k8s-investigate/used: "false" to force-flag them

Installation

pip install k8s-investigate

Or install from source:

git clone https://github.com/<org>/k8s-investigate.git
cd k8s-investigate
pip install .

Development

pip install -e ".[dev]"

Quick Start

# Scan all resource types across all namespaces
k8s-investigate scan all

# Scan with reasons why each resource is unused
k8s-investigate scan all --show-reason

Usage

Scan specific resource types

# Single resource type
k8s-investigate scan configmaps
k8s-investigate scan secrets --namespace default

# Multiple types using short names
k8s-investigate scan cm,svc,deploy,sa

Short names: cm=ConfigMaps, svc=Services, deploy=Deployments, sts=StatefulSets, ds=DaemonSets, rs=ReplicaSets, ing=Ingresses, sa=ServiceAccounts, rb=RoleBindings, crb=ClusterRoleBindings, sc=StorageClasses, pc=PriorityClasses, netpol=NetworkPolicies, pdb=PDBs

Filter by namespace

k8s-investigate scan all -n default
k8s-investigate scan all -n dev,staging,production
k8s-investigate scan all --exclude-namespace kube-system,kube-public

Filter by age

k8s-investigate scan all --older-than 7d      # Only resources older than 7 days
k8s-investigate scan all --newer-than 1h      # Only resources created in last hour
k8s-investigate scan all --older-than 1d12h   # Supports combined durations

Output formats

k8s-investigate scan all -o table             # Rich table (default)
k8s-investigate scan all -o json              # JSON output
k8s-investigate scan all -o yaml              # YAML output
k8s-investigate scan all -o json > report.json  # Save to file
k8s-investigate scan all --group-by resource  # Group by type instead of namespace

Delete unused resources

k8s-investigate scan all --delete             # Interactive confirmation
k8s-investigate scan all --delete --yes       # Skip confirmation (use with caution!)
k8s-investigate scan cm,secrets -n staging --delete  # Delete specific types in namespace

Prometheus exporter

k8s-investigate exporter                      # Default: port 8080, 10min interval
k8s-investigate exporter --port 9090 --interval 300
k8s-investigate exporter --exclude-namespace kube-system

Supported Resource Types

Resource Command Detection Logic
ConfigMaps configmaps Not mounted/referenced in any pod
Secrets secrets Not used in pods, ingress TLS, or imagePullSecrets
Services services No matching endpoints
Deployments deployments Zero replicas
StatefulSets statefulsets Zero replicas
DaemonSets daemonsets Not scheduled on any node
ReplicaSets replicasets Zero replicas and no ready/available pods
Pods pods Evicted or CrashLoopBackOff
Ingresses ingresses Backend services don't exist
PVCs pvcs Not mounted in any pod
PVs pvs Not bound to any PVC
Roles roles Not referenced by any RoleBinding
ClusterRoles clusterroles Not referenced by any binding
RoleBindings rolebindings Referenced role/subjects don't exist
ClusterRoleBindings clusterrolebindings Referenced role/subjects don't exist
ServiceAccounts serviceaccounts Not used by any pod or binding
HPAs hpas Target resource doesn't exist
Jobs jobs Completed or failed
PDBs pdbs Selector doesn't match any workload
NetworkPolicies networkpolicies Selector matches no pods
StorageClasses storageclasses Not used by any PV or PVC
PriorityClasses priorityclasses Not used by any pod

Docker

docker build -t k8s-investigate .
docker run --rm -v ~/.kube/config:/root/.kube/config k8s-investigate scan all

Helm

helm install k8s-investigate ./charts/k8s-investigate --namespace k8s-investigate --create-namespace

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

k8s_investigate-0.5.0.tar.gz (23.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

k8s_investigate-0.5.0-py3-none-any.whl (34.3 kB view details)

Uploaded Python 3

File details

Details for the file k8s_investigate-0.5.0.tar.gz.

File metadata

  • Download URL: k8s_investigate-0.5.0.tar.gz
  • Upload date:
  • Size: 23.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for k8s_investigate-0.5.0.tar.gz
Algorithm Hash digest
SHA256 5d7a028a451fc95b6dbd295c591e708688fa91d95a68ee1d2fde26d1022c161c
MD5 865d32cd76e3c65ccbe6abfafe8de725
BLAKE2b-256 f789fa16201eb3fbdaebf12893117efb1e1f3c39c8c01917ad661b51e366450e

See more details on using hashes here.

File details

Details for the file k8s_investigate-0.5.0-py3-none-any.whl.

File metadata

File hashes

Hashes for k8s_investigate-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8c182a620286e2cecdb45b89e4768ba5c248b6fb28cc1dcf03757b6261048327
MD5 21928cb33484b35199b5ca3ffb5c2d57
BLAKE2b-256 eb4f909df0df6c63c57fee4ae07c0b31b9e2a8851cf8123e347cdc7a100be9fe

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page