diff --git a/changelogs/unreleased/5211-cleverhu b/changelogs/unreleased/5211-cleverhu new file mode 100644 index 0000000000..1ed39117cf --- /dev/null +++ b/changelogs/unreleased/5211-cleverhu @@ -0,0 +1 @@ +fix run preHook and postHook on completed pods \ No newline at end of file diff --git a/pkg/podexec/pod_command_executor.go b/pkg/podexec/pod_command_executor.go index 569fca4e58..b268d77a39 100644 --- a/pkg/podexec/pod_command_executor.go +++ b/pkg/podexec/pod_command_executor.go @@ -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(). diff --git a/pkg/podexec/pod_command_executor_test.go b/pkg/podexec/pod_command_executor_test.go index 3f6937192e..bdd960a592 100644 --- a/pkg/podexec/pod_command_executor_test.go +++ b/pkg/podexec/pod_command_executor_test.go @@ -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 }