@@ -32,7 +32,6 @@ import (
3232 "github.com/pkg/errors"
3333 corev1 "k8s.io/api/core/v1"
3434 apierrors "k8s.io/apimachinery/pkg/api/errors"
35- metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3635 kerrors "k8s.io/apimachinery/pkg/util/errors"
3736 "k8s.io/client-go/tools/record"
3837 "k8s.io/klog/v2"
@@ -368,74 +367,26 @@ func (r *AWSMachineReconciler) reconcileDelete(ctx context.Context, machineScope
368367 machineScope .AWSMachine .Spec .DynamicHostAllocation != nil {
369368 hostID := * machineScope .AWSMachine .Status .DedicatedHost .ID
370369
371- // Check if we should retry host release
372- shouldRetry , retryAfter := shouldRetryHostRelease (machineScope )
373-
374- if shouldRetry {
375- // Mark that we're retrying
376- conditions .MarkFalse (machineScope .AWSMachine , infrav1 .DedicatedHostReleaseCondition ,
377- infrav1 .DedicatedHostReleaseRetryingReason , clusterv1 .ConditionSeverityWarning ,
378- "Retrying dedicated host release, attempt %d" , getHostReleaseAttempts (machineScope ))
379-
380- // Update retry tracking
381- updateHostReleaseRetryTracking (machineScope )
382-
383- // Patch the object to persist retry tracking
384- if err := machineScope .PatchObject (); err != nil {
385- machineScope .Error (err , "failed to patch object with retry tracking" )
386- return ctrl.Result {}, err
387- }
388-
389- machineScope .Info ("Retrying dedicated host release" , "hostID" , hostID , "attempt" , getHostReleaseAttempts (machineScope ))
390- return ctrl.Result {RequeueAfter : retryAfter }, nil
391- }
392-
393370 // Attempt to release the dedicated host
394- machineScope .Info ("Releasing dynamically allocated dedicated host" , "hostID" , hostID , "attempt" , getHostReleaseAttempts ( machineScope ) )
371+ machineScope .Info ("Releasing dynamically allocated dedicated host" , "hostID" , hostID )
395372 if err := ec2Service .ReleaseDedicatedHost (ctx , hostID ); err != nil {
396- // Host release failed, set up retry logic
397- machineScope . Error ( err , "failed to release dedicated host" , "hostID" , hostID , "attempt " , getHostReleaseAttempts ( machineScope ))
373+ machineScope . Error ( err , "failed to release dedicated host" , "hostID" , hostID )
374+ conditions . MarkFalse ( machineScope . AWSMachine , infrav1 . DedicatedHostReleaseCondition , infrav1 . DedicatedHostReleaseFailedReason , clusterv1 . ConditionSeverityError , "%s " , err . Error ( ))
398375 r .Recorder .Eventf (machineScope .AWSMachine , corev1 .EventTypeWarning , "FailedReleaseHost" , "Failed to release dedicated host %s: %v" , hostID , err )
376+ return ctrl.Result {}, err
377+ }
399378
400- // Update failure tracking
401- updateHostReleaseFailureTracking (machineScope , err .Error ())
402-
403- // Mark the condition as failed
404- conditions .MarkFalse (machineScope .AWSMachine , infrav1 .DedicatedHostReleaseCondition ,
405- infrav1 .DedicatedHostReleaseFailedReason , clusterv1 .ConditionSeverityWarning ,
406- "Failed to release dedicated host: %v" , err )
407-
408- // Patch the object to persist failure tracking
409- if err := machineScope .PatchObject (); err != nil {
410- machineScope .Error (err , "failed to patch object with failure tracking" )
411- return ctrl.Result {}, err
412- }
413-
414- // Check if we've exceeded max retries
415- if hasExceededMaxHostReleaseRetries (machineScope ) {
416- machineScope .Error (err , "exceeded maximum retry attempts for dedicated host release" , "hostID" , hostID , "maxAttempts" , getMaxHostReleaseRetries ())
417- r .Recorder .Eventf (machineScope .AWSMachine , corev1 .EventTypeWarning , "MaxRetriesExceeded" , "Exceeded maximum retry attempts for dedicated host %s release" , hostID )
418- // Continue with deletion even if host release fails permanently
419- } else {
420- // Return to trigger retry
421- return ctrl.Result {RequeueAfter : getInitialHostReleaseRetryDelay ()}, nil
422- }
423- } else {
424- // Host release succeeded
425- machineScope .Info ("Successfully released dedicated host" , "hostID" , hostID )
426- r .Recorder .Eventf (machineScope .AWSMachine , corev1 .EventTypeNormal , "SuccessfulReleaseHost" , "Released dedicated host %s" , hostID )
427-
428- // Mark the condition as succeeded
429- conditions .MarkTrue (machineScope .AWSMachine , infrav1 .DedicatedHostReleaseCondition )
379+ // Host release succeeded
380+ machineScope .Info ("Successfully released dedicated host" , "hostID" , hostID )
381+ r .Recorder .Eventf (machineScope .AWSMachine , corev1 .EventTypeNormal , "SuccessfulReleaseHost" , "Released dedicated host %s" , hostID )
430382
431- // Clear retry tracking since we succeeded
432- clearHostReleaseRetryTracking (machineScope )
383+ // Mark the condition as succeeded
384+ conditions . MarkTrue (machineScope . AWSMachine , infrav1 . DedicatedHostReleaseCondition )
433385
434- // Patch the object to persist success state
435- if err := machineScope .PatchObject (); err != nil {
436- machineScope .Error (err , "failed to patch object after successful host release" )
437- return ctrl.Result {}, err
438- }
386+ // Patch the object to persist success state
387+ if err := machineScope .PatchObject (); err != nil {
388+ machineScope .Error (err , "failed to patch object after successful host release" )
389+ return ctrl.Result {}, err
439390 }
440391 }
441392
@@ -527,6 +478,7 @@ func (r *AWSMachineReconciler) findInstance(machineScope *scope.MachineScope, ec
527478 } else {
528479 // If the ProviderID is populated, describe the instance using the ID.
529480 // InstanceIfExists() returns error (ErrInstanceNotFoundByID or ErrDescribeInstance) if the instance could not be found.
481+
530482 //nolint:staticcheck
531483 instance , err = ec2svc .InstanceIfExists (ptr.To [string ](pid .ID ()))
532484 if err != nil {
@@ -1373,94 +1325,3 @@ func (r *AWSMachineReconciler) ensureInstanceMetadataOptions(ec2svc services.EC2
13731325
13741326 return ec2svc .ModifyInstanceMetadataOptions (instance .ID , machine .Spec .InstanceMetadataOptions )
13751327}
1376-
1377- // getMaxHostReleaseRetries returns the maximum number of retry attempts for dedicated host release.
1378- func getMaxHostReleaseRetries () int32 {
1379- return 5
1380- }
1381-
1382- // getInitialHostReleaseRetryDelay returns the initial delay before the first retry.
1383- func getInitialHostReleaseRetryDelay () time.Duration {
1384- return 30 * time .Second
1385- }
1386-
1387- // getHostReleaseAttempts returns the current number of host release attempts.
1388- func getHostReleaseAttempts (scope * scope.MachineScope ) int32 {
1389- if scope .AWSMachine .Status .HostReleaseAttempts == nil {
1390- return 0
1391- }
1392- return * scope .AWSMachine .Status .HostReleaseAttempts
1393- }
1394-
1395- // shouldRetryHostRelease determines if we should retry host release based on retry tracking.
1396- func shouldRetryHostRelease (scope * scope.MachineScope ) (bool , time.Duration ) {
1397- attempts := getHostReleaseAttempts (scope )
1398-
1399- // If no attempts yet, don't retry
1400- if attempts == 0 {
1401- return false , 0
1402- }
1403-
1404- // Check if we've exceeded max retries
1405- if attempts >= getMaxHostReleaseRetries () {
1406- return false , 0
1407- }
1408-
1409- // Check if enough time has passed since last attempt
1410- lastAttempt := scope .AWSMachine .Status .LastHostReleaseAttempt
1411- if lastAttempt == nil {
1412- return false , 0
1413- }
1414-
1415- // Calculate exponential backoff delay
1416- baseDelay := getInitialHostReleaseRetryDelay ()
1417- multiplier := int64 (1 )
1418- for i := int32 (1 ); i < attempts ; i ++ {
1419- multiplier *= 2
1420- }
1421- backoffDelay := time .Duration (int64 (baseDelay ) * multiplier )
1422-
1423- // Cap the maximum delay at 5 minutes
1424- if backoffDelay > 5 * time .Minute {
1425- backoffDelay = 5 * time .Minute
1426- }
1427-
1428- // Check if enough time has passed
1429- timeSinceLastAttempt := time .Since (lastAttempt .Time )
1430- if timeSinceLastAttempt < backoffDelay {
1431- remainingDelay := backoffDelay - timeSinceLastAttempt
1432- return false , remainingDelay
1433- }
1434-
1435- return true , backoffDelay
1436- }
1437-
1438- // updateHostReleaseRetryTracking increments the retry attempt counter and updates the timestamp.
1439- func updateHostReleaseRetryTracking (scope * scope.MachineScope ) {
1440- attempts := getHostReleaseAttempts (scope ) + 1
1441- scope .AWSMachine .Status .HostReleaseAttempts = & attempts
1442-
1443- now := time .Now ()
1444- scope .AWSMachine .Status .LastHostReleaseAttempt = & metav1.Time {Time : now }
1445- }
1446-
1447- // updateHostReleaseFailureTracking updates the failure reason and timestamp.
1448- func updateHostReleaseFailureTracking (scope * scope.MachineScope , reason string ) {
1449- scope .AWSMachine .Status .HostReleaseFailedReason = & reason
1450-
1451- // Update the timestamp for the last attempt
1452- now := time .Now ()
1453- scope .AWSMachine .Status .LastHostReleaseAttempt = & metav1.Time {Time : now }
1454- }
1455-
1456- // clearHostReleaseRetryTracking resets all retry tracking fields after successful release.
1457- func clearHostReleaseRetryTracking (scope * scope.MachineScope ) {
1458- scope .AWSMachine .Status .HostReleaseAttempts = nil
1459- scope .AWSMachine .Status .LastHostReleaseAttempt = nil
1460- scope .AWSMachine .Status .HostReleaseFailedReason = nil
1461- }
1462-
1463- // hasExceededMaxHostReleaseRetries checks if we've exceeded the maximum retry attempts.
1464- func hasExceededMaxHostReleaseRetries (scope * scope.MachineScope ) bool {
1465- return getHostReleaseAttempts (scope ) >= getMaxHostReleaseRetries ()
1466- }
0 commit comments