OADP-8085: fix(gcp): use errors.Is/As for GCS bucket not-found checks#2232
OADP-8085: fix(gcp): use errors.Is/As for GCS bucket not-found checks#2232PrasadJoshi12 wants to merge 1 commit into
Conversation
cloud.google.com/go/storage now wraps ErrBucketNotExist around the underlying *googleapi.Error (PR #11519), breaking == comparisons. Replace direct equality and type assertions with errors.Is and errors.As throughout gcp.go to correctly detect 404 bucket-not-found errors.
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: Repository: openshift/coderabbit/.coderabbit.yaml Review profile: CHILL Plan: Enterprise Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: PrasadJoshi12 The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
|
pkg/bucket/gcp.go:395:2: import-shadowing: The name 'errors' shadows an import name (revive) |
There was a problem hiding this comment.
Pull request overview
This PR updates GCP/GCS bucket error handling to correctly detect “bucket not found” conditions after upstream changes in cloud.google.com/go/storage began wrapping storage.ErrBucketNotExist, making direct == and type-assertion checks unreliable.
Changes:
- Replaces
err == storage.ErrBucketNotExistchecks inExists()andDelete()with a newisBucketNotFoundError()helper. - Switches
*googleapi.Errortype assertions toerrors.Asin retry/error-handling helpers. - Adds
isBucketNotFoundError()to handle bothstorage.ErrBucketNotExist(including wrapped) and HTTP 404*googleapi.Error.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "errors" |
| var gerr *googleapi.Error | ||
| if errors.As(err, &gerr) { | ||
| switch gerr.Code { |
| if errors.Is(err, storage.ErrBucketNotExist) { | ||
| return true | ||
| } | ||
| var gerr *googleapi.Error | ||
| return errors.As(err, &gerr) && gerr.Code == http.StatusNotFound |
| // handleGCSError provides consistent error handling for GCS operations | ||
| func handleGCSError(err error, operation string, bucketName string) error { | ||
| if err == storage.ErrBucketNotExist { | ||
| if errors.Is(err, storage.ErrBucketNotExist) { |
| var gerr *googleapi.Error | ||
| if errors.As(err, &gerr) { |
| // isBucketNotFoundError reports whether err indicates a GCS bucket does not exist. | ||
| // The GCS library may return storage.ErrBucketNotExist directly, wrap it via fmt.Errorf("%w", ...), | ||
| // or surface a *googleapi.Error with HTTP 404 — all three must be handled. | ||
| func isBucketNotFoundError(err error) bool { | ||
| if errors.Is(err, storage.ErrBucketNotExist) { | ||
| return true | ||
| } | ||
| var gerr *googleapi.Error | ||
| return errors.As(err, &gerr) && gerr.Code == http.StatusNotFound |
|
/hold |
|
@PrasadJoshi12: This pull request references OADP-8085 which is a valid jira issue. DetailsIn response to this:
Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the openshift-eng/jira-lifecycle-plugin repository. |
|
@PrasadJoshi12: The following tests failed, say
Full PR test history. Your PR dashboard. DetailsInstructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. I understand the commands that are listed here. |
cloud.google.com/go/storage now wraps ErrBucketNotExist around the underlying *googleapi.Error (PR #11519), breaking == comparisons. Replace direct equality and type assertions with errors.Is and errors.As throughout gcp.go to correctly detect 404 bucket-not-found errors.
Fixes: https://redhat.atlassian.net/browse/OADP-8078
Why the changes were made
How to test the changes made