|
| 1 | +/* |
| 2 | +Copyright 2024 The Metal3 Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package ipam |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "time" |
| 22 | +) |
| 23 | + |
| 24 | +// ReconcileError represents an generic error of Reconcile loop. ErrorType indicates what type |
| 25 | +// of action is required to recover. It can take two values: |
| 26 | +// 1. `Transient` - Can be recovered , will be requeued after. |
| 27 | +// 2. `Terminal` - Cannot be recovered, will not be requeued. |
| 28 | + |
| 29 | +type ReconcileError struct { |
| 30 | + error |
| 31 | + errorType ReconcileErrorType |
| 32 | + RequeueAfter time.Duration |
| 33 | +} |
| 34 | + |
| 35 | +// ReconcileErrorType represents the type of a ReconcileError. |
| 36 | +type ReconcileErrorType string |
| 37 | + |
| 38 | +const ( |
| 39 | + // TransientErrorType can be recovered, will be requeued after a configured time interval. |
| 40 | + TransientErrorType ReconcileErrorType = "Transient" |
| 41 | + // TerminalErrorType cannot be recovered, will not be requeued. |
| 42 | + TerminalErrorType ReconcileErrorType = "Terminal" |
| 43 | +) |
| 44 | + |
| 45 | +// Error returns the error message for a ReconcileError. |
| 46 | +func (e ReconcileError) Error() string { |
| 47 | + var errStr string |
| 48 | + if e.error != nil { |
| 49 | + errStr = e.error.Error() |
| 50 | + } |
| 51 | + switch e.errorType { |
| 52 | + case TransientErrorType: |
| 53 | + return fmt.Sprintf("%s. Object will be requeued after %s", errStr, e.GetRequeueAfter()) |
| 54 | + case TerminalErrorType: |
| 55 | + return fmt.Sprintf("reconcile error that cannot be recovered occurred: %s. Object will not be requeued", errStr) |
| 56 | + default: |
| 57 | + return fmt.Sprintf("reconcile error occurred with unknown recovery type. The actual error is: %s", errStr) |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +// GetRequeueAfter gets the duration to wait until the managed object is |
| 62 | +// requeued for further processing. |
| 63 | +func (e ReconcileError) GetRequeueAfter() time.Duration { |
| 64 | + return e.RequeueAfter |
| 65 | +} |
| 66 | + |
| 67 | +// IsTransient returns if the ReconcileError is recoverable. |
| 68 | +func (e ReconcileError) IsTransient() bool { |
| 69 | + return e.errorType == TransientErrorType |
| 70 | +} |
| 71 | + |
| 72 | +// IsTerminal returns if the ReconcileError is non recoverable. |
| 73 | +func (e ReconcileError) IsTerminal() bool { |
| 74 | + return e.errorType == TerminalErrorType |
| 75 | +} |
| 76 | + |
| 77 | +// WithTransientError wraps the error in a ReconcileError with errorType as `Transient`. |
| 78 | +func WithTransientError(err error, requeueAfter time.Duration) ReconcileError { |
| 79 | + return ReconcileError{error: err, errorType: TransientErrorType, RequeueAfter: requeueAfter} |
| 80 | +} |
| 81 | + |
| 82 | +// WithTerminalError wraps the error in a ReconcileError with errorType as `Terminal`. |
| 83 | +func WithTerminalError(err error) ReconcileError { |
| 84 | + return ReconcileError{error: err, errorType: TerminalErrorType} |
| 85 | +} |
0 commit comments