From a33d1e488a3bcb7e865eac3c8e22210a48a2e183 Mon Sep 17 00:00:00 2001 From: Somefive Date: Tue, 28 Dec 2021 13:12:23 +0800 Subject: [PATCH] Feat: gc process ignore cluster not exists (#3007) * Feat: gc process ignore cluster not exists Signed-off-by: Yin Da * Feat: gc process ignore cluster not exists Signed-off-by: Yin Da --- pkg/multicluster/errors.go | 22 +++++++++++++++++++++- pkg/multicluster/errors_test.go | 32 ++++++++++++++++++++++++++++++++ pkg/resourcekeeper/cache.go | 3 +-- pkg/resourcekeeper/gc.go | 2 +- 4 files changed, 55 insertions(+), 4 deletions(-) create mode 100644 pkg/multicluster/errors_test.go diff --git a/pkg/multicluster/errors.go b/pkg/multicluster/errors.go index 99b1432e9..5d2d1191b 100644 --- a/pkg/multicluster/errors.go +++ b/pkg/multicluster/errors.go @@ -16,7 +16,12 @@ limitations under the License. package multicluster -import "fmt" +import ( + "fmt" + "strings" + + kerrors "k8s.io/apimachinery/pkg/api/errors" +) var ( // ErrClusterExists cluster already exists @@ -27,3 +32,18 @@ var ( // ClusterManagementError multicluster management error type ClusterManagementError error + +// IsClusterNotExists check if error is cluster not exists +func IsClusterNotExists(err error) bool { + return strings.Contains(err.Error(), "no such cluster") +} + +// IsNotFoundOrClusterNotExists check if error is not found or cluster not exists +func IsNotFoundOrClusterNotExists(err error) bool { + return kerrors.IsNotFound(err) || IsClusterNotExists(err) +} + +// IsClusterDisconnect check if error is cluster disconnect +func IsClusterDisconnect(err error) bool { + return strings.Contains(err.Error(), "dial tcp") +} diff --git a/pkg/multicluster/errors_test.go b/pkg/multicluster/errors_test.go new file mode 100644 index 000000000..b44ad1f18 --- /dev/null +++ b/pkg/multicluster/errors_test.go @@ -0,0 +1,32 @@ +/* +Copyright 2021 The KubeVela Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package multicluster + +import ( + "testing" + + "github.com/pkg/errors" + "github.com/stretchr/testify/require" +) + +func TestCluster(t *testing.T) { + r := require.New(t) + err := errors.New("no such cluster: example-cluster") + r.True(IsNotFoundOrClusterNotExists(err)) + err = errors.New("dial tcp: 127.0.0.1") + r.True(IsClusterDisconnect(err)) +} diff --git a/pkg/resourcekeeper/cache.go b/pkg/resourcekeeper/cache.go index 27b0d0f6f..87f24f6b9 100644 --- a/pkg/resourcekeeper/cache.go +++ b/pkg/resourcekeeper/cache.go @@ -20,7 +20,6 @@ import ( "context" "github.com/pkg/errors" - kerrors "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "sigs.k8s.io/controller-runtime/pkg/client" @@ -90,7 +89,7 @@ func (cache *resourceCache) get(ctx context.Context, mr v1beta1.ManagedResource) } if !entry.loaded { if err := cache.cli.Get(multicluster.ContextWithClusterName(ctx, mr.Cluster), mr.NamespacedName(), entry.obj); err != nil { - if kerrors.IsNotFound(err) { + if multicluster.IsNotFoundOrClusterNotExists(err) { entry.exists = false } else { entry.err = errors.Wrapf(err, "failed to get resource %s", key) diff --git a/pkg/resourcekeeper/gc.go b/pkg/resourcekeeper/gc.go index 63bc589af..77f7c6736 100644 --- a/pkg/resourcekeeper/gc.go +++ b/pkg/resourcekeeper/gc.go @@ -266,7 +266,7 @@ func (h *gcHandler) GarbageCollectComponentRevisionResourceTracker(ctx context.C if _, exists := inUseComponents[cr.ComponentKey()]; !exists && !skipGC { _cr := &v1.ControllerRevision{} err := h.Client.Get(multicluster.ContextWithClusterName(ctx, cr.Cluster), cr.NamespacedName(), _cr) - if err != nil && !kerrors.IsNotFound(err) { + if err != nil && !multicluster.IsNotFoundOrClusterNotExists(err) { return errors.Wrapf(err, "failed to get component revision %s", cr.ResourceKey()) } if err == nil {