Allow registry to be configured at build time

This adds a new `buildinfo` variable `ImageRegistry` that can set at
build time like the `Version` variable. This allows us to customise the
Velero binary to use different registries.

If the variable is set, this variable wille be used when creating the
URIs for both the main `velero` and `velero-restic-restore-helper`
images. If it is not set, default to using Dockerhub (`velero/velero`,
`velero/velero-restic-restore-helper`).

There are numerous ways in which the Velero binary can be built so all
of them have been updated to add the new link time flag to set the
variable:
* `make local` (used for local developer builds to build for the local
  OS and ARCH)
* `make build` (used by developers and also VMware internal builds to
  build a specific OS and ARCH)
* Goreleaser config (used when creating OSS release binaries)
* Dockerfile (used to build the Velero binary used within the image)

All of these workflows are currently triggered from our Makefile where
the variable `REGISTRY` is already available with the default value of
`velero` and used to build the image tag. Where the new `ImageRegistry`
build variable is needed, we pass through this Makefile variable to
those tasks so it can be used accordingly.

The GitHub action and the `./hack/docker-push.sh` script used to push
container images has not been modified. This will continue to use the
default registry specified in the Makefile and will not explicitly pass
it in.

Signed-off-by: Bridget McErlean <bmcerlean@vmware.com>
This commit is contained in:
Bridget McErlean
2021-07-14 15:42:53 -04:00
parent 02f3f5cd60
commit 198ea57407
16 changed files with 273 additions and 66 deletions
+51
View File
@@ -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())
}
+140
View File
@@ -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")
}