Skip to content

Commit 16aed13

Browse files
authored
[Improvement] Unify K8S Error handling (#1147)
1 parent 2ce6245 commit 16aed13

File tree

3 files changed

+57
-8
lines changed

3 files changed

+57
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
- (Bugfix) Propagate Lifecycle Mount
1010
- (Feature) PVC Member Status info
1111
- (Feature) Respect ToBeCleanedServers in Agency
12+
- (Improvement) Unify K8S Error Handling
1213

1314
## [1.2.19](https://github.com/arangodb/kube-arangodb/tree/1.2.19) (2022-10-05)
1415
- (Bugfix) Prevent changes when UID is wrong

pkg/util/k8sutil/errors.go

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,30 +26,68 @@ import (
2626
"github.com/arangodb/kube-arangodb/pkg/util/errors"
2727
)
2828

29+
func isError(err error, precondition func(err error) bool) bool {
30+
if err == nil {
31+
return false
32+
}
33+
34+
if precondition(err) {
35+
return true
36+
}
37+
38+
if c := errors.CauseWithNil(err); c == err || c == nil {
39+
return false
40+
} else {
41+
return isError(c, precondition)
42+
}
43+
}
44+
2945
// IsAlreadyExists returns true if the given error is or is caused by a
3046
// kubernetes AlreadyExistsError,
3147
func IsAlreadyExists(err error) bool {
32-
return apierrors.IsAlreadyExists(errors.Cause(err))
48+
return isError(err, isAlreadyExistsC)
49+
}
50+
51+
func isAlreadyExistsC(err error) bool {
52+
return apierrors.IsAlreadyExists(err)
3353
}
3454

3555
// IsConflict returns true if the given error is or is caused by a
3656
// kubernetes ConflictError,
3757
func IsConflict(err error) bool {
38-
return apierrors.IsConflict(errors.Cause(err))
58+
return isError(err, isConflictC)
59+
}
60+
61+
func isConflictC(err error) bool {
62+
return apierrors.IsConflict(err)
3963
}
4064

4165
// IsNotFound returns true if the given error is or is caused by a
4266
// kubernetes NotFoundError,
4367
func IsNotFound(err error) bool {
44-
return apierrors.IsNotFound(errors.Cause(err))
68+
return isError(err, isNotFoundC)
4569
}
4670

47-
// IsNotFound returns true if the given error is or is caused by a
71+
func isNotFoundC(err error) bool {
72+
return apierrors.IsNotFound(err)
73+
}
74+
75+
// IsInvalid returns true if the given error is or is caused by a
4876
// kubernetes InvalidError,
4977
func IsInvalid(err error) bool {
5078
return apierrors.IsInvalid(errors.Cause(err))
5179
}
5280

81+
func isInvalidC(err error) bool {
82+
return isError(err, isInvalidC)
83+
}
84+
85+
// IsForbiddenOrNotFound returns true if the given error is or is caused by a
86+
// kubernetes NotFound or Forbidden,
5387
func IsForbiddenOrNotFound(err error) bool {
88+
return isError(err, isForbiddenOrNotFoundC)
89+
}
90+
91+
func isForbiddenOrNotFoundC(err error) bool {
5492
return apierrors.IsNotFound(err) || apierrors.IsForbidden(err)
5593
}

pkg/util/k8sutil/errors_test.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,11 @@ import (
3333
)
3434

3535
var (
36-
conflictError = apierrors.NewConflict(schema.GroupResource{Group: "groupName", Resource: "resourceName"}, "something", os.ErrInvalid)
37-
existsError = apierrors.NewAlreadyExists(schema.GroupResource{Group: "groupName", Resource: "resourceName"}, "something")
38-
invalidError = apierrors.NewInvalid(schema.GroupKind{Group: "groupName", Kind: "kindName"}, "something", field.ErrorList{})
39-
notFoundError = apierrors.NewNotFound(schema.GroupResource{Group: "groupName", Resource: "resourceName"}, "something")
36+
conflictError = apierrors.NewConflict(schema.GroupResource{Group: "groupName", Resource: "resourceName"}, "something", os.ErrInvalid)
37+
existsError = apierrors.NewAlreadyExists(schema.GroupResource{Group: "groupName", Resource: "resourceName"}, "something")
38+
invalidError = apierrors.NewInvalid(schema.GroupKind{Group: "groupName", Kind: "kindName"}, "something", field.ErrorList{})
39+
notFoundError = apierrors.NewNotFound(schema.GroupResource{Group: "groupName", Resource: "resourceName"}, "something")
40+
forbiddenError = apierrors.NewForbidden(schema.GroupResource{Group: "groupName", Resource: "resourceName"}, "something", errors.Newf("error"))
4041
)
4142

4243
func TestIsAlreadyExists(t *testing.T) {
@@ -59,3 +60,12 @@ func TestIsNotFound(t *testing.T) {
5960
assert.True(t, IsNotFound(notFoundError))
6061
assert.True(t, IsNotFound(errors.WithStack(notFoundError)))
6162
}
63+
64+
func TestIsForbiddenOrNotFound(t *testing.T) {
65+
assert.False(t, IsNotFound(invalidError))
66+
assert.False(t, IsNotFound(errors.WithStack(invalidError)))
67+
assert.True(t, IsForbiddenOrNotFound(notFoundError))
68+
assert.True(t, IsForbiddenOrNotFound(errors.WithStack(notFoundError)))
69+
assert.True(t, IsForbiddenOrNotFound(forbiddenError))
70+
assert.True(t, IsForbiddenOrNotFound(errors.WithStack(forbiddenError)))
71+
}

0 commit comments

Comments
 (0)