Skip to content

Commit

Permalink
add --exclude-namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
linrl3 committed Sep 3, 2023
1 parent d28eb54 commit 0254d5f
Show file tree
Hide file tree
Showing 22 changed files with 110 additions and 75 deletions.
4 changes: 2 additions & 2 deletions cmd/kor/all.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ var allCmd = &cobra.Command{
Args: cobra.ExactArgs(0),
Run: func(cmd *cobra.Command, args []string) {
if outputFormat == "json" {
kor.GetUnusedAllJSON(namespace, kubeconfig)
kor.GetUnusedAllJSON(includeExcludeLists, kubeconfig)
} else {
kor.GetUnusedAll(namespace, kubeconfig)
kor.GetUnusedAll(includeExcludeLists, kubeconfig)
}

},
Expand Down
4 changes: 2 additions & 2 deletions cmd/kor/configmaps.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ var configmapCmd = &cobra.Command{
Args: cobra.ExactArgs(0),
Run: func(cmd *cobra.Command, args []string) {
if outputFormat == "json" {
kor.GetUnusedConfigmapsJSON(namespace, kubeconfig)
kor.GetUnusedConfigmapsJSON(includeExcludeLists, kubeconfig)
} else {
kor.GetUnusedConfigmaps(namespace, kubeconfig)
kor.GetUnusedConfigmaps(includeExcludeLists, kubeconfig)
}

},
Expand Down
4 changes: 2 additions & 2 deletions cmd/kor/deployments.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ var deployCmd = &cobra.Command{
Args: cobra.ExactArgs(0),
Run: func(cmd *cobra.Command, args []string) {
if outputFormat == "json" {
kor.GetUnusedDeploymentsJSON(namespace, kubeconfig)
kor.GetUnusedDeploymentsJSON(includeExcludeLists, kubeconfig)
} else {
kor.GetUnusedDeployments(namespace, kubeconfig)
kor.GetUnusedDeployments(includeExcludeLists, kubeconfig)
}

},
Expand Down
4 changes: 2 additions & 2 deletions cmd/kor/hpas.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ var hpaCmd = &cobra.Command{
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
if outputFormat == "json" {
kor.GetUnusedHpasJson(namespace, kubeconfig)
kor.GetUnusedHpasJson(includeExcludeLists, kubeconfig)
} else {
kor.GetUnusedHpas(namespace, kubeconfig)
kor.GetUnusedHpas(includeExcludeLists, kubeconfig)
}
},
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/kor/pvc.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ var pvcCmd = &cobra.Command{
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
if outputFormat == "json" {
kor.GetUnusedPvcsJson(namespace, kubeconfig)
kor.GetUnusedPvcsJson(includeExcludeLists, kubeconfig)
} else {
kor.GetUnusedPvcs(namespace, kubeconfig)
kor.GetUnusedPvcs(includeExcludeLists, kubeconfig)
}
},
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/kor/roles.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ var roleCmd = &cobra.Command{
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
if outputFormat == "json" {
kor.GetUnusedRolesJSON(namespace, kubeconfig)
kor.GetUnusedRolesJSON(includeExcludeLists, kubeconfig)
} else {
kor.GetUnusedRoles(namespace, kubeconfig)
kor.GetUnusedRoles(includeExcludeLists, kubeconfig)
}
},
}
Expand Down
13 changes: 8 additions & 5 deletions cmd/kor/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"

"github.com/spf13/cobra"
"github.com/yonahd/kor/pkg/kor"
)

var rootCmd = &cobra.Command{
Expand All @@ -14,17 +15,19 @@ var rootCmd = &cobra.Command{
kor can currently discover unused configmaps and secrets`,
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {

},
}

var namespace string
var outputFormat string
var kubeconfig string
var (
outputFormat string
kubeconfig string
includeExcludeLists kor.IncludeExcludeLists
)

func Execute() {
rootCmd.PersistentFlags().StringVarP(&kubeconfig, "kubeconfig", "k", "", "Path to kubeconfig file (optional)")
rootCmd.PersistentFlags().StringVarP(&namespace, "namespace", "n", "", "Namespace to run on")
rootCmd.PersistentFlags().StringVarP(&includeExcludeLists.IncludeListStr, "include-namespaces", "n", "", "Namespaces to run on, splited by comma. Example: --include-namespace ns1,ns2,ns3. ")
rootCmd.PersistentFlags().StringVarP(&includeExcludeLists.ExcludeListStr, "exclude-namespaces", "e", "", "Namespaces to be excluded, splited by comma. Example: --exclude-namespace ns1,ns2,ns3. If --include-namespace is set, --exclude-namespaces will be ignored.")
rootCmd.PersistentFlags().StringVar(&outputFormat, "output", "table", "Output format (table or json)")
if err := rootCmd.Execute(); err != nil {
fmt.Fprintf(os.Stderr, "Error while executing your CLI '%s'", err)
Expand Down
4 changes: 2 additions & 2 deletions cmd/kor/secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ var secretCmd = &cobra.Command{
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
if outputFormat == "json" {
kor.GetUnusedSecretsJSON(namespace, kubeconfig)
kor.GetUnusedSecretsJSON(includeExcludeLists, kubeconfig)
} else {
kor.GetUnusedSecrets(namespace, kubeconfig)
kor.GetUnusedSecrets(includeExcludeLists, kubeconfig)
}

},
Expand Down
4 changes: 2 additions & 2 deletions cmd/kor/serviceaccounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ var serviceAccountCmd = &cobra.Command{
Args: cobra.ExactArgs(0),
Run: func(cmd *cobra.Command, args []string) {
if outputFormat == "json" {
kor.GetUnusedServiceAccountsJSON(namespace, kubeconfig)
kor.GetUnusedServiceAccountsJSON(includeExcludeLists, kubeconfig)
} else {
kor.GetUnusedServiceAccounts(namespace, kubeconfig)
kor.GetUnusedServiceAccounts(includeExcludeLists, kubeconfig)
}

},
Expand Down
4 changes: 2 additions & 2 deletions cmd/kor/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ var serviceCmd = &cobra.Command{
Args: cobra.ExactArgs(0),
Run: func(cmd *cobra.Command, args []string) {
if outputFormat == "json" {
kor.GetUnusedServicesJSON(namespace, kubeconfig)
kor.GetUnusedServicesJSON(includeExcludeLists, kubeconfig)
} else {
kor.GetUnusedServices(namespace, kubeconfig)
kor.GetUnusedServices(includeExcludeLists, kubeconfig)
}

},
Expand Down
4 changes: 2 additions & 2 deletions cmd/kor/statefulsets.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ var stsCmd = &cobra.Command{
Args: cobra.ExactArgs(0),
Run: func(cmd *cobra.Command, args []string) {
if outputFormat == "json" {
kor.GetUnusedStatefulsetsJSON(namespace, kubeconfig)
kor.GetUnusedStatefulsetsJSON(includeExcludeLists, kubeconfig)
} else {
kor.GetUnusedStatefulsets(namespace, kubeconfig)
kor.GetUnusedStatefulsets(includeExcludeLists, kubeconfig)
}

},
Expand Down
8 changes: 4 additions & 4 deletions pkg/kor/all.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,13 @@ func getUnusedPvcs(kubeClient *kubernetes.Clientset, namespace string) ResourceD
return namespacePvcDiff
}

func GetUnusedAll(namespace string, kubeconfig string) {
func GetUnusedAll(includeExcludeLists IncludeExcludeLists, kubeconfig string) {
var kubeClient *kubernetes.Clientset
var namespaces []string

kubeClient = GetKubeClient(kubeconfig)

namespaces = SetNamespaceList(namespace, kubeClient)
namespaces = SetNamespaceList(includeExcludeLists, kubeClient)
for _, namespace := range namespaces {
var allDiffs []ResourceDiff
namespaceCMDiff := getUnusedCMs(kubeClient, namespace)
Expand All @@ -132,13 +132,13 @@ func GetUnusedAll(namespace string, kubeconfig string) {
}
}

func GetUnusedAllJSON(namespace string, kubeconfig string) (string, error) {
func GetUnusedAllJSON(includeExcludeLists IncludeExcludeLists, kubeconfig string) (string, error) {
var kubeClient *kubernetes.Clientset
var namespaces []string

kubeClient = GetKubeClient(kubeconfig)

namespaces = SetNamespaceList(namespace, kubeClient)
namespaces = SetNamespaceList(includeExcludeLists, kubeClient)

// Create the JSON response object
response := make(map[string]map[string][]string)
Expand Down
8 changes: 4 additions & 4 deletions pkg/kor/confimgmaps.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,13 @@ func processNamespaceCM(kubeClient *kubernetes.Clientset, namespace string) ([]s

}

func GetUnusedConfigmaps(namespace string, kubeconfig string) {
func GetUnusedConfigmaps(includeExcludeLists IncludeExcludeLists, kubeconfig string) {
var kubeClient *kubernetes.Clientset
var namespaces []string

kubeClient = GetKubeClient(kubeconfig)

namespaces = SetNamespaceList(namespace, kubeClient)
namespaces = SetNamespaceList(includeExcludeLists, kubeClient)

for _, namespace := range namespaces {
diff, err := processNamespaceCM(kubeClient, namespace)
Expand All @@ -126,12 +126,12 @@ func GetUnusedConfigmaps(namespace string, kubeconfig string) {
}
}

func GetUnusedConfigmapsJSON(namespace string, kubeconfig string) (string, error) {
func GetUnusedConfigmapsJSON(includeExcludeLists IncludeExcludeLists, kubeconfig string) (string, error) {
var kubeClient *kubernetes.Clientset
var namespaces []string

kubeClient = GetKubeClient(kubeconfig)
namespaces = SetNamespaceList(namespace, kubeClient)
namespaces = SetNamespaceList(includeExcludeLists, kubeClient)
response := make(map[string]map[string][]string)

for _, namespace := range namespaces {
Expand Down
8 changes: 4 additions & 4 deletions pkg/kor/deployments.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ func ProcessNamespaceDeployments(clientset *kubernetes.Clientset, namespace stri

}

func GetUnusedDeployments(namespace string, kubeconfig string) {
func GetUnusedDeployments(includeExcludeLists IncludeExcludeLists, kubeconfig string) {
var kubeClient *kubernetes.Clientset
var namespaces []string

kubeClient = GetKubeClient(kubeconfig)

namespaces = SetNamespaceList(namespace, kubeClient)
namespaces = SetNamespaceList(includeExcludeLists, kubeClient)

for _, namespace := range namespaces {
diff, err := ProcessNamespaceDeployments(kubeClient, namespace)
Expand All @@ -57,12 +57,12 @@ func GetUnusedDeployments(namespace string, kubeconfig string) {
}
}

func GetUnusedDeploymentsJSON(namespace string, kubeconfig string) (string, error) {
func GetUnusedDeploymentsJSON(includeExcludeLists IncludeExcludeLists, kubeconfig string) (string, error) {
var kubeClient *kubernetes.Clientset
var namespaces []string

kubeClient = GetKubeClient(kubeconfig)
namespaces = SetNamespaceList(namespace, kubeClient)
namespaces = SetNamespaceList(includeExcludeLists, kubeClient)
response := make(map[string]map[string][]string)

for _, namespace := range namespaces {
Expand Down
8 changes: 4 additions & 4 deletions pkg/kor/hpas.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,13 @@ func processNamespaceHpas(clientset *kubernetes.Clientset, namespace string) ([]
return unusedHpas, nil
}

func GetUnusedHpas(namespace string, kubeconfig string) {
func GetUnusedHpas(includeExcludeLists IncludeExcludeLists, kubeconfig string) {
var kubeClient *kubernetes.Clientset
var namespaces []string

kubeClient = GetKubeClient(kubeconfig)

namespaces = SetNamespaceList(namespace, kubeClient)
namespaces = SetNamespaceList(includeExcludeLists, kubeClient)

for _, namespace := range namespaces {
diff, err := processNamespaceHpas(kubeClient, namespace)
Expand All @@ -96,13 +96,13 @@ func GetUnusedHpas(namespace string, kubeconfig string) {

}

func GetUnusedHpasJson(namespace string, kubeconfig string) (string, error) {
func GetUnusedHpasJson(includeExcludeLists IncludeExcludeLists, kubeconfig string) (string, error) {
var kubeClient *kubernetes.Clientset
var namespaces []string

kubeClient = GetKubeClient(kubeconfig)

namespaces = SetNamespaceList(namespace, kubeClient)
namespaces = SetNamespaceList(includeExcludeLists, kubeClient)
response := make(map[string]map[string][]string)

for _, namespace := range namespaces {
Expand Down
52 changes: 42 additions & 10 deletions pkg/kor/kor.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import (
"bytes"
"context"
"fmt"
"log"
"os"
"path/filepath"
"sort"
"strings"

"github.com/olekukonko/tablewriter"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -19,6 +21,10 @@ type ExceptionResource struct {
ResourceName string
Namespace string
}
type IncludeExcludeLists struct {
IncludeListStr string
ExcludeListStr string
}

func RemoveDuplicatesAndSort(slice []string) []string {
uniqueSet := make(map[string]bool)
Expand Down Expand Up @@ -56,18 +62,44 @@ func GetKubeClient(kubeconfig string) *kubernetes.Clientset {
return clientset
}

func SetNamespaceList(namespace string, kubeClient *kubernetes.Clientset) []string {
var namespaces []string
if namespace != "" {
namespaces = append(namespaces, namespace)
} else {
namespaceList, err := kubeClient.CoreV1().Namespaces().List(context.TODO(), metav1.ListOptions{})
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to retrieve namespaces: %v\n", err)
os.Exit(1)
func SetNamespaceList(namespaceLists IncludeExcludeLists, kubeClient *kubernetes.Clientset) []string {
namespaces := make([]string, 0)
namespacesMap := make(map[string]bool)
if namespaceLists.IncludeListStr != "" && namespaceLists.ExcludeListStr != "" {
log.Printf("Exclude namespaces can't be used together with include namespaces. Ignoring --exclude-namespace(-e) flag\n")
namespaceLists.ExcludeListStr = ""
}
includeNamespaces := strings.Split(namespaceLists.IncludeListStr, ",")
excludeNamespaces := strings.Split(namespaceLists.ExcludeListStr, ",")
namespaceList, err := kubeClient.CoreV1().Namespaces().List(context.TODO(), metav1.ListOptions{})
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to retrieve namespaces: %v\n", err)
os.Exit(1)
}
if namespaceLists.IncludeListStr != "" {
for _, ns := range namespaceList.Items {
namespacesMap[ns.Name] = false
}
for _, ns := range includeNamespaces {
if _, exists := namespacesMap[ns]; exists {
namespacesMap[ns] = true
} else {
log.Printf("namespace [%s] not found\n", ns)
}
}
} else {
for _, ns := range namespaceList.Items {
namespaces = append(namespaces, ns.Name)
namespacesMap[ns.Name] = true
}
for _, ns := range excludeNamespaces {
if _, exists := namespacesMap[ns]; exists {
namespacesMap[ns] = false
}
}
}
for ns := range namespacesMap {
if namespacesMap[ns] {
namespaces = append(namespaces, ns)
}
}
return namespaces
Expand Down
8 changes: 4 additions & 4 deletions pkg/kor/pvc.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@ func processNamespacePvcs(kubeClient *kubernetes.Clientset, namespace string) ([
return diff, nil
}

func GetUnusedPvcs(namespace string, kubeconfig string) {
func GetUnusedPvcs(includeExcludeLists IncludeExcludeLists, kubeconfig string) {
var kubeClient *kubernetes.Clientset
var namespaces []string

kubeClient = GetKubeClient(kubeconfig)

namespaces = SetNamespaceList(namespace, kubeClient)
namespaces = SetNamespaceList(includeExcludeLists, kubeClient)

for _, namespace := range namespaces {
diff, err := processNamespacePvcs(kubeClient, namespace)
Expand All @@ -70,13 +70,13 @@ func GetUnusedPvcs(namespace string, kubeconfig string) {

}

func GetUnusedPvcsJson(namespace string, kubeconfig string) (string, error) {
func GetUnusedPvcsJson(includeExcludeLists IncludeExcludeLists, kubeconfig string) (string, error) {
var kubeClient *kubernetes.Clientset
var namespaces []string

kubeClient = GetKubeClient(kubeconfig)

namespaces = SetNamespaceList(namespace, kubeClient)
namespaces = SetNamespaceList(includeExcludeLists, kubeClient)
response := make(map[string]map[string][]string)

for _, namespace := range namespaces {
Expand Down
Loading

0 comments on commit 0254d5f

Please sign in to comment.