Cleanups in controller utils

1. Squash two identical sorters byTime
2. Move helper for searching active jobs into utils to exist next to its
  counterpart
This commit is contained in:
Maciej Szulik
2022-06-28 16:59:38 +02:00
parent 46f3821bf4
commit cb491a8d0f
2 changed files with 19 additions and 37 deletions

View File

@@ -33,7 +33,8 @@ import (
// Utilities for dealing with Jobs and CronJobs and time.
func inActiveList(cj batchv1.CronJob, uid types.UID) bool {
// inActiveList checks if cronjob's .status.active has a job with the same UID.
func inActiveList(cj *batchv1.CronJob, uid types.UID) bool {
for _, j := range cj.Status.Active {
if j.UID == uid {
return true
@@ -42,6 +43,17 @@ func inActiveList(cj batchv1.CronJob, uid types.UID) bool {
return false
}
// inActiveListByName checks if cronjob's status.active has a job with the same
// name and namespace.
func inActiveListByName(cj *batchv1.CronJob, job *batchv1.Job) bool {
for _, j := range cj.Status.Active {
if j.Name == job.Name && j.Namespace == job.Namespace {
return true
}
}
return false
}
func deleteFromActiveList(cj *batchv1.CronJob, uid types.UID) {
if cj == nil {
return
@@ -200,7 +212,7 @@ func IsJobFinished(j *batchv1.Job) bool {
}
// byJobStartTime sorts a list of jobs by start timestamp, using their names as a tie breaker.
type byJobStartTime []batchv1.Job
type byJobStartTime []*batchv1.Job
func (o byJobStartTime) Len() int { return len(o) }
func (o byJobStartTime) Swap(i, j int) { o[i], o[j] = o[j], o[i] }
@@ -217,22 +229,3 @@ func (o byJobStartTime) Less(i, j int) bool {
}
return o[i].Status.StartTime.Before(o[j].Status.StartTime)
}
// byJobStartTimeStar sorts a list of jobs by start timestamp, using their names as a tie breaker.
type byJobStartTimeStar []*batchv1.Job
func (o byJobStartTimeStar) Len() int { return len(o) }
func (o byJobStartTimeStar) Swap(i, j int) { o[i], o[j] = o[j], o[i] }
func (o byJobStartTimeStar) Less(i, j int) bool {
if o[i].Status.StartTime == nil && o[j].Status.StartTime != nil {
return false
}
if o[i].Status.StartTime != nil && o[j].Status.StartTime == nil {
return true
}
if o[i].Status.StartTime.Equal(o[j].Status.StartTime) {
return o[i].Name < o[j].Name
}
return o[i].Status.StartTime.Before(o[j].Status.StartTime)
}