-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #787 from Nordix/replace-requeafterError-type/sunnat
🌱 Introduce ReconcileError with Transient and Terminal Error type
- Loading branch information
Showing
9 changed files
with
171 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
Copyright 2024 The Metal3 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 ipam | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
) | ||
|
||
// ReconcileError represents an generic error of Reconcile loop. ErrorType indicates what type | ||
// of action is required to recover. It can take two values: | ||
// 1. `Transient` - Can be recovered , will be requeued after. | ||
// 2. `Terminal` - Cannot be recovered, will not be requeued. | ||
|
||
type ReconcileError struct { | ||
error | ||
errorType ReconcileErrorType | ||
RequeueAfter time.Duration | ||
} | ||
|
||
// ReconcileErrorType represents the type of a ReconcileError. | ||
type ReconcileErrorType string | ||
|
||
const ( | ||
// TransientErrorType can be recovered, will be requeued after a configured time interval. | ||
TransientErrorType ReconcileErrorType = "Transient" | ||
// TerminalErrorType cannot be recovered, will not be requeued. | ||
TerminalErrorType ReconcileErrorType = "Terminal" | ||
) | ||
|
||
// Error returns the error message for a ReconcileError. | ||
func (e ReconcileError) Error() string { | ||
var errStr string | ||
if e.error != nil { | ||
errStr = e.error.Error() | ||
} | ||
switch e.errorType { | ||
case TransientErrorType: | ||
return fmt.Sprintf("%s. Object will be requeued after %s", errStr, e.GetRequeueAfter()) | ||
case TerminalErrorType: | ||
return fmt.Sprintf("reconcile error that cannot be recovered occurred: %s. Object will not be requeued", errStr) | ||
default: | ||
return fmt.Sprintf("reconcile error occurred with unknown recovery type. The actual error is: %s", errStr) | ||
} | ||
} | ||
|
||
// GetRequeueAfter gets the duration to wait until the managed object is | ||
// requeued for further processing. | ||
func (e ReconcileError) GetRequeueAfter() time.Duration { | ||
return e.RequeueAfter | ||
} | ||
|
||
// IsTransient returns if the ReconcileError is recoverable. | ||
func (e ReconcileError) IsTransient() bool { | ||
return e.errorType == TransientErrorType | ||
} | ||
|
||
// IsTerminal returns if the ReconcileError is non recoverable. | ||
func (e ReconcileError) IsTerminal() bool { | ||
return e.errorType == TerminalErrorType | ||
} | ||
|
||
// WithTransientError wraps the error in a ReconcileError with errorType as `Transient`. | ||
func WithTransientError(err error, requeueAfter time.Duration) ReconcileError { | ||
return ReconcileError{error: err, errorType: TransientErrorType, RequeueAfter: requeueAfter} | ||
} | ||
|
||
// WithTerminalError wraps the error in a ReconcileError with errorType as `Terminal`. | ||
func WithTerminalError(err error) ReconcileError { | ||
return ReconcileError{error: err, errorType: TerminalErrorType} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
Copyright 2024 The Metal3 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 ipam | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"time" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
const ( | ||
duration = 50 * time.Second | ||
) | ||
|
||
var _ = Describe("Reconcile Error testing", func() { | ||
|
||
It("Returns correct values for Transient Error", func() { | ||
|
||
err := WithTransientError(errors.New("Transient Error"), duration) | ||
Expect(err.GetRequeueAfter()).To(Equal(duration)) | ||
Expect(err.IsTransient()).To(BeTrue()) | ||
Expect(err.IsTerminal()).To(BeFalse()) | ||
Expect(err.Error()).To(Equal(fmt.Sprintf("%s. Object will be requeued after %s", "Transient Error", duration))) | ||
}) | ||
|
||
It("Returns correct values for Terminal Error", func() { | ||
err := WithTerminalError(errors.New("Terminal Error")) | ||
Expect(err.IsTransient()).To(BeFalse()) | ||
Expect(err.IsTerminal()).To(BeTrue()) | ||
Expect(err.Error()).To(Equal(fmt.Sprintf("reconcile error that cannot be recovered occurred: %s. Object will not be requeued", "Terminal Error"))) | ||
}) | ||
|
||
It("Returns correct values for Unknown ReconcileError type", func() { | ||
err := ReconcileError{errors.New("Unknown Error"), "unknownErrorType", 0 * time.Second} | ||
Expect(err.IsTerminal()).To(BeFalse()) | ||
Expect(err.IsTransient()).To(BeFalse()) | ||
Expect(err.Error()).To(Equal(fmt.Sprintf("reconcile error occurred with unknown recovery type. The actual error is: %s", "Unknown Error"))) | ||
}) | ||
}) |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters