From 23345c56c694abf56bb2fbea3f5f4de7ff2eb309 Mon Sep 17 00:00:00 2001 From: Lukasz Raczylo Date: Fri, 10 Jan 2025 12:19:37 +0000 Subject: [PATCH] fixup! fixup! fixup! General improvements --- .../clusterimageexport_controller.go | 8 ++++++- internal/shared/jobs.go | 23 +++++++++++-------- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/internal/controller/raczylo.com/clusterimageexport_controller.go b/internal/controller/raczylo.com/clusterimageexport_controller.go index 14b4200..d5e1ba5 100644 --- a/internal/controller/raczylo.com/clusterimageexport_controller.go +++ b/internal/controller/raczylo.com/clusterimageexport_controller.go @@ -316,6 +316,10 @@ func (r *ClusterImageExportReconciler) runCleanupJob(ctx context.Context, cluste defaultCommands = append(defaultCommands, additionalCommands...) } + // Set up the cleanup job with retry limits and TTL + backoffLimit := int32(2) // 3 total attempts (initial + 2 retries) + ttlSecondsAfterFinished := int32(30) // Delete job 30 seconds after completion + jobParams := shared.JobParams{ Name: normalisedImageName, Namespace: clusterImageExport.Namespace, @@ -324,6 +328,8 @@ func (r *ClusterImageExportReconciler) runCleanupJob(ctx context.Context, cluste Annotations: clusterImageExport.Spec.JobAnnotations, ServiceAccount: "", ImagePullSecrets: clusterImageExport.Spec.ImagePullSecrets, + BackoffLimit: &backoffLimit, + TTLSecondsAfterFinished: &ttlSecondsAfterFinished, } cleanupJob := shared.CreateJob(jobParams, func(raczylocomv1.ClusterImageExport) []string { return nil }) @@ -334,5 +340,5 @@ func (r *ClusterImageExportReconciler) runCleanupJob(ctx context.Context, cluste return } - l.Info("Created cleanup job") + l.Info("Created cleanup job with retry limit and TTL") } diff --git a/internal/shared/jobs.go b/internal/shared/jobs.go index 58862ee..b209308 100644 --- a/internal/shared/jobs.go +++ b/internal/shared/jobs.go @@ -13,15 +13,17 @@ import ( ) type JobParams struct { - Name string - Namespace string - Annotations map[string]string - Image string - Commands []string - EnvVars []corev1.EnvVar - OwnerReferences []metav1.OwnerReference - ServiceAccount string // Can be empty to use controller's service account - ImagePullSecrets []corev1.LocalObjectReference + Name string + Namespace string + Annotations map[string]string + Image string + Commands []string + EnvVars []corev1.EnvVar + OwnerReferences []metav1.OwnerReference + ServiceAccount string // Can be empty to use controller's service account + ImagePullSecrets []corev1.LocalObjectReference + BackoffLimit *int32 // Optional: number of retries before marking the job as failed + TTLSecondsAfterFinished *int32 // Optional: seconds after which completed/failed job is automatically deleted } func CreateJob[T any](params JobParams, setupFunc func(T) []string) *batchv1.Job { @@ -62,7 +64,8 @@ func CreateJob[T any](params JobParams, setupFunc func(T) []string) *batchv1.Job Annotations: params.Annotations, }, Spec: batchv1.JobSpec{ - TTLSecondsAfterFinished: pointer.Int32(300), + BackoffLimit: params.BackoffLimit, + TTLSecondsAfterFinished: params.TTLSecondsAfterFinished, Template: corev1.PodTemplateSpec{ ObjectMeta: metav1.ObjectMeta{ Labels: map[string]string{