Skip to content

Commit

Permalink
Fix missing confirmation for reset
Browse files Browse the repository at this point in the history
The reset command is considered dangerous,
because it can destroy the whole cluster, hence
we should ask for confirmation first.

Signed-off-by: Christian Rebischke <chris@shibumi.dev>
  • Loading branch information
shibumi committed Feb 21, 2021
1 parent 1683388 commit 4042b18
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion pkg/cmd/reset.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,19 @@ limitations under the License.
package cmd

import (
"fmt"

"github.com/MakeNowJust/heredoc/v2"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/pflag"

"k8c.io/kubeone/pkg/state"
"k8c.io/kubeone/pkg/tasks"
)

type resetOpts struct {
globalOptions
AutoApprove bool `longflag:"auto-approve" shortflag:"y"`
DestroyWorkers bool `longflag:"destroy-workers"`
RemoveBinaries bool `longflag:"remove-binaries"`
}
Expand Down Expand Up @@ -68,6 +70,13 @@ func resetCmd(rootFlags *pflag.FlagSet) *cobra.Command {
},
}

cmd.Flags().BoolVarP(
&opts.AutoApprove,
longFlagName(opts, "AutoApprove"),
shortFlagName(opts, "AutoApprove"),
false,
"auto approve reset")

cmd.Flags().BoolVar(
&opts.DestroyWorkers,
longFlagName(opts, "DestroyWorkers"),
Expand All @@ -90,5 +99,23 @@ func runReset(opts *resetOpts) error {
return errors.Wrap(err, "failed to initialize State")
}

fmt.Println("The following nodes will be resetted: ")
for _, node := range s.Cluster.ControlPlane.Hosts {
fmt.Printf("\t+ reset control plane node %q (%s)\n", node.Hostname, node.PrivateAddress)
}
for _, node := range s.Cluster.StaticWorkers.Hosts {
fmt.Printf("\t+ reset static worker nodes %q (%s)\n", node.Hostname, node.PrivateAddress)
}

confirm, err := confirmApply(opts.AutoApprove)
if err != nil {
return err
}

if !confirm {
s.Logger.Println("Operation canceled.")
return nil
}

return errors.Wrap(tasks.WithReset(nil).Run(s), "failed to reset the cluster")
}

0 comments on commit 4042b18

Please sign in to comment.