mirror of
https://github.com/labring/sealos.git
synced 2026-09-01 15:38:06 +08:00
feature(main): fix confirm bug (#1008)
This commit is contained in:
@@ -17,10 +17,11 @@ package cmd
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/labring/sealos/pkg/apply/processor"
|
||||
|
||||
"github.com/labring/sealos/pkg/utils/logger"
|
||||
|
||||
"github.com/labring/sealos/pkg/apply"
|
||||
"github.com/labring/sealos/pkg/runtime"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
@@ -36,6 +37,9 @@ delete to default cluster:
|
||||
sealos delete --masters x.x.x.x-x.x.x.y --nodes x.x.x.x-x.x.x.y
|
||||
`,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
if err := processor.ConfirmDeleteNodes(); err != nil {
|
||||
return err
|
||||
}
|
||||
applier, err := apply.NewScaleApplierFromArgs(deleteArgs, "delete")
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -56,7 +60,7 @@ delete to default cluster:
|
||||
deleteCmd.Flags().StringVarP(&deleteArgs.Masters, "masters", "m", "", "reduce Count or IPList to masters")
|
||||
deleteCmd.Flags().StringVarP(&deleteArgs.Nodes, "nodes", "n", "", "reduce Count or IPList to nodes")
|
||||
deleteCmd.Flags().StringVarP(&deleteArgs.ClusterName, "cluster", "c", "default", "delete a kubernetes cluster with cluster name")
|
||||
deleteCmd.Flags().BoolVar(&runtime.ForceDelete, "force", false, "We also can input an --force flag to delete cluster by force")
|
||||
deleteCmd.Flags().BoolVar(&processor.ForceDelete, "force", false, "We also can input an --force flag to delete cluster by force")
|
||||
return deleteCmd
|
||||
}
|
||||
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"github.com/labring/sealos/pkg/runtime"
|
||||
"github.com/labring/sealos/pkg/apply/processor"
|
||||
"github.com/labring/sealos/pkg/utils/contants"
|
||||
|
||||
"github.com/labring/sealos/pkg/apply"
|
||||
@@ -39,6 +39,9 @@ func newResetCmd() *cobra.Command {
|
||||
Example: exampleReset,
|
||||
Args: cobra.NoArgs,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
if err := processor.ConfirmDeleteNodes(); err != nil {
|
||||
return err
|
||||
}
|
||||
filePath := contants.Clusterfile(resetClusterName)
|
||||
applier, err := apply.NewApplierFromFile(filePath)
|
||||
if err != nil {
|
||||
@@ -57,5 +60,5 @@ func init() {
|
||||
resetCmd := newResetCmd()
|
||||
rootCmd.AddCommand(resetCmd)
|
||||
resetCmd.Flags().StringVarP(&resetClusterName, "cluster", "c", "default", "reset kubernetes cluster with cluster name")
|
||||
resetCmd.Flags().BoolVar(&runtime.ForceDelete, "force", false, "we also can input an --force flag to reset cluster by force")
|
||||
resetCmd.Flags().BoolVar(&processor.ForceDelete, "force", false, "we also can input an --force flag to reset cluster by force")
|
||||
}
|
||||
|
||||
@@ -32,6 +32,8 @@ import (
|
||||
fileutil "github.com/labring/sealos/pkg/utils/file"
|
||||
)
|
||||
|
||||
var ForceDelete bool
|
||||
|
||||
type DeleteProcessor struct {
|
||||
ClusterManager types.ClusterService
|
||||
ImageManager types.Service
|
||||
|
||||
@@ -54,10 +54,13 @@ func (c *InstallProcessor) ConfirmOverrideApps(cluster *v2.Cluster) error {
|
||||
if ForceOverride {
|
||||
prompt := "are you sure to override these app?"
|
||||
cancel := "you have canceled to override these apps !"
|
||||
_, err := confirm.Confirm(prompt, cancel)
|
||||
pass, err := confirm.Confirm(prompt, cancel)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !pass {
|
||||
return errors.New(cancel)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -17,10 +17,12 @@ package processor
|
||||
import (
|
||||
"github.com/labring/sealos/pkg/image/types"
|
||||
v2 "github.com/labring/sealos/pkg/types/v1beta1"
|
||||
"github.com/labring/sealos/pkg/utils/confirm"
|
||||
"github.com/labring/sealos/pkg/utils/contants"
|
||||
"github.com/labring/sealos/pkg/utils/logger"
|
||||
"github.com/labring/sealos/pkg/utils/maps"
|
||||
"github.com/labring/sealos/pkg/utils/strings"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
type Interface interface {
|
||||
@@ -84,3 +86,16 @@ func OCIToImageMount(mount *v2.MountImage, imgService types.Service) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func ConfirmDeleteNodes() error {
|
||||
if !ForceDelete {
|
||||
prompt := "are you sure to delete these nodes?"
|
||||
cancel := "you have canceled to delete these nodes !"
|
||||
if pass, err := confirm.Confirm(prompt, cancel); err != nil {
|
||||
return err
|
||||
} else if !pass {
|
||||
return errors.New(cancel)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -64,9 +64,6 @@ type Interface interface {
|
||||
|
||||
func (k *KubeadmRuntime) Reset() error {
|
||||
logger.Info("start to delete Cluster: master %s, node %s", k.getMasterIPList(), k.getNodeIPList())
|
||||
if err := k.confirmDeleteNodes(); err != nil {
|
||||
return err
|
||||
}
|
||||
return k.reset()
|
||||
}
|
||||
|
||||
@@ -82,9 +79,6 @@ func (k *KubeadmRuntime) JoinNodes(newNodesIPList []string) error {
|
||||
func (k *KubeadmRuntime) DeleteNodes(nodesIPList []string) error {
|
||||
if len(nodesIPList) != 0 {
|
||||
logger.Info("worker %s will be deleted", nodesIPList)
|
||||
if err := k.confirmDeleteNodes(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return k.deleteNodes(nodesIPList)
|
||||
}
|
||||
@@ -99,9 +93,6 @@ func (k *KubeadmRuntime) JoinMasters(newMastersIPList []string) error {
|
||||
func (k *KubeadmRuntime) DeleteMasters(mastersIPList []string) error {
|
||||
if len(mastersIPList) != 0 {
|
||||
logger.Info("master %s will be deleted", mastersIPList)
|
||||
if err := k.confirmDeleteNodes(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return k.deleteMasters(mastersIPList)
|
||||
}
|
||||
|
||||
@@ -18,11 +18,9 @@ package runtime
|
||||
|
||||
import (
|
||||
"context"
|
||||
e "errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/labring/sealos/pkg/client-go/kubernetes"
|
||||
"github.com/labring/sealos/pkg/utils/confirm"
|
||||
"github.com/labring/sealos/pkg/utils/contants"
|
||||
"github.com/labring/sealos/pkg/utils/logger"
|
||||
"golang.org/x/sync/errgroup"
|
||||
@@ -31,26 +29,11 @@ import (
|
||||
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
)
|
||||
|
||||
var ForceDelete bool
|
||||
|
||||
const (
|
||||
KUBECONTROLLERCONFIGFILE = "/etc/kubernetes/controller-manager.conf"
|
||||
KUBESCHEDULERCONFIGFILE = "/etc/kubernetes/scheduler.conf"
|
||||
)
|
||||
|
||||
func (k *KubeadmRuntime) confirmDeleteNodes() error {
|
||||
if !ForceDelete {
|
||||
prompt := "are you sure to delete these nodes?"
|
||||
cancel := "you have canceled to delete these nodes !"
|
||||
if pass, err := confirm.Confirm(prompt, cancel); err != nil {
|
||||
return err
|
||||
} else if !pass {
|
||||
return e.New(cancel)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (k *KubeadmRuntime) pipeline(name string, pipeline []func() error) error {
|
||||
for _, f := range pipeline {
|
||||
if err := f(); err != nil {
|
||||
|
||||
@@ -124,18 +124,19 @@ func (c *Cluster) FindImage(targetImage string) *MountImage {
|
||||
return image
|
||||
}
|
||||
func (c *Cluster) SetMountImage(targetMount *MountImage) {
|
||||
tgMount := targetMount.DeepCopy()
|
||||
if c.Status.Mounts != nil {
|
||||
if targetMount != nil {
|
||||
if tgMount != nil {
|
||||
hasMount := false
|
||||
for i, img := range c.Status.Mounts {
|
||||
if img.Name == targetMount.Name && img.Type == targetMount.Type {
|
||||
c.Status.Mounts[i] = *targetMount
|
||||
if img.Name == tgMount.Name && img.Type == tgMount.Type {
|
||||
c.Status.Mounts[i] = *tgMount
|
||||
hasMount = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !hasMount {
|
||||
c.Status.Mounts = append(c.Status.Mounts, *targetMount)
|
||||
c.Status.Mounts = append(c.Status.Mounts, *tgMount)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user