Skip to content
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

Retry if/when ScheduledQueryRules encounter BadRequest #4312

Merged
merged 2 commits into from
Oct 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
Copyright (c) Microsoft Corporation.
Licensed under the MIT license.
*/

package customizations

import (
"strings"

"github.com/go-logr/logr"

"github.com/Azure/azure-service-operator/v2/internal/genericarmclient"
"github.com/Azure/azure-service-operator/v2/pkg/genruntime/core"
"github.com/Azure/azure-service-operator/v2/pkg/genruntime/extensions"
)

var _ extensions.ErrorClassifier = &ScheduledQueryRuleExtension{}

// ClassifyError evaluates the provided error, returning whether it is fatal or can be retried.
// A badrequest (400) is normally fatal, but ScheduledQueryRule resources may return 400 whilst
// a dependency is being created, so we override for that case.
// cloudError is the error returned from ARM.
// apiVersion is the ARM API version used for the request.
// log is a logger than can be used for telemetry.
// next is the next implementation to call.
func (e *ScheduledQueryRuleExtension) ClassifyError(
cloudError *genericarmclient.CloudError,
apiVersion string,
log logr.Logger,
next extensions.ErrorClassifierFunc,
) (core.CloudErrorDetails, error) {
details, err := next(cloudError)
if err != nil {
return core.CloudErrorDetails{}, err
}

// Override is to treat BadRequests as retryable for ScheduledQueryRules
if isRetryableError(cloudError) {
details.Classification = core.ErrorRetryable
}

return details, nil
}

// isRetryableError checks the passed error to see if it is a retryable bad request, returning true if it is.
func isRetryableError(err *genericarmclient.CloudError) bool {
if err == nil {
// No error, so no need for a retry
return false
}

// When any of the scopes to which a ScheduledQueryRule is suposed to apply are missing,
// we get a BadRequest error with a messaage like:
// "Scope 'target-law' does not exist"
// These should be retried to allow the rule to be created once the target scope resource exists.
if err.Code() == "BadRequest" && strings.Contains(err.Message(), "does not exist") {
return true
}

return false
}
Loading