diff --git a/cmd/helmvm/install.go b/cmd/helmvm/install.go index 6679edf19..4e19517e8 100644 --- a/cmd/helmvm/install.go +++ b/cmd/helmvm/install.go @@ -149,6 +149,7 @@ func updateConfigBundle(c *cli.Context, bundledir string) error { if err := createK0sctlConfigBackup(c.Context); err != nil { return fmt.Errorf("unable to create config backup: %w", err) } + cfgpath := defaults.PathToConfig("k0sctl.yaml") cfg, err := config.ReadConfigFile(cfgpath) if err != nil { @@ -259,7 +260,22 @@ func ensureK0sctlConfig(c *cli.Context, nodes []infra.Node, useprompt bool) erro } else if !os.IsNotExist(err) { return fmt.Errorf("unable to open config: %w", err) } - cfg, err := config.RenderClusterConfig(c, nodes, multi) + + opts := []addons.Option{} + if c.Bool("no-prompt") { + opts = append(opts, addons.WithoutPrompt()) + } + + for _, addon := range c.StringSlice("disable-addon") { + opts = append(opts, addons.WithoutAddon(addon)) + } + + helmConfigs, err := addons.NewApplier(opts...).GenerateHelmConfigs(c) + if err != nil { + return fmt.Errorf("generate helm configs: %w", err) + } + + cfg, err := config.RenderClusterConfig(c, nodes, multi, helmConfigs) if err != nil { return fmt.Errorf("unable to render config: %w", err) } diff --git a/pkg/addons/adminconsole/adminconsole.go b/pkg/addons/adminconsole/adminconsole.go index 75a5f0697..54e053e40 100644 --- a/pkg/addons/adminconsole/adminconsole.go +++ b/pkg/addons/adminconsole/adminconsole.go @@ -17,6 +17,7 @@ import ( "gopkg.in/yaml.v3" "github.com/replicatedhq/helmvm/pkg/addons/adminconsole/charts" + "github.com/replicatedhq/helmvm/pkg/config" "github.com/replicatedhq/helmvm/pkg/defaults" "github.com/replicatedhq/helmvm/pkg/prompts" ) @@ -87,6 +88,36 @@ func (a *AdminConsole) addLicenseToHelmValues() error { return nil } +func (a *AdminConsole) DefaultValues() map[string]interface{} { + return helmValues +} + +func getcurrentPassword() (string, error) { + cfgpath := defaults.PathToConfig("k0sctl.yaml") + cfg, err := config.ReadConfigFile(cfgpath) + if err != nil { + return "", fmt.Errorf("unable to read cluster config: %w", err) + } + + currentCharts := cfg.Spec.K0s.Config.Dig("spec", "extensions", "helm", "charts").([]interface{}) + + fmt.Printf("%+v\n", currentCharts) + + for _, chart := range currentCharts { + chartMapping := chart.(dig.Mapping) + if chartMapping["name"] == "adminconsole" { + values := dig.Mapping{} + err := yaml.Unmarshal([]byte(chartMapping["values"].(string)), &values) + if err != nil { + return "", fmt.Errorf("unable to unmarshal values: %w", err) + } + return values["password"].(string), nil + } + } + + return "", fmt.Errorf("unable to find adminconsole chart in cluster config") +} + // GenerateHelmConfig generates the helm config for the adminconsole // and writes the charts to the disk. func (a *AdminConsole) GenerateHelmConfig(ctx *cli.Context, isUpgrade bool) ([]dig.Mapping, error) { @@ -105,7 +136,13 @@ func (a *AdminConsole) GenerateHelmConfig(ctx *cli.Context, isUpgrade bool) ([]d return chartConfigs, fmt.Errorf("unable to add license to helm values: %w", err) } - if !isUpgrade { + if isUpgrade { + currentPassword, err := getcurrentPassword() + if err != nil { + return chartConfigs, fmt.Errorf("unable to get current password: %w", err) + } + helmValues["password"] = currentPassword + } else { pass, err := a.askPassword() if err != nil { return chartConfigs, fmt.Errorf("unable to ask for password: %w", err) diff --git a/pkg/addons/applier.go b/pkg/addons/applier.go index eb3d68fd1..984052725 100644 --- a/pkg/addons/applier.go +++ b/pkg/addons/applier.go @@ -20,10 +20,10 @@ import ( "sigs.k8s.io/controller-runtime/pkg/log" "sigs.k8s.io/controller-runtime/pkg/log/zap" - "github.com/replicatedhq/helmvm/pkg/addons/adminconsole" "github.com/replicatedhq/helmvm/pkg/addons/custom" //"github.com/replicatedhq/helmvm/pkg/addons/custom" + "github.com/replicatedhq/helmvm/pkg/addons/adminconsole" "github.com/replicatedhq/helmvm/pkg/addons/openebs" pb "github.com/replicatedhq/helmvm/pkg/progressbar" ) diff --git a/pkg/addons/options.go b/pkg/addons/options.go index 449a6603b..c4b283ac4 100644 --- a/pkg/addons/options.go +++ b/pkg/addons/options.go @@ -13,16 +13,17 @@ func WithoutAddon(addon string) Option { } } -func IsUpgrade() Option { +// WithoutPrompt disables the prompt before applying addons. +func WithoutPrompt() Option { return func(a *Applier) { - a.isUpgrade = true + a.prompt = false } } -// WithoutPrompt disables the prompt before applying addons. -func WithoutPrompt() Option { +// IsUpgrade sets the applier to upgrade addons. +func IsUpgrade() Option { return func(a *Applier) { - a.prompt = false + a.isUpgrade = true } } diff --git a/pkg/config/config.go b/pkg/config/config.go index 9d5cd5a6c..fa21c7777 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -20,7 +20,6 @@ import ( "github.com/urfave/cli/v2" "gopkg.in/yaml.v2" - "github.com/replicatedhq/helmvm/pkg/addons" "github.com/replicatedhq/helmvm/pkg/defaults" "github.com/replicatedhq/helmvm/pkg/infra" "github.com/replicatedhq/helmvm/pkg/prompts" @@ -54,11 +53,11 @@ func SetUploadBinary(config *v1beta1.Cluster) { } // RenderClusterConfig renders a cluster configuration interactively. -func RenderClusterConfig(ctx *cli.Context, nodes []infra.Node, multi bool) (*v1beta1.Cluster, error) { +func RenderClusterConfig(ctx *cli.Context, nodes []infra.Node, multi bool, helmConfigs dig.Mapping) (*v1beta1.Cluster, error) { if multi { - return renderMultiNodeConfig(ctx, nodes) + return renderMultiNodeConfig(ctx, nodes, helmConfigs) } - return renderSingleNodeConfig(ctx) + return renderSingleNodeConfig(ctx, helmConfigs) } // listUserSSHKeys returns a list of private SSH keys in the user's ~/.ssh directory. @@ -218,7 +217,7 @@ func askForLoadBalancer() (string, error) { // renderMultiNodeConfig renders a configuration to allow k0sctl to install in a multi-node // configuration. -func renderMultiNodeConfig(ctx *cli.Context, nodes []infra.Node) (*v1beta1.Cluster, error) { +func renderMultiNodeConfig(ctx *cli.Context, nodes []infra.Node, helmConfigs dig.Mapping) (*v1beta1.Cluster, error) { var err error var hosts []*cluster.Host fmt.Println("You are about to configure a new cluster.") @@ -242,25 +241,11 @@ func renderMultiNodeConfig(ctx *cli.Context, nodes []infra.Node) (*v1beta1.Clust return nil, fmt.Errorf("unable to ask for load balancer: %w", err) } } - return generateConfigForHosts(ctx, lb, hosts...) + return generateConfigForHosts(ctx, lb, helmConfigs, hosts...) } // generateConfigForHosts generates a v1beta1.Cluster object for a given list of hosts. -func generateConfigForHosts(c *cli.Context, lb string, hosts ...*cluster.Host) (*v1beta1.Cluster, error) { - - opts := []addons.Option{} - if c.Bool("no-prompt") { - opts = append(opts, addons.WithoutPrompt()) - } - - for _, addon := range c.StringSlice("disable-addon") { - opts = append(opts, addons.WithoutAddon(addon)) - } - - helmConfigs, err := addons.NewApplier(opts...).GenerateHelmConfigs(c) - if err != nil { - return nil, fmt.Errorf("unable to apply addons: %w", err) - } +func generateConfigForHosts(c *cli.Context, lb string, helmConfigs dig.Mapping, hosts ...*cluster.Host) (*v1beta1.Cluster, error) { var configSpec = dig.Mapping{ "network": dig.Mapping{ @@ -300,7 +285,7 @@ func generateConfigForHosts(c *cli.Context, lb string, hosts ...*cluster.Host) ( // renderSingleNodeConfig renders a configuration to allow k0sctl to install in the localhost // in a single-node configuration. -func renderSingleNodeConfig(ctx *cli.Context) (*v1beta1.Cluster, error) { +func renderSingleNodeConfig(ctx *cli.Context, helmConfigs dig.Mapping) (*v1beta1.Cluster, error) { usr, err := user.Current() if err != nil { return nil, fmt.Errorf("unable to get current user: %w", err) @@ -323,7 +308,7 @@ func renderSingleNodeConfig(ctx *cli.Context) (*v1beta1.Cluster, error) { return nil, fmt.Errorf("unable to connect to %s: %w", ipaddr, err) } rhost := host.render() - return generateConfigForHosts(ctx, "", rhost) + return generateConfigForHosts(ctx, "", helmConfigs, rhost) } // UpdateHostsFiles passes through all hosts in the provided configuration and makes sure