Skip to content

Commit 8306331

Browse files
committed
feat: return auth error
If controller encounter a auth error (token invalid), error stay silent to user
1 parent 4961b50 commit 8306331

4 files changed

Lines changed: 33 additions & 8 deletions

File tree

internal/controller/linodemachine_controller.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,13 @@ func (r *LinodeMachineReconciler) reconcile(ctx context.Context, logger logr.Log
178178
//nolint:dupl // Code duplication is simplicity in this case.
179179
defer func() {
180180
if err != nil {
181-
// Only set failure reason if the error is not retryable.
182-
if linodego.ErrHasStatus(err, http.StatusBadRequest) {
181+
// Set specific failure reason for authentication errors
182+
if util.IsAuthenticationError(err) {
183+
failureReason = util.CredentialError
184+
}
185+
186+
// Set failure status for terminal errors (400, 401, 403, 404)
187+
if util.IsTerminalError(err) {
183188
machineScope.LinodeMachine.Status.FailureReason = util.Pointer(failureReason)
184189
machineScope.LinodeMachine.Status.FailureMessage = util.Pointer(err.Error())
185190
machineScope.LinodeMachine.SetCondition(metav1.Condition{

internal/controller/linodemachine_controller_helpers.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,9 @@ func retryIfTransient(err error, logger logr.Logger) (ctrl.Result, error) {
7575
}
7676
return ctrl.Result{RequeueAfter: reconciler.DefaultMachineControllerRetryDelay}, nil
7777
}
78-
logger.Error(err, "unknown Linode API error")
79-
return ctrl.Result{RequeueAfter: reconciler.DefaultMachineControllerRetryDelay}, nil
78+
// Return the error for terminal errors (400, 401, 403, 404) so that
79+
// the reconciler can set appropriate conditions and events
80+
return ctrl.Result{}, err
8081
}
8182

8283
func fillCreateConfig(createConfig *linodego.InstanceCreateOptions, machineScope *scope.MachineScope) {

util/errors.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ var (
2929

3030
// List of failure reasons to use in the status fields of our resources
3131
var (
32-
CreateError = "CreateError"
33-
DeleteError = "DeleteError"
34-
UpdateError = "UpdateError"
35-
UnknownError = "UnknownError"
32+
CreateError = "CreateError"
33+
DeleteError = "DeleteError"
34+
UpdateError = "UpdateError"
35+
UnknownError = "UnknownError"
36+
CredentialError = "CredentialError"
3637
)

util/helpers.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,24 @@ func IsRetryableError(err error) bool {
6161
linodego.ErrorFromError) || errors.Is(err, http.ErrHandlerTimeout) || errors.Is(err, os.ErrDeadlineExceeded) || errors.Is(err, io.ErrUnexpectedEOF)
6262
}
6363

64+
// IsAuthenticationError determines if the error is an authentication or authorization error (401/403)
65+
// These errors are terminal and should not be retried without user intervention
66+
func IsAuthenticationError(err error) bool {
67+
return linodego.ErrHasStatus(err, http.StatusUnauthorized, http.StatusForbidden)
68+
}
69+
70+
// IsTerminalError determines if the error is terminal and should not be retried.
71+
// Terminal errors include bad requests (400), authentication errors (401/403), and not found (404)
72+
func IsTerminalError(err error) bool {
73+
return linodego.ErrHasStatus(
74+
err,
75+
http.StatusBadRequest,
76+
http.StatusUnauthorized,
77+
http.StatusForbidden,
78+
http.StatusNotFound,
79+
)
80+
}
81+
6482
// GetInstanceID determines the instance ID from the ProviderID
6583
func GetInstanceID(providerID *string) (int, error) {
6684
if providerID == nil {

0 commit comments

Comments
 (0)