Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Please read [CONTRIBUTING.md](CONTRIBUTING.md) for additional information on contributing to this repository!
<!-- Please read [CONTRIBUTING.md](CONTRIBUTING.md) for additional information on contributing to this repository! -->

<!--
!!!! README !!!! Please fill this out.
Expand All @@ -10,15 +10,17 @@ Please read [CONTRIBUTING.md](CONTRIBUTING.md) for additional information on con

<!-- A short description of what your PR does and what it solves. -->

## What this PR does / why we need it
## What this PR does / why we need it?

## PR Checklist

- [ ] This PR adds K8s exceptions (false positives)
- [ ] This PR adds new code
- [ ] This PR includes test for any new code
- [ ] This PR includes tests for new/existing code
- [ ] This PR adds docs
<!-- - [ ] This PR does something else -->

## Github Issue
## GitHub Issue

[XX-XX]

Expand Down
77 changes: 69 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,11 @@ Kor provides various subcommands to identify and list unused resources. The avai

```
--delete Delete unused resources
-l, --exclude-labels string Selector to filter out, Example: --exclude-labels key1=value1,key2=value2. If --include-labels is set, --exclude-labels will be ignored.
--exclude-namespaces strings Namespaces to be excluded, split by commas. Example: --exclude-namespaces ns1,ns2,ns3. If --include-namespaces is set, --exclude-namespaces will be ignored.
-l, --exclude-labels strings Selector to filter out, Example: --exclude-labels key1=value1,key2=value2. If --include-labels is set, --exclude-labels will be ignored.
-e, --exclude-namespaces strings Namespaces to be excluded, split by commas. Example: --exclude-namespaces ns1,ns2,ns3. If --include-namespaces is set, --exclude-namespaces will be ignored.
--group-by string Group output by (namespace, resource) (default "namespace")
-h, --help help for kor
--include-labels string Selector to filter in, Example: --include-labels key1=value1,key2=value2.
--include-labels string Selector to filter in, Example: --include-labels key1=value1.(currently supports one label)
-n, --include-namespaces strings Namespaces to run on, split by commas. Example: --include-namespaces ns1,ns2,ns3. If set, non-namespaced resources will be ignored.
-k, --kubeconfig string Path to kubeconfig file (optional)
--newer-than string The maximum age of the resources to be considered unused. This flag cannot be used together with older-than flag. Example: --newer-than=1h2m
Expand All @@ -151,7 +152,7 @@ For more information about each subcommand and its available flags, you can use
kor [subcommand] --help
```

## Supported resources and limitations
### Supported resources and limitations

| Resource | What it looks for | Known False Positives ⚠️ |
| --------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
Expand All @@ -169,12 +170,12 @@ kor [subcommand] --help
| CRDs | CRDs not used the cluster | |
| Pvs | PVs not bound to a PVC | |
| Pdbs | PDBs not used in Deployments<br/> PDBs not used in StatefulSets | |
| Jobs | Jobs status is completed | |
| Jobs | Jobs status is completed<br/> Jobs failed with no retries left | |
| ReplicaSets | replicaSets that specify replicas to 0 and has already completed it's work |
| DaemonSets | DaemonSets not scheduled on any nodes |
| StorageClasses | StorageClasses not used by any PVs/PVCs |

## Deleting Unused resources
### Deleting Unused resources

If you want to delete resources in an interactive way using Kor you can run:

Expand All @@ -194,7 +195,7 @@ To delete with no prompt ( ⚠️ use with caution):
kor configmap --include-namespaces my-namespace --delete --no-interactive
```

## Ignore Resources
### Ignore Resources

The resources labeled with:

Expand All @@ -204,7 +205,7 @@ kor/used=true

Will be ignored by kor even if they are unused. You can add this label to resources you want to ignore.

## Force clean Resources
### Force clean Resources

The resources labeled with:

Expand All @@ -214,6 +215,66 @@ kor/used=false

Will be cleaned always. This is a good way to mark resources for later cleanup.

### Output Formats

Kor supports three output formats: `table`, `json`, and `yaml`. The default output format is `table`.
Additionally, you can use the `--group-by` flag to group the output by `namespace` or `resource`.

#### Group by resource

```sh
kor all --group-by=resource --output=table
```
```
Unused ConfigMaps:
+---+-----------+---------------+
| # | NAMESPACE | RESOURCE NAME |
+---+-----------+---------------+
| 1 | ns1 | cm1 |
| 2 | ns1 | cm2 |
| 3 | ns2 | cm3 |
+---+-----------+---------------+
Unused Deployments:
+---+-----------+---------------+
| # | NAMESPACE | RESOURCE NAME |
+---+-----------+---------------+
| 1 | ns1 | deploy1 |
| 2 | ns2 | deploy2 |
+---+-----------+---------------+
Unused ReplicaSets:
+---+-----------+--------------------+
| # | NAMESPACE | RESOURCE NAME |
+---+-----------+--------------------+
| 1 | ns1 | deploy1-654d48b75f |
| 2 | ns2 | deploy2-79f48888c6 |
+---+-----------+--------------------+
```

#### Group by namespace

```sh
kor all --group-by=namespace --output=table
```
```
Unused resources in namespace: "ns1"
+---+---------------+--------------------+
| # | RESOURCE TYPE | RESOURCE NAME |
+---+---------------+--------------------+
| 1 | ConfigMap | cm1 |
| 2 | ConfigMap | cm2 |
| 3 | ReplicaSet | deploy1-654d48b75f |
| 4 | Deployment | deploy1 |
+---+---------------+--------------------+
Unused resources in namespace: "ns2"
+---+---------------+--------------------+
| # | RESOURCE TYPE | RESOURCE NAME |
+---+---------------+--------------------+
| 1 | ReplicaSet | deploy2-79f48888c6 |
| 2 | ConfigMap | cm3 |
| 3 | Deployment | deploy2 |
+---+---------------+--------------------+
```

## In Cluster Usage

To use this tool inside the cluster running as a CronJob and sending the results to a Slack Webhook as raw text(has characters limits of 4000) or to a Slack channel by uploading a file(recommended), you can use the following commands:
Expand Down
24 changes: 3 additions & 21 deletions charts/kor/Chart.yaml
Original file line number Diff line number Diff line change
@@ -1,24 +1,6 @@
apiVersion: v2
name: kor
description: A Helm chart for Kubernetes

# A chart can be either an 'application' or a 'library' chart.
#
# Application charts are a collection of templates that can be packaged into versioned archives
# to be deployed.
#
# Library charts provide useful utilities or functions for the chart developer. They're included as
# a dependency of application charts to inject those utilities and functions into the rendering
# pipeline. Library charts do not define any templates and therefore cannot be deployed.
description: A Kubernetes Helm Chart to discover orphaned resources using kor
type: application

# This is the chart version. This version number should be incremented each time you make changes
# to the chart and its templates, including the app version.
# Versions are expected to follow Semantic Versioning (https://semver.org/)
version: 0.1.4

# This is the version number of the application being deployed. This version number should be
# incremented each time you make changes to the application. Versions are not expected to
# follow Semantic Versioning. They should reflect the version the application is using.
# It is recommended to use it with quotes.
appVersion: "0.3.4"
version: 0.1.6
appVersion: "0.4.0"
23 changes: 12 additions & 11 deletions charts/kor/README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
# kor

![Version: 0.1.4](https://img.shields.io/badge/Version-0.1.4-informational?style=flat-square) ![Type: application](https://img.shields.io/badge/Type-application-informational?style=flat-square) ![AppVersion: 0.3.4](https://img.shields.io/badge/AppVersion-0.3.4-informational?style=flat-square)
![Version: 0.1.6](https://img.shields.io/badge/Version-0.1.6-informational?style=flat-square) ![Type: application](https://img.shields.io/badge/Type-application-informational?style=flat-square) ![AppVersion: 0.4.0](https://img.shields.io/badge/AppVersion-0.4.0-informational?style=flat-square)

A Helm chart for Kubernetes
A Kubernetes Helm Chart to discover orphaned resources using kor

## Values

| Key | Type | Default | Description |
|-----|------|---------|-------------|
| additionalLabels | object | `{}` | Custom labels to add into metadata |
| cronJob.args[0] | string | `"all"` | |
| cronJob.command[0] | string | `"kor"` | |
| cronJob.enabled | bool | `false` | |
Expand Down Expand Up @@ -41,17 +42,17 @@ A Helm chart for Kubernetes
| prometheusExporter.service.port | int | `8080` | |
| prometheusExporter.service.type | string | `"ClusterIP"` | |
| prometheusExporter.serviceMonitor.enabled | bool | `true` | |
| prometheusExporter.serviceMonitor.interval | string | `"30s"` | |
| prometheusExporter.serviceMonitor.labels | object | `{}` | |
| prometheusExporter.serviceMonitor.interval | string | `"30s"` | Set how frequently Prometheus should scrape |
| prometheusExporter.serviceMonitor.labels | object | `{}` | Service monitor labels |
| prometheusExporter.serviceMonitor.metricRelabelings | list | `[]` | |
| prometheusExporter.serviceMonitor.namespace | string | `""` | |
| prometheusExporter.serviceMonitor.namespace | string | `""` | Set the namespace the ServiceMonitor should be deployed, if empty namespace will be `.Release.Namespace` |
| prometheusExporter.serviceMonitor.relabelings | list | `[]` | |
| prometheusExporter.serviceMonitor.targetLabels | list | `[]` | |
| prometheusExporter.serviceMonitor.targetLabels | list | `[]` | Set of labels to transfer on the Kubernetes Service onto the target. |
| prometheusExporter.serviceMonitor.telemetryPath | string | `"/metrics"` | |
| prometheusExporter.serviceMonitor.timeout | string | `"10s"` | |
| serviceAccount.annotations | object | `{}` | |
| serviceAccount.create | bool | `true` | |
| serviceAccount.name | string | `""` | |
| prometheusExporter.serviceMonitor.timeout | string | `"10s"` | Set timeout for scrape |
| serviceAccount.annotations | object | `{}` | Annotations to add to the service account |
| serviceAccount.create | bool | `true` | Specifies whether a service account should be created |
| serviceAccount.name | string | `""` | If not set and create is true, a name is generated using the fullname template |

----------------------------------------------
Autogenerated from chart metadata using [helm-docs v1.12.0](https://github.com/norwoodj/helm-docs/releases/v1.12.0)
Autogenerated from chart metadata using [helm-docs v1.13.1](https://github.com/norwoodj/helm-docs/releases/v1.13.1)
3 changes: 3 additions & 0 deletions charts/kor/templates/_helpers.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ helm.sh/chart: {{ include "kor.chart" . }}
app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
{{- end }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
{{- with .Values.additionalLabels }}
{{ toYaml . }}
{{- end }}
{{- end }}

{{/*
Expand Down
1 change: 1 addition & 0 deletions charts/kor/templates/cronjob.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ spec:
spec:
template:
metadata:
{{- include "kor.labels" . | nindent 8 }}
name: {{ .Release.Name }}
spec:
serviceAccountName: {{ include "kor.serviceAccountName" . }}
Expand Down
1 change: 1 addition & 0 deletions charts/kor/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ spec:
template:
metadata:
labels:
{{- include "kor.labels" . | nindent 8 }}
app: {{ .Values.prometheusExporter.name }}
spec:
serviceAccountName: {{ include "kor.serviceAccountName" . }}
Expand Down
21 changes: 12 additions & 9 deletions charts/kor/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -61,25 +61,28 @@ prometheusExporter:

serviceMonitor:
enabled: true
# Set the namespace the ServiceMonitor should be deployed, if empty namespace will be .Release.Namespace
# -- Set the namespace the ServiceMonitor should be deployed, if empty namespace will be `.Release.Namespace`
namespace: ""
# Service monitor labels
# -- Service monitor labels
labels: {}
# Set how frequently Prometheus should scrape
# -- Set how frequently Prometheus should scrape
interval: 30s
telemetryPath: /metrics
# Set timeout for scrape
# -- Set timeout for scrape
timeout: 10s
relabelings: []
# Set of labels to transfer on the Kubernetes Service onto the target.
# -- Set of labels to transfer on the Kubernetes Service onto the target.
targetLabels: []
metricRelabelings: []

# -- Custom labels to add into metadata
additionalLabels: {}

serviceAccount:
# Specifies whether a service account should be created
# -- Specifies whether a service account should be created
create: true
# Annotations to add to the service account
# -- Annotations to add to the service account
annotations: {}
# The name of the service account to use.
# If not set and create is true, a name is generated using the fullname template
# -- The name of the service account to use.
# -- If not set and create is true, a name is generated using the fullname template
name: ""
3 changes: 2 additions & 1 deletion cmd/kor/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func init() {
rootCmd.PersistentFlags().BoolVar(&opts.DeleteFlag, "delete", false, "Delete unused resources")
rootCmd.PersistentFlags().BoolVar(&opts.NoInteractive, "no-interactive", false, "Do not prompt for confirmation when deleting resources. Be careful using this flag!")
rootCmd.PersistentFlags().BoolVarP(&opts.Verbose, "verbose", "v", false, "Verbose output (print empty namespaces)")
rootCmd.PersistentFlags().StringVar(&opts.GroupBy, "group-by", "namespace", "Group output by (namespace, resource)")
addFilterOptionsFlag(rootCmd, filterOptions)
}

Expand All @@ -80,6 +81,6 @@ func addFilterOptionsFlag(cmd *cobra.Command, opts *filters.Options) {
cmd.PersistentFlags().StringVar(&opts.NewerThan, "newer-than", opts.NewerThan, "The maximum age of the resources to be considered unused. This flag cannot be used together with older-than flag. Example: --newer-than=1h2m")
cmd.PersistentFlags().StringVar(&opts.OlderThan, "older-than", opts.OlderThan, "The minimum age of the resources to be considered unused. This flag cannot be used together with newer-than flag. Example: --older-than=1h2m")
cmd.PersistentFlags().StringVar(&opts.IncludeLabels, "include-labels", opts.IncludeLabels, "Selector to filter in, Example: --include-labels key1=value1.(currently supports one label)")
cmd.PersistentFlags().StringSliceVar(&opts.ExcludeNamespaces, "exclude-namespaces", opts.ExcludeNamespaces, "Namespaces to be excluded, split by commas. Example: --exclude-namespaces ns1,ns2,ns3. If --include-namespaces is set, --exclude-namespaces will be ignored.")
cmd.PersistentFlags().StringSliceVarP(&opts.ExcludeNamespaces, "exclude-namespaces", "e", opts.ExcludeNamespaces, "Namespaces to be excluded, split by commas. Example: --exclude-namespaces ns1,ns2,ns3. If --include-namespaces is set, --exclude-namespaces will be ignored.")
cmd.PersistentFlags().StringSliceVarP(&opts.IncludeNamespaces, "include-namespaces", "n", opts.IncludeNamespaces, "Namespaces to run on, split by commas. Example: --include-namespaces ns1,ns2,ns3. If set, non-namespaced resources will be ignored.")
}
12 changes: 6 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ go 1.22.0
toolchain go1.22.2

require (
github.com/fatih/color v1.16.0
github.com/fatih/color v1.17.0
github.com/olekukonko/tablewriter v0.0.5
github.com/prometheus/client_golang v1.19.0
github.com/prometheus/client_golang v1.19.1
github.com/spf13/cobra v1.8.0
k8s.io/api v0.30.0
k8s.io/apiextensions-apiserver v0.30.0
k8s.io/apimachinery v0.30.0
k8s.io/client-go v0.30.0
k8s.io/api v0.30.1
k8s.io/apiextensions-apiserver v0.30.1
k8s.io/apimachinery v0.30.1
k8s.io/client-go v0.30.1
k8s.io/utils v0.0.0-20230726121419-3b25d923346b
sigs.k8s.io/yaml v1.4.0
)
Expand Down
Loading