Skip to content

Commit

Permalink
don't ask for password if admin panel already has one
Browse files Browse the repository at this point in the history
  • Loading branch information
danj-replicated committed Oct 4, 2023
1 parent 3f5054b commit 61f772d
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 31 deletions.
18 changes: 17 additions & 1 deletion cmd/helmvm/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
Expand Down
39 changes: 38 additions & 1 deletion pkg/addons/adminconsole/adminconsole.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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) {
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion pkg/addons/applier.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down
11 changes: 6 additions & 5 deletions pkg/addons/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down
31 changes: 8 additions & 23 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.")
Expand All @@ -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{
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit 61f772d

Please sign in to comment.