Skip to content

Commit

Permalink
chore: ignore policy/v1 discovery error at cluster deletion (#6637)
Browse files Browse the repository at this point in the history
  • Loading branch information
leon-inf authored Feb 22, 2024
1 parent 0ee75fc commit 49ef259
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 3 deletions.
25 changes: 25 additions & 0 deletions controllers/apps/transform_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,15 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
package apps

import (
"errors"
"reflect"
"time"

corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/discovery"
"k8s.io/client-go/tools/record"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
Expand Down Expand Up @@ -83,6 +87,10 @@ func getClusterOwningObjectsWithOptions(transCtx *clusterTransformContext,
objs := make(clusterOwningObjects)
for _, list := range kinds {
if err := transCtx.Client.List(transCtx.Context, list, opts...); err != nil {
// check for policy/v1 discovery error, to support k8s clusters before 1.21.
if isPolicyV1DiscoveryNotFoundError(err) {
continue
}
return nil, err
}
// reflect get list.Items
Expand Down Expand Up @@ -156,3 +164,20 @@ func isVolumeClaimTemplatesEqual(a, b []appsv1alpha1.ClusterComponentVolumeClaim
}
return true
}

// isPolicyV1DiscoveryNotFoundError checks whether the @err is an error of type ErrGroupDiscoveryFailed for policy/v1 resource.
func isPolicyV1DiscoveryNotFoundError(err error) bool {
wrappedErr := errors.Unwrap(err)
if wrappedErr != nil {
err = wrappedErr
}
if !discovery.IsGroupDiscoveryFailedError(err) {
return false
}
discoveryErr, _ := err.(*discovery.ErrGroupDiscoveryFailed)
statusErr := discoveryErr.Groups[schema.GroupVersion{Group: "policy", Version: "v1"}]
if statusErr == nil {
return false
}
return apierrors.IsNotFound(statusErr)
}
36 changes: 33 additions & 3 deletions controllers/apps/transform_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,22 @@ import (
"testing"

"github.com/stretchr/testify/assert"
v1 "k8s.io/api/apps/v1"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/discovery"
"sigs.k8s.io/controller-runtime/pkg/client"

appsv1alpha1 "github.com/apecloud/kubeblocks/apis/apps/v1alpha1"
)

func TestReflect(t *testing.T) {
var list client.ObjectList
sts := v1.StatefulSet{}
sts := appsv1.StatefulSet{}
sts.SetName("hello")
list = &v1.StatefulSetList{Items: []v1.StatefulSet{sts}}
list = &appsv1.StatefulSetList{Items: []appsv1.StatefulSet{sts}}
v := reflect.ValueOf(list).Elem().FieldByName("Items")
if v.Kind() != reflect.Slice {
t.Error("not slice")
Expand Down Expand Up @@ -90,3 +93,30 @@ func TestIsResourceRequirementsEqual(t *testing.T) {
b := buildRR("1000m", "1024Mi")
assert.True(t, isResourceRequirementsEqual(a, b))
}

func TestIsPolicyV1DiscoveryNotFoundError(t *testing.T) {
var (
notFoundErr = apierrors.NewNotFound(schema.GroupResource{}, "abc")
alreadyExistErr = apierrors.NewAlreadyExists(schema.GroupResource{}, "abc")
discoveryErr = &discovery.ErrGroupDiscoveryFailed{Groups: map[schema.GroupVersion]error{}}
)

// normal not-found error
assert.True(t, apierrors.IsNotFound(notFoundErr))
assert.False(t, isPolicyV1DiscoveryNotFoundError(notFoundErr))

// not-found error for core/v1
discoveryErr.Groups[schema.GroupVersion{Group: "core", Version: "v1"}] = notFoundErr
assert.False(t, isPolicyV1DiscoveryNotFoundError(discoveryErr))

// policy/v1 already-exist error
discoveryErr.Groups[schema.GroupVersion{Group: "policy", Version: "v1"}] = alreadyExistErr
assert.False(t, isPolicyV1DiscoveryNotFoundError(discoveryErr))

// policy/v1 not-found error
discoveryErr.Groups[schema.GroupVersion{Group: "policy", Version: "v1"}] = notFoundErr
assert.True(t, isPolicyV1DiscoveryNotFoundError(discoveryErr))

// wrapped policy/v1 not-found error
assert.True(t, isPolicyV1DiscoveryNotFoundError(fmt.Errorf("%w", discoveryErr)))
}

0 comments on commit 49ef259

Please sign in to comment.