Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[kots]: enable use of a local registry #9155

Merged
merged 4 commits into from
Apr 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 113 additions & 1 deletion install/installer/cmd/mirror_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/gitpod-io/gitpod/installer/pkg/common"
configv1 "github.com/gitpod-io/gitpod/installer/pkg/config/v1"
"github.com/spf13/cobra"
"k8s.io/utils/pointer"
)

type mirrorListRepo struct {
Expand Down Expand Up @@ -85,6 +86,117 @@ func init() {
mirrorListCmd.Flags().StringVarP(&mirrorListOpts.ConfigFN, "config", "c", os.Getenv("GITPOD_INSTALLER_CONFIG"), "path to the config file")
}

func renderAllKubernetesObject(cfgVersion string, cfg *configv1.Config) ([]string, error) {
csweichel marked this conversation as resolved.
Show resolved Hide resolved
fns := []func() ([]string, error){
func() ([]string, error) {
// Render for in-cluster dependencies
return renderKubernetesObjects(cfgVersion, cfg)
},
func() ([]string, error) {
// Render for external depedencies - AWS
cfg.Database = configv1.Database{
InCluster: pointer.Bool(false),
External: &configv1.DatabaseExternal{
Certificate: configv1.ObjectRef{
Kind: configv1.ObjectRefSecret,
Name: "value",
},
},
}
cfg.ContainerRegistry = configv1.ContainerRegistry{
InCluster: pointer.Bool(false),
External: &configv1.ContainerRegistryExternal{
URL: "some-url",
Certificate: configv1.ObjectRef{
Kind: configv1.ObjectRefSecret,
Name: "value",
},
},
S3Storage: &configv1.S3Storage{
Bucket: "some-bucket",
Certificate: configv1.ObjectRef{
Kind: configv1.ObjectRefSecret,
Name: "value",
},
},
}
cfg.ObjectStorage = configv1.ObjectStorage{
InCluster: pointer.Bool(false),
S3: &configv1.ObjectStorageS3{
Endpoint: "endpoint",
Credentials: configv1.ObjectRef{
Kind: configv1.ObjectRefSecret,
Name: "value",
},
},
}
return renderKubernetesObjects(cfgVersion, cfg)
},
func() ([]string, error) {
// Render for external depedencies - Azure
cfg.Database.CloudSQL = nil
cfg.ContainerRegistry = configv1.ContainerRegistry{
InCluster: pointer.Bool(false),
External: &configv1.ContainerRegistryExternal{
URL: "some-url",
Certificate: configv1.ObjectRef{
Kind: configv1.ObjectRefSecret,
Name: "value",
},
},
}
cfg.ObjectStorage = configv1.ObjectStorage{
InCluster: pointer.Bool(false),
Azure: &configv1.ObjectStorageAzure{
Credentials: configv1.ObjectRef{
Kind: configv1.ObjectRefSecret,
Name: "value",
},
},
}

return renderKubernetesObjects(cfgVersion, cfg)
},
func() ([]string, error) {
// Render for external depedencies - GCP
cfg.Database = configv1.Database{
InCluster: pointer.Bool(false),
CloudSQL: &configv1.DatabaseCloudSQL{
Instance: "value",
ServiceAccount: configv1.ObjectRef{
Kind: configv1.ObjectRefSecret,
Name: "value",
},
},
}
cfg.ObjectStorage = configv1.ObjectStorage{
InCluster: pointer.Bool(false),
CloudStorage: &configv1.ObjectStorageCloudStorage{
Project: "project",
ServiceAccount: configv1.ObjectRef{
Kind: configv1.ObjectRefSecret,
Name: "value",
},
},
}

return renderKubernetesObjects(cfgVersion, cfg)
},
}

var k8s []string
for _, fn := range fns {
data, err := fn()
if err != nil {
return nil, err
}

k8s = append(k8s, data...)
}

return k8s, nil
}

func generateMirrorList(cfgVersion string, cfg *configv1.Config) ([]mirrorListRepo, error) {
// Throw error if set to the default Gitpod repository
if cfg.Repository == common.GitpodContainerRegistry {
Expand All @@ -97,7 +209,7 @@ func generateMirrorList(cfgVersion string, cfg *configv1.Config) ([]mirrorListRe
// Use the default Gitpod registry to pull from
cfg.Repository = common.GitpodContainerRegistry

k8s, err := renderKubernetesObjects(cfgVersion, cfg)
k8s, err := renderAllKubernetesObject(cfgVersion, cfg)
if err != nil {
return nil, err
}
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: ctx.ImageName(ctx.Config.Repository, "ca-updater", ctx.VersionManifest.Components.CAUpdater.Version),
ImagePullPolicy: corev1.PullIfNotPresent,
Command: []string{
"bash", "-c",
Expand Down
34 changes: 3 additions & 31 deletions install/installer/pkg/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
config "github.com/gitpod-io/gitpod/installer/pkg/config/v1"
"github.com/gitpod-io/gitpod/installer/pkg/config/v1/experimental"

"github.com/docker/distribution/reference"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
networkingv1 "k8s.io/api/networking/v1"
Expand Down Expand Up @@ -248,7 +247,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: ctx.ImageName(ctx.Config.Repository, "service-waiter", ctx.VersionManifest.Components.ServiceWaiter.Version),
Args: []string{
"-v",
"database",
Expand All @@ -266,7 +265,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: ctx.ImageName(ctx.Config.Repository, "service-waiter", ctx.VersionManifest.Components.ServiceWaiter.Version),
Args: []string{
"-v",
"messagebus",
Expand All @@ -284,7 +283,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: ctx.ImageName(ThirdPartyContainerRepo(ctx.Config.Repository, KubeRBACProxyRepo), KubeRBACProxyImage, KubeRBACProxyTag),
Args: []string{
"--v=5",
"--logtostderr",
Expand Down Expand Up @@ -339,33 +338,6 @@ func Affinity(orLabels ...string) *corev1.Affinity {
}
}

func RepoName(repo, name string) string {
var ref string
if repo == "" {
ref = name
} else {
ref = fmt.Sprintf("%s/%s", strings.TrimSuffix(repo, "/"), name)
}
pref, err := reference.ParseNormalizedNamed(ref)
if err != nil {
panic(fmt.Sprintf("cannot parse image repo %s: %v", ref, err))
}
return pref.String()
}

func ImageName(repo, name, tag string) string {
ref := fmt.Sprintf("%s:%s", RepoName(repo, name), tag)
pref, err := reference.ParseNamed(ref)
if err != nil {
panic(fmt.Sprintf("cannot parse image ref %s: %v", ref, err))
}
if _, ok := pref.(reference.Tagged); !ok {
panic(fmt.Sprintf("image ref %s has no tag: %v", ref, err))
}

return ref
}

// ObjectHash marshals the objects to YAML and produces a sha256 hash of the output.
// This function is useful for restarting pods when the config changes.
// Takes an error as argument to make calling it more conventient. If that error is not nil,
Expand Down
63 changes: 0 additions & 63 deletions install/installer/pkg/common/common_test.go

This file was deleted.

38 changes: 38 additions & 0 deletions install/installer/pkg/common/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@
package common

import (
"fmt"
"strings"

"github.com/docker/distribution/reference"
"github.com/gitpod-io/gitpod/installer/pkg/config/v1"
"github.com/gitpod-io/gitpod/installer/pkg/config/v1/experimental"
"github.com/gitpod-io/gitpod/installer/pkg/config/versions"

"helm.sh/helm/v3/pkg/cli/values"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/utils/pointer"
)

// Renderable turns the config into a set of Kubernetes runtime objects
Expand Down Expand Up @@ -85,6 +90,39 @@ func (r *RenderContext) WithExperimental(mod func(ucfg *experimental.Config) err
return mod(r.experimentalConfig)
}

func (r *RenderContext) RepoName(repo, name string) string {
var ref string
if repo == "" {
ref = name
} else {
ref = fmt.Sprintf("%s/%s", strings.TrimSuffix(repo, "/"), name)
}
pref, err := reference.ParseNormalizedNamed(ref)
if err != nil {
panic(fmt.Sprintf("cannot parse image repo %s: %v", ref, err))
}

if pointer.BoolDeref(r.Config.DropImageRepo, false) {
segs := strings.Split(reference.Path(pref), "/")
return fmt.Sprintf("%s/%s", r.Config.Repository, segs[len(segs)-1])
}

return pref.String()
}

func (r *RenderContext) ImageName(repo, name, tag string) string {
ref := fmt.Sprintf("%s:%s", r.RepoName(repo, name), tag)
pref, err := reference.ParseNamed(ref)
if err != nil {
panic(fmt.Sprintf("cannot parse image ref %s: %v", ref, err))
}
if _, ok := pref.(reference.Tagged); !ok {
panic(fmt.Sprintf("image ref %s has no tag: %v", ref, err))
}

return ref
}

// generateValues generates the random values used throughout the context
// todo(sje): find a way of persisting these values for updates
func (r *RenderContext) generateValues() error {
Expand Down
Loading