diff --git a/cmd/kor/configmaps.go b/cmd/kor/configmaps.go index 2211d3fb..fd13dd27 100644 --- a/cmd/kor/configmaps.go +++ b/cmd/kor/configmaps.go @@ -21,9 +21,9 @@ var configmapCmd = &cobra.Command{ fmt.Println(response) } } else if slackWebhookURL != "" { - kor.GetUnusedConfigmapsSendToSlackWebhook(includeExcludeLists, clientset, slackWebhookURL) + kor.GetUnusedConfigmaps(includeExcludeLists, clientset, slackWebhookURL) } else if slackChannel != "" && slackAuthToken != "" { - kor.GetUnusedConfigmapsSendToSlackAsFile(includeExcludeLists, clientset, slackChannel, slackAuthToken) + kor.GetUnusedConfigmaps(includeExcludeLists, clientset, slackChannel, slackAuthToken) } else { kor.GetUnusedConfigmaps(includeExcludeLists, clientset) } diff --git a/cmd/kor/deployments.go b/cmd/kor/deployments.go index b74e0967..3d1bf681 100644 --- a/cmd/kor/deployments.go +++ b/cmd/kor/deployments.go @@ -21,9 +21,9 @@ var deployCmd = &cobra.Command{ fmt.Println(response) } } else if slackWebhookURL != "" { - kor.GetUnusedDeploymentsSendToSlackWebhook(includeExcludeLists, clientset, slackWebhookURL) + kor.GetUnusedDeployments(includeExcludeLists, clientset, slackWebhookURL) } else if slackChannel != "" && slackAuthToken != "" { - kor.GetUnusedDeploymentsSendToSlackAsFile(includeExcludeLists, clientset, slackChannel, slackAuthToken) + kor.GetUnusedDeployments(includeExcludeLists, clientset, slackChannel, slackAuthToken) } else { kor.GetUnusedDeployments(includeExcludeLists, clientset) } diff --git a/cmd/kor/hpas.go b/cmd/kor/hpas.go index 643e049e..912e4ec3 100644 --- a/cmd/kor/hpas.go +++ b/cmd/kor/hpas.go @@ -21,9 +21,9 @@ var hpaCmd = &cobra.Command{ fmt.Println(response) } } else if slackWebhookURL != "" { - kor.GetUnusedHpasSendToSlackWebhook(includeExcludeLists, clientset, slackWebhookURL) + kor.GetUnusedHpas(includeExcludeLists, clientset, slackWebhookURL) } else if slackChannel != "" && slackAuthToken != "" { - kor.GetUnusedHpasSendToSlackAsFile(includeExcludeLists, clientset, slackChannel, slackAuthToken) + kor.GetUnusedHpas(includeExcludeLists, clientset, slackChannel, slackAuthToken) } else { kor.GetUnusedHpas(includeExcludeLists, clientset) } diff --git a/cmd/kor/ingresses.go b/cmd/kor/ingresses.go index c48cb938..7ab9e0ca 100644 --- a/cmd/kor/ingresses.go +++ b/cmd/kor/ingresses.go @@ -21,9 +21,9 @@ var ingressCmd = &cobra.Command{ fmt.Println(response) } } else if slackWebhookURL != "" { - kor.GetUnusedIngressesSendToSlackWebhook(includeExcludeLists, clientset, slackWebhookURL) + kor.GetUnusedIngresses(includeExcludeLists, clientset, slackWebhookURL) } else if slackChannel != "" && slackAuthToken != "" { - kor.GetUnusedIngressesSendToSlackAsFile(includeExcludeLists, clientset, slackChannel, slackAuthToken) + kor.GetUnusedIngresses(includeExcludeLists, clientset, slackChannel, slackAuthToken) } else { kor.GetUnusedIngresses(includeExcludeLists, clientset) } diff --git a/cmd/kor/pdbs.go b/cmd/kor/pdbs.go index 1afd840a..b637f436 100644 --- a/cmd/kor/pdbs.go +++ b/cmd/kor/pdbs.go @@ -21,9 +21,9 @@ var pdbCmd = &cobra.Command{ fmt.Println(response) } } else if slackWebhookURL != "" { - kor.GetUnusedPdbsSendToSlackWebhook(includeExcludeLists, clientset, slackWebhookURL) + kor.GetUnusedPdbs(includeExcludeLists, clientset, slackWebhookURL) } else if slackChannel != "" && slackAuthToken != "" { - kor.GetUnusedPdbsSendToSlackAsFile(includeExcludeLists, clientset, slackChannel, slackAuthToken) + kor.GetUnusedPdbs(includeExcludeLists, clientset, slackChannel, slackAuthToken) } else { kor.GetUnusedPdbs(includeExcludeLists, clientset) } diff --git a/cmd/kor/pvc.go b/cmd/kor/pvc.go index 41604098..42346169 100644 --- a/cmd/kor/pvc.go +++ b/cmd/kor/pvc.go @@ -21,9 +21,9 @@ var pvcCmd = &cobra.Command{ fmt.Println(response) } } else if slackWebhookURL != "" { - kor.GetUnusedPvcsSendToSlackWebhook(includeExcludeLists, clientset, slackWebhookURL) + kor.GetUnusedPvcs(includeExcludeLists, clientset, slackWebhookURL) } else if slackChannel != "" && slackAuthToken != "" { - kor.GetUnusedPvcsSendToSlackAsFile(includeExcludeLists, clientset, slackChannel, slackAuthToken) + kor.GetUnusedPvcs(includeExcludeLists, clientset, slackChannel, slackAuthToken) } else { kor.GetUnusedPvcs(includeExcludeLists, clientset) } diff --git a/cmd/kor/roles.go b/cmd/kor/roles.go index eabe1b50..b44caaf2 100644 --- a/cmd/kor/roles.go +++ b/cmd/kor/roles.go @@ -21,9 +21,9 @@ var roleCmd = &cobra.Command{ fmt.Println(response) } } else if slackWebhookURL != "" { - kor.GetUnusedRolesSendToSlackWebhook(includeExcludeLists, clientset, slackWebhookURL) + kor.GetUnusedRoles(includeExcludeLists, clientset, slackWebhookURL) } else if slackChannel != "" && slackAuthToken != "" { - kor.GetUnusedRolesSendToSlackAsFile(includeExcludeLists, clientset, slackChannel, slackAuthToken) + kor.GetUnusedRoles(includeExcludeLists, clientset, slackChannel, slackAuthToken) } else { kor.GetUnusedRoles(includeExcludeLists, clientset) } diff --git a/cmd/kor/secrets.go b/cmd/kor/secrets.go index bbeb590d..a634e99a 100644 --- a/cmd/kor/secrets.go +++ b/cmd/kor/secrets.go @@ -21,9 +21,9 @@ var secretCmd = &cobra.Command{ fmt.Println(response) } } else if slackWebhookURL != "" { - kor.GetUnusedSecretsSendToSlackWebhook(includeExcludeLists, clientset, slackWebhookURL) + kor.GetUnusedSecrets(includeExcludeLists, clientset, slackWebhookURL) } else if slackChannel != "" && slackAuthToken != "" { - kor.GetUnusedSecretsSendToSlackAsFile(includeExcludeLists, clientset, slackChannel, slackAuthToken) + kor.GetUnusedSecrets(includeExcludeLists, clientset, slackChannel, slackAuthToken) } else { kor.GetUnusedSecrets(includeExcludeLists, clientset) } diff --git a/cmd/kor/serviceaccounts.go b/cmd/kor/serviceaccounts.go index d39c1920..f3d82d86 100644 --- a/cmd/kor/serviceaccounts.go +++ b/cmd/kor/serviceaccounts.go @@ -21,9 +21,9 @@ var serviceAccountCmd = &cobra.Command{ fmt.Println(response) } } else if slackWebhookURL != "" { - kor.GetUnusedServiceAccountsSendToSlackWebhook(includeExcludeLists, clientset, slackWebhookURL) + kor.GetUnusedServiceAccounts(includeExcludeLists, clientset, slackWebhookURL) } else if slackChannel != "" && slackAuthToken != "" { - kor.GetUnusedServiceAccountsSendToSlackAsFile(includeExcludeLists, clientset, slackChannel, slackAuthToken) + kor.GetUnusedServiceAccounts(includeExcludeLists, clientset, slackChannel, slackAuthToken) } else { kor.GetUnusedServiceAccounts(includeExcludeLists, clientset) } diff --git a/cmd/kor/services.go b/cmd/kor/services.go index 72f2f896..6e8c65cf 100644 --- a/cmd/kor/services.go +++ b/cmd/kor/services.go @@ -21,9 +21,9 @@ var serviceCmd = &cobra.Command{ fmt.Println(response) } } else if slackWebhookURL != "" { - kor.GetUnusedServicesSendToSlackWebhook(includeExcludeLists, clientset, slackWebhookURL) + kor.GetUnusedServices(includeExcludeLists, clientset, slackWebhookURL) } else if slackChannel != "" && slackAuthToken != "" { - kor.GetUnusedServicesSendToSlackAsFile(includeExcludeLists, clientset, slackChannel, slackAuthToken) + kor.GetUnusedServices(includeExcludeLists, clientset, slackChannel, slackAuthToken) } else { kor.GetUnusedServices(includeExcludeLists, clientset) } diff --git a/cmd/kor/statefulsets.go b/cmd/kor/statefulsets.go index 3cbce64d..01c95455 100644 --- a/cmd/kor/statefulsets.go +++ b/cmd/kor/statefulsets.go @@ -21,9 +21,9 @@ var stsCmd = &cobra.Command{ fmt.Println(response) } } else if slackWebhookURL != "" { - kor.GetUnusedStatefulsetsSendToSlackWebhook(includeExcludeLists, clientset, slackWebhookURL) + kor.GetUnusedStatefulSets(includeExcludeLists, clientset, slackWebhookURL) } else if slackChannel != "" && slackAuthToken != "" { - kor.GetUnusedStatefulsetsSendToSlackAsFile(includeExcludeLists, clientset, slackChannel, slackAuthToken) + kor.GetUnusedStatefulSets(includeExcludeLists, clientset, slackChannel, slackAuthToken) } else { kor.GetUnusedStatefulSets(includeExcludeLists, clientset) } diff --git a/pkg/kor/confimgmaps.go b/pkg/kor/confimgmaps.go index 220e32bb..99e17618 100644 --- a/pkg/kor/confimgmaps.go +++ b/pkg/kor/confimgmaps.go @@ -129,22 +129,7 @@ func processNamespaceCM(clientset kubernetes.Interface, namespace string) ([]str } -func GetUnusedConfigmaps(includeExcludeLists IncludeExcludeLists, clientset kubernetes.Interface) { - namespaces := SetNamespaceList(includeExcludeLists, clientset) - - for _, namespace := range namespaces { - diff, err := processNamespaceCM(clientset, namespace) - if err != nil { - fmt.Fprintf(os.Stderr, "Failed to process namespace %s: %v\n", namespace, err) - continue - } - output := FormatOutput(namespace, diff, "Config Maps") - fmt.Println(output) - fmt.Println() - } -} - -func GetUnusedConfigmapsSendToSlackWebhook(includeExcludeLists IncludeExcludeLists, clientset kubernetes.Interface, slackWebhookURL string) { +func GetUnusedConfigmaps(includeExcludeLists IncludeExcludeLists, clientset kubernetes.Interface, slackParams ...string) { namespaces := SetNamespaceList(includeExcludeLists, clientset) var outputBuffer bytes.Buffer @@ -161,32 +146,18 @@ func GetUnusedConfigmapsSendToSlackWebhook(includeExcludeLists IncludeExcludeLis outputBuffer.WriteString("\n") } - if err := SendToSlackWebhook(slackWebhookURL, outputBuffer.String()); err != nil { - fmt.Fprintf(os.Stderr, "Failed to send output to Slack: %v\n", err) - } -} - -func GetUnusedConfigmapsSendToSlackAsFile(includeExcludeLists IncludeExcludeLists, clientset kubernetes.Interface, slackChannel string, slackAuthToken string) { - namespaces := SetNamespaceList(includeExcludeLists, clientset) - - var outputBuffer bytes.Buffer - - for _, namespace := range namespaces { - diff, err := processNamespaceCM(clientset, namespace) - if err != nil { - fmt.Fprintf(os.Stderr, "Failed to process namespace %s: %v\n", namespace, err) - continue + if len(slackParams) == 1 { + if err := SendToSlackWebhook(slackParams[0], outputBuffer.String()); err != nil { + fmt.Fprintf(os.Stderr, "Failed to send output to Slack: %v\n", err) } - output := FormatOutput(namespace, diff, "Config Maps") - - outputBuffer.WriteString(output) - outputBuffer.WriteString("\n") - } - - outputFilePath, _ := writeOutputToFile(outputBuffer) + } else if len(slackParams) == 2 { + outputFilePath, _ := writeOutputToFile(outputBuffer) - if err := SendFileToSlack(outputFilePath, "Unused Configmaps", slackChannel, slackAuthToken); err != nil { - fmt.Fprintf(os.Stderr, "Failed to send output to Slack: %v\n", err) + if err := SendFileToSlack(outputFilePath, "Unused Configmaps", slackParams[0], slackParams[1]); err != nil { + fmt.Fprintf(os.Stderr, "Failed to send output to Slack: %v\n", err) + } + } else { + fmt.Println(outputBuffer.String()) } } diff --git a/pkg/kor/deployments.go b/pkg/kor/deployments.go index 51fce65c..6cb7af8f 100644 --- a/pkg/kor/deployments.go +++ b/pkg/kor/deployments.go @@ -33,22 +33,7 @@ func ProcessNamespaceDeployments(clientset kubernetes.Interface, namespace strin return deploymentsWithoutReplicas, nil } -func GetUnusedDeployments(includeExcludeLists IncludeExcludeLists, clientset kubernetes.Interface) { - namespaces := SetNamespaceList(includeExcludeLists, clientset) - - for _, namespace := range namespaces { - diff, err := ProcessNamespaceDeployments(clientset, namespace) - if err != nil { - fmt.Fprintf(os.Stderr, "Failed to process namespace %s: %v\n", namespace, err) - continue - } - output := FormatOutput(namespace, diff, "Deployments") - fmt.Println(output) - fmt.Println() - } -} - -func GetUnusedDeploymentsSendToSlackWebhook(includeExcludeLists IncludeExcludeLists, clientset kubernetes.Interface, slackWebhookURL string) { +func GetUnusedDeployments(includeExcludeLists IncludeExcludeLists, clientset kubernetes.Interface, slackParams ...string) { namespaces := SetNamespaceList(includeExcludeLists, clientset) var outputBuffer bytes.Buffer @@ -65,32 +50,18 @@ func GetUnusedDeploymentsSendToSlackWebhook(includeExcludeLists IncludeExcludeLi outputBuffer.WriteString("\n") } - if err := SendToSlackWebhook(slackWebhookURL, outputBuffer.String()); err != nil { - fmt.Fprintf(os.Stderr, "Failed to send payload to Slack: %v\n", err) - } -} - -func GetUnusedDeploymentsSendToSlackAsFile(includeExcludeLists IncludeExcludeLists, clientset kubernetes.Interface, slackChannel string, slackAuthToken string) { - namespaces := SetNamespaceList(includeExcludeLists, clientset) - - var outputBuffer bytes.Buffer - - for _, namespace := range namespaces { - diff, err := ProcessNamespaceDeployments(clientset, namespace) - if err != nil { - fmt.Fprintf(os.Stderr, "Failed to process namespace %s: %v\n", namespace, err) - continue + if len(slackParams) == 1 { + if err := SendToSlackWebhook(slackParams[0], outputBuffer.String()); err != nil { + fmt.Fprintf(os.Stderr, "Failed to send output to Slack: %v\n", err) } - output := FormatOutput(namespace, diff, "Deployments") - - outputBuffer.WriteString(output) - outputBuffer.WriteString("\n") - } - - outputFilePath, _ := writeOutputToFile(outputBuffer) + } else if len(slackParams) == 2 { + outputFilePath, _ := writeOutputToFile(outputBuffer) - if err := SendFileToSlack(outputFilePath, "Unused Deployments", slackChannel, slackAuthToken); err != nil { - fmt.Fprintf(os.Stderr, "Failed to send output to Slack: %v\n", err) + if err := SendFileToSlack(outputFilePath, "Unused Deployments", slackParams[0], slackParams[1]); err != nil { + fmt.Fprintf(os.Stderr, "Failed to send output to Slack: %v\n", err) + } + } else { + fmt.Println(outputBuffer.String()) } } diff --git a/pkg/kor/hpas.go b/pkg/kor/hpas.go index 6dbbb51f..337cef3f 100644 --- a/pkg/kor/hpas.go +++ b/pkg/kor/hpas.go @@ -80,23 +80,7 @@ func processNamespaceHpas(clientset kubernetes.Interface, namespace string) ([]s return unusedHpas, nil } -func GetUnusedHpas(includeExcludeLists IncludeExcludeLists, clientset kubernetes.Interface) { - namespaces := SetNamespaceList(includeExcludeLists, clientset) - - for _, namespace := range namespaces { - diff, err := processNamespaceHpas(clientset, namespace) - if err != nil { - fmt.Fprintf(os.Stderr, "Failed to process namespace %s: %v\n", namespace, err) - continue - } - output := FormatOutput(namespace, diff, "Hpas") - fmt.Println(output) - fmt.Println() - } - -} - -func GetUnusedHpasSendToSlackWebhook(includeExcludeLists IncludeExcludeLists, clientset kubernetes.Interface, slackWebhookURL string) { +func GetUnusedHpas(includeExcludeLists IncludeExcludeLists, clientset kubernetes.Interface, slackParams ...string) { namespaces := SetNamespaceList(includeExcludeLists, clientset) var outputBuffer bytes.Buffer @@ -113,32 +97,18 @@ func GetUnusedHpasSendToSlackWebhook(includeExcludeLists IncludeExcludeLists, cl outputBuffer.WriteString("\n") } - if err := SendToSlackWebhook(slackWebhookURL, outputBuffer.String()); err != nil { - fmt.Fprintf(os.Stderr, "Failed to send payload to Slack: %v\n", err) - } -} - -func GetUnusedHpasSendToSlackAsFile(includeExcludeLists IncludeExcludeLists, clientset kubernetes.Interface, slackChannel string, slackAuthToken string) { - namespaces := SetNamespaceList(includeExcludeLists, clientset) - - var outputBuffer bytes.Buffer - - for _, namespace := range namespaces { - diff, err := processNamespaceHpas(clientset, namespace) - if err != nil { - fmt.Fprintf(os.Stderr, "Failed to process namespace %s: %v\n", namespace, err) - continue + if len(slackParams) == 1 { + if err := SendToSlackWebhook(slackParams[0], outputBuffer.String()); err != nil { + fmt.Fprintf(os.Stderr, "Failed to send output to Slack: %v\n", err) } - output := FormatOutput(namespace, diff, "Hpas") - - outputBuffer.WriteString(output) - outputBuffer.WriteString("\n") - } - - outputFilePath, _ := writeOutputToFile(outputBuffer) + } else if len(slackParams) == 2 { + outputFilePath, _ := writeOutputToFile(outputBuffer) - if err := SendFileToSlack(outputFilePath, "Unused HPAs", slackChannel, slackAuthToken); err != nil { - fmt.Fprintf(os.Stderr, "Failed to send output to Slack: %v\n", err) + if err := SendFileToSlack(outputFilePath, "Unused HPAs", slackParams[0], slackParams[1]); err != nil { + fmt.Fprintf(os.Stderr, "Failed to send output to Slack: %v\n", err) + } + } else { + fmt.Println(outputBuffer.String()) } } diff --git a/pkg/kor/ingresses.go b/pkg/kor/ingresses.go index 98ea7d1b..b59e5064 100644 --- a/pkg/kor/ingresses.go +++ b/pkg/kor/ingresses.go @@ -89,22 +89,7 @@ func processNamespaceIngresses(clientset kubernetes.Interface, namespace string) } -func GetUnusedIngresses(includeExcludeLists IncludeExcludeLists, clientset kubernetes.Interface) { - namespaces := SetNamespaceList(includeExcludeLists, clientset) - - for _, namespace := range namespaces { - diff, err := processNamespaceIngresses(clientset, namespace) - if err != nil { - fmt.Fprintf(os.Stderr, "Failed to process namespace %s: %v\n", namespace, err) - continue - } - output := FormatOutput(namespace, diff, "Ingresses") - fmt.Println(output) - fmt.Println() - } -} - -func GetUnusedIngressesSendToSlackWebhook(includeExcludeLists IncludeExcludeLists, clientset kubernetes.Interface, slackWebhookURL string) { +func GetUnusedIngresses(includeExcludeLists IncludeExcludeLists, clientset kubernetes.Interface, slackParams ...string) { namespaces := SetNamespaceList(includeExcludeLists, clientset) var outputBuffer bytes.Buffer @@ -121,32 +106,18 @@ func GetUnusedIngressesSendToSlackWebhook(includeExcludeLists IncludeExcludeList outputBuffer.WriteString("\n") } - if err := SendToSlackWebhook(slackWebhookURL, outputBuffer.String()); err != nil { - fmt.Fprintf(os.Stderr, "Failed to send payload to Slack: %v\n", err) - } -} - -func GetUnusedIngressesSendToSlackAsFile(includeExcludeLists IncludeExcludeLists, clientset kubernetes.Interface, slackChannel string, slackAuthToken string) { - namespaces := SetNamespaceList(includeExcludeLists, clientset) - - var outputBuffer bytes.Buffer - - for _, namespace := range namespaces { - diff, err := processNamespaceIngresses(clientset, namespace) - if err != nil { - fmt.Fprintf(os.Stderr, "Failed to process namespace %s: %v\n", namespace, err) - continue + if len(slackParams) == 1 { + if err := SendToSlackWebhook(slackParams[0], outputBuffer.String()); err != nil { + fmt.Fprintf(os.Stderr, "Failed to send output to Slack: %v\n", err) } - output := FormatOutput(namespace, diff, "Ingresses") - - outputBuffer.WriteString(output) - outputBuffer.WriteString("\n") - } - - outputFilePath, _ := writeOutputToFile(outputBuffer) + } else if len(slackParams) == 2 { + outputFilePath, _ := writeOutputToFile(outputBuffer) - if err := SendFileToSlack(outputFilePath, "Unused Ingresses", slackChannel, slackAuthToken); err != nil { - fmt.Fprintf(os.Stderr, "Failed to send output to Slack: %v\n", err) + if err := SendFileToSlack(outputFilePath, "Unused Ingresses", slackParams[0], slackParams[1]); err != nil { + fmt.Fprintf(os.Stderr, "Failed to send output to Slack: %v\n", err) + } + } else { + fmt.Println(outputBuffer.String()) } } diff --git a/pkg/kor/pdbs.go b/pkg/kor/pdbs.go index 0d30ec6f..9cc24483 100644 --- a/pkg/kor/pdbs.go +++ b/pkg/kor/pdbs.go @@ -49,22 +49,7 @@ func processNamespacePdbs(clientset kubernetes.Interface, namespace string) ([]s return unusedPdbs, nil } -func GetUnusedPdbs(includeExcludeLists IncludeExcludeLists, clientset kubernetes.Interface) { - namespaces := SetNamespaceList(includeExcludeLists, clientset) - - for _, namespace := range namespaces { - diff, err := processNamespacePdbs(clientset, namespace) - if err != nil { - fmt.Fprintf(os.Stderr, "Failed to process namespace %s: %v\n", namespace, err) - continue - } - output := FormatOutput(namespace, diff, "Pdbs") - fmt.Println(output) - fmt.Println() - } -} - -func GetUnusedPdbsSendToSlackWebhook(includeExcludeLists IncludeExcludeLists, clientset kubernetes.Interface, slackWebhookURL string) { +func GetUnusedPdbs(includeExcludeLists IncludeExcludeLists, clientset kubernetes.Interface, slackParams ...string) { namespaces := SetNamespaceList(includeExcludeLists, clientset) var outputBuffer bytes.Buffer @@ -81,32 +66,18 @@ func GetUnusedPdbsSendToSlackWebhook(includeExcludeLists IncludeExcludeLists, cl outputBuffer.WriteString("\n") } - if err := SendToSlackWebhook(slackWebhookURL, outputBuffer.String()); err != nil { - fmt.Fprintf(os.Stderr, "Failed to send payload to Slack: %v\n", err) - } -} - -func GetUnusedPdbsSendToSlackAsFile(includeExcludeLists IncludeExcludeLists, clientset kubernetes.Interface, slackChannel string, slackAuthToken string) { - namespaces := SetNamespaceList(includeExcludeLists, clientset) - - var outputBuffer bytes.Buffer - - for _, namespace := range namespaces { - diff, err := processNamespacePdbs(clientset, namespace) - if err != nil { - fmt.Fprintf(os.Stderr, "Failed to process namespace %s: %v\n", namespace, err) - continue + if len(slackParams) == 1 { + if err := SendToSlackWebhook(slackParams[0], outputBuffer.String()); err != nil { + fmt.Fprintf(os.Stderr, "Failed to send output to Slack: %v\n", err) } - output := FormatOutput(namespace, diff, "Pdbs") - - outputBuffer.WriteString(output) - outputBuffer.WriteString("\n") - } - - outputFilePath, _ := writeOutputToFile(outputBuffer) + } else if len(slackParams) == 2 { + outputFilePath, _ := writeOutputToFile(outputBuffer) - if err := SendFileToSlack(outputFilePath, "Unused Pdbs", slackChannel, slackAuthToken); err != nil { - fmt.Fprintf(os.Stderr, "Failed to send output to Slack: %v\n", err) + if err := SendFileToSlack(outputFilePath, "Unused Pdbs", slackParams[0], slackParams[1]); err != nil { + fmt.Fprintf(os.Stderr, "Failed to send output to Slack: %v\n", err) + } + } else { + fmt.Println(outputBuffer.String()) } } diff --git a/pkg/kor/pvc.go b/pkg/kor/pvc.go index cf7f4679..a35ec7d7 100644 --- a/pkg/kor/pvc.go +++ b/pkg/kor/pvc.go @@ -54,23 +54,7 @@ func processNamespacePvcs(clientset kubernetes.Interface, namespace string) ([]s return diff, nil } -func GetUnusedPvcs(includeExcludeLists IncludeExcludeLists, clientset kubernetes.Interface) { - namespaces := SetNamespaceList(includeExcludeLists, clientset) - - for _, namespace := range namespaces { - diff, err := processNamespacePvcs(clientset, namespace) - if err != nil { - fmt.Fprintf(os.Stderr, "Failed to process namespace %s: %v\n", namespace, err) - continue - } - output := FormatOutput(namespace, diff, "Pvcs") - fmt.Println(output) - fmt.Println() - } - -} - -func GetUnusedPvcsSendToSlackWebhook(includeExcludeLists IncludeExcludeLists, clientset kubernetes.Interface, slackWebhookURL string) { +func GetUnusedPvcs(includeExcludeLists IncludeExcludeLists, clientset kubernetes.Interface, slackParams ...string) { namespaces := SetNamespaceList(includeExcludeLists, clientset) var outputBuffer bytes.Buffer @@ -87,32 +71,18 @@ func GetUnusedPvcsSendToSlackWebhook(includeExcludeLists IncludeExcludeLists, cl outputBuffer.WriteString("\n") } - if err := SendToSlackWebhook(slackWebhookURL, outputBuffer.String()); err != nil { - fmt.Fprintf(os.Stderr, "Failed to send payload to Slack: %v\n", err) - } -} - -func GetUnusedPvcsSendToSlackAsFile(includeExcludeLists IncludeExcludeLists, clientset kubernetes.Interface, slackChannel string, slackAuthToken string) { - namespaces := SetNamespaceList(includeExcludeLists, clientset) - - var outputBuffer bytes.Buffer - - for _, namespace := range namespaces { - diff, err := processNamespacePvcs(clientset, namespace) - if err != nil { - fmt.Fprintf(os.Stderr, "Failed to process namespace %s: %v\n", namespace, err) - continue + if len(slackParams) == 1 { + if err := SendToSlackWebhook(slackParams[0], outputBuffer.String()); err != nil { + fmt.Fprintf(os.Stderr, "Failed to send output to Slack: %v\n", err) } - output := FormatOutput(namespace, diff, "Pvcs") - - outputBuffer.WriteString(output) - outputBuffer.WriteString("\n") - } - - outputFilePath, _ := writeOutputToFile(outputBuffer) + } else if len(slackParams) == 2 { + outputFilePath, _ := writeOutputToFile(outputBuffer) - if err := SendFileToSlack(outputFilePath, "Unused PVCs", slackChannel, slackAuthToken); err != nil { - fmt.Fprintf(os.Stderr, "Failed to send output to Slack: %v\n", err) + if err := SendFileToSlack(outputFilePath, "Unused Pvcs", slackParams[0], slackParams[1]); err != nil { + fmt.Fprintf(os.Stderr, "Failed to send output to Slack: %v\n", err) + } + } else { + fmt.Println(outputBuffer.String()) } } diff --git a/pkg/kor/roles.go b/pkg/kor/roles.go index fda6cd3f..41ce2029 100644 --- a/pkg/kor/roles.go +++ b/pkg/kor/roles.go @@ -67,22 +67,7 @@ func processNamespaceRoles(clientset kubernetes.Interface, namespace string) ([] } -func GetUnusedRoles(includeExcludeLists IncludeExcludeLists, clientset kubernetes.Interface) { - namespaces := SetNamespaceList(includeExcludeLists, clientset) - - for _, namespace := range namespaces { - diff, err := processNamespaceRoles(clientset, namespace) - if err != nil { - fmt.Fprintf(os.Stderr, "Failed to process namespace %s: %v\n", namespace, err) - continue - } - output := FormatOutput(namespace, diff, "Roles") - fmt.Println(output) - fmt.Println() - } -} - -func GetUnusedRolesSendToSlackWebhook(includeExcludeLists IncludeExcludeLists, clientset kubernetes.Interface, slackWebhookURL string) { +func GetUnusedRoles(includeExcludeLists IncludeExcludeLists, clientset kubernetes.Interface, slackParams ...string) { namespaces := SetNamespaceList(includeExcludeLists, clientset) var outputBuffer bytes.Buffer @@ -99,32 +84,18 @@ func GetUnusedRolesSendToSlackWebhook(includeExcludeLists IncludeExcludeLists, c outputBuffer.WriteString("\n") } - if err := SendToSlackWebhook(slackWebhookURL, outputBuffer.String()); err != nil { - fmt.Fprintf(os.Stderr, "Failed to send payload to Slack: %v\n", err) - } -} - -func GetUnusedRolesSendToSlackAsFile(includeExcludeLists IncludeExcludeLists, clientset kubernetes.Interface, slackChannel string, slackAuthToken string) { - namespaces := SetNamespaceList(includeExcludeLists, clientset) - - var outputBuffer bytes.Buffer - - for _, namespace := range namespaces { - diff, err := processNamespaceRoles(clientset, namespace) - if err != nil { - fmt.Fprintf(os.Stderr, "Failed to process namespace %s: %v\n", namespace, err) - continue + if len(slackParams) == 1 { + if err := SendToSlackWebhook(slackParams[0], outputBuffer.String()); err != nil { + fmt.Fprintf(os.Stderr, "Failed to send output to Slack: %v\n", err) } - output := FormatOutput(namespace, diff, "Roles") - - outputBuffer.WriteString(output) - outputBuffer.WriteString("\n") - } - - outputFilePath, _ := writeOutputToFile(outputBuffer) + } else if len(slackParams) == 2 { + outputFilePath, _ := writeOutputToFile(outputBuffer) - if err := SendFileToSlack(outputFilePath, "Unused Roles", slackChannel, slackAuthToken); err != nil { - fmt.Fprintf(os.Stderr, "Failed to send output to Slack: %v\n", err) + if err := SendFileToSlack(outputFilePath, "Unused Roles", slackParams[0], slackParams[1]); err != nil { + fmt.Fprintf(os.Stderr, "Failed to send output to Slack: %v\n", err) + } + } else { + fmt.Println(outputBuffer.String()) } } diff --git a/pkg/kor/secrets.go b/pkg/kor/secrets.go index 78b2eead..9b393d8e 100644 --- a/pkg/kor/secrets.go +++ b/pkg/kor/secrets.go @@ -141,22 +141,7 @@ func processNamespaceSecret(clientset kubernetes.Interface, namespace string) ([ } -func GetUnusedSecrets(includeExcludeLists IncludeExcludeLists, clientset kubernetes.Interface) { - namespaces := SetNamespaceList(includeExcludeLists, clientset) - - for _, namespace := range namespaces { - diff, err := processNamespaceSecret(clientset, namespace) - if err != nil { - fmt.Fprintf(os.Stderr, "Failed to process namespace %s: %v\n", namespace, err) - continue - } - output := FormatOutput(namespace, diff, "Secrets") - fmt.Println(output) - fmt.Println() - } -} - -func GetUnusedSecretsSendToSlackWebhook(includeExcludeLists IncludeExcludeLists, clientset kubernetes.Interface, slackWebhookURL string) { +func GetUnusedSecrets(includeExcludeLists IncludeExcludeLists, clientset kubernetes.Interface, slackParams ...string) { namespaces := SetNamespaceList(includeExcludeLists, clientset) var outputBuffer bytes.Buffer @@ -173,32 +158,18 @@ func GetUnusedSecretsSendToSlackWebhook(includeExcludeLists IncludeExcludeLists, outputBuffer.WriteString("\n") } - if err := SendToSlackWebhook(slackWebhookURL, outputBuffer.String()); err != nil { - fmt.Fprintf(os.Stderr, "Failed to send output to Slack: %v\n", err) - } -} - -func GetUnusedSecretsSendToSlackAsFile(includeExcludeLists IncludeExcludeLists, clientset kubernetes.Interface, slackChannel string, slackAuthToken string) { - namespaces := SetNamespaceList(includeExcludeLists, clientset) - - var outputBuffer bytes.Buffer - - for _, namespace := range namespaces { - diff, err := processNamespaceSecret(clientset, namespace) - if err != nil { - fmt.Fprintf(os.Stderr, "Failed to process namespace %s: %v\n", namespace, err) - continue + if len(slackParams) == 1 { + if err := SendToSlackWebhook(slackParams[0], outputBuffer.String()); err != nil { + fmt.Fprintf(os.Stderr, "Failed to send output to Slack: %v\n", err) } - output := FormatOutput(namespace, diff, "Secrets") - - outputBuffer.WriteString(output) - outputBuffer.WriteString("\n") - } - - outputFilePath, _ := writeOutputToFile(outputBuffer) + } else if len(slackParams) == 2 { + outputFilePath, _ := writeOutputToFile(outputBuffer) - if err := SendFileToSlack(outputFilePath, "Unused Secrets", slackChannel, slackAuthToken); err != nil { - fmt.Fprintf(os.Stderr, "Failed to send output to Slack: %v\n", err) + if err := SendFileToSlack(outputFilePath, "Unused Secrets", slackParams[0], slackParams[1]); err != nil { + fmt.Fprintf(os.Stderr, "Failed to send output to Slack: %v\n", err) + } + } else { + fmt.Println(outputBuffer.String()) } } diff --git a/pkg/kor/serviceaccounts.go b/pkg/kor/serviceaccounts.go index 507180e2..8e2d8b0e 100644 --- a/pkg/kor/serviceaccounts.go +++ b/pkg/kor/serviceaccounts.go @@ -140,22 +140,7 @@ func processNamespaceSA(clientset kubernetes.Interface, namespace string) ([]str } -func GetUnusedServiceAccounts(includeExcludeLists IncludeExcludeLists, clientset kubernetes.Interface) { - namespaces := SetNamespaceList(includeExcludeLists, clientset) - - for _, namespace := range namespaces { - diff, err := processNamespaceSA(clientset, namespace) - if err != nil { - fmt.Fprintf(os.Stderr, "Failed to process namespace %s: %v\n", namespace, err) - continue - } - output := FormatOutput(namespace, diff, "ServiceAccount") - fmt.Println(output) - fmt.Println() - } -} - -func GetUnusedServiceAccountsSendToSlackWebhook(includeExcludeLists IncludeExcludeLists, clientset kubernetes.Interface, slackWebhookURL string) { +func GetUnusedServiceAccounts(includeExcludeLists IncludeExcludeLists, clientset kubernetes.Interface, slackParams ...string) { namespaces := SetNamespaceList(includeExcludeLists, clientset) var outputBuffer bytes.Buffer @@ -172,32 +157,18 @@ func GetUnusedServiceAccountsSendToSlackWebhook(includeExcludeLists IncludeExclu outputBuffer.WriteString("\n") } - if err := SendToSlackWebhook(slackWebhookURL, outputBuffer.String()); err != nil { - fmt.Fprintf(os.Stderr, "Failed to send output to Slack: %v\n", err) - } -} - -func GetUnusedServiceAccountsSendToSlackAsFile(includeExcludeLists IncludeExcludeLists, clientset kubernetes.Interface, slackChannel string, slackAuthToken string) { - namespaces := SetNamespaceList(includeExcludeLists, clientset) - - var outputBuffer bytes.Buffer - - for _, namespace := range namespaces { - diff, err := processNamespaceSA(clientset, namespace) - if err != nil { - fmt.Fprintf(os.Stderr, "Failed to process namespace %s: %v\n", namespace, err) - continue + if len(slackParams) == 1 { + if err := SendToSlackWebhook(slackParams[0], outputBuffer.String()); err != nil { + fmt.Fprintf(os.Stderr, "Failed to send output to Slack: %v\n", err) } - output := FormatOutput(namespace, diff, "ServiceAccount") - - outputBuffer.WriteString(output) - outputBuffer.WriteString("\n") - } - - outputFilePath, _ := writeOutputToFile(outputBuffer) + } else if len(slackParams) == 2 { + outputFilePath, _ := writeOutputToFile(outputBuffer) - if err := SendFileToSlack(outputFilePath, "Unused ServiceAccounts", slackChannel, slackAuthToken); err != nil { - fmt.Fprintf(os.Stderr, "Failed to send output to Slack: %v\n", err) + if err := SendFileToSlack(outputFilePath, "Unused ServiceAccounts", slackParams[0], slackParams[1]); err != nil { + fmt.Fprintf(os.Stderr, "Failed to send output to Slack: %v\n", err) + } + } else { + fmt.Println(outputBuffer.String()) } } diff --git a/pkg/kor/services.go b/pkg/kor/services.go index 35419e72..86793266 100644 --- a/pkg/kor/services.go +++ b/pkg/kor/services.go @@ -33,22 +33,7 @@ func ProcessNamespaceServices(clientset kubernetes.Interface, namespace string) return endpointsWithoutSubsets, nil } -func GetUnusedServices(includeExcludeLists IncludeExcludeLists, clientset kubernetes.Interface) { - namespaces := SetNamespaceList(includeExcludeLists, clientset) - - for _, namespace := range namespaces { - diff, err := ProcessNamespaceServices(clientset, namespace) - if err != nil { - fmt.Fprintf(os.Stderr, "Failed to process namespace %s: %v\n", namespace, err) - continue - } - output := FormatOutput(namespace, diff, "Services") - fmt.Println(output) - fmt.Println() - } -} - -func GetUnusedServicesSendToSlackWebhook(includeExcludeLists IncludeExcludeLists, clientset kubernetes.Interface, slackWebhookURL string) { +func GetUnusedServices(includeExcludeLists IncludeExcludeLists, clientset kubernetes.Interface, slackParams ...string) { namespaces := SetNamespaceList(includeExcludeLists, clientset) var outputBuffer bytes.Buffer @@ -65,32 +50,18 @@ func GetUnusedServicesSendToSlackWebhook(includeExcludeLists IncludeExcludeLists outputBuffer.WriteString("\n") } - if err := SendToSlackWebhook(slackWebhookURL, outputBuffer.String()); err != nil { - fmt.Fprintf(os.Stderr, "Failed to send output to Slack: %v\n", err) - } -} - -func GetUnusedServicesSendToSlackAsFile(includeExcludeLists IncludeExcludeLists, clientset kubernetes.Interface, slackChannel string, slackAuthToken string) { - namespaces := SetNamespaceList(includeExcludeLists, clientset) - - var outputBuffer bytes.Buffer - - for _, namespace := range namespaces { - diff, err := ProcessNamespaceServices(clientset, namespace) - if err != nil { - fmt.Fprintf(os.Stderr, "Failed to process namespace %s: %v\n", namespace, err) - continue + if len(slackParams) == 1 { + if err := SendToSlackWebhook(slackParams[0], outputBuffer.String()); err != nil { + fmt.Fprintf(os.Stderr, "Failed to send output to Slack: %v\n", err) } - output := FormatOutput(namespace, diff, "Services") - - outputBuffer.WriteString(output) - outputBuffer.WriteString("\n") - } - - outputFilePath, _ := writeOutputToFile(outputBuffer) + } else if len(slackParams) == 2 { + outputFilePath, _ := writeOutputToFile(outputBuffer) - if err := SendFileToSlack(outputFilePath, "Unused Services", slackChannel, slackAuthToken); err != nil { - fmt.Fprintf(os.Stderr, "Failed to send output to Slack: %v\n", err) + if err := SendFileToSlack(outputFilePath, "Unused Services", slackParams[0], slackParams[1]); err != nil { + fmt.Fprintf(os.Stderr, "Failed to send output to Slack: %v\n", err) + } + } else { + fmt.Println(outputBuffer.String()) } } diff --git a/pkg/kor/statefulsets.go b/pkg/kor/statefulsets.go index cb72eacf..87d2a227 100644 --- a/pkg/kor/statefulsets.go +++ b/pkg/kor/statefulsets.go @@ -29,22 +29,7 @@ func ProcessNamespaceStatefulSets(clientset kubernetes.Interface, namespace stri return statefulSetsWithoutReplicas, nil } -func GetUnusedStatefulSets(includeExcludeLists IncludeExcludeLists, clientset kubernetes.Interface) { - namespaces := SetNamespaceList(includeExcludeLists, clientset) - - for _, namespace := range namespaces { - diff, err := ProcessNamespaceStatefulSets(clientset, namespace) - if err != nil { - fmt.Fprintf(os.Stderr, "Failed to process namespace %s: %v\n", namespace, err) - continue - } - output := FormatOutput(namespace, diff, "StatefulSets") - fmt.Println(output) - fmt.Println() - } -} - -func GetUnusedStatefulsetsSendToSlackWebhook(includeExcludeLists IncludeExcludeLists, clientset kubernetes.Interface, slackWebhookURL string) { +func GetUnusedStatefulSets(includeExcludeLists IncludeExcludeLists, clientset kubernetes.Interface, slackParams ...string) { namespaces := SetNamespaceList(includeExcludeLists, clientset) var outputBuffer bytes.Buffer @@ -61,32 +46,18 @@ func GetUnusedStatefulsetsSendToSlackWebhook(includeExcludeLists IncludeExcludeL outputBuffer.WriteString("\n") } - if err := SendToSlackWebhook(slackWebhookURL, outputBuffer.String()); err != nil { - fmt.Fprintf(os.Stderr, "Failed to send output to Slack: %v\n", err) - } -} - -func GetUnusedStatefulsetsSendToSlackAsFile(includeExcludeLists IncludeExcludeLists, clientset *kubernetes.Clientset, slackChannel string, slackAuthToken string) { - namespaces := SetNamespaceList(includeExcludeLists, clientset) - - var outputBuffer bytes.Buffer - - for _, namespace := range namespaces { - diff, err := ProcessNamespaceStatefulSets(clientset, namespace) - if err != nil { - fmt.Fprintf(os.Stderr, "Failed to process namespace %s: %v\n", namespace, err) - continue + if len(slackParams) == 1 { + if err := SendToSlackWebhook(slackParams[0], outputBuffer.String()); err != nil { + fmt.Fprintf(os.Stderr, "Failed to send output to Slack: %v\n", err) } - output := FormatOutput(namespace, diff, "Statefulsets") + } else if len(slackParams) == 2 { + outputFilePath, _ := writeOutputToFile(outputBuffer) - outputBuffer.WriteString(output) - outputBuffer.WriteString("\n") - } - - outputFilePath, _ := writeOutputToFile(outputBuffer) - - if err := SendFileToSlack(outputFilePath, "Unused Statefulsets", slackChannel, slackAuthToken); err != nil { - fmt.Fprintf(os.Stderr, "Failed to send output to Slack: %v\n", err) + if err := SendFileToSlack(outputFilePath, "Unused Statefulsets", slackParams[0], slackParams[1]); err != nil { + fmt.Fprintf(os.Stderr, "Failed to send output to Slack: %v\n", err) + } + } else { + fmt.Println(outputBuffer.String()) } }