From 423ab2d25e1a059767eb21c1b0947673af7fb4f0 Mon Sep 17 00:00:00 2001 From: Simon Emms Date: Fri, 8 Apr 2022 12:50:09 +0000 Subject: [PATCH] [installer]: give the RepoName function access to the config --- install/installer/cmd/mirror_list.go | 4 +- install/installer/pkg/common/ca.go | 2 +- install/installer/pkg/common/common.go | 29 +++++++--- install/installer/pkg/common/common_test.go | 64 ++++++++++++++++++++- 4 files changed, 88 insertions(+), 11 deletions(-) diff --git a/install/installer/cmd/mirror_list.go b/install/installer/cmd/mirror_list.go index d75a7460ef345d..b5ba93dced44db 100644 --- a/install/installer/cmd/mirror_list.go +++ b/install/installer/cmd/mirror_list.go @@ -139,7 +139,9 @@ func generateMirrorList(cfgVersion string, cfg *configv1.Config) ([]mirrorListRe images = append(images, mirrorListRepo{ Original: img, - Target: target, + Target: common.RepoName("", target, &configv1.Config{ + Repository: targetRepo, + }), }) } diff --git a/install/installer/pkg/common/ca.go b/install/installer/pkg/common/ca.go index b868495e3f7f96..6c30fca108bd3f 100644 --- a/install/installer/pkg/common/ca.go +++ b/install/installer/pkg/common/ca.go @@ -39,7 +39,7 @@ func InternalCAContainer(ctx *RenderContext, mod ...func(*corev1.Container)) *co res := &corev1.Container{ Name: "update-ca-certificates", // It's not possible to use images based on alpine due to errors running update-ca-certificates - Image: ImageName(ctx.Config.Repository, "ca-updater", ctx.VersionManifest.Components.CAUpdater.Version), + Image: ImageName(ctx.Config.Repository, "ca-updater", ctx.VersionManifest.Components.CAUpdater.Version, &ctx.Config), ImagePullPolicy: corev1.PullIfNotPresent, Command: []string{ "bash", "-c", diff --git a/install/installer/pkg/common/common.go b/install/installer/pkg/common/common.go index 806b81c1b47d5c..d47654cb01898d 100644 --- a/install/installer/pkg/common/common.go +++ b/install/installer/pkg/common/common.go @@ -248,7 +248,7 @@ func DatabaseEnv(cfg *config.Config) (res []corev1.EnvVar) { func DatabaseWaiterContainer(ctx *RenderContext) *corev1.Container { return &corev1.Container{ Name: "database-waiter", - Image: ImageName(ctx.Config.Repository, "service-waiter", ctx.VersionManifest.Components.ServiceWaiter.Version), + Image: ImageName(ctx.Config.Repository, "service-waiter", ctx.VersionManifest.Components.ServiceWaiter.Version, &ctx.Config), Args: []string{ "-v", "database", @@ -266,7 +266,7 @@ func DatabaseWaiterContainer(ctx *RenderContext) *corev1.Container { func MessageBusWaiterContainer(ctx *RenderContext) *corev1.Container { return &corev1.Container{ Name: "msgbus-waiter", - Image: ImageName(ctx.Config.Repository, "service-waiter", ctx.VersionManifest.Components.ServiceWaiter.Version), + Image: ImageName(ctx.Config.Repository, "service-waiter", ctx.VersionManifest.Components.ServiceWaiter.Version, &ctx.Config), Args: []string{ "-v", "messagebus", @@ -284,7 +284,7 @@ func MessageBusWaiterContainer(ctx *RenderContext) *corev1.Container { func KubeRBACProxyContainer(ctx *RenderContext) *corev1.Container { return &corev1.Container{ Name: "kube-rbac-proxy", - Image: ImageName(ThirdPartyContainerRepo(ctx.Config.Repository, KubeRBACProxyRepo), KubeRBACProxyImage, KubeRBACProxyTag), + Image: ImageName(ThirdPartyContainerRepo(ctx.Config.Repository, KubeRBACProxyRepo), KubeRBACProxyImage, KubeRBACProxyTag, &ctx.Config), Args: []string{ "--v=5", "--logtostderr", @@ -339,7 +339,7 @@ func Affinity(orLabels ...string) *corev1.Affinity { } } -func RepoName(repo, name string) string { +func RepoName(repo, name string, cfg *config.Config) string { var ref string if repo == "" { ref = name @@ -350,11 +350,26 @@ func RepoName(repo, name string) string { if err != nil { panic(fmt.Sprintf("cannot parse image repo %s: %v", ref, err)) } - return pref.String() + + prefString := pref.String() + + if cfg.Repository != GitpodContainerRegistry { + // If not in the Gitpod registry, don't use namespaces unless specified in the config. + // This is to match the KOTS registry format of no namespace in the name + s := strings.Split(prefString, "/") + + noNamespace := []string{ + cfg.Repository, + s[len(s)-1], + } + return strings.Join(noNamespace, "/") + } + + return prefString } -func ImageName(repo, name, tag string) string { - ref := fmt.Sprintf("%s:%s", RepoName(repo, name), tag) +func ImageName(repo, name, tag string, cfg *config.Config) string { + ref := fmt.Sprintf("%s:%s", RepoName(repo, name, cfg), tag) pref, err := reference.ParseNamed(ref) if err != nil { panic(fmt.Sprintf("cannot parse image ref %s: %v", ref, err)) diff --git a/install/installer/pkg/common/common_test.go b/install/installer/pkg/common/common_test.go index 8ba4236c47833d..7880d08c52fb82 100644 --- a/install/installer/pkg/common/common_test.go +++ b/install/installer/pkg/common/common_test.go @@ -5,9 +5,11 @@ package common_test import ( - "github.com/gitpod-io/gitpod/installer/pkg/common" "testing" + "github.com/gitpod-io/gitpod/installer/pkg/common" + "github.com/gitpod-io/gitpod/installer/pkg/config/v1" + "github.com/google/go-cmp/cmp" ) @@ -20,6 +22,7 @@ func TestRepoName(t *testing.T) { Repo string Name string Expectation Expectation + CfgRepo string }{ { Name: "gitpod-io/workspace-full", @@ -41,6 +44,54 @@ func TestRepoName(t *testing.T) { Panics: true, }, }, + // Custom repo, no namespace + { + Name: "gitpod-io/workspace-full", + Expectation: Expectation{ + Result: "some.registry.com/workspace-full", + }, + CfgRepo: "some.registry.com", + }, + { + Repo: "some-repo.com", + Name: "some-image", + Expectation: Expectation{ + Result: "some.registry.com/some-image", + }, + CfgRepo: "some.registry.com", + }, + { + Repo: "some-repo", + Name: "not@avalid#image-name", + Expectation: Expectation{ + Panics: true, + }, + CfgRepo: "some.registry.com", + }, + // Custom repo, namespace + { + Name: "gitpod-io/workspace-full", + Expectation: Expectation{ + Result: "some.registry.com/gitpod/workspace-full", + }, + CfgRepo: "some.registry.com/gitpod", + }, + { + Repo: "some-repo.com", + Name: "some-image", + Expectation: Expectation{ + Result: "some.registry.com/gitpod/some-image", + }, + CfgRepo: "some.registry.com/gitpod", + }, + { + Repo: "some-repo", + Name: "not@avalid#image-name", + Expectation: Expectation{ + Panics: true, + }, + CfgRepo: "some.registry.com/gitpod", + }, } for _, test := range tests { @@ -52,7 +103,16 @@ func TestRepoName(t *testing.T) { act.Panics = true } }() - act.Result = common.RepoName(test.Repo, test.Name) + cfg := config.Config{ + Repository: func() string { + if test.CfgRepo == "" { + return common.GitpodContainerRegistry + } + + return test.CfgRepo + }(), + } + act.Result = common.RepoName(test.Repo, test.Name, &cfg) }() if diff := cmp.Diff(test.Expectation, act); diff != "" {