mirror of
https://github.com/arangodb/kube-arangodb.git
synced 2024-12-14 11:57:37 +00:00
[Improvement] Unify K8S Error handling (#1147)
This commit is contained in:
parent
2ce6245e95
commit
16aed1365b
3 changed files with 57 additions and 8 deletions
|
@ -9,6 +9,7 @@
|
|||
- (Bugfix) Propagate Lifecycle Mount
|
||||
- (Feature) PVC Member Status info
|
||||
- (Feature) Respect ToBeCleanedServers in Agency
|
||||
- (Improvement) Unify K8S Error Handling
|
||||
|
||||
## [1.2.19](https://github.com/arangodb/kube-arangodb/tree/1.2.19) (2022-10-05)
|
||||
- (Bugfix) Prevent changes when UID is wrong
|
||||
|
|
|
@ -26,30 +26,68 @@ import (
|
|||
"github.com/arangodb/kube-arangodb/pkg/util/errors"
|
||||
)
|
||||
|
||||
func isError(err error, precondition func(err error) bool) bool {
|
||||
if err == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
if precondition(err) {
|
||||
return true
|
||||
}
|
||||
|
||||
if c := errors.CauseWithNil(err); c == err || c == nil {
|
||||
return false
|
||||
} else {
|
||||
return isError(c, precondition)
|
||||
}
|
||||
}
|
||||
|
||||
// IsAlreadyExists returns true if the given error is or is caused by a
|
||||
// kubernetes AlreadyExistsError,
|
||||
func IsAlreadyExists(err error) bool {
|
||||
return apierrors.IsAlreadyExists(errors.Cause(err))
|
||||
return isError(err, isAlreadyExistsC)
|
||||
}
|
||||
|
||||
func isAlreadyExistsC(err error) bool {
|
||||
return apierrors.IsAlreadyExists(err)
|
||||
}
|
||||
|
||||
// IsConflict returns true if the given error is or is caused by a
|
||||
// kubernetes ConflictError,
|
||||
func IsConflict(err error) bool {
|
||||
return apierrors.IsConflict(errors.Cause(err))
|
||||
return isError(err, isConflictC)
|
||||
}
|
||||
|
||||
func isConflictC(err error) bool {
|
||||
return apierrors.IsConflict(err)
|
||||
}
|
||||
|
||||
// IsNotFound returns true if the given error is or is caused by a
|
||||
// kubernetes NotFoundError,
|
||||
func IsNotFound(err error) bool {
|
||||
return apierrors.IsNotFound(errors.Cause(err))
|
||||
return isError(err, isNotFoundC)
|
||||
}
|
||||
|
||||
// IsNotFound returns true if the given error is or is caused by a
|
||||
func isNotFoundC(err error) bool {
|
||||
return apierrors.IsNotFound(err)
|
||||
}
|
||||
|
||||
// IsInvalid returns true if the given error is or is caused by a
|
||||
// kubernetes InvalidError,
|
||||
func IsInvalid(err error) bool {
|
||||
return apierrors.IsInvalid(errors.Cause(err))
|
||||
}
|
||||
|
||||
func isInvalidC(err error) bool {
|
||||
return isError(err, isInvalidC)
|
||||
}
|
||||
|
||||
// IsForbiddenOrNotFound returns true if the given error is or is caused by a
|
||||
// kubernetes NotFound or Forbidden,
|
||||
func IsForbiddenOrNotFound(err error) bool {
|
||||
return isError(err, isForbiddenOrNotFoundC)
|
||||
}
|
||||
|
||||
func isForbiddenOrNotFoundC(err error) bool {
|
||||
return apierrors.IsNotFound(err) || apierrors.IsForbidden(err)
|
||||
}
|
||||
|
|
|
@ -37,6 +37,7 @@ var (
|
|||
existsError = apierrors.NewAlreadyExists(schema.GroupResource{Group: "groupName", Resource: "resourceName"}, "something")
|
||||
invalidError = apierrors.NewInvalid(schema.GroupKind{Group: "groupName", Kind: "kindName"}, "something", field.ErrorList{})
|
||||
notFoundError = apierrors.NewNotFound(schema.GroupResource{Group: "groupName", Resource: "resourceName"}, "something")
|
||||
forbiddenError = apierrors.NewForbidden(schema.GroupResource{Group: "groupName", Resource: "resourceName"}, "something", errors.Newf("error"))
|
||||
)
|
||||
|
||||
func TestIsAlreadyExists(t *testing.T) {
|
||||
|
@ -59,3 +60,12 @@ func TestIsNotFound(t *testing.T) {
|
|||
assert.True(t, IsNotFound(notFoundError))
|
||||
assert.True(t, IsNotFound(errors.WithStack(notFoundError)))
|
||||
}
|
||||
|
||||
func TestIsForbiddenOrNotFound(t *testing.T) {
|
||||
assert.False(t, IsNotFound(invalidError))
|
||||
assert.False(t, IsNotFound(errors.WithStack(invalidError)))
|
||||
assert.True(t, IsForbiddenOrNotFound(notFoundError))
|
||||
assert.True(t, IsForbiddenOrNotFound(errors.WithStack(notFoundError)))
|
||||
assert.True(t, IsForbiddenOrNotFound(forbiddenError))
|
||||
assert.True(t, IsForbiddenOrNotFound(errors.WithStack(forbiddenError)))
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue