Skip to content

Commit

Permalink
refactor: Use new filter framework & unused labels (#387)
Browse files Browse the repository at this point in the history
  • Loading branch information
doronkg authored Dec 3, 2024
1 parent 050c06d commit b6614f3
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 6 deletions.
8 changes: 7 additions & 1 deletion pkg/kor/crds.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,13 @@ func processCrds(apiExtClient apiextensionsclientset.Interface, dynamicClient dy
}

for _, crd := range crds.Items {
if pass := filters.KorLabelFilter(&crd, &filters.Options{}); pass {
if pass, _ := filter.SetObject(&crd).Run(filterOpts); pass {
continue
}

if crd.Labels["kor/used"] == "false" {
reason := "Marked with unused label"
unusedCRDs = append(unusedCRDs, ResourceInfo{Name: crd.Name, Reason: reason})
continue
}

Expand Down
6 changes: 6 additions & 0 deletions pkg/kor/jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ func processNamespaceJobs(clientset kubernetes.Interface, namespace string, filt
continue
}

if job.Labels["kor/used"] == "false" {
reason := "Marked with unused label"
unusedJobNames = append(unusedJobNames, ResourceInfo{Name: job.Name, Reason: reason})
continue
}

exceptionFound, err := isResourceException(job.Name, job.Namespace, config.ExceptionJobs)
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion pkg/kor/pods.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func processNamespacePods(clientset kubernetes.Interface, namespace string, filt
var evictedPods []ResourceInfo

for _, pod := range podsList.Items {
if pass := filters.KorLabelFilter(&pod, &filters.Options{}); pass {
if pass, _ := filter.SetObject(&pod).Run(filterOpts); pass {
continue
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/kor/pv.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func processPvs(clientset kubernetes.Interface, filterOpts *filters.Options) ([]
var unusedPvs []ResourceInfo

for _, pv := range pvs.Items {
if pass := filters.KorLabelFilter(&pv, &filters.Options{}); pass {
if pass, _ := filter.SetObject(&pv).Run(filterOpts); pass {
continue
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/kor/pvc.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func processNamespacePvcs(clientset kubernetes.Interface, namespace string, filt
var unusedPvcNames []string
pvcNames := make([]string, 0, len(pvcs.Items))
for _, pvc := range pvcs.Items {
if pass := filters.KorLabelFilter(&pvc, &filters.Options{}); pass {
if pass, _ := filter.SetObject(&pvc).Run(filterOpts); pass {
continue
}

Expand Down
6 changes: 6 additions & 0 deletions pkg/kor/replicaset.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ func processNamespaceReplicaSets(clientset kubernetes.Interface, namespace strin
continue
}

if replicaSet.Labels["kor/used"] == "false" {
reason := "Marked with unused label"
unusedReplicaSetNames = append(unusedReplicaSetNames, ResourceInfo{Name: replicaSet.Name, Reason: reason})
continue
}

// if the replicaSet is specified 0 replica and current available & ready & fullyLabeled replica count is all 0, think the replicaSet is completed
if *replicaSet.Spec.Replicas == 0 && replicaSet.Status.AvailableReplicas == 0 && replicaSet.Status.ReadyReplicas == 0 && replicaSet.Status.FullyLabeledReplicas == 0 {
reason := "ReplicaSet is not in use"
Expand Down
6 changes: 6 additions & 0 deletions pkg/kor/rolebindings.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ func processNamespaceRoleBindings(clientset kubernetes.Interface, namespace stri
continue
}

if rb.Labels["kor/used"] == "false" {
reason := "Marked with unused label"
unusedRoleBindingNames = append(unusedRoleBindingNames, ResourceInfo{Name: rb.Name, Reason: reason})
continue
}

if exceptionFound, err := isResourceException(rb.Name, rb.Namespace, config.ExceptionRoleBindings); err != nil {
return nil, err
} else if exceptionFound {
Expand Down
3 changes: 2 additions & 1 deletion pkg/kor/roles.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,10 @@ func retrieveRoleNames(clientset kubernetes.Interface, namespace string, filterO
var unusedRoleNames []string
names := make([]string, 0, len(roles.Items))
for _, role := range roles.Items {
if pass := filters.KorLabelFilter(&role, &filters.Options{}); pass {
if pass, _ := filter.SetObject(&role).Run(filterOpts); pass {
continue
}

if role.Labels["kor/used"] == "false" {
unusedRoleNames = append(unusedRoleNames, role.Name)
continue
Expand Down
2 changes: 1 addition & 1 deletion pkg/kor/storageclasses.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func processStorageClasses(clientset kubernetes.Interface, filterOpts *filters.O
storageClassNames := make([]string, 0, len(scs.Items))

for _, sc := range scs.Items {
if pass := filters.KorLabelFilter(&sc, &filters.Options{}); pass {
if pass, _ := filter.SetObject(&sc).Run(filterOpts); pass {
continue
}

Expand Down

0 comments on commit b6614f3

Please sign in to comment.