Skip to content

Commit 8c83fed

Browse files
authored
fix: update unexpected error and API Server log error types (#94)
Signed-off-by: Britania Rodriguez Reyes <britaniar@microsoft.com>
1 parent 25d684b commit 8c83fed

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

pkg/utils/controller/controller.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ func NewExpectedBehaviorError(err error) error {
8787
// NewAPIServerError returns error types when accessing data from cache or API server.
8888
func NewAPIServerError(fromCache bool, err error) error {
8989
if err != nil {
90+
// The func may return other unexpected runtime errors other than API server errors.
91+
// https://github.com/kubernetes-sigs/controller-runtime/blob/main/pkg/client/client.go#L334-L339
9092
if fromCache && isUnexpectedCacheError(err) {
9193
return NewUnexpectedBehaviorError(err)
9294
}
@@ -98,8 +100,9 @@ func NewAPIServerError(fromCache bool, err error) error {
98100

99101
func isUnexpectedCacheError(err error) bool {
100102
// may need to add more error code based on the production
101-
// Cache will return notFound for GET.
102-
return !apierrors.IsNotFound(err)
103+
// When the cache is missed, it will query API server and return API server errors.
104+
var statusErr *apierrors.StatusError
105+
return !errors.Is(err, context.Canceled) && !errors.As(err, &statusErr) && !errors.Is(err, context.DeadlineExceeded)
103106
}
104107

105108
// NewUserError returns ErrUserError type error when err is not nil.

pkg/utils/controller/controller_test.go

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,10 @@ func TestNewAPIServerError(t *testing.T) {
111111
wantErr: ErrAPIServerError,
112112
},
113113
{
114-
name: "reading from cache: unexpectedBehaviorError",
114+
name: "reading from cache: apiServerError",
115115
fromCache: true,
116116
err: apierrors.NewConflict(schema.GroupResource{}, "conflict", nil),
117-
wantErr: ErrUnexpectedBehavior,
117+
wantErr: ErrAPIServerError,
118118
},
119119
{
120120
name: "reading from API server: apiServerError",
@@ -128,6 +128,36 @@ func TestNewAPIServerError(t *testing.T) {
128128
err: apierrors.NewConflict(schema.GroupResource{}, "conflict", nil),
129129
wantErr: ErrAPIServerError,
130130
},
131+
{
132+
name: "reading from API server: context canceled",
133+
fromCache: false,
134+
err: fmt.Errorf("client rate limiter Wait returned an error: %w", context.Canceled),
135+
wantErr: ErrAPIServerError,
136+
},
137+
{
138+
name: "reading from API server: deadline exceeded",
139+
fromCache: false,
140+
err: fmt.Errorf("client rate limiter Wait returned an error: %w", context.DeadlineExceeded),
141+
wantErr: ErrAPIServerError,
142+
},
143+
{
144+
name: "reading from cache: context canceled",
145+
fromCache: true,
146+
err: fmt.Errorf("client rate limiter Wait returned an error: %w", context.Canceled),
147+
wantErr: ErrAPIServerError,
148+
},
149+
{
150+
name: "reading from cache: deadline exceeded",
151+
fromCache: true,
152+
err: fmt.Errorf("client rate limiter Wait returned an error: %w", context.DeadlineExceeded),
153+
wantErr: ErrAPIServerError,
154+
},
155+
{
156+
name: "reading from cache: missing kind error",
157+
fromCache: true,
158+
err: runtime.NewMissingKindErr("unstructured object has no kind"),
159+
wantErr: ErrUnexpectedBehavior,
160+
},
131161
}
132162
for _, tc := range tests {
133163
t.Run(tc.name, func(t *testing.T) {

0 commit comments

Comments
 (0)