44 "context"
55 "errors"
66 "fmt"
7+ "net/url"
78 "time"
89
910 k8serrors "k8s.io/apimachinery/pkg/api/errors"
@@ -450,6 +451,14 @@ func (r *KonnectEntityReconciler[T, TEnt]) Reconcile(
450451
451452 if controllerutil .RemoveFinalizer (ent , KonnectCleanupFinalizer ) {
452453 if err := ops .Delete (ctx , sdk , r .Client , r .MetricRecorder , ent ); err != nil {
454+ // If the error was a network error, handle it here, there's no need to proceed,
455+ // as no state has changed.
456+ // Status conditions are updated in handleOpsErr.
457+ var errUrl * url.Error
458+ if errors .As (err , & errUrl ) {
459+ return r .handleOpsErr (ctx , ent , errUrl )
460+ }
461+
453462 // If the error is a rate limit error, requeue after the retry-after duration
454463 // instead of returning an error.
455464 if retryAfter , isRateLimited := ops .GetRetryAfterFromRateLimitError (err ); isRateLimited {
@@ -485,7 +494,16 @@ func (r *KonnectEntityReconciler[T, TEnt]) Reconcile(
485494 // Handle type specific operations and stop reconciliation if needed.
486495 // This can happen for instance when KongConsumer references credentials Secrets
487496 // that do not exist or populate some Status fields based on Konnect API.
488- if stop , res , err := handleTypeSpecific (ctx , sdk , r .Client , ent ); err != nil || ! res .IsZero () || stop {
497+ if stop , res , err := handleTypeSpecific (ctx , sdk , r .Client , ent ); err != nil {
498+ // If the error was a network error, handle it here, there's no need to proceed,
499+ // as no state has changed.
500+ // Status conditions are updated in handleOpsErr.
501+ var errUrl * url.Error
502+ if errors .As (err , & errUrl ) {
503+ return r .handleOpsErr (ctx , ent , errUrl )
504+ }
505+ return ctrl.Result {}, err
506+ } else if ! res .IsZero () || stop {
489507 return res , err
490508 }
491509
@@ -530,22 +548,31 @@ func (r *KonnectEntityReconciler[T, TEnt]) Reconcile(
530548 // Regardless of the error, patch the status as it can contain the Konnect ID,
531549 // Org ID, Server URL and status conditions.
532550 // Konnect ID will be needed for the finalizer to work.
533- if res , err := patch .ApplyStatusPatchIfNotEmpty (ctx , r .Client , logger , any (ent ).(client.Object ), obj ); err != nil {
551+ if _ , err := patch .ApplyStatusPatchIfNotEmpty (ctx , r .Client , logger , any (ent ).(client.Object ), obj ); err != nil {
534552 if k8serrors .IsConflict (err ) {
535553 return ctrl.Result {Requeue : true }, nil
536554 }
537555 return ctrl.Result {}, fmt .Errorf ("failed to update status after creating object: %w" , err )
538- } else if res != op .Noop {
539- return ctrl.Result {}, nil
540556 }
541557
542558 if err != nil {
559+ var (
560+ errUrl * url.Error
561+ rateLimitErr ops.RateLimitError
562+ )
563+ switch {
564+ // If the error was a network error, handle it here, there's no need to proceed,
565+ // as no state has changed.
566+ // Status conditions are updated in handleOpsErr.
567+ case errors .As (err , & errUrl ):
568+ return r .handleOpsErr (ctx , ent , errUrl )
569+
543570 // If the error is a rate limit error, requeue after the retry-after duration
544571 // instead of returning an error.
545- var rateLimitErr ops.RateLimitError
546- if errors .As (err , & rateLimitErr ) {
572+ case errors .As (err , & rateLimitErr ):
547573 return ctrl.Result {RequeueAfter : rateLimitErr .RetryAfter }, nil
548574 }
575+
549576 return ctrl.Result {}, ops.FailedKonnectOpError [T ]{
550577 Op : ops .CreateOp ,
551578 Err : err ,
@@ -557,6 +584,7 @@ func (r *KonnectEntityReconciler[T, TEnt]) Reconcile(
557584 }
558585
559586 res , err = ops .Update (ctx , sdk , r .SyncPeriod , r .Client , r .MetricRecorder , ent )
587+
560588 // Set the server URL and org ID regardless of the error.
561589 setStatusServerURLAndOrgID (ent , server , apiAuth .Status .OrganizationID )
562590 // Update the status of the object regardless of the error.
@@ -567,13 +595,25 @@ func (r *KonnectEntityReconciler[T, TEnt]) Reconcile(
567595 return ctrl.Result {}, fmt .Errorf ("failed to update in cluster resource after Konnect update: %w %w" , errUpd , err )
568596 }
569597 if err != nil {
598+ logger .Error (err , "failed to update" )
599+
600+ var (
601+ errUrl * url.Error
602+ rateLimitErr ops.RateLimitError
603+ )
604+ switch {
605+ // If the error was a network error, handle it here, there's no need to proceed,
606+ // as no state has changed.
607+ // Status conditions are updated in handleOpsErr.
608+ case errors .As (err , & errUrl ):
609+ return r .handleOpsErr (ctx , ent , errUrl )
610+
570611 // If the error is a rate limit error, requeue after the retry-after duration
571612 // instead of returning an error.
572- var rateLimitErr ops.RateLimitError
573- if errors .As (err , & rateLimitErr ) {
613+ case errors .As (err , & rateLimitErr ):
574614 return ctrl.Result {RequeueAfter : rateLimitErr .RetryAfter }, nil
575615 }
576- logger . Error ( err , "failed to update" )
616+
577617 } else if ! res .IsZero () {
578618 return res , nil
579619 }
0 commit comments