Skip to content

Commit

Permalink
feat: Added --namespaced flag to filter namespaced/non-namespaced res…
Browse files Browse the repository at this point in the history
…ources (#411)

* feat: Support --namespaced flag in CLI

* feat: Support --namespaced flag in Helm chart

* refactor: Namespaced flag state

* Minor fixes

* fix: namespaced flag template

* fix: Helm & Go linting
  • Loading branch information
doronkg authored Feb 20, 2025
1 parent 7f61584 commit 5cfae3f
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 5 deletions.
4 changes: 2 additions & 2 deletions charts/kor/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ apiVersion: v2
name: kor
description: A Kubernetes Helm Chart to discover orphaned resources using kor
type: application
version: 0.1.18
appVersion: "0.5.9"
version: 0.1.19
appVersion: "0.6.0"
maintainers:
- name: "yonahd"
url: "https://github.com/yonahd/kor"
Expand Down
4 changes: 3 additions & 1 deletion charts/kor/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# kor

![Version: 0.1.18](https://img.shields.io/badge/Version-0.1.18-informational?style=flat-square) ![Type: application](https://img.shields.io/badge/Type-application-informational?style=flat-square) ![AppVersion: 0.5.9](https://img.shields.io/badge/AppVersion-0.5.9-informational?style=flat-square)
![Version: 0.1.19](https://img.shields.io/badge/Version-0.1.19-informational?style=flat-square) ![Type: application](https://img.shields.io/badge/Type-application-informational?style=flat-square) ![AppVersion: 0.6.0](https://img.shields.io/badge/AppVersion-0.6.0-informational?style=flat-square)

A Kubernetes Helm Chart to discover orphaned resources using kor

Expand All @@ -22,6 +22,7 @@ A Kubernetes Helm Chart to discover orphaned resources using kor
| cronJob.image.repository | string | `"yonahdissen/kor"` | |
| cronJob.image.tag | string | `"latest"` | |
| cronJob.name | string | `"kor"` | |
| cronJob.namespaced | string | `nil` | Set true/false to explicitly return namespaced/non-namespaced resources |
| cronJob.restartPolicy | string | `"OnFailure"` | |
| cronJob.schedule | string | `"0 1 * * 1"` | |
| cronJob.slackAuthToken | string | `""` | |
Expand All @@ -45,6 +46,7 @@ A Kubernetes Helm Chart to discover orphaned resources using kor
| prometheusExporter.enabled | bool | `true` | |
| prometheusExporter.exporterInterval | string | `""` | |
| prometheusExporter.name | string | `"kor-exporter"` | |
| prometheusExporter.namespaced | string | `nil` | Set true/false to explicitly return namespaced/non-namespaced resources |
| prometheusExporter.service.port | int | `8080` | |
| prometheusExporter.service.type | string | `"ClusterIP"` | |
| prometheusExporter.serviceMonitor.enabled | bool | `true` | |
Expand Down
3 changes: 3 additions & 0 deletions charts/kor/templates/cronjob.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ spec:
{{- toYaml .Values.cronJob.command | nindent 16 }}
args:
{{- toYaml .Values.cronJob.args | nindent 16 }}
{{- if ne .Values.cronJob.namespaced nil }}
- --namespaced={{ .Values.cronJob.namespaced }}
{{- end}}
{{- if .Values.cronJob.slackWebhookUrl }}
- '--slack-webhook-url'
- "${SLACK_WEBHOOK_URL}"
Expand Down
5 changes: 4 additions & 1 deletion charts/kor/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ spec:
{{- toYaml .Values.prometheusExporter.command | nindent 12 }}
args:
{{- toYaml .Values.prometheusExporter.args | nindent 12 }}
{{- if ne .Values.prometheusExporter.namespaced nil}}
- --namespaced={{ .Values.prometheusExporter.namespaced }}
{{- end}}
ports:
- containerPort: 8080
name: http
Expand Down Expand Up @@ -67,4 +70,4 @@ spec:
securityContext:
{{- toYaml . | nindent 8}}
{{- end }}
{{- end }}
{{- end }}
4 changes: 4 additions & 0 deletions charts/kor/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ cronJob:
- kor
args:
- all
# -- Set true/false to explicitly return namespaced/non-namespaced resources
namespaced: null
slackWebhookUrl: ""
slackChannel: ""
slackAuthToken: ""
Expand All @@ -26,6 +28,8 @@ prometheusExporter:
- kor
args:
- exporter
# -- Set true/false to explicitly return namespaced/non-namespaced resources
namespaced: null
deployment:
image:
repository: yonahdissen/kor
Expand Down
2 changes: 2 additions & 0 deletions cmd/kor/all.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ var allCmd = &cobra.Command{
clientset := kor.GetKubeClient(kubeconfig)
apiExtClient := kor.GetAPIExtensionsClient(kubeconfig)
dynamicClient := kor.GetDynamicClient(kubeconfig)
kor.SetNamespacedFlagState(cmd.Flags().Changed("namespaced"))

if response, err := kor.GetUnusedAll(filterOptions, clientset, apiExtClient, dynamicClient, outputFormat, opts); err != nil {
fmt.Println(err)
Expand All @@ -28,5 +29,6 @@ var allCmd = &cobra.Command{
}

func init() {
allCmd.Flags().BoolVar(&opts.Namespaced, "namespaced", true, "If false, non-namespaced resources will be returned, otherwise returning namespaced resources by default. If not used, both are returned")
rootCmd.AddCommand(allCmd)
}
3 changes: 2 additions & 1 deletion cmd/kor/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ var exporterCmd = &cobra.Command{
clientset := kor.GetKubeClient(kubeconfig)
apiExtClient := kor.GetAPIExtensionsClient(kubeconfig)
dynamicClient := kor.GetDynamicClient(kubeconfig)

kor.SetNamespacedFlagState(cmd.Flags().Changed("namespaced"))
kor.Exporter(filterOptions, clientset, apiExtClient, dynamicClient, "json", opts, resourceList)

},
}

func init() {
exporterCmd.Flags().StringSliceVarP(&resourceList, "resources", "r", nil, "Comma-separated list of resources to monitor (e.g., deployment,service)")
exporterCmd.Flags().BoolVar(&opts.Namespaced, "namespaced", true, "If false, non-namespaced resources will be returned, otherwise returning namespaced resources by default. If not used, both are returned")
rootCmd.AddCommand(exporterCmd)
}
1 change: 1 addition & 0 deletions pkg/common/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ type Opts struct {
Token string
GroupBy string
ShowReason bool
Namespaced bool
}
13 changes: 13 additions & 0 deletions pkg/kor/all.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
"github.com/yonahd/kor/pkg/filters"
)

var NamespacedFlagUsed bool

type GetUnusedResourceJSONResponse struct {
ResourceType string `json:"resourceType"`
Namespaces map[string][]string `json:"namespaces"`
Expand Down Expand Up @@ -378,6 +380,13 @@ func GetUnusedAllNonNamespaced(filterOpts *filters.Options, clientset kubernetes
}

func GetUnusedAll(filterOpts *filters.Options, clientset kubernetes.Interface, apiExtClient apiextensionsclientset.Interface, dynamicClient dynamic.Interface, outputFormat string, opts common.Opts) (string, error) {
if NamespacedFlagUsed {
if opts.Namespaced {
return GetUnusedAllNamespaced(filterOpts, clientset, outputFormat, opts)
}
return GetUnusedAllNonNamespaced(filterOpts, clientset, apiExtClient, dynamicClient, outputFormat, opts)
}

unusedAllNamespaced, err := GetUnusedAllNamespaced(filterOpts, clientset, outputFormat, opts)
if err != nil {
fmt.Printf("err: %v\n", err)
Expand Down Expand Up @@ -424,3 +433,7 @@ func GetUnusedAll(filterOpts *filters.Options, clientset kubernetes.Interface, a
return string(jsonResponse), nil
}
}

func SetNamespacedFlagState(isFlagUsed bool) {
NamespacedFlagUsed = isFlagUsed
}

0 comments on commit 5cfae3f

Please sign in to comment.