Skip to content

Commit

Permalink
Add tests for unused kubernetes resources finders (#58)
Browse files Browse the repository at this point in the history
* add tests for some of the resources

* add tests for some of the resources

* adding tests for services sts deployments and ingress

* clean go mod

* adding tests for serviceAccounts

* rebase

* adding tests for pvcs

* adding tests for pdbs

* adding tests for secrets

* adding tests for secrets

* add tests for all modules

* add tests for services structured

* add tests for sts structured

* add tests for cm structured

* add tests for deploy structured

* add tests for deploy structured

* add test structured

* add test structured

* add test structured

* fix lint errors

---------

Co-authored-by: Yonah Dissen <[email protected]>
  • Loading branch information
yonahd and Yonah Dissen authored Sep 19, 2023
1 parent 77392bd commit 8cb5904
Show file tree
Hide file tree
Showing 31 changed files with 1,954 additions and 196 deletions.
8 changes: 7 additions & 1 deletion .github/workflows/merge-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,15 @@ jobs:
uses: golangci/golangci-lint-action@v3
with:
version: latest
skip-pkg-cache: true

- name: Build
run: go build -v ./...

- name: Test
run: go test -v ./...
run: go test -v ./... -coverprofile=coverage.txt

- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v3
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
![GitHub go.mod Go version (subdirectory of monorepo)](https://img.shields.io/github/go-mod/go-version/yonahd/kor)
![GitHub release (with filter)](https://img.shields.io/github/v/release/yonahd/kor?color=green&link=https://github.com/yonahd/kor/releases)
[![codecov](https://codecov.io/gh/yonahd/kor/branch/main/graph/badge.svg?token=tNKcOjlxLo)](https://codecov.io/gh/yonahd/kor)

# Kor - Kubernetes Orphaned Resources Finder

Expand Down
6 changes: 3 additions & 3 deletions cmd/kor/statefulsets.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@ import (
var stsCmd = &cobra.Command{
Use: "statefulset",
Aliases: []string{"sts", "statefulsets"},
Short: "Gets unused statefulsets",
Short: "Gets unused statefulSets",
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 {
if response, err := kor.GetUnusedStatefulSetsStructured(includeExcludeLists, clientset, outputFormat); err != nil {
fmt.Println(err)
} else {
fmt.Println(response)
}
} else {
kor.GetUnusedStatefulsets(includeExcludeLists, clientset)
kor.GetUnusedStatefulSets(includeExcludeLists, clientset)
}

},
Expand Down
6 changes: 5 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ require (
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emicklei/go-restful/v3 v3.10.1 // indirect
github.com/evanphx/json-patch v5.6.0+incompatible // indirect
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-openapi/jsonpointer v0.20.0 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
Expand All @@ -34,11 +35,14 @@ require (
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/onsi/ginkgo/v2 v2.11.0 // indirect
github.com/onsi/gomega v1.27.10 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/rivo/uniseg v0.4.4 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/net v0.13.0 // indirect
golang.org/x/oauth2 v0.10.0 // indirect
golang.org/x/sys v0.10.0 // indirect
golang.org/x/sys v0.11.0 // indirect
golang.org/x/term v0.10.0 // indirect
golang.org/x/text v0.11.0 // indirect
golang.org/x/time v0.3.0 // indirect
Expand Down
14 changes: 10 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/emicklei/go-restful/v3 v3.10.1 h1:rc42Y5YTp7Am7CS630D7JmhRjq4UlEUuEKfrDac4bSQ=
github.com/emicklei/go-restful/v3 v3.10.1/go.mod h1:6n3XBCmQQb25CM2LCACGz8ukIrRry+4bhvbpWn3mrbc=
github.com/evanphx/json-patch v5.6.0+incompatible h1:jBYDEEiFBPxA0v50tFdvOzQQTCvpL6mnFh5mB2/l16U=
github.com/evanphx/json-patch v5.6.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk=
github.com/go-logr/logr v1.2.0/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
github.com/go-logr/logr v1.2.4 h1:g01GSCwiDw2xSZfjJ2/T9M+S6pFdcNtFYsp+Y43HYDQ=
github.com/go-logr/logr v1.2.4/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
Expand Down Expand Up @@ -64,8 +66,12 @@ github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
github.com/onsi/ginkgo/v2 v2.9.4 h1:xR7vG4IXt5RWx6FfIjyAtsoMAtnc3C/rFXBBd2AjZwE=
github.com/onsi/gomega v1.27.6 h1:ENqfyGeS5AX/rlXDd/ETokDz93u0YufY1Pgxuy/PvWE=
github.com/onsi/ginkgo/v2 v2.11.0 h1:WgqUCUt/lT6yXoQ8Wef0fsNn5cAuMK7+KT9UFRz2tcU=
github.com/onsi/ginkgo/v2 v2.11.0/go.mod h1:ZhrRA5XmEE3x3rhlzamx/JJvujdZoJ2uvgI7kR0iZvM=
github.com/onsi/gomega v1.27.10 h1:naR28SdDFlqrG6kScpT8VWpu1xWY5nJRCF3XaYyBjhI=
github.com/onsi/gomega v1.27.10/go.mod h1:RsS8tutOdbdgzbPtzzATp12yT7kM5I5aElG3evPbQ0M=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
Expand Down Expand Up @@ -107,8 +113,8 @@ golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA=
golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM=
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.10.0 h1:3R7pNqamzBraeqj/Tj8qt1aQ2HpmlC+Cx/qL/7hn4/c=
golang.org/x/term v0.10.0/go.mod h1:lpqdcUyK/oCiQxvxVrppt5ggO2KCZ5QblwqPnfZ6d5o=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand Down
56 changes: 28 additions & 28 deletions pkg/kor/all.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,106 +19,106 @@ type ResourceDiff struct {
diff []string
}

func getUnusedCMs(kubeClient *kubernetes.Clientset, namespace string) ResourceDiff {
cmDiff, err := processNamespaceCM(kubeClient, namespace)
func getUnusedCMs(clientset kubernetes.Interface, namespace string) ResourceDiff {
cmDiff, err := processNamespaceCM(clientset, namespace)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to get %s namespace %s: %v\n", "configmaps", namespace, err)
}
namespaceCMDiff := ResourceDiff{"ConfigMap", cmDiff}
return namespaceCMDiff
}

func getUnusedSVCs(kubeClient *kubernetes.Clientset, namespace string) ResourceDiff {
svcDiff, err := ProcessNamespaceServices(kubeClient, namespace)
func getUnusedSVCs(clientset kubernetes.Interface, namespace string) ResourceDiff {
svcDiff, err := ProcessNamespaceServices(clientset, namespace)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to get %s namespace %s: %v\n", "services", namespace, err)
}
namespaceSVCDiff := ResourceDiff{"Service", svcDiff}
return namespaceSVCDiff
}

func getUnusedSecrets(kubeClient *kubernetes.Clientset, namespace string) ResourceDiff {
secretDiff, err := processNamespaceSecret(kubeClient, namespace)
func getUnusedSecrets(clientset kubernetes.Interface, namespace string) ResourceDiff {
secretDiff, err := processNamespaceSecret(clientset, namespace)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to get %s namespace %s: %v\n", "secrets", namespace, err)
}
namespaceSecretDiff := ResourceDiff{"Secret", secretDiff}
return namespaceSecretDiff
}

func getUnusedServiceAccounts(kubeClient *kubernetes.Clientset, namespace string) ResourceDiff {
saDiff, err := processNamespaceSA(kubeClient, namespace)
func getUnusedServiceAccounts(clientset kubernetes.Interface, namespace string) ResourceDiff {
saDiff, err := processNamespaceSA(clientset, namespace)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to get %s namespace %s: %v\n", "serviceaccounts", namespace, err)
}
namespaceSADiff := ResourceDiff{"ServiceAccount", saDiff}
return namespaceSADiff
}

func getUnusedDeployments(kubeClient *kubernetes.Clientset, namespace string) ResourceDiff {
deployDiff, err := ProcessNamespaceDeployments(kubeClient, namespace)
func getUnusedDeployments(clientset kubernetes.Interface, namespace string) ResourceDiff {
deployDiff, err := ProcessNamespaceDeployments(clientset, namespace)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to get %s namespace %s: %v\n", "deployments", namespace, err)
}
namespaceSADiff := ResourceDiff{"Deployment", deployDiff}
return namespaceSADiff
}

func getUnusedStatefulsets(kubeClient *kubernetes.Clientset, namespace string) ResourceDiff {
stsDiff, err := ProcessNamespaceStatefulsets(kubeClient, namespace)
func getUnusedStatefulSets(clientset kubernetes.Interface, namespace string) ResourceDiff {
stsDiff, err := ProcessNamespaceStatefulSets(clientset, namespace)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to get %s namespace %s: %v\n", "statefulsets", namespace, err)
fmt.Fprintf(os.Stderr, "Failed to get %s namespace %s: %v\n", "statefulSets", namespace, err)
}
namespaceSADiff := ResourceDiff{"Statefulset", stsDiff}
namespaceSADiff := ResourceDiff{"StatefulSet", stsDiff}
return namespaceSADiff
}

func getUnusedRoles(kubeClient *kubernetes.Clientset, namespace string) ResourceDiff {
roleDiff, err := processNamespaceRoles(kubeClient, namespace)
func getUnusedRoles(clientset kubernetes.Interface, namespace string) ResourceDiff {
roleDiff, err := processNamespaceRoles(clientset, namespace)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to get %s namespace %s: %v\n", "roles", namespace, err)
}
namespaceSADiff := ResourceDiff{"Role", roleDiff}
return namespaceSADiff
}

func getUnusedHpas(kubeClient *kubernetes.Clientset, namespace string) ResourceDiff {
hpaDiff, err := processNamespaceHpas(kubeClient, namespace)
func getUnusedHpas(clientset kubernetes.Interface, namespace string) ResourceDiff {
hpaDiff, err := processNamespaceHpas(clientset, namespace)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to get %s namespace %s: %v\n", "hpas", namespace, err)
}
namespaceHpaDiff := ResourceDiff{"Hpa", hpaDiff}
return namespaceHpaDiff
}

func getUnusedPvcs(kubeClient *kubernetes.Clientset, namespace string) ResourceDiff {
pvcDiff, err := processNamespacePvcs(kubeClient, namespace)
func getUnusedPvcs(clientset kubernetes.Interface, namespace string) ResourceDiff {
pvcDiff, err := processNamespacePvcs(clientset, namespace)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to get %s namespace %s: %v\n", "pvcs", namespace, err)
}
namespacePvcDiff := ResourceDiff{"Pvc", pvcDiff}
return namespacePvcDiff
}

func getUnusedIngresses(kubeClient *kubernetes.Clientset, namespace string) ResourceDiff {
ingressDiff, err := processNamespaceIngresses(kubeClient, namespace)
func getUnusedIngresses(clientset kubernetes.Interface, namespace string) ResourceDiff {
ingressDiff, err := processNamespaceIngresses(clientset, namespace)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to get %s namespace %s: %v\n", "ingresses", namespace, err)
}
namespaceIngressDiff := ResourceDiff{"Ingress", ingressDiff}
return namespaceIngressDiff
}

func getUnusedPdbs(kubeClient *kubernetes.Clientset, namespace string) ResourceDiff {
pdbDiff, err := processNamespacePdbs(kubeClient, namespace)
func getUnusedPdbs(clientset kubernetes.Interface, namespace string) ResourceDiff {
pdbDiff, err := processNamespacePdbs(clientset, namespace)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to get %s namespace %s: %v\n", "pdbs", namespace, err)
}
namespacePdbDiff := ResourceDiff{"Pdb", pdbDiff}
return namespacePdbDiff
}

func GetUnusedAll(includeExcludeLists IncludeExcludeLists, clientset *kubernetes.Clientset) {
func GetUnusedAll(includeExcludeLists IncludeExcludeLists, clientset kubernetes.Interface) {
namespaces := SetNamespaceList(includeExcludeLists, clientset)
for _, namespace := range namespaces {
var allDiffs []ResourceDiff
Expand All @@ -132,7 +132,7 @@ func GetUnusedAll(includeExcludeLists IncludeExcludeLists, clientset *kubernetes
allDiffs = append(allDiffs, namespaceSADiff)
namespaceDeploymentDiff := getUnusedDeployments(clientset, namespace)
allDiffs = append(allDiffs, namespaceDeploymentDiff)
namespaceStatefulsetDiff := getUnusedStatefulsets(clientset, namespace)
namespaceStatefulsetDiff := getUnusedStatefulSets(clientset, namespace)
allDiffs = append(allDiffs, namespaceStatefulsetDiff)
namespaceRoleDiff := getUnusedRoles(clientset, namespace)
allDiffs = append(allDiffs, namespaceRoleDiff)
Expand All @@ -150,7 +150,7 @@ func GetUnusedAll(includeExcludeLists IncludeExcludeLists, clientset *kubernetes
}
}

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

// Create the JSON response object
Expand All @@ -174,7 +174,7 @@ func GetUnusedAllStructured(includeExcludeLists IncludeExcludeLists, clientset *
namespaceDeploymentDiff := getUnusedDeployments(clientset, namespace)
allDiffs = append(allDiffs, namespaceDeploymentDiff)

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

namespaceRoleDiff := getUnusedRoles(clientset, namespace)
Expand Down
Loading

0 comments on commit 8cb5904

Please sign in to comment.