From 1d882c650910f89a60db1d640497fa03e77a5c2e Mon Sep 17 00:00:00 2001 From: Bridget McErlean Date: Fri, 16 Jul 2021 11:20:47 -0400 Subject: [PATCH] Merge pull request #3928 from zubron/customize-velero-image-at-build-time Allow image registry to be configured at build time Signed-off-by: Bridget McErlean --- .goreleaser.yml | 2 +- Dockerfile | 3 +- Makefile | 6 +- hack/build.sh | 6 + hack/docker-push.sh | 3 + hack/release-tools/goreleaser.sh | 5 + internal/velero/images.go | 51 +++++++ internal/velero/images_test.go | 140 ++++++++++++++++++ pkg/buildinfo/{version.go => buildinfo.go} | 6 +- .../{version_test.go => buildinfo_test.go} | 0 pkg/cmd/cli/install/install.go | 5 +- pkg/install/daemonset.go | 6 +- pkg/install/deployment.go | 5 +- pkg/install/resources.go | 11 -- pkg/restore/restic_restore_action.go | 30 ++-- pkg/restore/restic_restore_action_test.go | 60 ++++---- 16 files changed, 273 insertions(+), 66 deletions(-) create mode 100644 internal/velero/images.go create mode 100644 internal/velero/images_test.go rename pkg/buildinfo/{version.go => buildinfo.go} (86%) rename pkg/buildinfo/{version_test.go => buildinfo_test.go} (100%) diff --git a/.goreleaser.yml b/.goreleaser.yml index 02ad04934..fb6ee34b8 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -41,7 +41,7 @@ builds: - goos: windows goarch: ppc64le ldflags: - - -X "github.com/vmware-tanzu/velero/pkg/buildinfo.Version={{ .Tag }}" -X "github.com/vmware-tanzu/velero/pkg/buildinfo.GitSHA={{ .FullCommit }}" -X "github.com/vmware-tanzu/velero/pkg/buildinfo.GitTreeState={{ .Env.GIT_TREE_STATE }}" + - -X "github.com/vmware-tanzu/velero/pkg/buildinfo.Version={{ .Tag }}" -X "github.com/vmware-tanzu/velero/pkg/buildinfo.GitSHA={{ .FullCommit }}" -X "github.com/vmware-tanzu/velero/pkg/buildinfo.GitTreeState={{ .Env.GIT_TREE_STATE }}" -X "github.com/vmware-tanzu/velero/pkg/buildinfo.ImageRegistry={{ .Env.REGISTRY }}" archives: - name_template: "{{ .ProjectName }}-{{ .Tag }}-{{ .Os }}-{{ .Arch }}" wrap_in_directory: true diff --git a/Dockerfile b/Dockerfile index ebbb68988..44c765a8d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -18,11 +18,12 @@ ARG PKG ARG VERSION ARG GIT_SHA ARG GIT_TREE_STATE +ARG REGISTRY ENV CGO_ENABLED=0 \ GO111MODULE=on \ GOPROXY=${GOPROXY} \ - LDFLAGS="-X ${PKG}/pkg/buildinfo.Version=${VERSION} -X ${PKG}/pkg/buildinfo.GitSHA=${GIT_SHA} -X ${PKG}/pkg/buildinfo.GitTreeState=${GIT_TREE_STATE}" + LDFLAGS="-X ${PKG}/pkg/buildinfo.Version=${VERSION} -X ${PKG}/pkg/buildinfo.GitSHA=${GIT_SHA} -X ${PKG}/pkg/buildinfo.GitTreeState=${GIT_TREE_STATE} -X ${PKG}/pkg/buildinfo.ImageRegistry=${REGISTRY}" WORKDIR /go/src/github.com/vmware-tanzu/velero diff --git a/Makefile b/Makefile index 59ac301a6..6708145aa 100644 --- a/Makefile +++ b/Makefile @@ -110,7 +110,6 @@ GOPROXY ?= https://proxy.golang.org # If you want to build all binaries, see the 'all-build' rule. # If you want to build all containers, see the 'all-containers' rule. -# If you want to build AND push all containers, see the 'all-push' rule. all: @$(MAKE) build @$(MAKE) build BIN=velero-restic-restore-helper @@ -129,6 +128,7 @@ local: build-dirs GOOS=$(GOOS) \ GOARCH=$(GOARCH) \ VERSION=$(VERSION) \ + REGISTRY=$(REGISTRY) \ PKG=$(PKG) \ BIN=$(BIN) \ GIT_SHA=$(GIT_SHA) \ @@ -144,6 +144,7 @@ _output/bin/$(GOOS)/$(GOARCH)/$(BIN): build-dirs GOOS=$(GOOS) \ GOARCH=$(GOARCH) \ VERSION=$(VERSION) \ + REGISTRY=$(REGISTRY) \ PKG=$(PKG) \ BIN=$(BIN) \ GIT_SHA=$(GIT_SHA) \ @@ -186,6 +187,7 @@ endif --build-arg=VERSION=$(VERSION) \ --build-arg=GIT_SHA=$(GIT_SHA) \ --build-arg=GIT_TREE_STATE=$(GIT_TREE_STATE) \ + --build-arg=REGISTRY=$(REGISTRY) \ -f $(VELERO_DOCKERFILE) . container: @@ -201,6 +203,7 @@ endif --build-arg=VERSION=$(VERSION) \ --build-arg=GIT_SHA=$(GIT_SHA) \ --build-arg=GIT_TREE_STATE=$(GIT_TREE_STATE) \ + --build-arg=REGISTRY=$(REGISTRY) \ --build-arg=RESTIC_VERSION=$(RESTIC_VERSION) \ -f $(VELERO_DOCKERFILE) . @echo "container: $(IMAGE):$(VERSION)" @@ -346,6 +349,7 @@ release: GITHUB_TOKEN=$(GITHUB_TOKEN) \ RELEASE_NOTES_FILE=$(RELEASE_NOTES_FILE) \ PUBLISH=$(PUBLISH) \ + REGISTRY=$(REGISTRY) \ ./hack/release-tools/goreleaser.sh'" serve-docs: build-image-hugo diff --git a/hack/build.sh b/hack/build.sh index d5492887c..47052e81e 100755 --- a/hack/build.sh +++ b/hack/build.sh @@ -41,6 +41,11 @@ if [[ -z "${VERSION}" ]]; then exit 1 fi +if [[ -z "${REGISTRY}" ]]; then + echo "REGISTRY must be set" + exit 1 +fi + if [[ -z "${GIT_SHA}" ]]; then echo "GIT_SHA must be set" exit 1 @@ -59,6 +64,7 @@ fi export CGO_ENABLED=0 LDFLAGS="-X ${PKG}/pkg/buildinfo.Version=${VERSION}" +LDFLAGS="${LDFLAGS} -X ${PKG}/pkg/buildinfo.ImageRegistry=${REGISTRY}" LDFLAGS="${LDFLAGS} -X ${PKG}/pkg/buildinfo.GitSHA=${GIT_SHA}" LDFLAGS="${LDFLAGS} -X ${PKG}/pkg/buildinfo.GitTreeState=${GIT_TREE_STATE}" diff --git a/hack/docker-push.sh b/hack/docker-push.sh index 5da0119fb..cf3a0c5f8 100755 --- a/hack/docker-push.sh +++ b/hack/docker-push.sh @@ -91,6 +91,9 @@ echo "BUILDX_PLATFORMS: $BUILDX_PLATFORMS" echo "Building and pushing container images." +# The use of "registry" as the buildx output type below instructs +# Docker to push the image + VERSION="$VERSION" \ TAG_LATEST="$TAG_LATEST" \ BUILDX_PLATFORMS="$BUILDX_PLATFORMS" \ diff --git a/hack/release-tools/goreleaser.sh b/hack/release-tools/goreleaser.sh index 4b55fffad..61eecde32 100755 --- a/hack/release-tools/goreleaser.sh +++ b/hack/release-tools/goreleaser.sh @@ -29,6 +29,11 @@ if [ -z "${RELEASE_NOTES_FILE}" ]; then exit 1 fi +if [ -z "${REGISTRY}" ]; then + echo "REGISTRY must be set" + exit 1 +fi + GIT_DIRTY=$(git status --porcelain 2> /dev/null) if [[ -z "${GIT_DIRTY}" ]]; then export GIT_TREE_STATE=clean diff --git a/internal/velero/images.go b/internal/velero/images.go new file mode 100644 index 000000000..c6319aadd --- /dev/null +++ b/internal/velero/images.go @@ -0,0 +1,51 @@ +/* +Copyright the Velero contributors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package velero + +import ( + "fmt" + + "github.com/vmware-tanzu/velero/pkg/buildinfo" +) + +// Use Dockerhub as the default registry if the build process didn't supply a registry +func imageRegistry() string { + if buildinfo.ImageRegistry == "" { + return "velero" + } + return buildinfo.ImageRegistry +} + +// ImageTag returns the image tag that should be used by Velero images. +// It uses the Version from the buildinfo or "latest" if the build process didn't supply a version. +func ImageTag() string { + if buildinfo.Version == "" { + return "latest" + } + return buildinfo.Version +} + +// DefaultVeleroImage returns the default container image to use for this version of Velero. +func DefaultVeleroImage() string { + return fmt.Sprintf("%s/%s:%s", imageRegistry(), "velero", ImageTag()) +} + +// DefaultResticRestoreHelperImage returns the default container image to use for the restic restore helper +// for this version of Velero. +func DefaultResticRestoreHelperImage() string { + return fmt.Sprintf("%s/%s:%s", imageRegistry(), "velero-restic-restore-helper", ImageTag()) +} diff --git a/internal/velero/images_test.go b/internal/velero/images_test.go new file mode 100644 index 000000000..d04c66b9d --- /dev/null +++ b/internal/velero/images_test.go @@ -0,0 +1,140 @@ +/* +Copyright the Velero contributors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package velero + +import ( + "fmt" + "testing" + + "github.com/stretchr/testify/assert" + + "github.com/vmware-tanzu/velero/pkg/buildinfo" +) + +func TestImageTag(t *testing.T) { + testCases := []struct { + name string + buildInfoVersion string + want string + }{ + { + name: "tag is latest when buildinfo.Version is empty", + want: "latest", + }, + { + name: "tag is buildinfo.Version when not empty", + buildInfoVersion: "custom-build-version", + want: "custom-build-version", + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + originalVersion := buildinfo.Version + buildinfo.Version = tc.buildInfoVersion + defer func() { + buildinfo.Version = originalVersion + }() + + assert.Equal(t, tc.want, ImageTag()) + }) + } +} + +func TestImageRegistry(t *testing.T) { + testCases := []struct { + name string + buildInfoRegistry string + want string + }{ + { + name: "registry is velero when buildinfo.ImageRegistry is empty", + want: "velero", + }, + { + name: "registry is buildinfo.ImageRegistry when not empty", + buildInfoRegistry: "custom-build-image-registry", + want: "custom-build-image-registry", + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + originalImageRegistry := buildinfo.ImageRegistry + buildinfo.ImageRegistry = tc.buildInfoRegistry + defer func() { + buildinfo.ImageRegistry = originalImageRegistry + }() + + assert.Equal(t, tc.want, imageRegistry()) + }) + } +} + +func testDefaultImage(t *testing.T, defaultImageFn func() string, imageName string) { + testCases := []struct { + name string + buildInfoVersion string + buildInfoRegistry string + want string + }{ + { + name: "image uses velero as registry and latest as tag when buildinfo.ImageRegistry and buildinfo.Version are empty", + want: fmt.Sprintf("velero/%s:latest", imageName), + }, + { + name: "image uses buildinfo.ImageRegistry as registry when not empty", + buildInfoRegistry: "custom-build-image-registry", + want: fmt.Sprintf("custom-build-image-registry/%s:latest", imageName), + }, + { + name: "image uses buildinfo.Version as tag when not empty", + buildInfoVersion: "custom-build-version", + want: fmt.Sprintf("velero/%s:custom-build-version", imageName), + }, + { + name: "image uses both buildinfo.ImageRegistry and buildinfo.Version when not empty", + buildInfoRegistry: "custom-build-image-registry", + buildInfoVersion: "custom-build-version", + want: fmt.Sprintf("custom-build-image-registry/%s:custom-build-version", imageName), + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + originalImageRegistry := buildinfo.ImageRegistry + originalVersion := buildinfo.Version + buildinfo.ImageRegistry = tc.buildInfoRegistry + buildinfo.Version = tc.buildInfoVersion + defer func() { + buildinfo.ImageRegistry = originalImageRegistry + buildinfo.Version = originalVersion + }() + + assert.Equal(t, tc.want, defaultImageFn()) + }) + } + +} + +func TestDefaultVeleroImage(t *testing.T) { + testDefaultImage(t, DefaultVeleroImage, "velero") +} + +func TestDefaultResticRestoreHelperImage(t *testing.T) { + testDefaultImage(t, DefaultResticRestoreHelperImage, "velero-restic-restore-helper") +} diff --git a/pkg/buildinfo/version.go b/pkg/buildinfo/buildinfo.go similarity index 86% rename from pkg/buildinfo/version.go rename to pkg/buildinfo/buildinfo.go index 60668ac63..eca5f1b5a 100644 --- a/pkg/buildinfo/version.go +++ b/pkg/buildinfo/buildinfo.go @@ -1,5 +1,5 @@ /* -Copyright 2017 the Velero contributors. +Copyright the Velero contributors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -31,6 +31,10 @@ var ( // GitTreeState indicates if the git tree is clean or dirty, set by the go linker's -X flag at build // time. GitTreeState string + + // ImageRegistry is the image registry that this build of Velero should use by default to pull the + // Velero and Restic Restore Helper images from. + ImageRegistry string ) // FormattedGitSHA renders the Git SHA with an indicator of the tree state. diff --git a/pkg/buildinfo/version_test.go b/pkg/buildinfo/buildinfo_test.go similarity index 100% rename from pkg/buildinfo/version_test.go rename to pkg/buildinfo/buildinfo_test.go diff --git a/pkg/cmd/cli/install/install.go b/pkg/cmd/cli/install/install.go index d2ed61c6a..037fca2f7 100644 --- a/pkg/cmd/cli/install/install.go +++ b/pkg/cmd/cli/install/install.go @@ -1,5 +1,5 @@ /* -Copyright 2020 the Velero contributors. +Copyright the Velero contributors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -29,6 +29,7 @@ import ( "github.com/spf13/pflag" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" + "github.com/vmware-tanzu/velero/internal/velero" velerov1api "github.com/vmware-tanzu/velero/pkg/apis/velero/v1" "github.com/vmware-tanzu/velero/pkg/client" "github.com/vmware-tanzu/velero/pkg/cmd" @@ -111,7 +112,7 @@ func (o *InstallOptions) BindFlags(flags *pflag.FlagSet) { func NewInstallOptions() *InstallOptions { return &InstallOptions{ Namespace: velerov1api.DefaultNamespace, - Image: install.DefaultImage, + Image: velero.DefaultVeleroImage(), BackupStorageConfig: flag.NewMap(), VolumeSnapshotConfig: flag.NewMap(), PodAnnotations: flag.NewMap(), diff --git a/pkg/install/daemonset.go b/pkg/install/daemonset.go index 2d8c8344d..74e841f3d 100644 --- a/pkg/install/daemonset.go +++ b/pkg/install/daemonset.go @@ -1,5 +1,5 @@ /* -Copyright 2018, 2019, 2020 the Velero contributors. +Copyright the Velero contributors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -23,11 +23,13 @@ import ( appsv1 "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + + "github.com/vmware-tanzu/velero/internal/velero" ) func DaemonSet(namespace string, opts ...podTemplateOption) *appsv1.DaemonSet { c := &podTemplateConfig{ - image: DefaultImage, + image: velero.DefaultVeleroImage(), } for _, opt := range opts { diff --git a/pkg/install/deployment.go b/pkg/install/deployment.go index cdc501c19..d07ded213 100644 --- a/pkg/install/deployment.go +++ b/pkg/install/deployment.go @@ -1,5 +1,5 @@ /* -Copyright 2020 the Velero contributors. +Copyright the Velero contributors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -25,6 +25,7 @@ import ( corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "github.com/vmware-tanzu/velero/internal/velero" "github.com/vmware-tanzu/velero/pkg/builder" ) @@ -117,7 +118,7 @@ func WithDefaultVolumesToRestic() podTemplateOption { func Deployment(namespace string, opts ...podTemplateOption) *appsv1.Deployment { // TODO: Add support for server args c := &podTemplateConfig{ - image: DefaultImage, + image: velero.DefaultVeleroImage(), } for _, opt := range opts { diff --git a/pkg/install/resources.go b/pkg/install/resources.go index 9ca639388..c03b4308c 100644 --- a/pkg/install/resources.go +++ b/pkg/install/resources.go @@ -29,20 +29,9 @@ import ( "k8s.io/apimachinery/pkg/runtime/schema" "github.com/vmware-tanzu/velero/config/crd/crds" - "github.com/vmware-tanzu/velero/pkg/buildinfo" ) -// Use "latest" if the build process didn't supply a version -func imageVersion() string { - if buildinfo.Version == "" { - return "latest" - } - return buildinfo.Version -} - -// DefaultImage is the default image to use for the Velero deployment and restic daemonset containers. var ( - DefaultImage = "velero/velero:" + imageVersion() DefaultVeleroPodCPURequest = "500m" DefaultVeleroPodMemRequest = "128Mi" DefaultVeleroPodCPULimit = "1000m" diff --git a/pkg/restore/restic_restore_action.go b/pkg/restore/restic_restore_action.go index 42c755a93..c20af38c7 100644 --- a/pkg/restore/restic_restore_action.go +++ b/pkg/restore/restic_restore_action.go @@ -1,5 +1,5 @@ /* -Copyright 2018, 2019, 2020 the Velero contributors. +Copyright the Velero contributors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -29,9 +29,9 @@ import ( "k8s.io/apimachinery/pkg/runtime" corev1client "k8s.io/client-go/kubernetes/typed/core/v1" + 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" velerov1client "github.com/vmware-tanzu/velero/pkg/generated/clientset/versioned/typed/velero/v1" "github.com/vmware-tanzu/velero/pkg/label" "github.com/vmware-tanzu/velero/pkg/plugin/framework" @@ -41,7 +41,6 @@ import ( ) const ( - defaultImageBase = "velero/velero-restic-restore-helper" defaultCPURequestLimit = "100m" defaultMemRequestLimit = "128Mi" defaultCommand = "/velero-restic-restore-helper" @@ -193,13 +192,13 @@ func getCommand(log logrus.FieldLogger, config *corev1.ConfigMap) []string { func getImage(log logrus.FieldLogger, config *corev1.ConfigMap) string { if config == nil { log.Debug("No config found for plugin") - return initContainerImage(defaultImageBase) + return veleroimage.DefaultResticRestoreHelperImage() } image := config.Data["image"] if image == "" { log.Debugf("No custom image configured") - return initContainerImage(defaultImageBase) + return veleroimage.DefaultResticRestoreHelperImage() } log = log.WithField("image", image) @@ -207,15 +206,17 @@ func getImage(log logrus.FieldLogger, config *corev1.ConfigMap) string { parts := strings.Split(image, "/") if len(parts) == 1 { + defaultImage := veleroimage.DefaultResticRestoreHelperImage() // Image supplied without registry part - log.Debugf("Plugin config contains image name without registry name. Return defaultImageBase") - return initContainerImage(defaultImageBase) + log.Infof("Plugin config contains image name without registry name. Using default init container image: %q", defaultImage) + return defaultImage } if !(strings.Contains(parts[len(parts)-1], ":")) { - // tag-less image name: add tag - log.Debugf("Plugin config contains image name without tag. Adding tag.") - return initContainerImage(image) + tag := veleroimage.ImageTag() + // tag-less image name: add default image tag for this version of Velero + log.Infof("Plugin config contains image name without tag. Adding tag: %q", tag) + return fmt.Sprintf("%s:%s", image, tag) } else { // tagged image name log.Debugf("Plugin config contains image name with tag") @@ -306,12 +307,3 @@ func newResticInitContainerBuilder(image, restoreUID string) *builder.ContainerB }, }...) } - -func initContainerImage(imageBase string) string { - tag := buildinfo.Version - if tag == "" { - tag = "latest" - } - - return fmt.Sprintf("%s:%s", imageBase, tag) -} diff --git a/pkg/restore/restic_restore_action_test.go b/pkg/restore/restic_restore_action_test.go index dc1c65acc..10148db68 100644 --- a/pkg/restore/restic_restore_action_test.go +++ b/pkg/restore/restic_restore_action_test.go @@ -1,5 +1,5 @@ /* -Copyright 2019 the Velero contributors. +Copyright the Velero contributors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -18,7 +18,6 @@ package restore import ( "context" - "fmt" "sort" "testing" @@ -31,6 +30,7 @@ import ( "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" @@ -49,41 +49,40 @@ func TestGetImage(t *testing.T) { } } - originalVersion := buildinfo.Version - buildinfo.Version = "buildinfo-version" - defer func() { - buildinfo.Version = originalVersion - }() + defaultImage := veleroimage.DefaultResticRestoreHelperImage() tests := []struct { - name string - configMap *corev1api.ConfigMap - want string + name string + configMap *corev1api.ConfigMap + buildInfoVersion string + want string }{ { - name: "nil config map returns default image with buildinfo.Version as tag", + name: "nil config map returns default image", configMap: nil, - want: fmt.Sprintf("%s:%s", defaultImageBase, buildinfo.Version), + want: defaultImage, }, { - name: "config map without 'image' key returns default image with buildinfo.Version as tag", + name: "config map without 'image' key returns default image", configMap: configMapWithData("non-matching-key", "val"), - want: fmt.Sprintf("%s:%s", defaultImageBase, buildinfo.Version), + want: defaultImage, }, { - name: "config map without '/' in image name returns default image with buildinfo.Version as tag", + name: "config map without '/' in image name returns default image", configMap: configMapWithData("image", "my-image"), - want: fmt.Sprintf("%s:%s", defaultImageBase, buildinfo.Version), + want: defaultImage, }, { - name: "config map with untagged image returns image with buildinfo.Version as tag", - configMap: configMapWithData("image", "myregistry.io/my-image"), - want: fmt.Sprintf("%s:%s", "myregistry.io/my-image", buildinfo.Version), + name: "config map with untagged image returns image with buildinfo.Version as tag", + configMap: configMapWithData("image", "myregistry.io/my-image"), + buildInfoVersion: "buildinfo-version", + want: "myregistry.io/my-image:buildinfo-version", }, { - name: "config map with untagged image and custom registry port with ':' returns image with buildinfo.Version as tag", - configMap: configMapWithData("image", "myregistry.io:34567/my-image"), - want: fmt.Sprintf("%s:%s", "myregistry.io:34567/my-image", buildinfo.Version), + name: "config map with untagged image and custom registry port with ':' returns image with buildinfo.Version as tag", + configMap: configMapWithData("image", "myregistry.io:34567/my-image"), + buildInfoVersion: "buildinfo-version", + want: "myregistry.io:34567/my-image:buildinfo-version", }, { name: "config map with tagged image returns tagged image", @@ -99,6 +98,13 @@ func TestGetImage(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { + if test.buildInfoVersion != "" { + originalVersion := buildinfo.Version + buildinfo.Version = test.buildInfoVersion + defer func() { + buildinfo.Version = originalVersion + }() + } assert.Equal(t, test.want, getImage(velerotest.NewLogger(), test.configMap)) }) } @@ -119,6 +125,8 @@ func TestResticRestoreActionExecute(t *testing.T) { veleroNs = "velero" ) + defaultResticRestoreHelperImage := veleroimage.DefaultResticRestoreHelperImage() + tests := []struct { name string pod *corev1api.Pod @@ -135,7 +143,7 @@ func TestResticRestoreActionExecute(t *testing.T) { ObjectMeta( builder.WithAnnotations("snapshot.velero.io/myvol", "")). InitContainers( - newResticInitContainerBuilder(initContainerImage(defaultImageBase), ""). + newResticInitContainerBuilder(defaultResticRestoreHelperImage, ""). Resources(&resourceReqs). SecurityContext(&securityContext). VolumeMounts(builder.ForVolumeMount("myvol", "/restores/myvol").Result()). @@ -152,7 +160,7 @@ func TestResticRestoreActionExecute(t *testing.T) { ObjectMeta( builder.WithAnnotations("snapshot.velero.io/myvol", "")). InitContainers( - newResticInitContainerBuilder(initContainerImage(defaultImageBase), ""). + newResticInitContainerBuilder(defaultResticRestoreHelperImage, ""). Resources(&resourceReqs). SecurityContext(&securityContext). VolumeMounts(builder.ForVolumeMount("myvol", "/restores/myvol").Result()). @@ -195,7 +203,7 @@ func TestResticRestoreActionExecute(t *testing.T) { ObjectMeta( builder.WithAnnotations("snapshot.velero.io/not-used", "")). InitContainers( - newResticInitContainerBuilder(initContainerImage(defaultImageBase), ""). + newResticInitContainerBuilder(defaultResticRestoreHelperImage, ""). Resources(&resourceReqs). SecurityContext(&securityContext). VolumeMounts(builder.ForVolumeMount("vol-1", "/restores/vol-1").Result(), builder.ForVolumeMount("vol-2", "/restores/vol-2").Result()). @@ -239,7 +247,7 @@ func TestResticRestoreActionExecute(t *testing.T) { builder.ForVolume("vol-2").PersistentVolumeClaimSource("pvc-2").Result(), ). InitContainers( - newResticInitContainerBuilder(initContainerImage(defaultImageBase), ""). + newResticInitContainerBuilder(defaultResticRestoreHelperImage, ""). Resources(&resourceReqs). SecurityContext(&securityContext). VolumeMounts(builder.ForVolumeMount("vol-1", "/restores/vol-1").Result(), builder.ForVolumeMount("vol-2", "/restores/vol-2").Result()).