-
Notifications
You must be signed in to change notification settings - Fork 431
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
detect Retry-After during async “does resource exist?” flow #2688
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,8 +18,11 @@ package async | |
|
||
import ( | ||
"context" | ||
"net/http" | ||
"strconv" | ||
"time" | ||
|
||
"github.com/Azure/go-autorest/autorest" | ||
azureautorest "github.com/Azure/go-autorest/autorest/azure" | ||
"github.com/pkg/errors" | ||
infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1" | ||
|
@@ -76,7 +79,7 @@ func processOngoingOperation(ctx context.Context, scope FutureScope, client Futu | |
|
||
// Operation is still in progress, update conditions and requeue. | ||
log.V(2).Info("long running operation is still ongoing", "service", serviceName, "resource", resourceName) | ||
return nil, azure.WithTransientError(azure.NewOperationNotDoneError(future), retryAfter(sdkFuture)) | ||
return nil, azure.WithTransientError(azure.NewOperationNotDoneError(future), getRequeueAfterFromFuture(sdkFuture)) | ||
} | ||
if err != nil { | ||
log.V(2).Error(err, "error checking long running operation status after it finished") | ||
|
@@ -111,7 +114,8 @@ func (s *Service) CreateResource(ctx context.Context, spec azure.ResourceSpecGet | |
// Get the resource if it already exists, and use it to construct the desired resource parameters. | ||
var existingResource interface{} | ||
if existing, err := s.Creator.Get(ctx, spec); err != nil && !azure.ResourceNotFound(err) { | ||
return nil, errors.Wrapf(err, "failed to get existing resource %s/%s (service: %s)", rgName, resourceName, serviceName) | ||
errWrapped := errors.Wrapf(err, "failed to get existing resource %s/%s (service: %s)", rgName, resourceName, serviceName) | ||
return nil, azure.WithTransientError(errWrapped, getRetryAfterFromError(err)) | ||
} else if err == nil { | ||
existingResource = existing | ||
log.V(2).Info("successfully got existing resource", "service", serviceName, "resource", resourceName, "resourceGroup", rgName) | ||
|
@@ -136,7 +140,7 @@ func (s *Service) CreateResource(ctx context.Context, spec azure.ResourceSpecGet | |
return nil, errors.Wrapf(err, "failed to create resource %s/%s (service: %s)", rgName, resourceName, serviceName) | ||
} | ||
s.Scope.SetLongRunningOperationState(future) | ||
return nil, azure.WithTransientError(azure.NewOperationNotDoneError(future), retryAfter(sdkFuture)) | ||
return nil, azure.WithTransientError(azure.NewOperationNotDoneError(future), getRequeueAfterFromFuture(sdkFuture)) | ||
} else if err != nil { | ||
return nil, errors.Wrapf(err, "failed to create resource %s/%s (service: %s)", rgName, resourceName, serviceName) | ||
} | ||
|
@@ -170,7 +174,7 @@ func (s *Service) DeleteResource(ctx context.Context, spec azure.ResourceSpecGet | |
return errors.Wrapf(err, "failed to delete resource %s/%s (service: %s)", rgName, resourceName, serviceName) | ||
} | ||
s.Scope.SetLongRunningOperationState(future) | ||
return azure.WithTransientError(azure.NewOperationNotDoneError(future), retryAfter(sdkFuture)) | ||
return azure.WithTransientError(azure.NewOperationNotDoneError(future), getRequeueAfterFromFuture(sdkFuture)) | ||
} else if err != nil { | ||
if azure.ResourceNotFound(err) { | ||
// already deleted | ||
|
@@ -183,12 +187,40 @@ func (s *Service) DeleteResource(ctx context.Context, spec azure.ResourceSpecGet | |
return nil | ||
} | ||
|
||
// retryAfter returns the max between the `RETRY-AFTER` header and the default requeue time. | ||
// getRequeueAfterFromFuture returns the max between the `RETRY-AFTER` header and the default requeue time. | ||
// This ensures we respect the retry-after header if it is set and avoid retrying too often during an API throttling event. | ||
func retryAfter(sdkFuture azureautorest.FutureAPI) time.Duration { | ||
func getRequeueAfterFromFuture(sdkFuture azureautorest.FutureAPI) time.Duration { | ||
retryAfter, _ := sdkFuture.GetPollingDelay() | ||
if retryAfter < reconciler.DefaultReconcilerRequeue { | ||
retryAfter = reconciler.DefaultReconcilerRequeue | ||
} | ||
return retryAfter | ||
} | ||
|
||
// getRetryAfterFromError returns the time.Duration from the http.Response in the autorest.DetailedError. | ||
// If there is no Response object, or if there is no meaningful Retry-After header data, we return a default. | ||
func getRetryAfterFromError(err error) time.Duration { | ||
// In case we aren't able to introspect Retry-After from the error type, we'll return this default | ||
ret := reconciler.DefaultReconcilerRequeue | ||
var detailedError autorest.DetailedError | ||
// if we have a strongly typed autorest.DetailedError then we can introspect the HTTP response data | ||
if errors.As(err, &detailedError) { | ||
if detailedError.Response != nil { | ||
// If we have Retry-After HTTP header data for any reason, prefer it | ||
if retryAfter := detailedError.Response.Header.Get("Retry-After"); retryAfter != "" { | ||
// This handles the case where Retry-After data is in the form of units of seconds | ||
if rai, err := strconv.Atoi(retryAfter); err == nil { | ||
ret = time.Duration(rai) * time.Second | ||
// This handles the case where Retry-After data is in the form of absolute time | ||
} else if t, err := time.Parse(time.RFC1123, retryAfter); err == nil { | ||
ret = time.Until(t) | ||
} | ||
// If we didn't find Retry-After HTTP header data but the response type is 429, | ||
// we'll have to come up with our sane default. | ||
} else if detailedError.Response.StatusCode == http.StatusTooManyRequests { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here, is that a real scenario or are we doing that just in case? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is definitely a real scenario, as a The larger idea here is that not all API responses are well behaving, and if we get an HTTP 429 without |
||
ret = reconciler.DefaultHTTP429RetryAfter | ||
} | ||
} | ||
} | ||
return ret | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
are we expecting both scenarios to be possible (units of seconds and absolute time)? Same question with
autorest.DetailedError
vs not ? Are there any SDK contracts for those error responses / headers that we can follow? Seems strange that we have to handle multiple possibilities, it's like we're guessing what the reponse might look like instead of expecting a specific format/unit/type.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As far as I can tell from research, the spec is overloaded to expect both value type flavors, so I think it's the best practice to deal with both types wherever we parse
Retry-After
HTTP header data.The
autorest.DetailedError
error implementation is one that we definitely know about from the usage above in the async flow, based on the specific SDK API we're currently re-using. In that sense you could say that this helper function is sort of tightly coupled to the particular implementation of capz at this point in time (initially this foo was inline in theCreateResource
func but I split it out for code maintenance reasons). So tl;dr we only have access toRetry-After
data in this particular case via theerr
response from the underlying serviceGet
implementations, and we only know how to get at it (as of right now) if the err is of "type"autorest.DetailedError
. Hope that makes sense!