Skip to content

Commit

Permalink
Merge pull request #8574 from ywk253100/241223_restore_helper
Browse files Browse the repository at this point in the history
Merge restore helper image into Velero server image
  • Loading branch information
reasonerjt authored Jan 6, 2025
2 parents 3120e33 + eb5230e commit 3eaa739
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 35 deletions.
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ RUN mkdir -p /output/usr/bin && \
export GOARM=$( echo "${GOARM}" | cut -c2-) && \
go build -o /output/${BIN} \
-ldflags "${LDFLAGS}" ${PKG}/cmd/${BIN} && \
go build -o /output/velero-restore-helper \
-ldflags "${LDFLAGS}" ${PKG}/cmd/velero-restore-helper && \
go build -o /output/velero-helper \
-ldflags "${LDFLAGS}" ${PKG}/cmd/velero-helper && \
go clean -modcache -cache
Expand Down
3 changes: 0 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -148,17 +148,14 @@ GOBIN=$$(pwd)/.go/bin
# If you want to build all containers, see the 'all-containers' rule.
all:
@$(MAKE) build
@$(MAKE) build BIN=velero-restore-helper

build-%:
@$(MAKE) --no-print-directory ARCH=$* build
@$(MAKE) --no-print-directory ARCH=$* build BIN=velero-restore-helper

all-build: $(addprefix build-, $(CLI_PLATFORMS))

all-containers:
@$(MAKE) --no-print-directory container
@$(MAKE) --no-print-directory container BIN=velero-restore-helper

local: build-dirs
# Add DEBUG=1 to enable debug locally
Expand Down
1 change: 1 addition & 0 deletions changelogs/unreleased/8574-ywk253100
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Merge restore helper image into Velero server image
6 changes: 0 additions & 6 deletions internal/velero/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,3 @@ func ImageTag() string {
func DefaultVeleroImage() string {
return fmt.Sprintf("%s/%s:%s", imageRegistry(), "velero", ImageTag())
}

// DefaultRestoreHelperImage returns the default container image to use for the restore helper
// for this version of Velero.
func DefaultRestoreHelperImage() string {
return fmt.Sprintf("%s/%s:%s", imageRegistry(), "velero-restore-helper", ImageTag())
}
4 changes: 0 additions & 4 deletions internal/velero/images_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,3 @@ func testDefaultImage(t *testing.T, defaultImageFn func() string, imageName stri
func TestDefaultVeleroImage(t *testing.T) {
testDefaultImage(t, DefaultVeleroImage, "velero")
}

func TestDefaultRestoreHelperImage(t *testing.T) {
testDefaultImage(t, DefaultRestoreHelperImage, "velero-restore-helper")
}
2 changes: 1 addition & 1 deletion pkg/cmd/server/plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ func newPodVolumeRestoreItemAction(f client.Factory) plugincommon.HandlerInitial
return nil, err
}

return ria.NewPodVolumeRestoreAction(logger, client.CoreV1().ConfigMaps(f.Namespace()), crClient), nil
return ria.NewPodVolumeRestoreAction(logger, client.CoreV1().ConfigMaps(f.Namespace()), crClient, f.Namespace())
}
}

Expand Down
35 changes: 22 additions & 13 deletions pkg/restore/actions/pod_volume_restore_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ import (

"github.com/pkg/errors"
"github.com/sirupsen/logrus"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
corev1client "k8s.io/client-go/kubernetes/typed/core/v1"
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"

Expand All @@ -40,6 +42,7 @@ import (
"github.com/vmware-tanzu/velero/pkg/podvolume"
"github.com/vmware-tanzu/velero/pkg/restorehelper"
"github.com/vmware-tanzu/velero/pkg/util/kube"
veleroutil "github.com/vmware-tanzu/velero/pkg/util/velero"
)

const (
Expand All @@ -50,17 +53,24 @@ const (
)

type PodVolumeRestoreAction struct {
logger logrus.FieldLogger
client corev1client.ConfigMapInterface
crClient ctrlclient.Client
logger logrus.FieldLogger
client corev1client.ConfigMapInterface
crClient ctrlclient.Client
veleroImage string
}

func NewPodVolumeRestoreAction(logger logrus.FieldLogger, client corev1client.ConfigMapInterface, crClient ctrlclient.Client) *PodVolumeRestoreAction {
return &PodVolumeRestoreAction{
logger: logger,
client: client,
crClient: crClient,
func NewPodVolumeRestoreAction(logger logrus.FieldLogger, client corev1client.ConfigMapInterface, crClient ctrlclient.Client, namespace string) (*PodVolumeRestoreAction, error) {
deployment := &appsv1.Deployment{}
if err := crClient.Get(context.TODO(), types.NamespacedName{Name: "velero", Namespace: namespace}, deployment); err != nil {
return nil, err
}
image := veleroutil.GetVeleroServerImage(deployment)
return &PodVolumeRestoreAction{
logger: logger,
client: client,
crClient: crClient,
veleroImage: image,
}, nil
}

func (a *PodVolumeRestoreAction) AppliesTo() (velero.ResourceSelector, error) {
Expand Down Expand Up @@ -117,7 +127,7 @@ func (a *PodVolumeRestoreAction) Execute(input *velero.RestoreItemActionExecuteI
return nil, err
}

image := getImage(log, config)
image := getImage(log, config, a.veleroImage)
log.Infof("Using image %q", image)

cpuRequest, memRequest := getResourceRequests(log, config)
Expand Down Expand Up @@ -200,24 +210,23 @@ func getCommand(log logrus.FieldLogger, config *corev1.ConfigMap) []string {
return []string{config.Data["command"]}
}

func getImage(log logrus.FieldLogger, config *corev1.ConfigMap) string {
func getImage(log logrus.FieldLogger, config *corev1.ConfigMap, defaultImage string) string {
if config == nil {
log.Debug("No config found for plugin")
return veleroimage.DefaultRestoreHelperImage()
return defaultImage
}

image := config.Data["image"]
if image == "" {
log.Debugf("No custom image configured")
return veleroimage.DefaultRestoreHelperImage()
return defaultImage
}

log = log.WithField("image", image)

parts := strings.Split(image, "/")

if len(parts) == 1 {
defaultImage := veleroimage.DefaultRestoreHelperImage()
// Image supplied without registry part
log.Infof("Plugin config contains image name without registry name. Using default init container image: %q", defaultImage)
return defaultImage
Expand Down
39 changes: 33 additions & 6 deletions pkg/restore/actions/pod_volume_restore_action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@ import (
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
appsv1 "k8s.io/api/apps/v1"
corev1api "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes/fake"

veleroimage "github.com/vmware-tanzu/velero/internal/velero"
velerov1api "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
"github.com/vmware-tanzu/velero/pkg/builder"
"github.com/vmware-tanzu/velero/pkg/buildinfo"
Expand All @@ -48,7 +49,7 @@ func TestGetImage(t *testing.T) {
}
}

defaultImage := veleroimage.DefaultRestoreHelperImage()
defaultImage := "velero/velero:v1.0"

tests := []struct {
name string
Expand Down Expand Up @@ -104,7 +105,7 @@ func TestGetImage(t *testing.T) {
buildinfo.Version = originalVersion
}()
}
assert.Equal(t, test.want, getImage(velerotest.NewLogger(), test.configMap))
assert.Equal(t, test.want, getImage(velerotest.NewLogger(), test.configMap, defaultImage))
})
}
}
Expand Down Expand Up @@ -134,7 +135,7 @@ func TestPodVolumeRestoreActionExecute(t *testing.T) {
veleroNs = "velero"
)

defaultRestoreHelperImage := veleroimage.DefaultRestoreHelperImage()
defaultRestoreHelperImage := "velero/velero:v1.0"

tests := []struct {
name string
Expand Down Expand Up @@ -265,10 +266,34 @@ func TestPodVolumeRestoreActionExecute(t *testing.T) {
},
}

veleroDeployment := &appsv1.Deployment{
TypeMeta: metav1.TypeMeta{
APIVersion: appsv1.SchemeGroupVersion.String(),
Kind: "Deployment",
},
ObjectMeta: metav1.ObjectMeta{
Namespace: "velero",
Name: "velero",
},
Spec: appsv1.DeploymentSpec{
Template: corev1api.PodTemplateSpec{
Spec: corev1api.PodSpec{
Containers: []corev1api.Container{
{
Image: "velero/velero:v1.0",
},
},
},
},
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
clientset := fake.NewSimpleClientset()
crClient := velerotest.NewFakeControllerRuntimeClient(t, tc.podVolumeBackups...)

objects := []runtime.Object{veleroDeployment}
objects = append(objects, tc.podVolumeBackups...)
crClient := velerotest.NewFakeControllerRuntimeClient(t, objects...)

unstructuredPod, err := runtime.DefaultUnstructuredConverter.ToUnstructured(tc.pod)
require.NoError(t, err)
Expand All @@ -295,11 +320,13 @@ func TestPodVolumeRestoreActionExecute(t *testing.T) {
Result(),
}

a := NewPodVolumeRestoreAction(
a, err := NewPodVolumeRestoreAction(
logrus.StandardLogger(),
clientset.CoreV1().ConfigMaps(veleroNs),
crClient,
"velero",
)
require.NoError(t, err)

// method under test
res, err := a.Execute(input)
Expand Down
3 changes: 1 addition & 2 deletions site/content/docs/main/file-system-backup.md
Original file line number Diff line number Diff line change
Expand Up @@ -356,8 +356,7 @@ with an infinite sleep) to mount these PVC/PV pairs prior taking a Velero backup

## Customize Restore Helper Container

Velero uses a helper init container when performing a FSB restore. By default, the image for this container is
`velero/velero-restore-helper:<VERSION>`, where `VERSION` matches the version/tag of the main Velero image.
Velero uses a helper init container when performing a FSB restore. By default, the image for this container is same with the Velero server container.
You can customize the image that is used for this helper by creating a ConfigMap in the Velero namespace with the alternate image.

In addition, you can customize the resource requirements for the init container, should you need.
Expand Down

0 comments on commit 3eaa739

Please sign in to comment.