Skip to content

Commit

Permalink
chore: move slack to utils directory (#327)
Browse files Browse the repository at this point in the history
* chore: move slack to utils directory

* chore: move slack to utils directory

* chore: create common directory

* chore: create common directory

* chore: create common directory

* chore: create common directory

* chore: create common directory

* fix order

* fix order

---------

Co-authored-by: Yonah Dissen <yonahdissen@Yonahs-MacBook-Pro.local>
  • Loading branch information
yonahd and Yonah Dissen committed Jul 24, 2024
1 parent a602b45 commit 01ce4a6
Show file tree
Hide file tree
Showing 49 changed files with 126 additions and 74 deletions.
3 changes: 2 additions & 1 deletion cmd/kor/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/spf13/cobra"

"github.com/yonahd/kor/pkg/common"
"github.com/yonahd/kor/pkg/filters"
"github.com/yonahd/kor/pkg/kor"
"github.com/yonahd/kor/pkg/utils"
Expand Down Expand Up @@ -46,7 +47,7 @@ var rootCmd = &cobra.Command{
var (
outputFormat string
kubeconfig string
opts kor.Opts
opts common.Opts
filterOptions = &filters.Options{}
)

Expand Down
12 changes: 12 additions & 0 deletions pkg/common/params.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package common

type Opts struct {
DeleteFlag bool
NoInteractive bool
Verbose bool
WebhookURL string
Channel string
Token string
GroupBy string
ShowReason bool
}
7 changes: 4 additions & 3 deletions pkg/kor/all.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"

"github.com/yonahd/kor/pkg/common"
"github.com/yonahd/kor/pkg/filters"
)

Expand Down Expand Up @@ -263,7 +264,7 @@ func getUnusedNetworkPolicies(clientset kubernetes.Interface, namespace string,
return namespaceNetpolDiff
}

func GetUnusedAllNamespaced(filterOpts *filters.Options, clientset kubernetes.Interface, outputFormat string, opts Opts) (string, error) {
func GetUnusedAllNamespaced(filterOpts *filters.Options, clientset kubernetes.Interface, outputFormat string, opts common.Opts) (string, error) {
resources := make(map[string]map[string][]ResourceInfo)
for _, namespace := range filterOpts.Namespaces(clientset) {
switch opts.GroupBy {
Expand Down Expand Up @@ -325,7 +326,7 @@ func GetUnusedAllNamespaced(filterOpts *filters.Options, clientset kubernetes.In
return unusedAllNamespaced, nil
}

func GetUnusedAllNonNamespaced(filterOpts *filters.Options, clientset kubernetes.Interface, apiExtClient apiextensionsclientset.Interface, dynamicClient dynamic.Interface, outputFormat string, opts Opts) (string, error) {
func GetUnusedAllNonNamespaced(filterOpts *filters.Options, clientset kubernetes.Interface, apiExtClient apiextensionsclientset.Interface, dynamicClient dynamic.Interface, outputFormat string, opts common.Opts) (string, error) {
resources := make(map[string]map[string][]ResourceInfo)
switch opts.GroupBy {
case "namespace":
Expand Down Expand Up @@ -361,7 +362,7 @@ func GetUnusedAllNonNamespaced(filterOpts *filters.Options, clientset kubernetes
return unusedAllNonNamespaced, nil
}

func GetUnusedAll(filterOpts *filters.Options, clientset kubernetes.Interface, apiExtClient apiextensionsclientset.Interface, dynamicClient dynamic.Interface, outputFormat string, opts Opts) (string, error) {
func GetUnusedAll(filterOpts *filters.Options, clientset kubernetes.Interface, apiExtClient apiextensionsclientset.Interface, dynamicClient dynamic.Interface, outputFormat string, opts common.Opts) (string, error) {
unusedAllNamespaced, err := GetUnusedAllNamespaced(filterOpts, clientset, outputFormat, opts)
if err != nil {
fmt.Printf("err: %v\n", err)
Expand Down
3 changes: 2 additions & 1 deletion pkg/kor/clusterroles.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
_ "k8s.io/client-go/plugin/pkg/client/auth/oidc"
"k8s.io/utils/strings/slices"

"github.com/yonahd/kor/pkg/common"
"github.com/yonahd/kor/pkg/filters"
)

Expand Down Expand Up @@ -179,7 +180,7 @@ func processClusterRoles(clientset kubernetes.Interface, filterOpts *filters.Opt

}

func GetUnusedClusterRoles(filterOpts *filters.Options, clientset kubernetes.Interface, outputFormat string, opts Opts) (string, error) {
func GetUnusedClusterRoles(filterOpts *filters.Options, clientset kubernetes.Interface, outputFormat string, opts common.Opts) (string, error) {
resources := make(map[string]map[string][]ResourceInfo)
diff, err := processClusterRoles(clientset, filterOpts)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion pkg/kor/clusterroles_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"k8s.io/client-go/kubernetes/fake"
"k8s.io/client-go/kubernetes/scheme"

"github.com/yonahd/kor/pkg/common"
"github.com/yonahd/kor/pkg/filters"
)

Expand Down Expand Up @@ -138,7 +139,7 @@ func TestProcessClusterRoles(t *testing.T) {
func TestGetUnusedClusterRolesStructured(t *testing.T) {
clientset := createTestClusterRoles(t)

opts := Opts{
opts := common.Opts{
WebhookURL: "",
Channel: "",
Token: "",
Expand Down
3 changes: 2 additions & 1 deletion pkg/kor/configmaps.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"k8s.io/client-go/kubernetes"
_ "k8s.io/client-go/plugin/pkg/client/auth/oidc"

"github.com/yonahd/kor/pkg/common"
"github.com/yonahd/kor/pkg/filters"
)

Expand Down Expand Up @@ -158,7 +159,7 @@ func processNamespaceCM(clientset kubernetes.Interface, namespace string, filter
return diff, nil
}

func GetUnusedConfigmaps(filterOpts *filters.Options, clientset kubernetes.Interface, outputFormat string, opts Opts) (string, error) {
func GetUnusedConfigmaps(filterOpts *filters.Options, clientset kubernetes.Interface, outputFormat string, opts common.Opts) (string, error) {
resources := make(map[string]map[string][]ResourceInfo)
for _, namespace := range filterOpts.Namespaces(clientset) {
diff, err := processNamespaceCM(clientset, namespace, filterOpts)
Expand Down
3 changes: 2 additions & 1 deletion pkg/kor/configmaps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"k8s.io/client-go/kubernetes/fake"
"k8s.io/client-go/kubernetes/scheme"

"github.com/yonahd/kor/pkg/common"
"github.com/yonahd/kor/pkg/filters"
)

Expand Down Expand Up @@ -197,7 +198,7 @@ func TestRetrieveUsedCM(t *testing.T) {
func TestGetUnusedConfigmapsStructured(t *testing.T) {
clientset := createTestConfigmaps(t)

opts := Opts{
opts := common.Opts{
WebhookURL: "",
Channel: "",
Token: "",
Expand Down
3 changes: 2 additions & 1 deletion pkg/kor/crds.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"k8s.io/client-go/dynamic"
_ "k8s.io/client-go/plugin/pkg/client/auth/oidc"

"github.com/yonahd/kor/pkg/common"
"github.com/yonahd/kor/pkg/filters"
)

Expand Down Expand Up @@ -65,7 +66,7 @@ func processCrds(apiExtClient apiextensionsclientset.Interface, dynamicClient dy
return unusedCRDs, nil
}

func GetUnusedCrds(_ *filters.Options, apiExtClient apiextensionsclientset.Interface, dynamicClient dynamic.Interface, outputFormat string, opts Opts) (string, error) {
func GetUnusedCrds(_ *filters.Options, apiExtClient apiextensionsclientset.Interface, dynamicClient dynamic.Interface, outputFormat string, opts common.Opts) (string, error) {
resources := make(map[string]map[string][]ResourceInfo)
diff, err := processCrds(apiExtClient, dynamicClient, &filters.Options{})
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion pkg/kor/daemonsets.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"

"github.com/yonahd/kor/pkg/common"
"github.com/yonahd/kor/pkg/filters"
)

Expand Down Expand Up @@ -59,7 +60,7 @@ func processNamespaceDaemonSets(clientset kubernetes.Interface, namespace string
return daemonSetsWithoutReplicas, nil
}

func GetUnusedDaemonSets(filterOpts *filters.Options, clientset kubernetes.Interface, outputFormat string, opts Opts) (string, error) {
func GetUnusedDaemonSets(filterOpts *filters.Options, clientset kubernetes.Interface, outputFormat string, opts common.Opts) (string, error) {
resources := make(map[string]map[string][]ResourceInfo)
for _, namespace := range filterOpts.Namespaces(clientset) {
diff, err := processNamespaceDaemonSets(clientset, namespace, filterOpts)
Expand Down
3 changes: 2 additions & 1 deletion pkg/kor/daemonsets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"k8s.io/client-go/kubernetes/fake"
"k8s.io/client-go/kubernetes/scheme"

"github.com/yonahd/kor/pkg/common"
"github.com/yonahd/kor/pkg/filters"
)

Expand Down Expand Up @@ -82,7 +83,7 @@ func TestProcessNamespaceDaemonSets(t *testing.T) {
func TestGetUnusedDaemonSetsStructured(t *testing.T) {
clientset := createTestDaemonSets(t)

opts := Opts{
opts := common.Opts{
WebhookURL: "",
Channel: "",
Token: "",
Expand Down
3 changes: 2 additions & 1 deletion pkg/kor/deployments.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"

"github.com/yonahd/kor/pkg/common"
"github.com/yonahd/kor/pkg/filters"
)

Expand Down Expand Up @@ -41,7 +42,7 @@ func processNamespaceDeployments(clientset kubernetes.Interface, namespace strin
return deploymentsWithoutReplicas, nil
}

func GetUnusedDeployments(filterOpts *filters.Options, clientset kubernetes.Interface, outputFormat string, opts Opts) (string, error) {
func GetUnusedDeployments(filterOpts *filters.Options, clientset kubernetes.Interface, outputFormat string, opts common.Opts) (string, error) {
resources := make(map[string]map[string][]ResourceInfo)
for _, namespace := range filterOpts.Namespaces(clientset) {
diff, err := processNamespaceDeployments(clientset, namespace, filterOpts)
Expand Down
3 changes: 2 additions & 1 deletion pkg/kor/deployments_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"k8s.io/client-go/kubernetes/fake"
"k8s.io/client-go/kubernetes/scheme"

"github.com/yonahd/kor/pkg/common"
"github.com/yonahd/kor/pkg/filters"
)

Expand Down Expand Up @@ -74,7 +75,7 @@ func TestProcessNamespaceDeployments(t *testing.T) {
func TestGetUnusedDeploymentsStructured(t *testing.T) {
clientset := createTestDeployments(t)

opts := Opts{
opts := common.Opts{
WebhookURL: "",
Channel: "",
Token: "",
Expand Down
7 changes: 4 additions & 3 deletions pkg/kor/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"

"github.com/yonahd/kor/pkg/common"
"github.com/yonahd/kor/pkg/filters"
)

Expand All @@ -33,7 +34,7 @@ func init() {
}

// TODO: add option to change port / url !?
func Exporter(filterOptions *filters.Options, clientset kubernetes.Interface, apiExtClient apiextensionsclientset.Interface, dynamicClient dynamic.Interface, outputFormat string, opts Opts, resourceList []string) {
func Exporter(filterOptions *filters.Options, clientset kubernetes.Interface, apiExtClient apiextensionsclientset.Interface, dynamicClient dynamic.Interface, outputFormat string, opts common.Opts, resourceList []string) {
http.Handle("/metrics", promhttp.Handler())
fmt.Println("Server listening on :8080")
go exportMetrics(filterOptions, clientset, apiExtClient, dynamicClient, outputFormat, opts, resourceList) // Start exporting metrics in the background
Expand All @@ -42,7 +43,7 @@ func Exporter(filterOptions *filters.Options, clientset kubernetes.Interface, ap
}
}

func exportMetrics(filterOptions *filters.Options, clientset kubernetes.Interface, apiExtClient apiextensionsclientset.Interface, dynamicClient dynamic.Interface, outputFormat string, opts Opts, resourceList []string) {
func exportMetrics(filterOptions *filters.Options, clientset kubernetes.Interface, apiExtClient apiextensionsclientset.Interface, dynamicClient dynamic.Interface, outputFormat string, opts common.Opts, resourceList []string) {
exporterInterval := os.Getenv("EXPORTER_INTERVAL")
if exporterInterval == "" {
exporterInterval = "10"
Expand Down Expand Up @@ -79,7 +80,7 @@ func exportMetrics(filterOptions *filters.Options, clientset kubernetes.Interfac
}
}

func getUnusedResources(filterOptions *filters.Options, clientset kubernetes.Interface, apiExtClient apiextensionsclientset.Interface, dynamicClient dynamic.Interface, outputFormat string, opts Opts, resourceList []string) (string, error) {
func getUnusedResources(filterOptions *filters.Options, clientset kubernetes.Interface, apiExtClient apiextensionsclientset.Interface, dynamicClient dynamic.Interface, outputFormat string, opts common.Opts, resourceList []string) (string, error) {
if len(resourceList) == 0 || (len(resourceList) == 1 && resourceList[0] == "all") {
return GetUnusedAll(filterOptions, clientset, apiExtClient, dynamicClient, outputFormat, opts)
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/kor/finalizers.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
_ "k8s.io/client-go/plugin/pkg/client/auth/oidc"
"k8s.io/utils/strings/slices"

"github.com/yonahd/kor/pkg/common"
"github.com/yonahd/kor/pkg/filters"
)

Expand Down Expand Up @@ -78,7 +79,7 @@ func getResourcesWithFinalizersPendingDeletion(clientset kubernetes.Interface, d
return retrievePendingDeletionResources(resourceTypes, dynamicClient, filterOpts)
}

func GetUnusedfinalizers(filterOpts *filters.Options, clientset kubernetes.Interface, dynamicClient *dynamic.DynamicClient, outputFormat string, opts Opts) (string, error) {
func GetUnusedfinalizers(filterOpts *filters.Options, clientset kubernetes.Interface, dynamicClient *dynamic.DynamicClient, outputFormat string, opts common.Opts) (string, error) {
var outputBuffer bytes.Buffer
namespaces := filterOpts.Namespaces(clientset)
response := make(map[string]map[string][]ResourceInfo)
Expand Down
15 changes: 9 additions & 6 deletions pkg/kor/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import (

"github.com/olekukonko/tablewriter"
"sigs.k8s.io/yaml"

"github.com/yonahd/kor/pkg/common"
"github.com/yonahd/kor/pkg/utils"
)

type ResourceInfo struct {
Expand All @@ -22,13 +25,13 @@ func getTableRow(index int, columns ...string) []string {
return row
}

func unusedResourceFormatter(outputFormat string, outputBuffer bytes.Buffer, opts Opts, jsonResponse []byte) (string, error) {
func unusedResourceFormatter(outputFormat string, outputBuffer bytes.Buffer, opts common.Opts, jsonResponse []byte) (string, error) {
switch outputFormat {
case "table":
if opts.WebhookURL == "" || opts.Channel == "" || opts.Token != "" {
return outputBuffer.String(), nil
}
if err := SendToSlack(SlackMessage{}, opts, outputBuffer.String()); err != nil {
if err := utils.SendToSlack(utils.SlackMessage{}, opts, outputBuffer.String()); err != nil {
return "", fmt.Errorf("failed to send message to slack: %w", err)
}
case "json", "yaml":
Expand Down Expand Up @@ -81,7 +84,7 @@ func unusedResourceFormatter(outputFormat string, outputBuffer bytes.Buffer, opt
return "", fmt.Errorf("unsupported output format: %s", outputFormat)
}

func FormatOutput(resources map[string]map[string][]ResourceInfo, opts Opts) bytes.Buffer {
func FormatOutput(resources map[string]map[string][]ResourceInfo, opts common.Opts) bytes.Buffer {
var output bytes.Buffer
switch opts.GroupBy {
case "namespace":
Expand All @@ -96,7 +99,7 @@ func FormatOutput(resources map[string]map[string][]ResourceInfo, opts Opts) byt
return output
}

func formatOutputForNamespace(namespace string, resources map[string][]ResourceInfo, opts Opts) string {
func formatOutputForNamespace(namespace string, resources map[string][]ResourceInfo, opts common.Opts) string {
var buf strings.Builder
table := tablewriter.NewWriter(&buf)
table.SetColWidth(60)
Expand Down Expand Up @@ -125,7 +128,7 @@ func formatOutputForNamespace(namespace string, resources map[string][]ResourceI
return fmt.Sprintf("Unused resources in namespace: %q\n%s\n", namespace, buf.String())
}

func formatOutputForResource(resource string, resources map[string][]ResourceInfo, opts Opts) string {
func formatOutputForResource(resource string, resources map[string][]ResourceInfo, opts common.Opts) string {
if len(resources) == 0 {
if opts.Verbose {
return fmt.Sprintf("No unused %ss found\n", resource)
Expand Down Expand Up @@ -207,7 +210,7 @@ func getTableRowResourceInfo(index int, resourceType string, resource ResourceIn
return row
}

func FormatOutputAll(namespace string, allDiffs []ResourceDiff, opts Opts) string {
func FormatOutputAll(namespace string, allDiffs []ResourceDiff, opts common.Opts) string {
var buf strings.Builder
table := tablewriter.NewWriter(&buf)
table.SetColWidth(60)
Expand Down
3 changes: 2 additions & 1 deletion pkg/kor/hpas.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
_ "k8s.io/client-go/plugin/pkg/client/auth/oidc"
"k8s.io/utils/strings/slices"

"github.com/yonahd/kor/pkg/common"
"github.com/yonahd/kor/pkg/filters"
)

Expand Down Expand Up @@ -80,7 +81,7 @@ func processNamespaceHpas(clientset kubernetes.Interface, namespace string, filt
return unusedHpas, nil
}

func GetUnusedHpas(filterOpts *filters.Options, clientset kubernetes.Interface, outputFormat string, opts Opts) (string, error) {
func GetUnusedHpas(filterOpts *filters.Options, clientset kubernetes.Interface, outputFormat string, opts common.Opts) (string, error) {
resources := make(map[string]map[string][]ResourceInfo)
for _, namespace := range filterOpts.Namespaces(clientset) {
diff, err := processNamespaceHpas(clientset, namespace, filterOpts)
Expand Down
3 changes: 2 additions & 1 deletion pkg/kor/hpas_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"k8s.io/client-go/kubernetes/fake"
"k8s.io/client-go/kubernetes/scheme"

"github.com/yonahd/kor/pkg/common"
"github.com/yonahd/kor/pkg/filters"
)

Expand Down Expand Up @@ -83,7 +84,7 @@ func TestExtractUnusedHpas(t *testing.T) {
func TestGetUnusedHpasStructured(t *testing.T) {
clientset := createTestHpas(t)

opts := Opts{
opts := common.Opts{
WebhookURL: "",
Channel: "",
Token: "",
Expand Down
3 changes: 2 additions & 1 deletion pkg/kor/ingresses.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"k8s.io/client-go/kubernetes"
_ "k8s.io/client-go/plugin/pkg/client/auth/oidc"

"github.com/yonahd/kor/pkg/common"
"github.com/yonahd/kor/pkg/filters"
)

Expand Down Expand Up @@ -116,7 +117,7 @@ func processNamespaceIngresses(clientset kubernetes.Interface, namespace string,

}

func GetUnusedIngresses(filterOpts *filters.Options, clientset kubernetes.Interface, outputFormat string, opts Opts) (string, error) {
func GetUnusedIngresses(filterOpts *filters.Options, clientset kubernetes.Interface, outputFormat string, opts common.Opts) (string, error) {
resources := make(map[string]map[string][]ResourceInfo)
for _, namespace := range filterOpts.Namespaces(clientset) {
diff, err := processNamespaceIngresses(clientset, namespace, filterOpts)
Expand Down
3 changes: 2 additions & 1 deletion pkg/kor/ingresses_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"k8s.io/client-go/kubernetes/fake"
"k8s.io/client-go/kubernetes/scheme"

"github.com/yonahd/kor/pkg/common"
"github.com/yonahd/kor/pkg/filters"
)

Expand Down Expand Up @@ -81,7 +82,7 @@ func TestRetrieveUsedIngress(t *testing.T) {
func TestGetUnusedIngressesStructured(t *testing.T) {
clientset := createTestIngresses(t)

opts := Opts{
opts := common.Opts{
WebhookURL: "",
Channel: "",
Token: "",
Expand Down
Loading

0 comments on commit 01ce4a6

Please sign in to comment.