Skip to content

Commit

Permalink
Fix typo
Browse files Browse the repository at this point in the history
  • Loading branch information
isindir committed Jul 7, 2024
1 parent eb19f01 commit 02a3f7e
Show file tree
Hide file tree
Showing 14 changed files with 1,567 additions and 10 deletions.
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
.vscode
*.iml
.idea/
.vscode/
dist/**
main.exe
coverage.txt
coverage.*
build/
kor
!kor/
*.swp
hack/exceptions
.envrc
10 changes: 9 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ EXCEPTIONS_FILE_PATTERN := *.json
build:
go build -o build/kor main.go

clean:
rm -fr build coverage.txt coverage.html

lint:
golangci-lint run

Expand All @@ -15,6 +18,11 @@ lint-fix:
test:
go test -race -coverprofile=coverage.txt -shuffle on ./...

# TODO: EZ: remove once finished
cover: test
go tool cover -func=coverage.txt
go tool cover -o coverage.html -html=coverage.txt

sort-exception-files:
@echo "Sorting exception files..."
@find $(EXCEPTIONS_DIR) -name '$(EXCEPTIONS_FILE_PATTERN)' -exec sh -c ' \
Expand All @@ -36,4 +44,4 @@ validate-exception-sorting:
done; \
if [ "$$PRINT_ERR" = 0 ]; then \
echo "Run the following command to sort all files recursively: make sort-exception-files"; \
fi; \
fi; \
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Kor is a tool to discover unused Kubernetes resources. Currently, Kor can identi
- DaemonSets
- StorageClasses
- NetworkPolicies
- Namespaces

![Kor Screenshot](/images/show_reason_screenshot.png)

Expand Down Expand Up @@ -118,6 +119,7 @@ Kor provides various subcommands to identify and list unused resources. The avai
- `daemonset`- Gets unused DaemonSets for the specified namespace or all namespaces.
- `finalizer` - Gets unused pending deletion resources for the specified namespace or all namespaces.
- `networkpolicy` - Gets unused NetworkPolicies for the specified namespace or all namespaces.
- `namespace` - Gets unused Namespaces for the specified namespace or all namespaces.
- `exporter` - Export Prometheus metrics.
- `version` - Print kor version information.

Expand Down
2 changes: 1 addition & 1 deletion cmd/kor/all.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

var allCmd = &cobra.Command{
Use: "all",
Short: "Gets unused resources",
Short: "Gets unused namespaced resources",
Args: cobra.ExactArgs(0),
Run: func(cmd *cobra.Command, args []string) {
clientset := kor.GetKubeClient(kubeconfig)
Expand Down
50 changes: 50 additions & 0 deletions cmd/kor/namespaces.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package kor

import (
"context"
"fmt"

"github.com/spf13/cobra"

"github.com/yonahd/kor/pkg/kor"
"github.com/yonahd/kor/pkg/utils"
)

var namespaceCmd = &cobra.Command{
Use: "namespace",
Aliases: []string{"ns", "namespaces"},
Short: "Gets unused namespaces",
Args: cobra.ExactArgs(0),
Run: func(cmd *cobra.Command, args []string) {
ctx := context.Background()
clientset := kor.GetKubeClient(kubeconfig)
dynamicClient := kor.GetDynamicClient(kubeconfig)

if response, err := kor.GetUnusedNamespaces(
ctx,
filterOptions,
clientset,
dynamicClient,
outputFormat,
opts,
); err != nil {
fmt.Println(err)
} else {
utils.PrintLogo(outputFormat)
fmt.Println(response)
}
},
}

func init() {
namespaceCmd.PersistentFlags().StringSliceVarP(
&filterOptions.IgnoreResourceTypes,
"ignore-resource-types",
"i",
filterOptions.IgnoreResourceTypes,
"Child resource type selector to filter out from namespace emptiness evaluation,"+
" example: --ignore-resource-types secrets,configmaps."+
" Types should be specified in a format printed out in NAME column by 'kubectl api-resources --namespaced=true'.",
)
rootCmd.AddCommand(namespaceCmd)
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ toolchain go1.22.2
require (
github.com/fatih/color v1.17.0
github.com/olekukonko/tablewriter v0.0.5
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.19.1
github.com/spf13/cobra v1.8.1
k8s.io/api v0.30.2
Expand Down Expand Up @@ -43,7 +44,6 @@ require (
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/common v0.48.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
Expand Down
66 changes: 66 additions & 0 deletions pkg/filters/filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,26 @@ const (
KorLabelFilterName = "korlabel"
)

var (
SystemNamespaceNames = []string{"default", "kube-system", "kube-public", "kube-node-lease"}
)

type FilterFunction func(object runtime.Object, opts *Options) bool

// ApplyFilters is a function to apply a list of FilterFunctions to a given object
func ApplyFilters(
object runtime.Object,
opts *Options,
funcsToCall ...FilterFunction,
) bool {
for _, fn := range funcsToCall {
if pass := fn(object, opts); pass {
return true
}
}
return false
}

// KorLabelFilter is a filter that filters out resources that are ["kor/used"] != "true"
func KorLabelFilter(object runtime.Object, opts *Options) bool {
if meta, ok := object.(metav1.Object); ok {
Expand Down Expand Up @@ -107,3 +127,49 @@ func HasIncludedAge(creationTime metav1.Time, filterOpts *Options) (bool, error)

return true, nil
}

// SystemNamespaceFilter is a filter that filters out namespaces that are created with the cluster by default
func SystemNamespaceFilter(object runtime.Object, filterOpts *Options) bool {
if meta, ok := object.(metav1.Object); ok {
namespaceName := meta.GetName()
for _, systemNamespace := range SystemNamespaceNames {
if namespaceName == systemNamespace {
return true
}
}
}
return false
}

// ExcludeNamespacesFilter is a filter that filters out namespaces specified by user
func ExcludeNamespacesFilter(object runtime.Object, filterOpts *Options) bool {
if filterOpts.ExcludeNamespaces != nil {
excludeList := filterOpts.ExcludeNamespaces
if meta, ok := object.(metav1.Object); ok {
namespaceName := meta.GetName()
for _, unwantedNamespace := range excludeList {
if namespaceName == unwantedNamespace {
return true
}
}
}
}
return false
}

// IncludeNamespacesFilter is a filter that acts as a whitelist, only these namespaces will be processed if specified
func IncludeNamespacesFilter(object runtime.Object, filterOpts *Options) bool {
if filterOpts.IncludeNamespaces != nil {
includeList := filterOpts.IncludeNamespaces
if meta, ok := object.(metav1.Object); ok {
namespaceName := meta.GetName()
for _, wantedNamespace := range includeList {
if namespaceName == wantedNamespace {
return false
}
}
}
return true
}
return false
}
Loading

0 comments on commit 02a3f7e

Please sign in to comment.