Skip to content

Commit

Permalink
fix: delete pending pod when workflow terminated
Browse files Browse the repository at this point in the history
Co-authored-by: shuangkun <[email protected]>
Co-authored-by: zjgemi <[email protected]>
Signed-off-by: shuangkun <[email protected]>
  • Loading branch information
shuangkun and zjgemi committed Dec 4, 2023
1 parent feaed7a commit 753a0bf
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
5 changes: 4 additions & 1 deletion workflow/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,10 @@ func (wfc *WorkflowController) processNextPodCleanupItem(ctx context.Context) bo
pods := wfc.kubeclientset.CoreV1().Pods(namespace)
switch action {
case terminateContainers:
if terminationGracePeriod, err := wfc.signalContainers(namespace, podName, syscall.SIGTERM); err != nil {
pod, err := wfc.getPod(namespace, podName)
if err == nil && pod != nil && pod.Status.Phase == "Pending" {
wfc.queuePodForCleanup(namespace, podName, deletePod)
} else if terminationGracePeriod, err := wfc.signalContainers(namespace, podName, syscall.SIGTERM); err != nil {
return err
} else if terminationGracePeriod > 0 {
wfc.queuePodForCleanupAfter(namespace, podName, killContainers, terminationGracePeriod)
Expand Down
34 changes: 34 additions & 0 deletions workflow/controller/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1069,3 +1069,37 @@ spec:
podCleanupKey := "test/my-wf/labelPodCompleted"
assert.Equal(t, 0, controller.podCleanupQueue.NumRequeues(podCleanupKey))
}

func TestPodCleanupDeletePendingPodWhenTerminate(t *testing.T) {
wf := wfv1.MustUnmarshalWorkflow(`
metadata:
name: my-wf
namespace: test
spec:
entrypoint: main
templates:
- name: main
container:
image: my-image
`)
cancel, controller := newController(wf)
defer cancel()

ctx := context.Background()
assert.True(t, controller.processNextItem(ctx))

woc := newWorkflowOperationCtx(wf, controller)
woc.operate(ctx)
assert.Equal(t, wfv1.WorkflowRunning, woc.wf.Status.Phase)
makePodsPhase(ctx, woc, apiv1.PodPending)
woc.execWf.Spec.Shutdown = wfv1.ShutdownStrategyTerminate
woc.operate(ctx)
assert.True(t, controller.processNextPodCleanupItem(ctx))
assert.True(t, controller.processNextPodCleanupItem(ctx))
assert.True(t, controller.processNextPodCleanupItem(ctx))
assert.True(t, controller.processNextPodCleanupItem(ctx))
assert.Equal(t, wfv1.WorkflowFailed, woc.wf.Status.Phase)
pods, err := listPods(woc)
assert.NoError(t, err)
assert.Len(t, pods.Items, 0)
}

0 comments on commit 753a0bf

Please sign in to comment.