func()

in common/controllers/cronanything_controller.go [446:489]


func (r *ReconcileCronAnything) cleanupHistory(ca cronanything.CronAnything, childResources []*unstructured.Unstructured, resource schema.GroupVersionResource, now time.Time) []*unstructured.Unstructured {
	if ca.CronAnythingSpec().Retention == nil {
		return childResources
	}
	historyTimeLimit := ca.CronAnythingSpec().Retention.HistoryTimeLimitSeconds
	historyCountLimit := ca.CronAnythingSpec().Retention.HistoryCountLimit
	if historyTimeLimit == nil && historyCountLimit == nil {
		return childResources
	}

	sort.Sort(newByTimestamp(ca, childResources))

	keeperCount := int32(0)
	var toBeDeleted []*unstructured.Unstructured
	for _, child := range childResources {
		finished, err := isFinished(ca, child)
		if err != nil {
			r.Log.Error(err, "Error checking if resource is finished", "childName", child.GetName())
			continue
		}
		if !finished {
			continue
		}

		timestamp, err := getResourceTimestamp(ca, child)
		if err != nil {
			r.Log.Error(err, "Error looking up finish time on resource", "childName", child.GetName())
			continue
		}

		if historyTimeLimit != nil && timestamp.Before(now) && now.Sub(timestamp).Seconds() > float64(*historyTimeLimit) {
			toBeDeleted = append(toBeDeleted, child)
			continue
		}

		if historyCountLimit != nil && keeperCount >= *historyCountLimit {
			toBeDeleted = append(toBeDeleted, child)
			continue
		}
		keeperCount++
	}

	return r.deleteInactiveResources(ca, toBeDeleted, childResources, resource)
}