Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: add failed jobs as unused #283

Merged
merged 13 commits into from
May 23, 2024
Merged
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ kor [subcommand] --help
| CRDs | CRDs not used the cluster | |
| Pvs | PVs not bound to a PVC | |
| Pdbs | PDBs not used in Deployments<br/> PDBs not used in StatefulSets | |
| Jobs | Jobs status is completed | |
| Jobs | Jobs status is completed<br/> Jobs failed with no retries left | |
| ReplicaSets | replicaSets that specify replicas to 0 and has already completed it's work |
| DaemonSets | DaemonSets not scheduled on any nodes |
| StorageClasses | StorageClasses not used by any PVs/PVCs |
Expand Down
10 changes: 10 additions & 0 deletions pkg/kor/jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"os"

batchv1 "k8s.io/api/batch/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"

Expand Down Expand Up @@ -42,6 +43,15 @@ func processNamespaceJobs(clientset kubernetes.Interface, namespace string, filt
// if the job has completionTime and succeeded count greater than zero, think the job is completed
if job.Status.CompletionTime != nil && job.Status.Succeeded > 0 {
unusedJobNames = append(unusedJobNames, job.Name)
continue
} else {
// Check if the job has a condition indicating it has exceeded the backoff limit
for _, condition := range job.Status.Conditions {
if condition.Type == batchv1.JobFailed && condition.Reason == "BackoffLimitExceeded" {
unusedJobNames = append(unusedJobNames, job.Name)
break
}
}
}
}

Expand Down
32 changes: 27 additions & 5 deletions pkg/kor/jobs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,23 +71,44 @@ func createTestJobs(t *testing.T) *fake.Clientset {
t.Fatalf("Error creating fake job: %v", err)
}

job5 := CreateTestJob(testNamespace, "test-job5", &batchv1.JobStatus{
Succeeded: 0,
Failed: 1,
Conditions: []batchv1.JobCondition{
{
Type: batchv1.JobFailed,
Status: corev1.ConditionTrue,
Reason: "BackoffLimitExceeded",
Message: "Job has reached the specified backoff limit",
},
},
}, AppLabels)

_, err = clientset.BatchV1().Jobs(testNamespace).Create(context.TODO(), job5, v1.CreateOptions{})
if err != nil {
t.Fatalf("Error creating fake job: %v", err)
}

return clientset
}

func TestProcessNamespaceJobs(t *testing.T) {
clientset := createTestJobs(t)

completedJobs, err := processNamespaceJobs(clientset, testNamespace, &filters.Options{})
unusedJobs, err := processNamespaceJobs(clientset, testNamespace, &filters.Options{})
if err != nil {
t.Errorf("Expected no error, got %v", err)
}

if len(completedJobs) != 2 {
t.Errorf("Expected 2 job been completed, got %d", len(completedJobs))
if len(unusedJobs) != 3 {
t.Errorf("Expected 3 job been completed, got %d", len(unusedJobs))
yonahd marked this conversation as resolved.
Show resolved Hide resolved
}

if completedJobs[0] != "test-job2" && completedJobs[1] != "test-job4" {
t.Errorf("job2', got %s", completedJobs[0])
expectedJobs := []string{"test-job2", "test-job4", "test-job5"}
for _, jobName := range expectedJobs {
if !contains(unusedJobs, jobName) {
t.Errorf("Expected job %s to be considered unused, but it was not found", jobName)
}
}
}

Expand All @@ -113,6 +134,7 @@ func TestGetUnusedJobsStructured(t *testing.T) {
"Job": {
"test-job2",
"test-job4",
"test-job5",
},
},
}
Expand Down
Loading