feat: add namespace detection for finding empty namespaces#490
feat: add namespace detection for finding empty namespaces#490matanryngler wants to merge 1 commit intoyonahd:mainfrom
Conversation
Add functionality to detect unused namespaces that contain no resources (excluding default resources like default service account and kube-root-ca.crt). Changes: - Add pkg/kor/namespaces.go with comprehensive resource counting - Add cmd/kor/namespaces.go for CLI command (kor namespace, kor ns) - Add namespace support to 'kor all' and multi-resource commands - Add comprehensive test suite in pkg/kor/namespaces_test.go - Add exception configuration in pkg/kor/exceptions/namespaces/ - Update pkg/kor/kor.go to include ExceptionNamespaces in Config - Check 15+ resource types including workloads, services, storage, RBAC - Include recent events check to avoid removing actively used namespaces - Skip system namespaces (kube-system, kube-public, kube-node-lease) - Support standard kor filtering options (labels, age, exceptions) Resolves #92
|
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #490 +/- ##
==========================================
+ Coverage 44.02% 44.22% +0.19%
==========================================
Files 67 69 +2
Lines 4502 4785 +283
==========================================
+ Hits 1982 2116 +134
- Misses 2248 2359 +111
- Partials 272 310 +38 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Pull Request Overview
This PR adds namespace detection functionality to find empty namespaces that contain no resources (excluding default resources like default service account and kube-root-ca.crt configmap). This feature helps identify unused namespaces that can be safely removed from the cluster.
Key changes:
- Implements comprehensive resource counting across 15+ resource types to determine if a namespace is truly empty
- Adds CLI commands for namespace detection with standard kor filtering options
- Integrates namespace detection into existing multi-resource and "all" commands
Reviewed Changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| pkg/kor/namespaces.go | Core implementation with resource counting logic and namespace processing |
| pkg/kor/namespaces_test.go | Comprehensive test suite covering empty/non-empty namespaces and filtering |
| cmd/kor/namespaces.go | CLI command implementation with aliases (ns, namespaces) |
| pkg/kor/kor.go | Configuration update to include ExceptionNamespaces |
| pkg/kor/exceptions/namespaces/namespaces.json | Exception configuration file for namespaces |
| pkg/kor/all.go | Integration of namespace detection into "all" command |
| pkg/kor/multi.go | Integration into multi-resource commands |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
| } | ||
|
|
||
| // Default service account token secret (for older Kubernetes versions) | ||
| if resourceType == "secret" && name == "default-token-" { |
There was a problem hiding this comment.
The string comparison for default token secrets is incorrect. It should check if the name starts with 'default-token-' using strings.HasPrefix(name, 'default-token-') instead of exact equality.
| continue | ||
| } | ||
|
|
||
| if pass, _ := filter.SetObject(&ns).Run(filterOpts); pass { |
There was a problem hiding this comment.
The variable 'filter' is undefined. This should likely be 'filters.Filter' or a similar properly imported/defined filter object.
| if pass, _ := filter.SetObject(&ns).Run(filterOpts); pass { | |
| if pass, _ := filters.NewFilter().SetObject(&ns).Run(filterOpts); pass { |
|
|
||
| if opts.DeleteFlag { | ||
| if unusedNamespaces, err = DeleteResource(unusedNamespaces, clientset, "", "Namespace", opts.NoInteractive); err != nil { | ||
| fmt.Fprintf(os.Stderr, "Failed to delete Namespace %s: %v\n", unusedNamespaces, err) |
There was a problem hiding this comment.
The error message format is incorrect. It's trying to print the entire slice 'unusedNamespaces' where it should print individual namespace names. This will result in an unreadable error message.
| fmt.Fprintf(os.Stderr, "Failed to delete Namespace %s: %v\n", unusedNamespaces, err) | |
| var nsNames []string | |
| for _, ns := range unusedNamespaces { | |
| nsNames = append(nsNames, ns.Name) | |
| } | |
| fmt.Fprintf(os.Stderr, "Failed to delete Namespace(s) %s: %v\n", strings.Join(nsNames, ", "), err) |
Add functionality to detect unused namespaces that contain no resources (excluding default resources like default service account and kube-root-ca.crt).
Changes:
pkg/kor/namespaces.gowith comprehensive resource countingcmd/kor/namespaces.gofor CLI command (kor namespace,kor ns)kor alland multi-resource commandspkg/kor/namespaces_test.gopkg/kor/exceptions/namespaces/pkg/kor/kor.goto include ExceptionNamespaces in ConfigUsage:
Testing:
Resolves #92