@@ -147,41 +147,55 @@ func copyUserProvidedConfig(c *cli.Context) error {
147
147
return nil
148
148
}
149
149
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
+
150
168
// ensureK0sctlConfig ensures that a k0sctl.yaml file exists in the configuration
151
169
// 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
+ }
153
175
bundledir := c .String ("bundle-dir" )
154
176
bundledir = strings .TrimRight (bundledir , "/" )
155
- multi := c .Bool ("multi-node" ) || len (nodes ) > 0
156
177
cfgpath := defaults .PathToConfig ("k0sctl.yaml" )
157
178
if usercfg := c .String ("config" ); usercfg != "" {
158
179
logrus .Infof ("Using %s config file" , usercfg )
159
180
return copyUserProvidedConfig (c )
160
181
}
161
- var useCurrent = & survey.Confirm {
162
- Message : "Do you want to use the existing configuration ?" ,
163
- Default : true ,
164
- }
165
182
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
+ }
175
192
}
176
193
if err := createK0sctlConfigBackup (c .Context ); err != nil {
177
194
return fmt .Errorf ("unable to create config backup: %w" , err )
178
195
}
179
196
} else if ! os .IsNotExist (err ) {
180
197
return fmt .Errorf ("unable to open config: %w" , err )
181
198
}
182
- if ! multi && runtime .GOOS != "linux" {
183
- return fmt .Errorf ("single node clusters only supported on linux" )
184
- }
185
199
cfg , err := config .RenderClusterConfig (c .Context , nodes , multi )
186
200
if err != nil {
187
201
return fmt .Errorf ("unable to render config: %w" , err )
@@ -258,9 +272,9 @@ func dumpApplyLogs() {
258
272
259
273
// applyK0sctl runs the k0sctl apply command and waits for it to finish. If
260
274
// 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 {
262
276
logrus .Infof ("Processing cluster configuration" )
263
- if err := ensureK0sctlConfig (c , nodes ); err != nil {
277
+ if err := ensureK0sctlConfig (c , nodes , prompt ); err != nil {
264
278
return fmt .Errorf ("unable to create config file: %w" , err )
265
279
}
266
280
logrus .Infof ("Applying cluster configuration" )
@@ -313,6 +327,11 @@ var installCommand = &cli.Command{
313
327
Usage : "Only apply addons. Skips cluster install" ,
314
328
Value : false ,
315
329
},
330
+ & cli.BoolFlag {
331
+ Name : "no-prompt" ,
332
+ Usage : "Do not prompt user when it is not necessary" ,
333
+ Value : false ,
334
+ },
316
335
},
317
336
Action : func (c * cli.Context ) error {
318
337
if defaults .DecentralizedInstall () {
@@ -321,6 +340,7 @@ var installCommand = &cli.Command{
321
340
logrus .Warnf ("Run '%s node --help' for more information." , defaults .BinaryName ())
322
341
return fmt .Errorf ("decentralized install detected" )
323
342
}
343
+ prompt := ! c .Bool ("no-prompt" )
324
344
logrus .Infof ("Materializing binaries" )
325
345
if err := goods .Materialize (); err != nil {
326
346
return fmt .Errorf ("unable to materialize binaries: %w" , err )
@@ -330,11 +350,11 @@ var installCommand = &cli.Command{
330
350
var nodes []infra.Node
331
351
if dir := c .String ("infra" ); dir != "" {
332
352
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 {
334
354
return fmt .Errorf ("unable to create infra: %w" , err )
335
355
}
336
356
}
337
- if err := applyK0sctl (c , nodes ); err != nil {
357
+ if err := applyK0sctl (c , prompt , nodes ); err != nil {
338
358
return fmt .Errorf ("unable update cluster: %w" , err )
339
359
}
340
360
}
@@ -346,7 +366,7 @@ var installCommand = &cli.Command{
346
366
ccfg := defaults .PathToConfig ("k0sctl.yaml" )
347
367
kcfg := defaults .PathToConfig ("kubeconfig" )
348
368
os .Setenv ("KUBECONFIG" , kcfg )
349
- if applier , err := addons .NewApplier (); err != nil {
369
+ if applier , err := addons .NewApplier (prompt ); err != nil {
350
370
return fmt .Errorf ("unable to create applier: %w" , err )
351
371
} else if err := applier .Apply (c .Context ); err != nil {
352
372
return fmt .Errorf ("unable to apply addons: %w" , err )
0 commit comments