From a3232f0a811a6ce40bda0c9dc99fe498719142b1 Mon Sep 17 00:00:00 2001
From: Tarun Pothulapati <tarun@gitpod.io>
Date: Wed, 23 Feb 2022 05:54:59 +0000
Subject: [PATCH] installer: always override `-c` config on default values

Fixes https://github.com/gitpod-io/gitpod/issues/8267

Currently, Users run `init` command first, update it and
pass full config to `render` to generate Kubernetes manifests. The
passage of `-c` is a requirement here, and users can't skip it.

This PR makes the passage of `config` to `render` optional, and
flexible. This means
- Users can skip `-c` entirely, in which case we use the default
  values on the default config version for that installer binary
- Users can selectively override fields and *thus no need to pass full
  config* all the time. This means `-c` flag acts as a flag through
  which they can override the default fields.

For the second case, When a user explicitely sets the `apiVersion`
field in the passed config, we use the default values for that
version. If no `apiVersion` is passed, we override the passed config
onto the default values on the default config version for that
installer binary.

After this change, For users This means that they only store and use
the config that has their changes only (on the default config), and
not the entire config.

Signed-off-by: Tarun Pothulapati <tarun@gitpod.io>
---
 install/installer/cmd/render.go           | 14 +++++++++-
 install/installer/pkg/config/loader.go    | 32 +++++++++++++++--------
 install/installer/pkg/config/v1/config.go |  1 -
 3 files changed, 34 insertions(+), 13 deletions(-)

diff --git a/install/installer/cmd/render.go b/install/installer/cmd/render.go
index 4749d403743209..902e82ea7215c1 100644
--- a/install/installer/cmd/render.go
+++ b/install/installer/cmd/render.go
@@ -6,6 +6,7 @@ package cmd
 
 import (
 	"fmt"
+	"io/ioutil"
 	"os"
 
 	_ "embed"
@@ -66,7 +67,18 @@ A config file is required which can be generated with the init command.`,
 }
 
 func loadConfig(cfgFN string) (rawCfg interface{}, cfgVersion string, cfg *configv1.Config, err error) {
-	rawCfg, cfgVersion, err = config.Load(cfgFN)
+	var overrideConfig string
+	// Update overrideConfig if cfgFN is not empty
+	if cfgFN != "" {
+		cfgBytes, err := ioutil.ReadFile(cfgFN)
+		if err != nil {
+			panic(fmt.Sprintf("couldn't read file %s, %s", cfgFN, err))
+
+		}
+		overrideConfig = string(cfgBytes)
+	}
+
+	rawCfg, cfgVersion, err = config.Load(overrideConfig)
 	if err != nil {
 		err = fmt.Errorf("error loading config: %w", err)
 		return
diff --git a/install/installer/pkg/config/loader.go b/install/installer/pkg/config/loader.go
index ebc75405dd251c..684eaf7b7da803 100644
--- a/install/installer/pkg/config/loader.go
+++ b/install/installer/pkg/config/loader.go
@@ -6,7 +6,6 @@ package config
 
 import (
 	"fmt"
-	"io/ioutil"
 
 	"github.com/gitpod-io/gitpod/installer/pkg/cluster"
 	"github.com/go-playground/validator/v10"
@@ -73,32 +72,43 @@ func LoadConfigVersion(version string) (ConfigVersion, error) {
 	return v, nil
 }
 
-func Load(fn string) (cfg interface{}, version string, err error) {
-	fc, err := ioutil.ReadFile(fn)
+// Load takes a config string and overrides that onto the default
+// config for that version (passed in the config). If no config version
+// is passed, It overrides it onto the default CurrentVersion of the binary
+func Load(overrideConfig string) (cfg interface{}, version string, err error) {
+	var overrideVS struct {
+		APIVersion string `json:"apiVersion"`
+	}
+	err = yaml.Unmarshal([]byte(overrideConfig), &overrideVS)
 	if err != nil {
 		return
 	}
-	var vs struct {
-		APIVersion string `json:"apiVersion"`
+
+	apiVersion := overrideVS.APIVersion
+	// fall-back to default CurrentVersion if no apiVersion was passed
+	if version == "" {
+		apiVersion = CurrentVersion
 	}
-	err = yaml.Unmarshal(fc, &vs)
+
+	v, err := LoadConfigVersion(apiVersion)
 	if err != nil {
 		return
 	}
 
-	v, err := LoadConfigVersion(vs.APIVersion)
+	// Load default configuration
+	cfg = v.Factory()
+	err = v.Defaults(cfg)
 	if err != nil {
 		return
 	}
 
-	cfg = v.Factory()
-	version = vs.APIVersion
-	err = yaml.Unmarshal(fc, cfg)
+	// Override passed configuration onto the default
+	err = yaml.Unmarshal([]byte(overrideConfig), cfg)
 	if err != nil {
 		return
 	}
 
-	return cfg, version, nil
+	return cfg, apiVersion, nil
 }
 
 func Marshal(version string, cfg interface{}) ([]byte, error) {
diff --git a/install/installer/pkg/config/v1/config.go b/install/installer/pkg/config/v1/config.go
index 9b784bc7c14ddc..43f5251a25ae76 100644
--- a/install/installer/pkg/config/v1/config.go
+++ b/install/installer/pkg/config/v1/config.go
@@ -36,7 +36,6 @@ func (v version) Defaults(in interface{}) error {
 	}
 
 	cfg.Kind = InstallationFull
-	cfg.Domain = "gitpod.example.com"
 	cfg.Repository = "eu.gcr.io/gitpod-core-dev/build"
 	cfg.Observability = Observability{
 		LogLevel: LogLevelInfo,