Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion changelogs/unreleased/5211-cleverhu

This file was deleted.

1 change: 1 addition & 0 deletions changelogs/unreleased/6811-haslersn
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Do not run preHook/postHook on Succeeded/Pending/Failed pods and log error for Pending/Failed/Unknown pods with hooks.
11 changes: 9 additions & 2 deletions pkg/podexec/pod_command_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,15 @@ func (e *defaultPodCommandExecutor) ExecutePodCommand(log logrus.FieldLogger, it
},
)

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)
if pod.Status.Phase == corev1api.PodUnknown {
hookLog.Error("Pod phase is Unknown, trying to run hook anyway")
} else if pod.Status.Phase == corev1api.PodPending || pod.Status.Phase == corev1api.PodFailed {
// Taking a backup of a Pending or Failed Pod that has a hook defined is probably something
// which the user didn't intend. We return an error, so this doesn't happen silently.
// Possible improvement in the future: When Pod is Pending, defer the backup a bit.
return errors.Errorf("Pod phase is %s", pod.Status.Phase)
} else if pod.Status.Phase == corev1api.PodSucceeded {
hookLog.Info("Skipping hook because Pod phase is Succeeded")
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/podexec/pod_command_executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ func TestEnsureContainerExists(t *testing.T) {
assert.NoError(t, err)
}

func TestPodCompeted(t *testing.T) {
func TestPodCompleted(t *testing.T) {
pod := &corev1api.Pod{
Spec: corev1api.PodSpec{
Containers: []corev1api.Container{
Expand Down
4 changes: 3 additions & 1 deletion site/content/docs/main/backup-hooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ Velero supports executing commands in containers in pods during a backup.
## Backup Hooks

When performing a backup, you can specify one or more commands to execute in a container in a pod
when that pod is being backed up. The commands can be configured to run *before* any custom action
when that pod is being backed up.
Such hooks do _not_ run when the pod is Pending, Failed or Succeeded.
The commands can be configured to run *before* any custom action
processing ("pre" hooks), or after all custom actions have been completed and any additional items
specified by custom action have been backed up ("post" hooks). Note that hooks are _not_ executed within a shell
on the containers.
Expand Down