From 866cc47af5b6051904eabdd20ae56617d8024d25 Mon Sep 17 00:00:00 2001 From: Ozgur Demir Date: Thu, 31 Aug 2023 13:34:03 -0700 Subject: [PATCH 1/2] report if no unused resources found in a namespace --- pkg/kor/all.go | 7 ++++++- pkg/kor/kor.go | 12 ++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/pkg/kor/all.go b/pkg/kor/all.go index 4d1abed0..fbc5fe9f 100644 --- a/pkg/kor/all.go +++ b/pkg/kor/all.go @@ -149,7 +149,12 @@ func GetUnusedAll(namespace string, kubeconfig string) { namespacePdbDiff := getUnusedPdbs(kubeClient, namespace) allDiffs = append(allDiffs, namespacePdbDiff) output := FormatOutputAll(namespace, allDiffs) - fmt.Println(output) + + if len(output) == 0 { + fmt.Printf("No unused resources found in the namespace: %s\n", namespace) + } else { + fmt.Println(output) + } fmt.Println() } } diff --git a/pkg/kor/kor.go b/pkg/kor/kor.go index a63e79e4..64c6f3cf 100644 --- a/pkg/kor/kor.go +++ b/pkg/kor/kor.go @@ -96,8 +96,16 @@ func FormatOutputAll(namespace string, allDiffs []ResourceDiff) string { var buf bytes.Buffer table := tablewriter.NewWriter(&buf) table.SetHeader([]string{"#", "Resource Type", "Resource Name"}) + // TODO parse resourceType, diff + + allEmpty := true for _, data := range allDiffs { + if len(data.diff) == 0 { + continue + } + + allEmpty = false for _, val := range data.diff { row := []string{fmt.Sprintf("%d", i+1), data.resourceType, val} table.Append(row) @@ -105,6 +113,10 @@ func FormatOutputAll(namespace string, allDiffs []ResourceDiff) string { } } + if allEmpty { + return fmt.Sprintf("No unused resources found in the namespace: %s", namespace) + } + table.Render() return fmt.Sprintf("Unused Resources in Namespace: %s\n%s", namespace, buf.String()) } From 4a4965e678994c6f70a1cf581f21371e207e4695 Mon Sep 17 00:00:00 2001 From: Ozgur Demir Date: Thu, 31 Aug 2023 14:02:39 -0700 Subject: [PATCH 2/2] no changes required in this file, reverting --- pkg/kor/all.go | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/pkg/kor/all.go b/pkg/kor/all.go index fbc5fe9f..4d1abed0 100644 --- a/pkg/kor/all.go +++ b/pkg/kor/all.go @@ -149,12 +149,7 @@ func GetUnusedAll(namespace string, kubeconfig string) { namespacePdbDiff := getUnusedPdbs(kubeClient, namespace) allDiffs = append(allDiffs, namespacePdbDiff) output := FormatOutputAll(namespace, allDiffs) - - if len(output) == 0 { - fmt.Printf("No unused resources found in the namespace: %s\n", namespace) - } else { - fmt.Println(output) - } + fmt.Println(output) fmt.Println() } }