You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by zh...@apache.org on 2024/12/24 05:45:15 UTC

(dubbo-kubernetes) branch master updated: [operator] Fix uninstalling panic

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 8aa1e91a [operator] Fix uninstalling panic
8aa1e91a is described below

commit 8aa1e91a92d932599a78eaf9feb581196e34dc47
Author: mfordjody <11...@qq.com>
AuthorDate: Tue Dec 24 13:45:01 2024 +0800

    [operator] Fix uninstalling panic
---
 operator/cmd/cluster/uninstall.go     |  2 +-
 operator/pkg/install/installer.go     | 35 ++++++++++++++++++++++++++++++++++-
 operator/pkg/uninstall/uninstaller.go |  7 ++++++-
 3 files changed, 41 insertions(+), 3 deletions(-)

diff --git a/operator/cmd/cluster/uninstall.go b/operator/cmd/cluster/uninstall.go
index c799d9ba..2c85e5e0 100644
--- a/operator/cmd/cluster/uninstall.go
+++ b/operator/cmd/cluster/uninstall.go
@@ -65,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, _ cli.Context, rootArgs *RootArgs, uiArgs *uninstallArgs) error {
 	cl := clog.NewConsoleLogger(cmd.OutOrStdout(), cmd.ErrOrStderr(), installerScope)
 	var kubeClient kube.CLIClient
 	var err error
diff --git a/operator/pkg/install/installer.go b/operator/pkg/install/installer.go
index 782d8c71..b6e542b3 100644
--- a/operator/pkg/install/installer.go
+++ b/operator/pkg/install/installer.go
@@ -5,6 +5,8 @@ import (
 	"fmt"
 	"github.com/apache/dubbo-kubernetes/operator/pkg/component"
 	"github.com/apache/dubbo-kubernetes/operator/pkg/manifest"
+	"github.com/apache/dubbo-kubernetes/operator/pkg/uninstall"
+	"github.com/apache/dubbo-kubernetes/operator/pkg/util"
 	"github.com/apache/dubbo-kubernetes/operator/pkg/util/clog"
 	"github.com/apache/dubbo-kubernetes/operator/pkg/util/dmultierr"
 	"github.com/apache/dubbo-kubernetes/operator/pkg/util/progress"
@@ -74,6 +76,11 @@ func (i Installer) install(manifests []manifest.ManifestSet) error {
 	if err := errors.ErrorOrNil(); err != nil {
 		return err
 	}
+
+	if err := i.prune(manifests); err != nil {
+		return fmt.Errorf("pruning: %v", err)
+	}
+	i.ProgressInfo.SetState(progress.StateComplete)
 	return nil
 }
 
@@ -123,17 +130,43 @@ func (i Installer) prune(manifests []manifest.ManifestSet) error {
 	if i.DryRun {
 		return nil
 	}
+
 	i.ProgressInfo.SetState(progress.StatePruning)
+
 	excluded := map[component.Name]sets.String{}
 	for _, c := range component.AllComponents {
 		excluded[c.UserFacingName] = sets.New[string]()
 	}
+
 	for _, mfs := range manifests {
 		for _, m := range mfs.Manifests {
 			excluded[mfs.Components].Insert(m.Hash())
 		}
 	}
-	return nil
+
+	var errs util.Errors
+	resources := uninstall.PrunedResourcesSchemas()
+	for _, gvk := range resources {
+		dc, err := i.Kube.DynamicClientFor(gvk, nil, "")
+		if err != nil {
+			return err
+		}
+		objs, err := dc.List(context.Background(), metav1.ListOptions{})
+		if objs == nil {
+			continue
+		}
+		for _, excluded := range excluded {
+			for _, obj := range objs.Items {
+				if excluded.Contains(manifest.ObjectHash(&obj)) {
+					continue
+				}
+				if err := uninstall.DeleteResource(i.Kube, i.DryRun, i.Logger, &obj); err != nil {
+					errs = append(errs, err)
+				}
+			}
+		}
+	}
+	return errs.ToErrors()
 }
 
 var componentDependencies = map[component.Name][]component.Name{
diff --git a/operator/pkg/uninstall/uninstaller.go b/operator/pkg/uninstall/uninstaller.go
index e420bb7f..dae48994 100644
--- a/operator/pkg/uninstall/uninstaller.go
+++ b/operator/pkg/uninstall/uninstaller.go
@@ -17,9 +17,9 @@ import (
 
 var (
 	ClusterResources    = []schema.GroupVersionKind{}
+	ClusterCPResources  = []schema.GroupVersionKind{}
 	AllClusterResources = append(ClusterResources,
 		gvk.CustomResourceDefinition.K8s())
-	ClusterCPResources []schema.GroupVersionKind
 )
 
 func GetPrunedResources(clt kube.CLIClient, dopName, dopNamespace string, includeClusterResources bool) ([]*unstructured.UnstructuredList, error) {
@@ -58,6 +58,10 @@ func NamespacedResources() []schema.GroupVersionKind {
 	return res
 }
 
+func PrunedResourcesSchemas() []schema.GroupVersionKind {
+	return append(NamespacedResources(), ClusterResources...)
+}
+
 func DeleteObjectsList(c kube.CLIClient, dryRun bool, log clog.Logger, objectsList []*unstructured.UnstructuredList) error {
 	var errs util.Errors
 	for _, ul := range objectsList {
@@ -76,6 +80,7 @@ func DeleteResource(clt kube.CLIClient, dryRun bool, log clog.Logger, obj *unstr
 		log.LogAndPrintf("Not pruning object %s because of dry run.", name)
 		return nil
 	}
+
 	c, err := clt.DynamicClientFor(obj.GroupVersionKind(), obj, "")
 	if err != nil {
 		return err