Skip to content

Commit

Permalink
add --force for kcctl delete cluster (#360)
Browse files Browse the repository at this point in the history
Signed-off-by: x893675 <x893675@icloud.com>

Signed-off-by: x893675 <x893675@icloud.com>
  • Loading branch information
x893675 committed Nov 10, 2022
1 parent a31132a commit 7ce88bf
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 22 deletions.
28 changes: 19 additions & 9 deletions pkg/apis/core/v1/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ import (
"github.com/kubeclipper/kubeclipper/pkg/utils/strutil"
)

var (
allowedDeleteStatus = sets.NewString(string(v1.ClusterInstallFailed), string(v1.ClusterRunning), string(v1.ClusterUpgradeFailed),
string(v1.ClusterBackupError), string(v1.ClusterRestoreFailed), string(v1.ClusterTerminateFailed))
)

type handler struct {
clusterOperator cluster.Operator
leaseOperator lease.Operator
Expand Down Expand Up @@ -320,14 +325,8 @@ func (h *handler) DeleteCluster(request *restful.Request, response *restful.Resp
restplus.HandleBadRequest(response, request, errors.New("can't delete cluster which belongs to provider"))
return
}

extraMeta, err := h.getClusterMetadata(request.Request.Context(), c)
if err != nil {
if apimachineryErrors.IsNotFound(err) || err == ErrNodesRegionDifferent {
restplus.HandleBadRequest(response, request, err)
return
}
restplus.HandleInternalError(response, request, err)
if !allowedDeleteStatus.Has(string(c.Status.Phase)) {
restplus.HandleBadRequest(response, request, fmt.Errorf("can't delete cluster when cluster is %s", c.Status.Phase))
return
}

Expand All @@ -339,9 +338,20 @@ func (h *handler) DeleteCluster(request *restful.Request, response *restful.Resp
return
}
if backups.TotalCount > 0 {
restplus.HandleInternalError(response, request, fmt.Errorf("before deleting the cluster, please delete the cluster backup file first"))
restplus.HandleBadRequest(response, request, fmt.Errorf("before deleting the cluster, please delete the cluster backup file first"))
return
}

extraMeta, err := h.getClusterMetadata(request.Request.Context(), c)
if err != nil {
if apimachineryErrors.IsNotFound(err) || err == ErrNodesRegionDifferent {
restplus.HandleBadRequest(response, request, err)
return
}
restplus.HandleInternalError(response, request, err)
return
}

extraMeta.OperationType = v1.OperationDeleteCluster
op, err := h.parseOperationFromCluster(extraMeta, c, v1.ActionUninstall)
if err != nil {
Expand Down
23 changes: 21 additions & 2 deletions pkg/cli/delete/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package delete
import (
"context"
"fmt"
"net/url"
"strings"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -63,6 +64,7 @@ type DeleteOptions struct {
BaseOptions
resource string
name string
force bool
}

var (
Expand All @@ -80,11 +82,15 @@ func NewCmdDelete(streams options.IOStreams) *cobra.Command {
Run: func(cmd *cobra.Command, args []string) {
utils.CheckErr(o.Complete(o.CliOpts))
utils.CheckErr(o.ValidateArgs(cmd, args))
if !o.precheck() {
return
}
utils.CheckErr(o.RunDelete())
},
ValidArgsFunction: ValidArgsFunction(o),
}

o.CliOpts.AddFlags(cmd.Flags())
cmd.Flags().BoolVarP(&o.force, "force", "F", o.force, "Force delete resource. Now is only support cluster.")
return cmd
}

Expand Down Expand Up @@ -142,7 +148,11 @@ func (l *DeleteOptions) RunDelete() error {
return err
}
case options.ResourceCluster:
err = l.Client.DeleteCluster(context.TODO(), l.name)
queryString := url.Values{}
if l.force {
queryString.Set(query.ParameterForce, "true")
}
err = l.Client.DeleteClusterWithQuery(context.TODO(), l.name, queryString)
if err != nil {
return err
}
Expand All @@ -152,6 +162,15 @@ func (l *DeleteOptions) RunDelete() error {
return nil
}

func (l *DeleteOptions) precheck() bool {
if l.force {
_, _ = l.IOStreams.Out.Write([]byte("Force delete cluster which is in used maybe cause data inconsistency." +
" Are you sure this cluster are not in used or your really want to force delete? Please input (yes/no)"))
return utils.AskForConfirmation()
}
return true
}

func ValidArgsFunction(o *DeleteOptions) func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
utils.CheckErr(o.Complete(o.CliOpts))
Expand Down
3 changes: 2 additions & 1 deletion pkg/cli/registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,15 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/kubeclipper/kubeclipper/pkg/simple/client/kc"
"net/http"
"path"
"path/filepath"
"strconv"
"strings"
"text/template"

"github.com/kubeclipper/kubeclipper/pkg/simple/client/kc"

pkgerr "github.com/pkg/errors"
"k8s.io/apimachinery/pkg/util/sets"

Expand Down
21 changes: 11 additions & 10 deletions pkg/clustermanage/kubeadm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,17 @@ import (
"strings"

"github.com/google/uuid"
"github.com/pkg/errors"
"gopkg.in/yaml.v2"
v13 "k8s.io/api/core/v1"
v14 "k8s.io/api/rbac/v1"
apimachineryErrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/selection"
"k8s.io/client-go/kubernetes"

"github.com/kubeclipper/kubeclipper/cmd/kcctl/app/options"
agentconfig "github.com/kubeclipper/kubeclipper/pkg/agent/config"
"github.com/kubeclipper/kubeclipper/pkg/cli/config"
Expand All @@ -22,16 +33,6 @@ import (
"github.com/kubeclipper/kubeclipper/pkg/scheme/core/v1/k8s"
"github.com/kubeclipper/kubeclipper/pkg/utils/sshutils"
tmplutil "github.com/kubeclipper/kubeclipper/pkg/utils/template"
"github.com/pkg/errors"
"gopkg.in/yaml.v2"
v13 "k8s.io/api/core/v1"
v14 "k8s.io/api/rbac/v1"
apimachineryErrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/selection"
"k8s.io/client-go/kubernetes"
)

func init() {
Expand Down
13 changes: 13 additions & 0 deletions pkg/simple/client/kc/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"context"
"encoding/json"
"fmt"
"net/url"

apimachineryversion "k8s.io/apimachinery/pkg/version"

Expand Down Expand Up @@ -248,6 +249,8 @@ func (cli *Client) DeleteRole(ctx context.Context, name string) error {
return nil
}

// DeleteCluster Delete Cluster with name
// Deprecated. Use DeleteClusterWithQuery instead
func (cli *Client) DeleteCluster(ctx context.Context, name string) error {
serverResp, err := cli.delete(ctx, fmt.Sprintf("%s/%s", clustersPath, name), nil, nil)
defer ensureReaderClosed(serverResp)
Expand All @@ -257,6 +260,16 @@ func (cli *Client) DeleteCluster(ctx context.Context, name string) error {
return nil
}

// DeleteClusterWithQuery Delete Cluster with name and custom query parameter
func (cli *Client) DeleteClusterWithQuery(ctx context.Context, name string, queryString url.Values) error {
serverResp, err := cli.delete(ctx, fmt.Sprintf("%s/%s", clustersPath, name), queryString, nil)
defer ensureReaderClosed(serverResp)
if err != nil {
return err
}
return nil
}

func (cli *Client) GetPlatformSetting(ctx context.Context) (*v1.DockerRegistry, error) {
serverResp, err := cli.get(ctx, platformPath, nil, nil)
defer ensureReaderClosed(serverResp)
Expand Down

0 comments on commit 7ce88bf

Please sign in to comment.