Skip to content

Commit

Permalink
[installer]: give the RepoName function access to the config
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Emms committed Apr 8, 2022
1 parent 057b63c commit 423ab2d
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 11 deletions.
4 changes: 3 additions & 1 deletion install/installer/cmd/mirror_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}),
})
}

Expand Down
2 changes: 1 addition & 1 deletion install/installer/pkg/common/ca.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
29 changes: 22 additions & 7 deletions install/installer/pkg/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -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
Expand All @@ -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))
Expand Down
64 changes: 62 additions & 2 deletions install/installer/pkg/common/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -20,6 +22,7 @@ func TestRepoName(t *testing.T) {
Repo string
Name string
Expectation Expectation
CfgRepo string
}{
{
Name: "gitpod-io/workspace-full",
Expand All @@ -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 {
Expand All @@ -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 != "" {
Expand Down

0 comments on commit 423ab2d

Please sign in to comment.