Skip to content

Commit

Permalink
check pod status before hook (vmware-tanzu#5211)
Browse files Browse the repository at this point in the history
Signed-off-by: cleverhu <[email protected]>
Co-authored-by: cleverhu <[email protected]>
  • Loading branch information
cleverhu and cleverhu authored Sep 13, 2023
1 parent 402703f commit 9b1cffc
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
1 change: 1 addition & 0 deletions changelogs/unreleased/5211-cleverhu
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fix run preHook and postHook on completed pods
6 changes: 6 additions & 0 deletions pkg/podexec/pod_command_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,12 @@ func (e *defaultPodCommandExecutor) ExecutePodCommand(log logrus.FieldLogger, it
"hookTimeout": localHook.Timeout,
},
)

if pod.Status.Phase == corev1api.PodSucceeded || pod.Status.Phase == corev1api.PodFailed {
hookLog.Infof("Pod entered phase %s before some post-backup exec hooks ran", pod.Status.Phase)
return nil
}

hookLog.Info("running exec hook")

req := e.restClient.Post().
Expand Down
31 changes: 31 additions & 0 deletions pkg/podexec/pod_command_executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,37 @@ func TestEnsureContainerExists(t *testing.T) {
assert.NoError(t, err)
}

func TestPodCompeted(t *testing.T) {
pod := &corev1api.Pod{
Spec: corev1api.PodSpec{
Containers: []corev1api.Container{
{
Name: "foo",
},
},
},
Status: corev1api.PodStatus{
Phase: corev1api.PodSucceeded,
},
}

obj, err := runtime.DefaultUnstructuredConverter.ToUnstructured(pod)
require.NoError(t, err)

clientConfig := &rest.Config{}
poster := &mockPoster{}
defer poster.AssertExpectations(t)
podCommandExecutor := NewPodCommandExecutor(clientConfig, poster).(*defaultPodCommandExecutor)

hook := v1.ExecHook{
Container: "foo",
Command: []string{"some", "command"},
}

err = podCommandExecutor.ExecutePodCommand(velerotest.NewLogger(), obj, "namespace", "name", "hookName", &hook)
require.NoError(t, err)
}

type mockStreamExecutorFactory struct {
mock.Mock
}
Expand Down

0 comments on commit 9b1cffc

Please sign in to comment.