Skip to content

Commit

Permalink
merge functions for configmaps (#108)
Browse files Browse the repository at this point in the history
* merge functions for configmaps

* merge functions for more resources

* merge functions for more resources

* merge functions for more resources

* merge functions for more resources

---------

Co-authored-by: Yonah Dissen <[email protected]>
  • Loading branch information
yonahd and Yonah Dissen authored Oct 19, 2023
1 parent 0210609 commit ce0998a
Show file tree
Hide file tree
Showing 39 changed files with 272 additions and 559 deletions.
12 changes: 4 additions & 8 deletions cmd/kor/all.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,12 @@ var allCmd = &cobra.Command{
Args: cobra.ExactArgs(0),
Run: func(cmd *cobra.Command, args []string) {
clientset := kor.GetKubeClient(kubeconfig)
if outputFormat == "json" || outputFormat == "yaml" {
if response, err := kor.GetUnusedAllStructured(includeExcludeLists, clientset, outputFormat); err != nil {
fmt.Println(err)
} else {
fmt.Println(response)
}

if response, err := kor.GetUnusedAll(includeExcludeLists, clientset, outputFormat, slackOpts); err != nil {
fmt.Println(err)
} else {
kor.GetUnusedAll(includeExcludeLists, clientset, slackOpts)
fmt.Println(response)
}

},
}

Expand Down
11 changes: 3 additions & 8 deletions cmd/kor/configmaps.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,11 @@ var configmapCmd = &cobra.Command{
Args: cobra.ExactArgs(0),
Run: func(cmd *cobra.Command, args []string) {
clientset := kor.GetKubeClient(kubeconfig)
if outputFormat == "json" || outputFormat == "yaml" {
if response, err := kor.GetUnusedConfigmapsStructured(includeExcludeLists, clientset, outputFormat); err != nil {
fmt.Println(err)
} else {
fmt.Println(response)
}
if response, err := kor.GetUnusedConfigmaps(includeExcludeLists, clientset, outputFormat, slackOpts); err != nil {
fmt.Println(err)
} else {
kor.GetUnusedConfigmaps(includeExcludeLists, clientset, slackOpts)
fmt.Println(response)
}

},
}

Expand Down
11 changes: 3 additions & 8 deletions cmd/kor/deployments.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,11 @@ var deployCmd = &cobra.Command{
Args: cobra.ExactArgs(0),
Run: func(cmd *cobra.Command, args []string) {
clientset := kor.GetKubeClient(kubeconfig)
if outputFormat == "json" || outputFormat == "yaml" {
if response, err := kor.GetUnusedDeploymentsStructured(includeExcludeLists, clientset, outputFormat); err != nil {
fmt.Println(err)
} else {
fmt.Println(response)
}
if response, err := kor.GetUnusedDeployments(includeExcludeLists, clientset, outputFormat, slackOpts); err != nil {
fmt.Println(err)
} else {
kor.GetUnusedDeployments(includeExcludeLists, clientset, slackOpts)
fmt.Println(response)
}

},
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/kor/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ var exporterCmd = &cobra.Command{
Args: cobra.ExactArgs(0),
Run: func(cmd *cobra.Command, args []string) {
clientset := kor.GetKubeClient(kubeconfig)
kor.Exporter(includeExcludeLists, clientset, "json")
kor.Exporter(includeExcludeLists, clientset, "json", slackOpts)

},
}
Expand Down
12 changes: 5 additions & 7 deletions cmd/kor/hpas.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,13 @@ var hpaCmd = &cobra.Command{
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
clientset := kor.GetKubeClient(kubeconfig)
if outputFormat == "json" || outputFormat == "yaml" {
if response, err := kor.GetUnusedHpasStructured(includeExcludeLists, clientset, outputFormat); err != nil {
fmt.Println(err)
} else {
fmt.Println(response)
}

if response, err := kor.GetUnusedHpas(includeExcludeLists, clientset, outputFormat, slackOpts); err != nil {
fmt.Println(err)
} else {
kor.GetUnusedHpas(includeExcludeLists, clientset, slackOpts)
fmt.Println(response)
}

},
}

Expand Down
11 changes: 4 additions & 7 deletions cmd/kor/ingresses.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,11 @@ var ingressCmd = &cobra.Command{
Args: cobra.ExactArgs(0),
Run: func(cmd *cobra.Command, args []string) {
clientset := kor.GetKubeClient(kubeconfig)
if outputFormat == "json" || outputFormat == "yaml" {
if response, err := kor.GetUnusedIngressesStructured(includeExcludeLists, clientset, outputFormat); err != nil {
fmt.Println(err)
} else {
fmt.Println(response)
}

if response, err := kor.GetUnusedIngresses(includeExcludeLists, clientset, outputFormat, slackOpts); err != nil {
fmt.Println(err)
} else {
kor.GetUnusedIngresses(includeExcludeLists, clientset, slackOpts)
fmt.Println(response)
}
},
}
Expand Down
12 changes: 4 additions & 8 deletions cmd/kor/pdbs.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,12 @@ var pdbCmd = &cobra.Command{
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
clientset := kor.GetKubeClient(kubeconfig)
if outputFormat == "json" || outputFormat == "yaml" {
if response, err := kor.GetUnusedPdbsStructured(includeExcludeLists, clientset, outputFormat); err != nil {
fmt.Println(err)
} else {
fmt.Println(response)
}

if response, err := kor.GetUnusedPdbs(includeExcludeLists, clientset, outputFormat, slackOpts); err != nil {
fmt.Println(err)
} else {
kor.GetUnusedPdbs(includeExcludeLists, clientset, slackOpts)
fmt.Println(response)
}

},
}

Expand Down
12 changes: 5 additions & 7 deletions cmd/kor/pvc.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,13 @@ var pvcCmd = &cobra.Command{
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
clientset := kor.GetKubeClient(kubeconfig)
if outputFormat == "json" || outputFormat == "yaml" {
if response, err := kor.GetUnusedPvcsStructured(includeExcludeLists, clientset, outputFormat); err != nil {
fmt.Println(err)
} else {
fmt.Println(response)
}

if response, err := kor.GetUnusedPvcs(includeExcludeLists, clientset, outputFormat, slackOpts); err != nil {
fmt.Println(err)
} else {
kor.GetUnusedPvcs(includeExcludeLists, clientset, slackOpts)
fmt.Println(response)
}

},
}

Expand Down
12 changes: 4 additions & 8 deletions cmd/kor/roles.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,12 @@ var roleCmd = &cobra.Command{
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
clientset := kor.GetKubeClient(kubeconfig)
if outputFormat == "json" || outputFormat == "yaml" {
if response, err := kor.GetUnusedRolesStructured(includeExcludeLists, clientset, outputFormat); err != nil {
fmt.Println(err)
} else {
fmt.Println(response)
}

if response, err := kor.GetUnusedRoles(includeExcludeLists, clientset, outputFormat, slackOpts); err != nil {
fmt.Println(err)
} else {
kor.GetUnusedRoles(includeExcludeLists, clientset, slackOpts)
fmt.Println(response)
}

},
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/kor/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func Execute() {
rootCmd.PersistentFlags().StringVarP(&kubeconfig, "kubeconfig", "k", "", "Path to kubeconfig file (optional)")
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)")
rootCmd.PersistentFlags().StringVar(&outputFormat, "output", "table", "Output format (table, json or yaml)")
rootCmd.PersistentFlags().StringVar(&slackOpts.WebhookURL, "slack-webhook-url", "", "Slack webhook URL to send notifications to")
rootCmd.PersistentFlags().StringVar(&slackOpts.Channel, "slack-channel", "", "Slack channel to send notifications to. --slack-channel requires --slack-auth-token to be set.")
rootCmd.PersistentFlags().StringVar(&slackOpts.Token, "slack-auth-token", "", "Slack auth token to send notifications to. --slack-auth-token requires --slack-channel to be set.")
Expand Down
12 changes: 4 additions & 8 deletions cmd/kor/secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,12 @@ var secretCmd = &cobra.Command{
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
clientset := kor.GetKubeClient(kubeconfig)
if outputFormat == "json" || outputFormat == "yaml" {
if response, err := kor.GetUnusedSecretsStructured(includeExcludeLists, clientset, outputFormat); err != nil {
fmt.Println(err)
} else {
fmt.Println(response)
}

if response, err := kor.GetUnusedSecrets(includeExcludeLists, clientset, outputFormat, slackOpts); err != nil {
fmt.Println(err)
} else {
kor.GetUnusedSecrets(includeExcludeLists, clientset, slackOpts)
fmt.Println(response)
}

},
}

Expand Down
12 changes: 4 additions & 8 deletions cmd/kor/serviceaccounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,12 @@ var serviceAccountCmd = &cobra.Command{
Args: cobra.ExactArgs(0),
Run: func(cmd *cobra.Command, args []string) {
clientset := kor.GetKubeClient(kubeconfig)
if outputFormat == "json" || outputFormat == "yaml" {
if response, err := kor.GetUnusedServiceAccountsStructured(includeExcludeLists, clientset, outputFormat); err != nil {
fmt.Println(err)
} else {
fmt.Println(response)
}

if response, err := kor.GetUnusedServiceAccounts(includeExcludeLists, clientset, outputFormat, slackOpts); err != nil {
fmt.Println(err)
} else {
kor.GetUnusedServiceAccounts(includeExcludeLists, clientset, slackOpts)
fmt.Println(response)
}

},
}

Expand Down
12 changes: 4 additions & 8 deletions cmd/kor/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,12 @@ var serviceCmd = &cobra.Command{
Args: cobra.ExactArgs(0),
Run: func(cmd *cobra.Command, args []string) {
clientset := kor.GetKubeClient(kubeconfig)
if outputFormat == "json" || outputFormat == "yaml" {
if response, err := kor.GetUnusedServicesStructured(includeExcludeLists, clientset, outputFormat); err != nil {
fmt.Println(err)
} else {
fmt.Println(response)
}

if response, err := kor.GetUnusedServices(includeExcludeLists, clientset, outputFormat, slackOpts); err != nil {
fmt.Println(err)
} else {
kor.GetUnusedServices(includeExcludeLists, clientset, slackOpts)
fmt.Println(response)
}

},
}

Expand Down
12 changes: 4 additions & 8 deletions cmd/kor/statefulsets.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,12 @@ var stsCmd = &cobra.Command{
Args: cobra.ExactArgs(0),
Run: func(cmd *cobra.Command, args []string) {
clientset := kor.GetKubeClient(kubeconfig)
if outputFormat == "json" || outputFormat == "yaml" {
if response, err := kor.GetUnusedStatefulSetsStructured(includeExcludeLists, clientset, outputFormat); err != nil {
fmt.Println(err)
} else {
fmt.Println(response)
}

if response, err := kor.GetUnusedStatefulSets(includeExcludeLists, clientset, outputFormat, slackOpts); err != nil {
fmt.Println(err)
} else {
kor.GetUnusedStatefulSets(includeExcludeLists, clientset, slackOpts)
fmt.Println(response)
}

},
}

Expand Down
76 changes: 9 additions & 67 deletions pkg/kor/all.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"os"

"k8s.io/client-go/kubernetes"
"sigs.k8s.io/yaml"
)

type GetUnusedResourceJSONResponse struct {
Expand Down Expand Up @@ -119,11 +118,12 @@ func getUnusedPdbs(clientset kubernetes.Interface, namespace string) ResourceDif
return namespacePdbDiff
}

func GetUnusedAll(includeExcludeLists IncludeExcludeLists, clientset kubernetes.Interface, slackOpts SlackOpts) {
namespaces := SetNamespaceList(includeExcludeLists, clientset)

func GetUnusedAll(includeExcludeLists IncludeExcludeLists, clientset kubernetes.Interface, outputFormat string, slackOpts SlackOpts) (string, error) {
var outputBuffer bytes.Buffer

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

for _, namespace := range namespaces {
var allDiffs []ResourceDiff
namespaceCMDiff := getUnusedCMs(clientset, namespace)
Expand Down Expand Up @@ -153,81 +153,23 @@ func GetUnusedAll(includeExcludeLists IncludeExcludeLists, clientset kubernetes.

outputBuffer.WriteString(output)
outputBuffer.WriteString("\n")
}

if slackOpts != (SlackOpts{}) {
if err := SendToSlack(SlackMessage{}, slackOpts, outputBuffer.String()); err != nil {
fmt.Fprintf(os.Stderr, "Failed to send message to slack: %v\n", err)
os.Exit(1)
}
} else {
fmt.Println(outputBuffer.String())
}
}

func GetUnusedAllStructured(includeExcludeLists IncludeExcludeLists, clientset kubernetes.Interface, outputFormat string) (string, error) {
namespaces := SetNamespaceList(includeExcludeLists, clientset)

// Create the JSON response object
response := make(map[string]map[string][]string)

for _, namespace := range namespaces {
var allDiffs []ResourceDiff

namespaceCMDiff := getUnusedCMs(clientset, namespace)
allDiffs = append(allDiffs, namespaceCMDiff)

namespaceSVCDiff := getUnusedSVCs(clientset, namespace)
allDiffs = append(allDiffs, namespaceSVCDiff)

namespaceSecretDiff := getUnusedSecrets(clientset, namespace)
allDiffs = append(allDiffs, namespaceSecretDiff)

namespaceSADiff := getUnusedServiceAccounts(clientset, namespace)
allDiffs = append(allDiffs, namespaceSADiff)

namespaceDeploymentDiff := getUnusedDeployments(clientset, namespace)
allDiffs = append(allDiffs, namespaceDeploymentDiff)

namespaceStatefulsetDiff := getUnusedStatefulSets(clientset, namespace)
allDiffs = append(allDiffs, namespaceStatefulsetDiff)

namespaceRoleDiff := getUnusedRoles(clientset, namespace)
allDiffs = append(allDiffs, namespaceRoleDiff)

namespaceHpaDiff := getUnusedHpas(clientset, namespace)
allDiffs = append(allDiffs, namespaceHpaDiff)

namespacePvcDiff := getUnusedPvcs(clientset, namespace)
allDiffs = append(allDiffs, namespacePvcDiff)

namespaceIngressDiff := getUnusedIngresses(clientset, namespace)
allDiffs = append(allDiffs, namespaceIngressDiff)

namespacePdbDiff := getUnusedPdbs(clientset, namespace)
allDiffs = append(allDiffs, namespacePdbDiff)

// Store the unused resources for each resource type in the JSON response
resourceMap := make(map[string][]string)
for _, diff := range allDiffs {
resourceMap[diff.resourceType] = diff.diff
}
response[namespace] = resourceMap
}

// Convert the response object to JSON
jsonResponse, err := json.MarshalIndent(response, "", " ")
if err != nil {
return "", err
}

if outputFormat == "yaml" {
yamlResponse, err := yaml.JSONToYAML(jsonResponse)
if err != nil {
fmt.Printf("err: %v\n", err)
}
return string(yamlResponse), nil
} else {
return string(jsonResponse), nil
unusedAll, err := unusedResourceFormatter(outputFormat, outputBuffer, slackOpts, jsonResponse)
if err != nil {
fmt.Printf("err: %v\n", err)
}

return unusedAll, nil
}
8 changes: 7 additions & 1 deletion pkg/kor/configmaps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,13 @@ func TestGetUnusedConfigmapsStructured(t *testing.T) {
ExcludeListStr: "",
}

output, err := GetUnusedConfigmapsStructured(includeExcludeLists, clientset, "json")
slackopts := SlackOpts{
WebhookURL: "",
Channel: "",
Token: "",
}

output, err := GetUnusedConfigmaps(includeExcludeLists, clientset, "json", slackopts)
if err != nil {
t.Fatalf("Error calling GetUnusedConfigmapsStructured: %v", err)
}
Expand Down
Loading

0 comments on commit ce0998a

Please sign in to comment.