Repository navigation
fix(gcp): retry on IAM replication lag and etag conflicts in resourceManager - #11043
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces a generic retryWithBackoff utility in src/utils.ts (migrating exponential backoff helpers from the throttler module) and applies it to addServiceAccountToRoles in src/gcp/resourceManager.ts to handle transient IAM replication delays (HTTP 400) and concurrency conflicts (HTTP 409). The review feedback suggests optimizing addServiceAccountToRoles by moving the service account lookup (getServiceAccount) outside of the retry loop to prevent redundant API calls on retry.
shettyvarun268
left a comment
There was a problem hiding this comment.
Clean fix Andy! LGTM
joehan
left a comment
There was a problem hiding this comment.
Logic seems fine to me, but a few minor improvments to make before merging.
| }; | ||
|
|
||
| describe("iam", () => { | ||
| afterEach(() => { |
There was a problem hiding this comment.
Note since I had to sanity check this - afterEach works downwards thru nested describes, so this still executes after every case in each inner describe
There was a problem hiding this comment.
Yes. This change is actually independent of my other changes. While running these tests a lot Jetski root caused some flakiness because some of the nested describe's had forgotten to call nock.cleanAll() in afterEach and could pollute future tests. So this fixes some small amount of the general CLI test flakiness.
| for (const roleName of roles) { | ||
| let binding = projectPolicy.bindings.find((b: Binding) => b.role === roleName); | ||
|
|
||
| // create a new binding if the role doesn't exist in the policy yet |
There was a problem hiding this comment.
Can we use the existing helper function iam.mergeBindings here instead of reimplementing this create or update logic?
There was a problem hiding this comment.
Done! I guess Thomas's AI didn't find that and mine didn't think to fix it. But I love using helpers where they exist.
…Manager Resource Manager setIamPolicy calls can fail transiently during service account provisioning due to replication lag (HTTP 400 INVALID_ARGUMENT with message "Service account ... does not exist.") and optimistic concurrency control conflicts (HTTP 409 ABORTED). This change adds retryWithBackoff to src/utils.ts (migrating backoff calculation utilities from throttler to avoid circular dependencies) and wraps addServiceAccountToRoles in src/gcp/resourceManager.ts with exponential backoff retries. Retrying with backoff allows IAM replication to settle for newly created service accounts (resolving 400 errors) and re-fetches the policy to obtain a fresh etag (resolving 409 conflicts). - Unit tests in src/utils.spec.ts verifying timeToWait and retryWithBackoff with successful runs, predicate retries, fast failures, and retry exhaustion. - Unit tests in src/gcp/resourceManager.spec.ts verifying: - Retry on HTTP 400 "does not exist" - Retry on HTTP 409 conflict with etag refresh - Fast failure on HTTP 403 permission errors without retrying - Fast failure on HTTP 404 not found errors without retrying - Existing throttler and run tests pass. - npm run test - Manual testing with repeated deploy and delete of kits verifying consistent success. npx mocha src/utils.spec.ts src/throttler/throttler.spec.ts src/gcp/resourceManager.spec.ts npm run test firebase deploy --only functions:storage-resize-images firebase functions:delete storage-resize-images
…llution
### Description
Move nock.cleanAll() to the top-level describe("iam") afterEach hook to ensure mock interceptors from all suites (including testIamPermissions) are cleaned up, preventing test flakiness and unconsumed mock pollution in subsequent tests.
### Scenarios Tested
- Unit tests in src/gcp/iam.spec.ts pass.
- npm run lint:changed-files passes with 0 errors.
…y and policy merging
e5841ac to
69762a6
Compare
Description
Resource Manager setIamPolicy calls can fail transiently during service account provisioning due to replication lag (HTTP 400 INVALID_ARGUMENT with message "Service account ... does not exist.") and optimistic concurrency control conflicts (HTTP 409 ABORTED).
This change adds retryWithBackoff to src/utils.ts (migrating backoff calculation utilities from throttler to avoid circular dependencies) and wraps addServiceAccountToRoles in src/gcp/resourceManager.ts with exponential backoff retries. Retrying with backoff allows IAM replication to settle for newly created service accounts (resolving 400 errors) and re-fetches the policy to obtain a fresh etag (resolving 409 conflicts).
Scenarios Tested
Sample Commands
npx mocha src/utils.spec.ts src/throttler/throttler.spec.ts src/gcp/resourceManager.spec.ts
npm run test
firebase deploy --only functions:storage-resize-images
firebase functions:delete storage-resize-images