From 81de3419719052e2f0efc58655da88dc7dac96f1 Mon Sep 17 00:00:00 2001 From: Haim Kortovich Date: Thu, 14 Nov 2024 17:29:34 -0500 Subject: [PATCH] delete job if not equal and create new [ZITADOPER-7] --- .../controller/zitadelcluster_controller.go | 13 +++++++++---- src/pkg/builder/job_builder.go | 6 ++++-- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/internal/controller/zitadelcluster_controller.go b/src/internal/controller/zitadelcluster_controller.go index af5d463..eba8732 100644 --- a/src/internal/controller/zitadelcluster_controller.go +++ b/src/internal/controller/zitadelcluster_controller.go @@ -23,6 +23,7 @@ import ( "encoding/json" "encoding/pem" "fmt" + "reflect" "strings" "time" @@ -291,10 +292,14 @@ func (r *ZitadelClusterReconciler) reconcileInitJob(ctx context.Context, zitadel } return ctrl.Result{}, nil } - - patch := client.MergeFrom(existingJob.DeepCopy()) - existingJob.Spec.Template.Spec = desiredInitJob.Spec.Template.Spec - return ctrl.Result{}, r.Patch(ctx, &existingJob, patch) + if !reflect.DeepEqual(existingJob.Spec.Template.Spec, desiredInitJob.Spec.Template.Spec) { + err := r.Delete(ctx, &existingJob) + if err != nil { + return ctrl.Result{}, err + } + return ctrl.Result{}, r.Create(ctx, desiredInitJob) + } + return ctrl.Result{}, nil } func (r *ZitadelClusterReconciler) reconcileSetupJob(ctx context.Context, zitadel *zitadelv1alpha1.ZitadelCluster) (ctrl.Result, error) { diff --git a/src/pkg/builder/job_builder.go b/src/pkg/builder/job_builder.go index d168970..522a0c4 100644 --- a/src/pkg/builder/job_builder.go +++ b/src/pkg/builder/job_builder.go @@ -16,6 +16,7 @@ import ( func (b *Builder) BuildInitJob(zitadel *zitadelv1alpha1.ZitadelCluster, key types.NamespacedName) (*batchv1.Job, error) { backOffLimit := int32(5) + ttlAfterFinish := int32(100) activeDeadlineSeconds := int64(300) runAsNonRoot := true enableServiceLinks := false @@ -27,8 +28,9 @@ func (b *Builder) BuildInitJob(zitadel *zitadelv1alpha1.ZitadelCluster, key type Namespace: key.Namespace, }, Spec: batchv1.JobSpec{ - BackoffLimit: &backOffLimit, - ActiveDeadlineSeconds: &activeDeadlineSeconds, + BackoffLimit: &backOffLimit, + ActiveDeadlineSeconds: &activeDeadlineSeconds, + TTLSecondsAfterFinished: &ttlAfterFinish, Template: corev1.PodTemplateSpec{ Spec: corev1.PodSpec{ RestartPolicy: corev1.RestartPolicyOnFailure,