Skip to content

Commit

Permalink
Added -o option to provide files with override values (kyma-project#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
igor-karpukhin authored Apr 2, 2019
1 parent 8516bba commit 222f04e
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 10 deletions.
97 changes: 87 additions & 10 deletions pkg/kyma/cmd/install/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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()
}
Expand Down Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions pkg/kyma/cmd/install/opts.go
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -19,6 +39,7 @@ type Options struct {
LocalInstallerDir string
Timeout time.Duration
Password string
OverrideConfigs overrideFileList
}

//NewOptions creates options with default values
Expand Down

0 comments on commit 222f04e

Please sign in to comment.