Skip to content

Commit 1e44f25

Browse files
feat: adding --no-prompt flag (#25)
with --no-prompt flag user can control if the installer should or not ask for confirmations. this is useful for when we start deploying this as part of the compatibility matrix.
1 parent 7fc7ec1 commit 1e44f25

File tree

5 files changed

+71
-31
lines changed

5 files changed

+71
-31
lines changed

cmd/helmvm/install.go

Lines changed: 43 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -147,41 +147,55 @@ func copyUserProvidedConfig(c *cli.Context) error {
147147
return nil
148148
}
149149

150+
// overwriteExistingConfig asks user if they want to overwrite the existing cluster
151+
// configuration file.
152+
func overwriteExistingConfig() (bool, error) {
153+
var useCurrent = &survey.Confirm{
154+
Message: "Do you want to use the existing configuration ?",
155+
Default: true,
156+
}
157+
logrus.Warn("A cluster configuration file was found. This means you already")
158+
logrus.Warn("have created a cluster configured. You can either use the existing")
159+
logrus.Warn("configuration or create a new one (the original configuration will")
160+
logrus.Warn("be backed up).")
161+
var answer bool
162+
if err := survey.AskOne(useCurrent, &answer); err != nil {
163+
return false, err
164+
}
165+
return answer, nil
166+
}
167+
150168
// ensureK0sctlConfig ensures that a k0sctl.yaml file exists in the configuration
151169
// directory. If none exists then this directs the user to a wizard to create one.
152-
func ensureK0sctlConfig(c *cli.Context, nodes []infra.Node) error {
170+
func ensureK0sctlConfig(c *cli.Context, nodes []infra.Node, prompt bool) error {
171+
multi := c.Bool("multi-node") || len(nodes) > 0
172+
if !multi && runtime.GOOS != "linux" {
173+
return fmt.Errorf("single node clusters only supported on linux")
174+
}
153175
bundledir := c.String("bundle-dir")
154176
bundledir = strings.TrimRight(bundledir, "/")
155-
multi := c.Bool("multi-node") || len(nodes) > 0
156177
cfgpath := defaults.PathToConfig("k0sctl.yaml")
157178
if usercfg := c.String("config"); usercfg != "" {
158179
logrus.Infof("Using %s config file", usercfg)
159180
return copyUserProvidedConfig(c)
160181
}
161-
var useCurrent = &survey.Confirm{
162-
Message: "Do you want to use the existing configuration ?",
163-
Default: true,
164-
}
165182
if _, err := os.Stat(cfgpath); err == nil {
166-
var answer bool
167-
logrus.Warn("A cluster configuration file was found. This means you already")
168-
logrus.Warn("have created a cluster configured. You can either use the existing")
169-
logrus.Warn("configuration or create a new one (the original configuration will")
170-
logrus.Warn("be backed up).")
171-
if err := survey.AskOne(useCurrent, &answer); err != nil {
172-
return fmt.Errorf("unable to process answers: %w", err)
173-
} else if answer {
174-
return updateConfigBundle(c.Context, bundledir)
183+
if len(nodes) == 0 {
184+
if !prompt {
185+
return updateConfigBundle(c.Context, bundledir)
186+
}
187+
if over, err := overwriteExistingConfig(); err != nil {
188+
return fmt.Errorf("unable to process answers: %w", err)
189+
} else if !over {
190+
return updateConfigBundle(c.Context, bundledir)
191+
}
175192
}
176193
if err := createK0sctlConfigBackup(c.Context); err != nil {
177194
return fmt.Errorf("unable to create config backup: %w", err)
178195
}
179196
} else if !os.IsNotExist(err) {
180197
return fmt.Errorf("unable to open config: %w", err)
181198
}
182-
if !multi && runtime.GOOS != "linux" {
183-
return fmt.Errorf("single node clusters only supported on linux")
184-
}
185199
cfg, err := config.RenderClusterConfig(c.Context, nodes, multi)
186200
if err != nil {
187201
return fmt.Errorf("unable to render config: %w", err)
@@ -258,9 +272,9 @@ func dumpApplyLogs() {
258272

259273
// applyK0sctl runs the k0sctl apply command and waits for it to finish. If
260274
// no configuration is found one is generated.
261-
func applyK0sctl(c *cli.Context, nodes []infra.Node) error {
275+
func applyK0sctl(c *cli.Context, prompt bool, nodes []infra.Node) error {
262276
logrus.Infof("Processing cluster configuration")
263-
if err := ensureK0sctlConfig(c, nodes); err != nil {
277+
if err := ensureK0sctlConfig(c, nodes, prompt); err != nil {
264278
return fmt.Errorf("unable to create config file: %w", err)
265279
}
266280
logrus.Infof("Applying cluster configuration")
@@ -313,6 +327,11 @@ var installCommand = &cli.Command{
313327
Usage: "Only apply addons. Skips cluster install",
314328
Value: false,
315329
},
330+
&cli.BoolFlag{
331+
Name: "no-prompt",
332+
Usage: "Do not prompt user when it is not necessary",
333+
Value: false,
334+
},
316335
},
317336
Action: func(c *cli.Context) error {
318337
if defaults.DecentralizedInstall() {
@@ -321,6 +340,7 @@ var installCommand = &cli.Command{
321340
logrus.Warnf("Run '%s node --help' for more information.", defaults.BinaryName())
322341
return fmt.Errorf("decentralized install detected")
323342
}
343+
prompt := !c.Bool("no-prompt")
324344
logrus.Infof("Materializing binaries")
325345
if err := goods.Materialize(); err != nil {
326346
return fmt.Errorf("unable to materialize binaries: %w", err)
@@ -330,11 +350,11 @@ var installCommand = &cli.Command{
330350
var nodes []infra.Node
331351
if dir := c.String("infra"); dir != "" {
332352
logrus.Infof("Processing infrastructure manifests")
333-
if nodes, err = infra.Apply(c.Context, dir); err != nil {
353+
if nodes, err = infra.Apply(c.Context, dir, prompt); err != nil {
334354
return fmt.Errorf("unable to create infra: %w", err)
335355
}
336356
}
337-
if err := applyK0sctl(c, nodes); err != nil {
357+
if err := applyK0sctl(c, prompt, nodes); err != nil {
338358
return fmt.Errorf("unable update cluster: %w", err)
339359
}
340360
}
@@ -346,7 +366,7 @@ var installCommand = &cli.Command{
346366
ccfg := defaults.PathToConfig("k0sctl.yaml")
347367
kcfg := defaults.PathToConfig("kubeconfig")
348368
os.Setenv("KUBECONFIG", kcfg)
349-
if applier, err := addons.NewApplier(); err != nil {
369+
if applier, err := addons.NewApplier(prompt); err != nil {
350370
return fmt.Errorf("unable to create applier: %w", err)
351371
} else if err := applier.Apply(c.Context); err != nil {
352372
return fmt.Errorf("unable to apply addons: %w", err)

cmd/helmvm/upgrade.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ func canRunUpgrade(c *cli.Context) error {
4545
var upgradeCommand = &cli.Command{
4646
Name: "upgrade",
4747
Usage: "Upgrade the local node",
48+
Flags: []cli.Flag{
49+
&cli.BoolFlag{
50+
Name: "no-prompt",
51+
Usage: "Do not prompt user when it is not necessary",
52+
Value: false,
53+
},
54+
},
4855
Action: func(c *cli.Context) error {
4956
if err := canRunUpgrade(c); err != nil {
5057
return err
@@ -74,7 +81,7 @@ var upgradeCommand = &cli.Command{
7481
}
7582
os.Setenv("KUBECONFIG", kcfg)
7683
logrus.Infof("Upgrading addons")
77-
if applier, err := addons.NewApplier(); err != nil {
84+
if applier, err := addons.NewApplier(c.Bool("no-prompt")); err != nil {
7885
return fmt.Errorf("unable to create applier: %w", err)
7986
} else if err := applier.Apply(c.Context); err != nil {
8087
return fmt.Errorf("unable to apply addons: %w", err)

pkg/addons/adminconsole/adminconsole.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,14 @@ type AdminConsole struct {
3636
config *action.Configuration
3737
logger action.DebugLog
3838
namespace string
39+
prompt bool
3940
}
4041

4142
func (a *AdminConsole) askPassword() (string, error) {
43+
if !a.prompt {
44+
logrus.Warnf("Admin Console password set to: password")
45+
return "password", nil
46+
}
4247
question := &survey.Password{Message: "Enter a new Admin Console password:"}
4348
var pass string
4449
for pass == "" {
@@ -155,12 +160,17 @@ func (a *AdminConsole) installedRelease(ctx context.Context) (*release.Release,
155160
return releases[0], nil
156161
}
157162

158-
func New(namespace string, logger action.DebugLog) (*AdminConsole, error) {
163+
func New(namespace string, prompt bool, logger action.DebugLog) (*AdminConsole, error) {
159164
env := cli.New()
160165
env.SetNamespace(namespace)
161166
config := &action.Configuration{}
162167
if err := config.Init(env.RESTClientGetter(), namespace, "", logger); err != nil {
163168
return nil, fmt.Errorf("unable to init configuration: %w", err)
164169
}
165-
return &AdminConsole{namespace: namespace, config: config, logger: logger}, nil
170+
return &AdminConsole{
171+
namespace: namespace,
172+
config: config,
173+
logger: logger,
174+
prompt: prompt,
175+
}, nil
166176
}

pkg/addons/applier.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func (a *Applier) waitForKubernetes(ctx context.Context) error {
7777
}
7878

7979
// NewApplier creates a new Applier instance with all addons registered.
80-
func NewApplier() (*Applier, error) {
80+
func NewApplier(prompt bool) (*Applier, error) {
8181
k8slogger := zap.New(func(o *zap.Options) {
8282
o.DestWriter = io.Discard
8383
})
@@ -101,7 +101,7 @@ func NewApplier() (*Applier, error) {
101101
}
102102
applier.addons["openebs"] = obs
103103
logger = logrus.WithField("addon", "adminconsole")
104-
aconsole, err := adminconsole.New("helmvm", logger.Infof)
104+
aconsole, err := adminconsole.New("helmvm", prompt, logger.Infof)
105105
if err != nil {
106106
return nil, fmt.Errorf("unable to create admin console addon: %w", err)
107107
}

pkg/infra/infra.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ type Node struct {
2929

3030
// Apply uses "terraform apply" to apply the infrastructe defined in the
3131
// directory passed as argument.
32-
func Apply(ctx context.Context, dir string) ([]Node, error) {
32+
func Apply(ctx context.Context, dir string, prompt bool) ([]Node, error) {
3333
log, end := pb.Start()
3434
outputs, err := runApply(ctx, dir, log)
3535
if err != nil {
@@ -45,10 +45,13 @@ func Apply(ctx context.Context, dir string) ([]Node, error) {
4545
return nil, fmt.Errorf("unable to process terraform output: %w", err)
4646
}
4747
printNodes(nodes)
48+
if !prompt {
49+
return nodes, nil
50+
}
4851
logrus.Info("You may want to take note of the output for later use")
49-
prompt := &survey.Input{Message: "Press enter to proceed"}
52+
question := &survey.Input{Message: "Press enter to proceed"}
5053
var ignore string
51-
if err := survey.AskOne(prompt, &ignore); err != nil {
54+
if err := survey.AskOne(question, &ignore); err != nil {
5255
return nil, fmt.Errorf("unable to ask for enter: %w", err)
5356
}
5457
return nodes, nil

0 commit comments

Comments
 (0)