@@ -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 )
0 commit comments