mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-20 10:20:51 +00:00
kubeadm: De-dup the confirmation on the interactive cmds
Signed-off-by: Dave Chen <dave.chen@arm.com>
This commit is contained in:
parent
e8eb7a5e19
commit
8aeee521e6
@ -17,15 +17,14 @@ limitations under the License.
|
|||||||
package phases
|
package phases
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"k8s.io/klog/v2"
|
"k8s.io/klog/v2"
|
||||||
|
|
||||||
"k8s.io/kubernetes/cmd/kubeadm/app/cmd/options"
|
"k8s.io/kubernetes/cmd/kubeadm/app/cmd/options"
|
||||||
"k8s.io/kubernetes/cmd/kubeadm/app/cmd/phases/workflow"
|
"k8s.io/kubernetes/cmd/kubeadm/app/cmd/phases/workflow"
|
||||||
|
"k8s.io/kubernetes/cmd/kubeadm/app/cmd/util"
|
||||||
"k8s.io/kubernetes/cmd/kubeadm/app/preflight"
|
"k8s.io/kubernetes/cmd/kubeadm/app/preflight"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -53,15 +52,7 @@ func runPreflight(c workflow.RunData) error {
|
|||||||
|
|
||||||
if !r.ForceReset() && !r.DryRun() {
|
if !r.ForceReset() && !r.DryRun() {
|
||||||
klog.Warning("[reset] WARNING: Changes made to this host by 'kubeadm init' or 'kubeadm join' will be reverted.")
|
klog.Warning("[reset] WARNING: Changes made to this host by 'kubeadm init' or 'kubeadm join' will be reverted.")
|
||||||
fmt.Print("[reset] Are you sure you want to proceed? [y/N]: ")
|
return util.InteractivelyConfirmAction("reset", "Are you sure you want to proceed?", r.InputReader())
|
||||||
s := bufio.NewScanner(r.InputReader())
|
|
||||||
s.Scan()
|
|
||||||
if err := s.Err(); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if strings.ToLower(s.Text()) != "y" {
|
|
||||||
return errors.New("aborted reset operation")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println("[preflight] Running pre-flight checks")
|
fmt.Println("[preflight] Running pre-flight checks")
|
||||||
|
@ -18,6 +18,7 @@ package upgrade
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
@ -30,6 +31,7 @@ import (
|
|||||||
|
|
||||||
kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
|
kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
|
||||||
"k8s.io/kubernetes/cmd/kubeadm/app/cmd/options"
|
"k8s.io/kubernetes/cmd/kubeadm/app/cmd/options"
|
||||||
|
cmdutil "k8s.io/kubernetes/cmd/kubeadm/app/cmd/util"
|
||||||
kubeadmconstants "k8s.io/kubernetes/cmd/kubeadm/app/constants"
|
kubeadmconstants "k8s.io/kubernetes/cmd/kubeadm/app/constants"
|
||||||
"k8s.io/kubernetes/cmd/kubeadm/app/features"
|
"k8s.io/kubernetes/cmd/kubeadm/app/features"
|
||||||
"k8s.io/kubernetes/cmd/kubeadm/app/phases/upgrade"
|
"k8s.io/kubernetes/cmd/kubeadm/app/phases/upgrade"
|
||||||
@ -133,7 +135,7 @@ func runApply(flags *applyFlags, args []string) error {
|
|||||||
|
|
||||||
// If the current session is interactive, ask the user whether they really want to upgrade.
|
// If the current session is interactive, ask the user whether they really want to upgrade.
|
||||||
if flags.sessionIsInteractive() {
|
if flags.sessionIsInteractive() {
|
||||||
if err := InteractivelyConfirmUpgrade("Are you sure you want to proceed with the upgrade?"); err != nil {
|
if err := cmdutil.InteractivelyConfirmAction("upgrade", "Are you sure you want to proceed?", os.Stdin); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -19,7 +19,6 @@ package upgrade
|
|||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
@ -321,21 +320,3 @@ func getWaiter(dryRun bool, client clientset.Interface, timeout time.Duration) a
|
|||||||
}
|
}
|
||||||
return apiclient.NewKubeWaiter(client, timeout, os.Stdout)
|
return apiclient.NewKubeWaiter(client, timeout, os.Stdout)
|
||||||
}
|
}
|
||||||
|
|
||||||
// InteractivelyConfirmUpgrade asks the user whether they _really_ want to upgrade.
|
|
||||||
func InteractivelyConfirmUpgrade(question string) error {
|
|
||||||
|
|
||||||
fmt.Printf("[upgrade/confirm] %s [y/N]: ", question)
|
|
||||||
|
|
||||||
scanner := bufio.NewScanner(os.Stdin)
|
|
||||||
scanner.Scan()
|
|
||||||
if err := scanner.Err(); err != nil {
|
|
||||||
return errors.Wrap(err, "couldn't read from standard input")
|
|
||||||
}
|
|
||||||
answer := scanner.Text()
|
|
||||||
if strings.ToLower(answer) == "y" || strings.ToLower(answer) == "yes" {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return errors.New("won't proceed; the user didn't answer (Y|y) in order to continue")
|
|
||||||
}
|
|
||||||
|
@ -17,6 +17,11 @@ limitations under the License.
|
|||||||
package util
|
package util
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"github.com/spf13/pflag"
|
"github.com/spf13/pflag"
|
||||||
@ -100,3 +105,19 @@ func DefaultInitConfiguration() *kubeadmapiv1.InitConfiguration {
|
|||||||
}
|
}
|
||||||
return initCfg
|
return initCfg
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// InteractivelyConfirmAction asks the user whether they _really_ want to take the action.
|
||||||
|
func InteractivelyConfirmAction(action, question string, r io.Reader) error {
|
||||||
|
fmt.Printf("[%s] %s [y/N]: ", action, question)
|
||||||
|
scanner := bufio.NewScanner(r)
|
||||||
|
scanner.Scan()
|
||||||
|
if err := scanner.Err(); err != nil {
|
||||||
|
return errors.Wrap(err, "couldn't read from standard input")
|
||||||
|
}
|
||||||
|
answer := scanner.Text()
|
||||||
|
if strings.ToLower(answer) == "y" || strings.ToLower(answer) == "yes" {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return errors.New("won't proceed; the user didn't answer (Y|y) in order to continue")
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user