Skip to content

Commit

Permalink
fix: conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
patricktalmeida committed Nov 3, 2023
2 parents bdcb3b4 + d55b4a9 commit 5c185ba
Show file tree
Hide file tree
Showing 39 changed files with 1,687 additions and 156 deletions.
54 changes: 19 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,17 @@ Kor provides various subcommands to identify and list unused resources. The avai

### Supported Flags
```
-e, --exclude-namespaces string Namespaces to be excluded, split by comma. Example: --exclude-namespace ns1,ns2,ns3. If --include-namespace is set, --exclude-namespaces will be ignored.
-h, --help help for kor
-n, --include-namespaces string Namespaces to run on, split by comma. Example: --include-namespace ns1,ns2,ns3.
-k, --kubeconfig string Path to kubeconfig file (optional)
--output string Output format (table or json) (default "table")
--slack-auth-token string Slack auth token to send notifications to. --slack-auth-token requires --slack-channel to be set.
--slack-channel string Slack channel to send notifications to. --slack-channel requires --slack-auth-token to be set.
--slack-webhook-url string Slack webhook URL to send notifications to
-l, --exclude-labels string Selector to filter out, Example: --exclude-labels key1=value1,key2=value2.
-e, --exclude-namespaces string Namespaces to be excluded, splited by comma. Example: --exclude-namespace ns1,ns2,ns3. If --include-namespace is set, --exclude-namespaces will be ignored.
-h, --help help for kor
-n, --include-namespaces string Namespaces to run on, splited by comma. Example: --include-namespace ns1,ns2,ns3.
-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
--older-than string The minimum age of the resources to be considered unused. This flag cannot be used together with newer-than flag. Example: --older-than=1h2m
--output string Output format (table, json or yaml) (default "table")
--slack-auth-token string Slack auth token to send notifications to. --slack-auth-token requires --slack-channel to be set.
--slack-channel string Slack channel to send notifications to. --slack-channel requires --slack-auth-token to be set.
--slack-webhook-url string Slack webhook URL to send notifications to
```

To use a specific subcommand, run `kor [subcommand] [flags]`.
Expand Down Expand Up @@ -127,34 +130,11 @@ kor [subcommand] --help


## Ignore Resources
The resources labeled with "kor/used = true" will be ignored by kor even if they are unused. You can add this label to resources you want to ignore.

## Import Option
You can also use kor as a Go library to programmatically discover unused resources. By importing the github.com/yonahd/kor/pkg/kor package, you can call the relevant functions to retrieve unused resources. The library provides the option to get the results in JSON format by specifying the outputFormat parameter.

```go
import (
"github.com/yonahd/kor/pkg/kor"
)

func main() {
myNamespaces := kor.IncludeExcludeLists{
IncludeListStr: "my-namespace1, my-namespace2",
}
outputFormat := "json" // Set to "json" for JSON output

if outputFormat == "json" {
jsonResponse, err := kor.GetUnusedDeploymentsStructured(myNamespaces, kubeconfig, "json")
if err != nil {
// Handle error
}
// Process the JSON response
// ...
} else {
kor.GetUnusedDeployments(namespace)
}
}
The resources labeled with:
```sh
kor/used=true
```
will be ignored by kor even if they are unused. You can add this label to resources you want to ignore.

## In Cluster Usage

Expand Down Expand Up @@ -192,6 +172,10 @@ helm upgrade -i kor \
./charts/kor
```

## Grafana Dashboard
Dashboard can be found [here](https://grafana.com/grafana/dashboards/19863-kor-dashboard/).
![Grafana Dashboard](/grafana/dashboard-screenshot-1.png)

## Contributing

Contributions are welcome! If you encounter any bugs or have suggestions for improvements, please open an issue in the [issue tracker](https://github.com/yonahd/kor/issues).
Expand Down
2 changes: 1 addition & 1 deletion cmd/kor/all.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var allCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
clientset := kor.GetKubeClient(kubeconfig)

if response, err := kor.GetUnusedAll(includeExcludeLists, clientset, outputFormat, opts); err != nil {
if response, err := kor.GetUnusedAll(includeExcludeLists, filterOptions, clientset, outputFormat, opts); err != nil {
fmt.Println(err)
} else {
fmt.Println(response)
Expand Down
2 changes: 1 addition & 1 deletion cmd/kor/configmaps.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var configmapCmd = &cobra.Command{
Args: cobra.ExactArgs(0),
Run: func(cmd *cobra.Command, args []string) {
clientset := kor.GetKubeClient(kubeconfig)
if response, err := kor.GetUnusedConfigmaps(includeExcludeLists, clientset, outputFormat, opts); err != nil {
if response, err := kor.GetUnusedConfigmaps(includeExcludeLists, filterOptions, clientset, outputFormat, opts); err != nil {
fmt.Println(err)
} else {
fmt.Println(response)
Expand Down
2 changes: 1 addition & 1 deletion cmd/kor/deployments.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var deployCmd = &cobra.Command{
Args: cobra.ExactArgs(0),
Run: func(cmd *cobra.Command, args []string) {
clientset := kor.GetKubeClient(kubeconfig)
if response, err := kor.GetUnusedDeployments(includeExcludeLists, clientset, outputFormat, opts); err != nil {
if response, err := kor.GetUnusedDeployments(includeExcludeLists, filterOptions, clientset, outputFormat, opts); err != nil {
fmt.Println(err)
} else {
fmt.Println(response)
Expand Down
2 changes: 1 addition & 1 deletion cmd/kor/hpas.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ var hpaCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
clientset := kor.GetKubeClient(kubeconfig)

if response, err := kor.GetUnusedHpas(includeExcludeLists, clientset, outputFormat, opts); err != nil {
if response, err := kor.GetUnusedHpas(includeExcludeLists, filterOptions, clientset, outputFormat, opts); err != nil {
fmt.Println(err)
} else {
fmt.Println(response)
Expand Down
2 changes: 1 addition & 1 deletion cmd/kor/ingresses.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ var ingressCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
clientset := kor.GetKubeClient(kubeconfig)

if response, err := kor.GetUnusedIngresses(includeExcludeLists, clientset, outputFormat, opts); err != nil {
if response, err := kor.GetUnusedIngresses(includeExcludeLists, filterOptions, clientset, outputFormat, opts); err != nil {
fmt.Println(err)
} else {
fmt.Println(response)
Expand Down
2 changes: 1 addition & 1 deletion cmd/kor/pdbs.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ var pdbCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
clientset := kor.GetKubeClient(kubeconfig)

if response, err := kor.GetUnusedPdbs(includeExcludeLists, clientset, outputFormat, opts); err != nil {
if response, err := kor.GetUnusedPdbs(includeExcludeLists, filterOptions, clientset, outputFormat, opts); err != nil {
fmt.Println(err)
} else {
fmt.Println(response)
Expand Down
2 changes: 1 addition & 1 deletion cmd/kor/pvc.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ var pvcCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
clientset := kor.GetKubeClient(kubeconfig)

if response, err := kor.GetUnusedPvcs(includeExcludeLists, clientset, outputFormat, opts); err != nil {
if response, err := kor.GetUnusedPvcs(includeExcludeLists, filterOptions, clientset, outputFormat, opts); err != nil {
fmt.Println(err)
} else {
fmt.Println(response)
Expand Down
2 changes: 1 addition & 1 deletion cmd/kor/roles.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ var roleCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
clientset := kor.GetKubeClient(kubeconfig)

if response, err := kor.GetUnusedRoles(includeExcludeLists, clientset, outputFormat, opts); err != nil {
if response, err := kor.GetUnusedRoles(includeExcludeLists, filterOptions, clientset, outputFormat, opts); err != nil {
fmt.Println(err)
} else {
fmt.Println(response)
Expand Down
16 changes: 16 additions & 0 deletions cmd/kor/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ var (
kubeconfig string
includeExcludeLists kor.IncludeExcludeLists
opts kor.Opts
filterOptions = kor.NewFilterOptions()
)

func Execute() {
Expand All @@ -54,8 +55,23 @@ func Execute() {
rootCmd.PersistentFlags().StringVar(&opts.Token, "slack-auth-token", "", "Slack auth token to send notifications to. --slack-auth-token requires --slack-channel to be set.")
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().StringVar(&opts.WebhookURL, "slack-webhook-url", "", "Slack webhook URL to send notifications to")
rootCmd.PersistentFlags().StringVar(&opts.Channel, "slack-channel", "", "Slack channel to send notifications to. --slack-channel requires --slack-auth-token to be set.")
rootCmd.PersistentFlags().StringVar(&opts.Token, "slack-auth-token", "", "Slack auth token to send notifications to. --slack-auth-token requires --slack-channel to be set.")
addFilterOptionsFlag(rootCmd, filterOptions)

if err := filterOptions.Validate(); err != nil {
fmt.Fprintf(os.Stderr, "Error while validating filter options '%s'", err)
os.Exit(1)
}
if err := rootCmd.Execute(); err != nil {
fmt.Fprintf(os.Stderr, "Error while executing your CLI '%s'", err)
os.Exit(1)
}
}

func addFilterOptionsFlag(cmd *cobra.Command, opts *kor.FilterOptions) {
cmd.PersistentFlags().StringVarP(&opts.ExcludeLabels, "exclude-labels", "l", opts.ExcludeLabels, "Selector to filter out, Example: --exclude-labels key1=value1,key2=value2.")
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")
}
2 changes: 1 addition & 1 deletion cmd/kor/secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ var secretCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
clientset := kor.GetKubeClient(kubeconfig)

if response, err := kor.GetUnusedSecrets(includeExcludeLists, clientset, outputFormat, opts); err != nil {
if response, err := kor.GetUnusedSecrets(includeExcludeLists, filterOptions, clientset, outputFormat, opts); err != nil {
fmt.Println(err)
} else {
fmt.Println(response)
Expand Down
2 changes: 1 addition & 1 deletion cmd/kor/statefulsets.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ var stsCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
clientset := kor.GetKubeClient(kubeconfig)

if response, err := kor.GetUnusedStatefulSets(includeExcludeLists, clientset, outputFormat, opts); err != nil {
if response, err := kor.GetUnusedStatefulSets(includeExcludeLists, filterOptions, clientset, outputFormat, opts); err != nil {
fmt.Println(err)
} else {
fmt.Println(response)
Expand Down
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ require (
github.com/olekukonko/tablewriter v0.0.5
github.com/prometheus/client_golang v1.17.0
github.com/spf13/cobra v1.7.0
github.com/stretchr/testify v1.8.4
k8s.io/api v0.28.3
k8s.io/apimachinery v0.28.3
k8s.io/client-go v0.28.3
k8s.io/utils v0.0.0-20230406110748-d93618cff8a2
sigs.k8s.io/yaml v1.3.0
sigs.k8s.io/yaml v1.4.0
)

require (
Expand Down Expand Up @@ -45,6 +46,7 @@ require (
github.com/onsi/ginkgo/v2 v2.11.0 // indirect
github.com/onsi/gomega v1.27.10 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_model v0.4.1-0.20230718164431-9a2bf3000d16 // indirect
github.com/prometheus/common v0.44.0 // indirect
github.com/prometheus/procfs v0.11.1 // indirect
Expand Down
5 changes: 3 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
Expand Down Expand Up @@ -191,5 +192,5 @@ sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd h1:EDPBXCAspyGV4jQlpZSudPeMm
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd/go.mod h1:B8JuhiUyNFVKdsE8h686QcCxMaH6HrOAZj4vswFpcB0=
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 h1:PRbqxJClWWYMNV1dhaG4NsibJbArud9kFxnAMREiWFE=
sigs.k8s.io/structured-merge-diff/v4 v4.2.3/go.mod h1:qjx8mGObPmV2aSZepjQjbmb2ihdVs8cGKBraizNC69E=
sigs.k8s.io/yaml v1.3.0 h1:a2VclLzOGrwOHDiV8EfBGhvjHvP46CtW5j6POvhYGGo=
sigs.k8s.io/yaml v1.3.0/go.mod h1:GeOyir5tyXNByN85N/dRIT9es5UQNerPYEKK56eTBm8=
sigs.k8s.io/yaml v1.4.0 h1:Mk1wCc2gy/F0THH0TAp1QYyJNzRm2KCLy3o5ASXVI5E=
sigs.k8s.io/yaml v1.4.0/go.mod h1:Ejl7/uTz7PSA4eKMyQCUTnhZYNmLIl+5c2lQPGR2BPY=
Binary file added grafana/dashboard-screenshot-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 5c185ba

Please sign in to comment.