Merge pull request #1878 from lonelyCZ/pr-optimize-deinit
Optimize deinit according former comments
This commit is contained in:
commit
9b8fec2537
|
@ -4,7 +4,6 @@ import (
|
|||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
|
@ -13,20 +12,23 @@ import (
|
|||
"k8s.io/client-go/kubernetes"
|
||||
|
||||
"github.com/karmada-io/karmada/pkg/karmadactl/cmdinit/utils"
|
||||
"github.com/karmada-io/karmada/pkg/karmadactl/options"
|
||||
)
|
||||
|
||||
// LabelSelector karmada bootstrapping label
|
||||
const (
|
||||
LabelSelector = "karmada.io/bootstrapping"
|
||||
karmadaNodeLabel = "karmada.io/etcd"
|
||||
karmadaBootstrappingLabelKey = "karmada.io/bootstrapping"
|
||||
karmadaNodeLabel = "karmada.io/etcd"
|
||||
)
|
||||
|
||||
// CommandDeInitOption options for deinit.
|
||||
type CommandDeInitOption struct {
|
||||
options.GlobalCommandOptions
|
||||
Namespace string
|
||||
Context string
|
||||
// KubeConfig holds host cluster KUBECONFIG file path.
|
||||
KubeConfig string
|
||||
Context string
|
||||
Namespace string
|
||||
|
||||
// DryRun tells if run the command in dry-run mode, without making any server requests.
|
||||
DryRun bool
|
||||
|
||||
KubeClientSet *kubernetes.Clientset
|
||||
}
|
||||
|
||||
|
@ -49,26 +51,31 @@ func NewCmdDeInit(parentCommand string) *cobra.Command {
|
|||
return nil
|
||||
},
|
||||
}
|
||||
opts.GlobalCommandOptions.AddFlags(cmd.Flags())
|
||||
|
||||
flags := cmd.PersistentFlags()
|
||||
flags.StringVarP(&opts.Namespace, "namespace", "n", "karmada-system", "namespace where Karmada components are installed.")
|
||||
flags.StringVar(&opts.KubeConfig, "kubeconfig", "", "Path to the host cluster kubeconfig file.")
|
||||
flags.StringVar(&opts.Context, "context", "", "The name of the kubeconfig context to use")
|
||||
flags.BoolVar(&opts.DryRun, "dry-run", false, "Run the command in dry-run mode, without making any server requests.")
|
||||
return cmd
|
||||
}
|
||||
|
||||
func deInitExample(parentCommand string) string {
|
||||
example := fmt.Sprintf(`
|
||||
# Remove Karmada from the Kubernetes cluster.
|
||||
%s deinit
|
||||
`, parentCommand)
|
||||
|
||||
example := `
|
||||
# Remove Karmada from the Kubernetes cluster` + "\n" +
|
||||
fmt.Sprintf("%s deinit", parentCommand)
|
||||
return example
|
||||
}
|
||||
|
||||
// Complete the conditions required to be able to run deinit.
|
||||
func (o *CommandDeInitOption) Complete() error {
|
||||
if o.KubeConfig == "" {
|
||||
o.KubeConfig = filepath.Join(os.Getenv("HOME"), ".kube/config")
|
||||
env := os.Getenv("KUBECONFIG")
|
||||
if env != "" {
|
||||
o.KubeConfig = env
|
||||
} else {
|
||||
o.KubeConfig = defaultKubeConfig
|
||||
}
|
||||
}
|
||||
|
||||
if !Exists(o.KubeConfig) {
|
||||
|
@ -98,9 +105,9 @@ func (o *CommandDeInitOption) delete() error {
|
|||
return err
|
||||
}
|
||||
|
||||
// Delete Service by label LabelSelector
|
||||
// Delete Service by karmadaBootstrappingLabelKey
|
||||
serviceClient := o.KubeClientSet.CoreV1().Services(o.Namespace)
|
||||
services, err := serviceClient.List(context.TODO(), metav1.ListOptions{LabelSelector: LabelSelector})
|
||||
services, err := serviceClient.List(context.TODO(), metav1.ListOptions{LabelSelector: karmadaBootstrappingLabelKey})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -115,9 +122,9 @@ func (o *CommandDeInitOption) delete() error {
|
|||
}
|
||||
}
|
||||
|
||||
// Delete Secret by label LabelSelector
|
||||
// Delete Secret by karmadaBootstrappingLabelKey
|
||||
secretClient := o.KubeClientSet.CoreV1().Secrets(o.Namespace)
|
||||
secrets, err := secretClient.List(context.TODO(), metav1.ListOptions{LabelSelector: LabelSelector})
|
||||
secrets, err := secretClient.List(context.TODO(), metav1.ListOptions{LabelSelector: karmadaBootstrappingLabelKey})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -131,29 +138,61 @@ func (o *CommandDeInitOption) delete() error {
|
|||
}
|
||||
}
|
||||
|
||||
// Delete ClusterRole by label LabelSelector
|
||||
clusterRoleClient := o.KubeClientSet.RbacV1().ClusterRoles()
|
||||
clusterRoles, err := clusterRoleClient.List(context.TODO(), metav1.ListOptions{LabelSelector: LabelSelector})
|
||||
if err != nil {
|
||||
if err = o.deleteRBAC(); err != nil {
|
||||
return err
|
||||
}
|
||||
for _, service := range clusterRoles.Items {
|
||||
fmt.Printf("delete ClusterRole %q\n", service.Name)
|
||||
if o.DryRun {
|
||||
continue
|
||||
}
|
||||
if err := clusterRoleClient.Delete(context.TODO(), service.Name, metav1.DeleteOptions{}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Delete namespace where Karmada components are installed
|
||||
fmt.Printf("delete Namespace %q\n", o.Namespace)
|
||||
if o.DryRun {
|
||||
return nil
|
||||
}
|
||||
if err = o.KubeClientSet.CoreV1().Namespaces().Delete(context.Background(), o.Namespace, metav1.DeleteOptions{}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (o *CommandDeInitOption) deleteRBAC() error {
|
||||
// Delete ClusterRole by karmadaBootstrappingLabelKey
|
||||
clusterRoleClient := o.KubeClientSet.RbacV1().ClusterRoles()
|
||||
clusterRoles, err := clusterRoleClient.List(context.TODO(), metav1.ListOptions{LabelSelector: karmadaBootstrappingLabelKey})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, clusterRole := range clusterRoles.Items {
|
||||
fmt.Printf("delete ClusterRole %q\n", clusterRole.Name)
|
||||
if o.DryRun {
|
||||
continue
|
||||
}
|
||||
if err := clusterRoleClient.Delete(context.TODO(), clusterRole.Name, metav1.DeleteOptions{}); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// Delete ClusterRoleBinding by karmadaBootstrappingLabelKey
|
||||
clusterRoleBindingClient := o.KubeClientSet.RbacV1().ClusterRoleBindings()
|
||||
clusterRoleBindings, err := clusterRoleBindingClient.List(context.TODO(), metav1.ListOptions{LabelSelector: karmadaBootstrappingLabelKey})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, clusterRoleBinding := range clusterRoleBindings.Items {
|
||||
fmt.Printf("delete ClusterRoleBinding %q\n", clusterRoleBinding.Name)
|
||||
if o.DryRun {
|
||||
continue
|
||||
}
|
||||
if err := clusterRoleBindingClient.Delete(context.TODO(), clusterRoleBinding.Name, metav1.DeleteOptions{}); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (o *CommandDeInitOption) deleteWorkload() error {
|
||||
// Delete deployment by label LabelSelector
|
||||
// Delete deployment by karmadaBootstrappingLabelKey
|
||||
deploymentClient := o.KubeClientSet.AppsV1().Deployments(o.Namespace)
|
||||
deployments, err := deploymentClient.List(context.TODO(), metav1.ListOptions{LabelSelector: LabelSelector})
|
||||
deployments, err := deploymentClient.List(context.TODO(), metav1.ListOptions{LabelSelector: karmadaBootstrappingLabelKey})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -169,7 +208,7 @@ func (o *CommandDeInitOption) deleteWorkload() error {
|
|||
|
||||
// Delete StatefulSet by label LabelSelector
|
||||
statefulSetClient := o.KubeClientSet.AppsV1().StatefulSets(o.Namespace)
|
||||
statefulSets, err := statefulSetClient.List(context.TODO(), metav1.ListOptions{LabelSelector: LabelSelector})
|
||||
statefulSets, err := statefulSetClient.List(context.TODO(), metav1.ListOptions{LabelSelector: karmadaBootstrappingLabelKey})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -255,7 +294,7 @@ func (o *CommandDeInitOption) Run() error {
|
|||
return err
|
||||
}
|
||||
|
||||
fmt.Println("remove Karmada from Kubernetes successfully." +
|
||||
fmt.Println("remove Karmada from Kubernetes successfully.\n" +
|
||||
"\ndeinit will not delete etcd data, if the etcd data is persistent, please delete it yourself.")
|
||||
return nil
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue