Skip to content

Commit

Permalink
installer: always override -c config on default values
Browse files Browse the repository at this point in the history
Fixes #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 <[email protected]>
  • Loading branch information
Pothulapati authored and roboquat committed Feb 24, 2022
1 parent e720c49 commit 63d893c
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 13 deletions.
14 changes: 13 additions & 1 deletion install/installer/cmd/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package cmd

import (
"fmt"
"io/ioutil"
"os"

_ "embed"
Expand Down Expand Up @@ -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
Expand Down
32 changes: 21 additions & 11 deletions install/installer/pkg/config/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package config

import (
"fmt"
"io/ioutil"

"github.com/gitpod-io/gitpod/installer/pkg/cluster"
"github.com/go-playground/validator/v10"
Expand Down Expand Up @@ -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) {
Expand Down
1 change: 0 additions & 1 deletion install/installer/pkg/config/v1/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 63d893c

Please sign in to comment.