This is an automated email from the ASF dual-hosted git repository.

zhongxjian pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/dubbo-kubernetes.git


The following commit(s) were added to refs/heads/master by this push:
     new d86545f4 [operator] Supplemental uninstallation logic
d86545f4 is described below

commit d86545f4f1d28bc4520eb55850e66778d09ef9f8
Author: mfordjody <[email protected]>
AuthorDate: Mon Dec 23 13:44:57 2024 +0800

    [operator] Supplemental uninstallation logic
---
 operator/cmd/cluster/uninstall.go      | 31 ++++++++++++++++++++++---------
 operator/pkg/config/protogvk.go        |  8 ++++++++
 operator/pkg/uninstall/uninstaller.go  | 30 +++++++++++++++++++++++++++++-
 pkg/config/schema/gvk/resources.gen.go |  7 +++++++
 4 files changed, 66 insertions(+), 10 deletions(-)

diff --git a/operator/cmd/cluster/uninstall.go 
b/operator/cmd/cluster/uninstall.go
index c405e89e..c8b60ede 100644
--- a/operator/cmd/cluster/uninstall.go
+++ b/operator/cmd/cluster/uninstall.go
@@ -30,15 +30,18 @@ func addUninstallFlags(cmd *cobra.Command, args 
*uninstallArgs) {
                "The filename of the DubboOperator CR.")
        cmd.PersistentFlags().StringArrayVarP(&args.sets, "set", "s", nil, 
"Override dubboOperator values, such as selecting profiles, etc")
        cmd.PersistentFlags().BoolVar(&args.purge, "purge", false, "Remove all 
dubbo-related source code")
+       cmd.PersistentFlags().BoolVarP(&args.skipConfirmation, 
"skip-confirmation", "y", false, `The skipConfirmation determines whether the 
user is prompted for confirmation.`)
 }
 
 func UninstallCmd(ctx cli.Context) *cobra.Command {
        rootArgs := &RootArgs{}
        uiArgs := &uninstallArgs{}
        uicmd := &cobra.Command{
-               Use:   "uninstall",
-               Short: "Uninstall Dubbo-related resources",
-               Long:  "This uninstall command will uninstall the dubbo 
cluster",
+               Use:           "uninstall",
+               Short:         "Uninstall Dubbo-related resources",
+               Long:          "This uninstall command will uninstall the dubbo 
cluster",
+               SilenceUsage:  true,
+               SilenceErrors: false,
                Example: ` # Uninstall a single control plane by dop file
   dubboctl uninstall -f dop.yaml
   
@@ -54,7 +57,7 @@ func UninstallCmd(ctx cli.Context) *cobra.Command {
                        return nil
                },
                RunE: func(cmd *cobra.Command, args []string) error {
-                       return UnInstall(cmd, ctx, rootArgs, uiArgs)
+                       return Uninstall(cmd, ctx, rootArgs, uiArgs)
                },
        }
        addFlags(uicmd, rootArgs)
@@ -62,7 +65,7 @@ func UninstallCmd(ctx cli.Context) *cobra.Command {
        return uicmd
 }
 
-func UnInstall(cmd *cobra.Command, ctx cli.Context, rootArgs *RootArgs, uiArgs 
*uninstallArgs) error {
+func Uninstall(cmd *cobra.Command, ctx cli.Context, rootArgs *RootArgs, uiArgs 
*uninstallArgs) error {
        cl := clog.NewConsoleLogger(cmd.OutOrStdout(), cmd.ErrOrStderr(), 
installerScope)
        var kubeClient kube.CLIClient
        var err error
@@ -75,40 +78,50 @@ func UnInstall(cmd *cobra.Command, ctx cli.Context, 
rootArgs *RootArgs, uiArgs *
        if uiArgs.purge && uiArgs.files != "" {
                cl.LogAndPrint(PurgeWithRevisionOrOperatorSpecifiedWarning)
        }
+
        setFlags := applyFlagAliases(uiArgs.sets, uiArgs.manifestPath)
+
        files := []string{}
        if uiArgs.files != "" {
                files = append(files, uiArgs.files)
        }
+
        vals, err := render.MergeInputs(files, setFlags)
        if err != nil {
                return err
        }
+
        objectsList, err := uninstall.GetPrunedResources(
+               kubeClient,
                vals.GetPathString("metadata.name"),
                vals.GetPathString("metadata.namespace"),
+               uiArgs.purge,
        )
        if err != nil {
                return err
        }
+
        preCheck(cmd, uiArgs, cl, rootArgs.DryRun)
+
        if err := uninstall.DeleteObjectsList(kubeClient, rootArgs.DryRun, cl, 
objectsList); err != nil {
                return fmt.Errorf("failed to delete control plane resources by 
revision: %v", err)
        }
+
        pl.SetState(progress.StateUninstallComplete)
+
        return nil
 }
 
 func preCheck(cmd *cobra.Command, uiArgs *uninstallArgs, cl 
*clog.ConsoleLogger, dryRun bool) {
        needConfirmation, message := false, ""
-       if dryRun || uiArgs.skipConfirmation {
-               cl.LogAndPrint(message)
-               return
-       }
        if uiArgs.purge {
                needConfirmation = true
                message += AllResourcesRemovedWarning
        }
+       if dryRun || uiArgs.skipConfirmation {
+               cl.LogAndPrint(message)
+               return
+       }
        message += "Proceed? (y/N)"
        if needConfirmation && !OptionDeterminate(message, cmd.OutOrStdout()) {
                cmd.Print("Canceled Completed.\n")
diff --git a/operator/pkg/config/protogvk.go b/operator/pkg/config/protogvk.go
index 40c92b43..a45680a0 100644
--- a/operator/pkg/config/protogvk.go
+++ b/operator/pkg/config/protogvk.go
@@ -27,6 +27,14 @@ func (g GroupVersionKind) CanonicalGroup() string {
        return CanoncalGroup(g.Group)
 }
 
+func (g GroupVersionKind) K8s() schema.GroupVersionKind {
+       return schema.GroupVersionKind{
+               Group:   g.Group,
+               Version: g.Version,
+               Kind:    g.Kind,
+       }
+}
+
 func CanoncalGroup(group string) string {
        if group != "" {
                return group
diff --git a/operator/pkg/uninstall/uninstaller.go 
b/operator/pkg/uninstall/uninstaller.go
index 050b803f..629e5011 100644
--- a/operator/pkg/uninstall/uninstaller.go
+++ b/operator/pkg/uninstall/uninstaller.go
@@ -6,14 +6,21 @@ import (
        "github.com/apache/dubbo-kubernetes/operator/pkg/manifest"
        "github.com/apache/dubbo-kubernetes/operator/pkg/util"
        "github.com/apache/dubbo-kubernetes/operator/pkg/util/clog"
+       "github.com/apache/dubbo-kubernetes/pkg/config/schema/gvk"
        "github.com/apache/dubbo-kubernetes/pkg/kube"
        "github.com/apache/dubbo-kubernetes/pkg/pointer"
        kerrors "k8s.io/apimachinery/pkg/api/errors"
        metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
        "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
+       "k8s.io/apimachinery/pkg/runtime/schema"
 )
 
-func GetPrunedResources(dopName, dopNamespace string) 
([]*unstructured.UnstructuredList, error) {
+var (
+       ClusterResources    = []schema.GroupVersionKind{}
+       AllClusterResources = append(ClusterResources, 
gvk.CustomResourceDefinition.K8s())
+)
+
+func GetPrunedResources(clt kube.CLIClient, dopName, dopNamespace string, 
includeClusterResources bool) ([]*unstructured.UnstructuredList, error) {
        var usList []*unstructured.UnstructuredList
        labels := make(map[string]string)
        if dopName != "" {
@@ -22,9 +29,30 @@ func GetPrunedResources(dopName, dopNamespace string) 
([]*unstructured.Unstructu
        if dopNamespace != "" {
                labels[manifest.OwningResourceNamespace] = dopNamespace
        }
+       resources := NamespacedResources()
+       gvkList := append(resources, AllClusterResources...)
+       for _, gvks := range gvkList {
+               var result *unstructured.UnstructuredList
+               c, err := clt.DynamicClientFor(gvks, nil, "")
+               if err != nil {
+                       return nil, err
+               }
+               if includeClusterResources {
+                       result, err = c.List(context.Background(), 
metav1.ListOptions{})
+               }
+               if result == nil || len(result.Items) == 0 {
+                       continue
+               }
+               usList = append(usList, result)
+       }
        return usList, nil
 }
 
+func NamespacedResources() []schema.GroupVersionKind {
+       var res []schema.GroupVersionKind
+       return res
+}
+
 func DeleteObjectsList(c kube.CLIClient, dryRun bool, log clog.Logger, 
objectsList []*unstructured.UnstructuredList) error {
        var errs util.Errors
        for _, ul := range objectsList {
diff --git a/pkg/config/schema/gvk/resources.gen.go 
b/pkg/config/schema/gvk/resources.gen.go
new file mode 100644
index 00000000..2d8073b9
--- /dev/null
+++ b/pkg/config/schema/gvk/resources.gen.go
@@ -0,0 +1,7 @@
+package gvk
+
+import "github.com/apache/dubbo-kubernetes/operator/pkg/config"
+
+var (
+       CustomResourceDefinition = config.GroupVersionKind{Group: 
"apiextensions.k8s.io", Version: "v1", Kind: "CustomResourceDefinition"}
+)

Reply via email to