-
Notifications
You must be signed in to change notification settings - Fork 194
Fix error setting app password in hack create due to eventual consistency in ms graph api #4736
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
base: master
Are you sure you want to change the base?
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 |
|---|---|---|
|
|
@@ -5,7 +5,9 @@ package cluster | |
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
| "net/http" | ||
| "time" | ||
|
|
||
| "k8s.io/apimachinery/pkg/util/wait" | ||
|
|
@@ -15,6 +17,11 @@ import ( | |
| msgraph_errors "github.com/Azure/ARO-RP/pkg/util/graph/graphsdk/models/odataerrors" | ||
| ) | ||
|
|
||
| const ( | ||
| GraphApiMaxRetries int = 5 | ||
| GraphApiRetryInterval time.Duration = 5 * time.Second | ||
| ) | ||
mrWinston marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| func (c *Cluster) createApplication(ctx context.Context, displayName string) (string, string, error) { | ||
| appBody := msgraph_models.NewApplication() | ||
| appBody.SetDisplayName(&displayName) | ||
|
|
@@ -35,7 +42,31 @@ func (c *Cluster) createApplication(ctx context.Context, displayName string) (st | |
| // ByApplicationId is confusingly named, but it refers to | ||
| // the application's Object ID, not to the Application ID. | ||
| // https://learn.microsoft.com/en-us/graph/api/application-addpassword?view=graph-rest-1.0&tabs=http#http-request | ||
| pwResult, err := c.spGraphClient.Applications().ByApplicationId(id).AddPassword().Post(ctx, pwCredentialRequestBody, nil) | ||
|
|
||
| // retry loop, due to eventual consistency, the application we just created might not be found when queried immediately, only after a retry | ||
| // check if returned error is 404 not found, if so, retry the operation | ||
| numRetries := 0 | ||
|
Collaborator
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. Can we add the retry logic directly in the https://github.com/Azure/ARO-RP/blob/master/pkg/util/graph/graphsdk/applications/item_add_password_request_builder.go#L49 ?? |
||
| var pwResult msgraph_models.PasswordCredentialable | ||
| err = wait.PollUntilWithContext(ctx, GraphApiRetryInterval, func(ctx context.Context) (done bool, err error) { | ||
|
Collaborator
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. Suggestion: use wait.PollImmediateUntilWithContext (or do one direct call before polling) so retries still happen, but success path stays fast.
Collaborator
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. Could we add a focused unit test around this retry loop? |
||
| pwResult, err = c.spGraphClient.Applications().ByApplicationId(id).AddPassword().Post(ctx, pwCredentialRequestBody, nil) | ||
| if err == nil { | ||
| return true, nil | ||
| } | ||
|
|
||
| var mainErr *msgraph_errors.ODataError | ||
| // some unknown error, bubble it up to caller | ||
| if ok := errors.As(err, &mainErr); !ok || mainErr.GetStatusCode() != http.StatusNotFound { | ||
| return false, err | ||
| } | ||
|
|
||
| // Check if we already tried too often | ||
| numRetries++ | ||
| if numRetries > GraphApiMaxRetries { | ||
mrWinston marked this conversation as resolved.
Show resolved
Hide resolved
mrWinston marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return false, mainErr | ||
mrWinston marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| return false, nil | ||
| }) | ||
| if err != nil { | ||
| return "", "", err | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.