Merge pull request #3266 from my-git9/addons-confirm

feat: add flags of force for addon disable
This commit is contained in:
karmada-bot 2023-03-20 20:32:24 +08:00 committed by GitHub
commit 0bbf03d323
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 39 additions and 23 deletions

View File

@ -57,6 +57,8 @@ func NewCmdAddonsDisable(parentCommand string) *cobra.Command {
return nil
},
}
opts.GlobalCommandOptions.AddFlags(cmd.PersistentFlags())
flags := cmd.PersistentFlags()
opts.GlobalCommandOptions.AddFlags(flags)
flags.BoolVarP(&opts.Force, "force", "f", false, "Disable addons without prompting for confirmation.")
return cmd
}

View File

@ -7,6 +7,7 @@ import (
"k8s.io/klog/v2"
"k8s.io/utils/strings/slices"
"github.com/karmada-io/karmada/pkg/karmadactl/util"
"github.com/karmada-io/karmada/pkg/karmadactl/util/apiclient"
)
@ -15,6 +16,8 @@ type CommandAddonsDisableOption struct {
GlobalCommandOptions
KarmadaKubeClientSet *kubernetes.Clientset
Force bool
}
// Complete the conditions required to be able to run disable.
@ -47,6 +50,11 @@ func (o *CommandAddonsDisableOption) Validate(args []string) error {
// Run start disable Karmada addons
func (o *CommandAddonsDisableOption) Run(args []string) error {
fmt.Printf("Disable Karmada addon %s\n", args)
if !o.Force && !util.DeleteConfirmation() {
return nil
}
var disableAddons = map[string]*Addon{}
// collect disabled addons

View File

@ -3,7 +3,6 @@ package deinit
import (
"context"
"fmt"
"os"
"strings"
"github.com/spf13/cobra"
@ -224,31 +223,11 @@ func removeLabels(node *corev1.Node, removesLabel string) {
}
}
// deleteConfirmation delete karmada confirmation
func deleteConfirmation() bool {
fmt.Print("Please type (y)es or (n)o and then press enter:")
var response string
_, err := fmt.Scanln(&response)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
switch strings.ToLower(response) {
case "y", "yes":
return true
case "n", "no":
return false
default:
return deleteConfirmation()
}
}
// Run start delete
func (o *CommandDeInitOption) Run() error {
fmt.Println("removes Karmada from Kubernetes")
// delete confirmation,exit the delete action when false.
if !o.Force && !deleteConfirmation() {
if !o.Force && !util.DeleteConfirmation() {
return nil
}

View File

@ -0,0 +1,27 @@
package util
import (
"fmt"
"os"
"strings"
)
// DeleteConfirmation delete karmada resource confirmation
func DeleteConfirmation() bool {
fmt.Print("Please type (y)es or (n)o and then press enter:")
var response string
_, err := fmt.Scanln(&response)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
switch strings.ToLower(response) {
case "y", "yes":
return true
case "n", "no":
return false
default:
return DeleteConfirmation()
}
}