Skip to content

Commit

Permalink
refactor: condense GetUnsedSlack functions into GetUnused functions
Browse files Browse the repository at this point in the history
  • Loading branch information
patricktalmeida committed Sep 21, 2023
1 parent a217e51 commit bb94a6e
Show file tree
Hide file tree
Showing 22 changed files with 143 additions and 464 deletions.
4 changes: 2 additions & 2 deletions cmd/kor/configmaps.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/kor/deployments.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/kor/hpas.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/kor/ingresses.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/kor/pdbs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/kor/pvc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/kor/roles.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/kor/secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/kor/serviceaccounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/kor/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/kor/statefulsets.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
51 changes: 11 additions & 40 deletions pkg/kor/confimgmaps.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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())
}
}

Expand Down
51 changes: 11 additions & 40 deletions pkg/kor/deployments.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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())
}
}

Expand Down
52 changes: 11 additions & 41 deletions pkg/kor/hpas.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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())
}
}

Expand Down
Loading

0 comments on commit bb94a6e

Please sign in to comment.