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: 1 addition & 0 deletions changelogs/unreleased/9403-GabriFedi97
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Include InitContainer configured as Sidecars when validating the existence of the target containers configured for the Backup Hooks
21 changes: 17 additions & 4 deletions pkg/podexec/pod_command_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"bytes"
"context"
"net/url"
"slices"
"time"

"github.com/pkg/errors"
Expand Down Expand Up @@ -184,10 +185,22 @@ func (e *defaultPodCommandExecutor) ExecutePodCommand(log logrus.FieldLogger, it
}

func ensureContainerExists(pod *corev1api.Pod, container string) error {
for _, c := range pod.Spec.Containers {
if c.Name == container {
return nil
}
existsAsMainContainer := slices.ContainsFunc(pod.Spec.Containers, func(c corev1api.Container) bool {
return c.Name == container
})

if existsAsMainContainer {
return nil
}

existsAsSidecar := slices.ContainsFunc(pod.Spec.InitContainers, func(c corev1api.Container) bool {
return c.RestartPolicy != nil &&
*c.RestartPolicy == corev1api.ContainerRestartPolicyAlways &&
c.Name == container
})

if existsAsSidecar {
return nil
}

return errors.Errorf("no such container: %q", container)
Expand Down
18 changes: 17 additions & 1 deletion pkg/podexec/pod_command_executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/remotecommand"
"k8s.io/utils/ptr"

v1 "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
velerotest "github.com/vmware-tanzu/velero/pkg/test"
Expand Down Expand Up @@ -253,14 +254,29 @@ func TestEnsureContainerExists(t *testing.T) {
Name: "foo",
},
},
InitContainers: []corev1api.Container{
{
Name: "baz",
},
{
Name: "qux",
RestartPolicy: ptr.To(corev1api.ContainerRestartPolicyAlways),
},
},
},
}

err := ensureContainerExists(pod, "bar")
require.EqualError(t, err, `no such container: "bar"`)

err = ensureContainerExists(pod, "foo")
assert.NoError(t, err)
require.NoError(t, err)

err = ensureContainerExists(pod, "baz")
require.EqualError(t, err, `no such container: "baz"`)

err = ensureContainerExists(pod, "qux")
require.NoError(t, err)
}

func TestPodCompeted(t *testing.T) {
Expand Down
Loading