From 222f04e7c61546323f9f7fcad4fb0442f4fbe0f5 Mon Sep 17 00:00:00 2001 From: Igor Date: Tue, 2 Apr 2019 10:11:48 +0200 Subject: [PATCH] Added -o option to provide files with override values (#52) --- pkg/kyma/cmd/install/cmd.go | 97 ++++++++++++++++++++++++++++++++---- pkg/kyma/cmd/install/opts.go | 21 ++++++++ 2 files changed, 108 insertions(+), 10 deletions(-) diff --git a/pkg/kyma/cmd/install/cmd.go b/pkg/kyma/cmd/install/cmd.go index dda0cc487..a9646f7cd 100644 --- a/pkg/kyma/cmd/install/cmd.go +++ b/pkg/kyma/cmd/install/cmd.go @@ -67,6 +67,7 @@ The command will: cobraCmd.Flags().StringVarP(&o.LocalInstallerDir, "installer-dir", "", "", "Directory of installer docker image to use while building locally") cobraCmd.Flags().DurationVarP(&o.Timeout, "timeout", "", 0, "Timeout after which CLI should give up watching installation") cobraCmd.Flags().StringVarP(&o.Password, "password", "p", "", "Pre-defined cluster password") + cobraCmd.Flags().VarP(&o.OverrideConfigs, "override", "o", "Path to YAML file with parameters to override. Multiple entries of this flag allowed") return cobraCmd } @@ -243,12 +244,16 @@ func (cmd *command) configureInstallerFromRelease() error { return err } - if cmd.opts.Password != "" { - err = cmd.setAdminPassword() - if err != nil { - return err - } + err = cmd.applyOverrideFiles() + if err != nil { + return err } + + err = cmd.setAdminPassword() + if err != nil { + return err + } + return nil } @@ -277,11 +282,15 @@ func (cmd *command) installInstallerFromLocalSources() error { if err != nil { return err } - if cmd.opts.Password != "" { - err = cmd.setAdminPassword() - if err != nil { - return err - } + + err = cmd.applyOverrideFiles() + if err != nil { + return err + } + + err = cmd.setAdminPassword() + if err != nil { + return err } return cmd.labelInstallerNamespace() } @@ -403,7 +412,75 @@ func (cmd *command) activateInstaller() error { return nil } +func (cmd *command) applyOverrideFiles() error { + oFiles := cmd.opts.OverrideConfigs.Len() + if oFiles == 0 { + return nil + } + + for _, file := range cmd.opts.OverrideConfigs { + oFile, err := os.Open(file) + if err != nil { + return fmt.Errorf("unable to open file: %s. Error: %s", + file, err.Error()) + } + rawData, err := ioutil.ReadAll(oFile) + if err != nil { + return fmt.Errorf("unable to read data from file: %s. Error: %s", + file, err.Error()) + } + + configs := strings.Split(string(rawData), "---") + + for _, c := range configs { + cfg := make(map[interface{}]interface{}) + err = yaml.Unmarshal([]byte(c), &cfg) + if err != nil { + return fmt.Errorf("unable to parse file data: %s. Error: %s", + file, err.Error()) + } + + kind, ok := cfg["kind"].(string) + if !ok { + return fmt.Errorf("unable get kind of config. File: %s", file) + } + + meta, ok := cfg["metadata"].(map[interface{}]interface{}) + if !ok { + return fmt.Errorf("unable to get metadata from config. File: %s", file) + } + + namespace, ok := meta["namespace"].(string) + if !ok { + return fmt.Errorf("unable to get namespace from config. File: %s", file) + } + + name, ok := meta["name"].(string) + if !ok { + return fmt.Errorf("unable to get name from config. File: %s", file) + } + + _, err := cmd.Kubectl().RunCmd("-n", + strings.ToLower(namespace), + "patch", + kind, + strings.ToLower(name), + "-p", + c) + if err != nil { + return fmt.Errorf("unable to override values. File: %s. Error: %s", file, err.Error()) + } + } + + } + + return nil +} + func (cmd *command) setAdminPassword() error { + if cmd.opts.Password == "" { + return nil + } encPass := base64.StdEncoding.EncodeToString([]byte(cmd.opts.Password)) _, err := cmd.Kubectl().RunCmd("-n", "kyma-installer", "patch", "configmap", "installation-config-overrides", fmt.Sprintf(`-p='{"data": {"global.adminPassword": "%s"}}'`, encPass), "-v=1") return err diff --git a/pkg/kyma/cmd/install/opts.go b/pkg/kyma/cmd/install/opts.go index 1750b6bd0..21d8c34ad 100644 --- a/pkg/kyma/cmd/install/opts.go +++ b/pkg/kyma/cmd/install/opts.go @@ -1,11 +1,31 @@ package install import ( + "strings" "time" "github.com/kyma-incubator/kyma-cli/pkg/kyma/core" ) +type overrideFileList []string + +func (ovf *overrideFileList) String() string { + return "[" + strings.Join(*ovf, " ,") + "]" +} + +func (ovf *overrideFileList) Set(value string) error { + *ovf = append(*ovf, value) + return nil +} + +func (ovf *overrideFileList) Type() string { + return "[]string" +} + +func (ovf *overrideFileList) Len() int { + return len(*ovf) +} + //Options defines available options for the command type Options struct { *core.Options @@ -19,6 +39,7 @@ type Options struct { LocalInstallerDir string Timeout time.Duration Password string + OverrideConfigs overrideFileList } //NewOptions creates options with default values