Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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
16 changes: 16 additions & 0 deletions pkg/podexec/pod_command_executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"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,6 +254,15 @@
Name: "foo",
},
},
InitContainers: []corev1api.Container{
{
Name: "baz",
},
{
Name: "qux",
RestartPolicy: ptr.To(corev1api.ContainerRestartPolicyAlways),
},
},
},
}

Expand All @@ -260,7 +270,13 @@
require.EqualError(t, err, `no such container: "bar"`)

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

Check failure on line 273 in pkg/podexec/pod_command_executor_test.go

View workflow job for this annotation

GitHub Actions / Run Linter Check

require-error: for error assertions use require (testifylint)

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

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

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