Skip to content

Commit

Permalink
kor/used label check included into related funcs to ignore them (#62)
Browse files Browse the repository at this point in the history
* kor/used label check included into related funcs to ignore them

* comments removed & README updated

---------

Co-authored-by: Ozgur Demir <ozgur.demir@itutor.com>
  • Loading branch information
ozgurcd and Ozgur Demir committed Sep 11, 2023
1 parent 0dbc169 commit e3d760f
Show file tree
Hide file tree
Showing 12 changed files with 55 additions and 4 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ kor [subcommand] --help
| Pdbs | Pdbs not used in Deployments <br/> Pdbs not used in StatefulSets | |


## Ignore Resources
The resources labeled with "kor/used = true" will be ignored by kor even if they are unused. You can add this label to resources you want to ignore.

## Import Option
You can also use kor as a Go library to programmatically discover unused resources. By importing the github.com/yonahd/kor/pkg/kor package, you can call the relevant functions to retrieve unused resources. The library provides the option to get the results in JSON format by specifying the outputFormat parameter.
Expand Down
4 changes: 4 additions & 0 deletions pkg/kor/confimgmaps.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ func retrieveConfigMapNames(kubeClient *kubernetes.Clientset, namespace string)
}
names := make([]string, 0, len(configmaps.Items))
for _, configmap := range configmaps.Items {
if configmap.Labels["kor/used"] == "true" {
continue
}

names = append(names, configmap.Name)
}
return names, nil
Expand Down
5 changes: 4 additions & 1 deletion pkg/kor/deployments.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ func getDeploymentsWithoutReplicas(kubeClient *kubernetes.Clientset, namespace s
var deploymentsWithoutReplicas []string

for _, deployment := range deploymentsList.Items {
if deployment.Labels["kor/used"] == "true" {
continue
}

if *deployment.Spec.Replicas == 0 {
deploymentsWithoutReplicas = append(deploymentsWithoutReplicas, deployment.Name)
}
Expand All @@ -35,7 +39,6 @@ func ProcessNamespaceDeployments(clientset *kubernetes.Clientset, namespace stri
}

return usedServices, nil

}

func GetUnusedDeployments(includeExcludeLists IncludeExcludeLists, kubeconfig string) {
Expand Down
4 changes: 4 additions & 0 deletions pkg/kor/hpas.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ func extractUnusedHpas(clientset *kubernetes.Clientset, namespace string) ([]str

var diff []string
for _, hpa := range hpas.Items {
if hpa.Labels["kor/used"] == "true" {
continue
}

switch hpa.Spec.ScaleTargetRef.Kind {
case "Deployment":
if !slices.Contains(deploymentNames, hpa.Spec.ScaleTargetRef.Name) {
Expand Down
4 changes: 4 additions & 0 deletions pkg/kor/ingresses.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ func retrieveUsedIngress(kubeClient *kubernetes.Clientset, namespace string) ([]
usedIngresses := []string{}

for _, ingress := range ingresses.Items {
if ingress.Labels["kor/used"] == "true" {
continue
}

used := true

if ingress.Spec.DefaultBackend != nil {
Expand Down
4 changes: 4 additions & 0 deletions pkg/kor/pdbs.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ func processNamespacePdbs(clientset *kubernetes.Clientset, namespace string) ([]
}

for _, pdb := range pdbs.Items {
if pdb.Labels["kor/used"] == "true" {
continue
}

selector := pdb.Spec.Selector
deployments, err := clientset.AppsV1().Deployments(namespace).List(context.TODO(), metav1.ListOptions{
LabelSelector: metav1.FormatLabelSelector(selector),
Expand Down
4 changes: 4 additions & 0 deletions pkg/kor/pvc.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ func processNamespacePvcs(kubeClient *kubernetes.Clientset, namespace string) ([
}
pvcNames := make([]string, 0, len(pvcs.Items))
for _, pvc := range pvcs.Items {
if pvc.Labels["kor/used"] == "true" {
continue
}

pvcNames = append(pvcNames, pvc.Name)
}

Expand Down
4 changes: 4 additions & 0 deletions pkg/kor/roles.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ func retrieveRoleNames(kubeClient *kubernetes.Clientset, namespace string) ([]st
}
names := make([]string, 0, len(roles.Items))
for _, role := range roles.Items {
if role.Labels["kor/used"] == "true" {
continue
}

names = append(names, role.Name)
}
return names, nil
Expand Down
4 changes: 4 additions & 0 deletions pkg/kor/secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ func retrieveSecretNames(kubeClient *kubernetes.Clientset, namespace string) ([]
}
names := make([]string, 0, len(secrets.Items))
for _, secret := range secrets.Items {
if secret.Labels["kor/used"] == "true" {
continue
}

if !slices.Contains(exceptionSecretTypes, string(secret.Type)) {
names = append(names, secret.Name)
}
Expand Down
14 changes: 13 additions & 1 deletion pkg/kor/serviceaccounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@ func getServiceAccountsFromClusterRoleBindings(clientset *kubernetes.Clientset,

// Extract service account names from the role bindings
for _, rb := range roleBindings.Items {
if rb.Labels["kor/used"] == "true" {
continue
}

for _, subject := range rb.Subjects {

if subject.Kind == "ServiceAccount" {
serviceAccounts = append(serviceAccounts, subject.Name)
}
Expand All @@ -49,6 +54,10 @@ func getServiceAccountsFromRoleBindings(clientset *kubernetes.Clientset, namespa

// Extract service account names from the role bindings
for _, rb := range roleBindings.Items {
if rb.Labels["kor/used"] == "true" {
continue
}

for _, subject := range rb.Subjects {
if subject.Kind == "ServiceAccount" {
serviceAccounts = append(serviceAccounts, subject.Name)
Expand All @@ -60,7 +69,6 @@ func getServiceAccountsFromRoleBindings(clientset *kubernetes.Clientset, namespa
}

func retrieveUsedSA(kubeClient *kubernetes.Clientset, namespace string) ([]string, []string, []string, error) {

var podServiceAccounts []string

// Retrieve pods in the specified namespace
Expand Down Expand Up @@ -100,6 +108,10 @@ func retrieveServiceAccountNames(kubeClient *kubernetes.Clientset, namespace str
}
names := make([]string, 0, len(serviceaccounts.Items))
for _, serviceaccount := range serviceaccounts.Items {
if serviceaccount.Labels["kor/used"] == "true" {
continue
}

names = append(names, serviceaccount.Name)
}
return names, nil
Expand Down
5 changes: 4 additions & 1 deletion pkg/kor/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ func getEndpointsWithoutSubsets(kubeClient *kubernetes.Clientset, namespace stri
var endpointsWithoutSubsets []string

for _, endpoints := range endpointsList.Items {
if endpoints.Labels["kor/used"] == "true" {
continue
}

if len(endpoints.Subsets) == 0 {
endpointsWithoutSubsets = append(endpointsWithoutSubsets, endpoints.Name)
}
Expand All @@ -35,7 +39,6 @@ func ProcessNamespaceServices(clientset *kubernetes.Clientset, namespace string)
}

return usedServices, nil

}

func GetUnusedServices(includeExcludeLists IncludeExcludeLists, kubeconfig string) {
Expand Down
5 changes: 4 additions & 1 deletion pkg/kor/statefulsets.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ func getStatefulsetsWithoutReplicas(kubeClient *kubernetes.Clientset, namespace
var statefulsetsWithoutReplicas []string

for _, statefulset := range statefulsetsList.Items {
if statefulset.Labels["kor/used"] == "true" {
continue
}

if *statefulset.Spec.Replicas == 0 {
statefulsetsWithoutReplicas = append(statefulsetsWithoutReplicas, statefulset.Name)
}
Expand All @@ -35,7 +39,6 @@ func ProcessNamespaceStatefulsets(clientset *kubernetes.Clientset, namespace str
}

return usedServices, nil

}

func GetUnusedStatefulsets(includeExcludeLists IncludeExcludeLists, kubeconfig string) {
Expand Down

0 comments on commit e3d760f

Please sign in to comment.