From d1d0cee7807836b87622c5a0d8f32a76b2783725 Mon Sep 17 00:00:00 2001 From: Rohan Kumar Date: Sat, 25 Jun 2022 19:50:55 +0530 Subject: [PATCH] feat: add finalizer to workflow pod to prevent "pod deleted" Signed-off-by: Rohan Kumar --- workflow/common/common.go | 2 ++ workflow/controller/controller.go | 15 +++++++++++++-- workflow/controller/workflowpod.go | 1 + 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/workflow/common/common.go b/workflow/common/common.go index 3af8521e87ac..97588d6c64c2 100644 --- a/workflow/common/common.go +++ b/workflow/common/common.go @@ -223,6 +223,8 @@ const ( ErrDeadlineExceeded = "DeadlineExceeded" ConfigMapName = "workflow-controller-configmap" + + WorkflowFinalizer = "argoproj.io/workflow-finalizer" ) // AnnotationKeyKillCmd specifies the command to use to kill to container, useful for injected sidecars diff --git a/workflow/controller/controller.go b/workflow/controller/controller.go index df063d6c400b..b9ad388d3ede 100644 --- a/workflow/controller/controller.go +++ b/workflow/controller/controller.go @@ -490,15 +490,26 @@ func (wfc *WorkflowController) processNextPodCleanupItem(ctx context.Context) bo ctx, podName, types.MergePatchType, - []byte(`{"metadata": {"labels": {"workflows.argoproj.io/completed": "true"}}}`), + []byte(`{"metadata": {"labels": {"workflows.argoproj.io/completed": "true"}, "finalizers": []}}`), metav1.PatchOptions{}, ) if err != nil { return err } case deletePod: + // remove the finalizer to allow the pod to be deleted + _, err := pods.Patch( + ctx, + podName, + types.MergePatchType, + []byte(`{"metadata": {"finalizers": []}}`), + metav1.PatchOptions{}, + ) + if err != nil { + return err + } propagation := metav1.DeletePropagationBackground - err := pods.Delete(ctx, podName, metav1.DeleteOptions{ + err = pods.Delete(ctx, podName, metav1.DeleteOptions{ PropagationPolicy: &propagation, GracePeriodSeconds: wfc.Config.PodGCGracePeriodSeconds, }) diff --git a/workflow/controller/workflowpod.go b/workflow/controller/workflowpod.go index f8e0bc0e4428..a987e943b860 100644 --- a/workflow/controller/workflowpod.go +++ b/workflow/controller/workflowpod.go @@ -172,6 +172,7 @@ func (woc *wfOperationCtx) createWorkflowPod(ctx context.Context, nodeName strin OwnerReferences: []metav1.OwnerReference{ *metav1.NewControllerRef(woc.wf, wfv1.SchemeGroupVersion.WithKind(workflow.WorkflowKind)), }, + Finalizers: []string{common.WorkflowFinalizer}, }, Spec: apiv1.PodSpec{ RestartPolicy: apiv1.RestartPolicyNever,