Skip to content

Skip image creation if the image already exists#829

Open
afritzler wants to merge 1 commit intomainfrom
fix/createimage
Open

Skip image creation if the image already exists#829
afritzler wants to merge 1 commit intomainfrom
fix/createimage

Conversation

@afritzler
Copy link
Member

@afritzler afritzler commented Feb 6, 2026

Summary by CodeRabbit

  • Bug Fixes
    • Enhanced resilience for RBD image creation operations. The system now gracefully handles scenarios where an image already exists, logging an informational message and continuing instead of terminating with an error.

@afritzler afritzler requested a review from a team as a code owner February 6, 2026 12:58
@github-actions github-actions bot added size/XS bug Something isn't working labels Feb 6, 2026
@coderabbitai
Copy link

coderabbitai bot commented Feb 6, 2026

Walkthrough

Modified error handling in RBD image creation to treat duplicate image scenarios as non-fatal. When librbd.CreateImage returns ErrExist, the function logs an informational message and proceeds normally instead of propagating the error.

Changes

Cohort / File(s) Summary
RBD Image Creation Error Handling
internal/controllers/snapshot_controller.go
Adjusted error handling in reconcileIroncoreImageSnapshot to distinguish between ErrExist and other CreateImage failures. ErrExist is now logged informatively and skipped, while other errors are still returned.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Description check ⚠️ Warning The pull request description is missing entirely. The repository requires a 'Proposed Changes' section with bullet points and issue reference, but none was provided. Add a pull request description following the template with proposed changes and issue reference (Fixes #829).
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and specifically describes the main change: skipping RBD image creation when the image already exists instead of returning an error.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix/createimage

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Fix all issues with AI agents
In `@internal/controllers/snapshot_controller.go`:
- Around line 318-326: When CreateImage returns librbd.ErrExist, don't log
"Created rbd image" and make the subsequent steps idempotent: update
createSnapshot to treat a duplicate-snapshot error as success (or check for
snapshot existence before creating) so repeated reconciliations don't fail;
before calling prepareSnapshotContent, verify the existing image's size equals
roundedSize and skip re-population if it matches (or error if it doesn't); and
only emit the "Created rbd image" log from the success path of
librbd.CreateImage (leave it out when ErrExist is true). Reference symbols:
librbd.CreateImage, librbd.ErrExist, roundedSize, prepareSnapshotContent,
createSnapshot.

Comment on lines +318 to 326

if err = librbd.CreateImage(ioCtx, imageName, roundedSize, options); err != nil {
return fmt.Errorf("failed to create os rbd image: %w", err)
if errors.Is(err, librbd.ErrExist) {
log.V(2).Info("RBD image already exists, skipping creation", "ImageID", imageName)
} else {
return fmt.Errorf("failed to create os rbd image: %w", err)
}
}
log.V(2).Info("Created rbd image", "bytes", roundedSize)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

🧩 Analysis chain

🏁 Script executed:

rg -n 'func createSnapshot' --type go -A 25

Repository: ironcore-dev/ceph-provider

Length of output: 1740


🏁 Script executed:

rg -n 'prepareSnapshotContent' --type go -B 3 -A 20

Repository: ironcore-dev/ceph-provider

Length of output: 4037


🏁 Script executed:

rg -n 'reconcileIroncoreImageSnapshot' --type go -A 5

Repository: ironcore-dev/ceph-provider

Length of output: 1258


When ErrExist is hit, the createSnapshot call at line 333 will fail if a snapshot already exists from a prior attempt, making the reconciliation non-idempotent.

The change skips image creation when it already exists but does not account for the snapshot creation step (line 333), which also needs idempotency. The createSnapshot function (common.go:107-128) does not check for duplicate snapshot errors — if a prior reconciliation reached line 333 but failed afterward, the retry will fail when attempting to create the same snapshot.

Additionally:

  • prepareSnapshotContent (line 328) re-populates the image unconditionally. If the image already exists from a prior partial failure, re-writing its content is wasteful. More importantly, there's no verification that the existing image's size matches roundedSize; if the image was created with a different size previously, the re-population could produce incorrect results.
  • The log message "Created rbd image" (line 326) is misleading when the image already existed — it should only appear in the success case.

For true idempotency, consider either:

  1. Checking if the snapshot already exists in createSnapshot before attempting creation, or
  2. Verifying the image size before proceeding to prepareSnapshotContent, or
  3. Guarding both the population and snapshot steps with existence checks.

At minimum, move or adjust the log message on line 326 to avoid confusion.

🤖 Prompt for AI Agents
In `@internal/controllers/snapshot_controller.go` around lines 318 - 326, When
CreateImage returns librbd.ErrExist, don't log "Created rbd image" and make the
subsequent steps idempotent: update createSnapshot to treat a duplicate-snapshot
error as success (or check for snapshot existence before creating) so repeated
reconciliations don't fail; before calling prepareSnapshotContent, verify the
existing image's size equals roundedSize and skip re-population if it matches
(or error if it doesn't); and only emit the "Created rbd image" log from the
success path of librbd.CreateImage (leave it out when ErrExist is true).
Reference symbols: librbd.CreateImage, librbd.ErrExist, roundedSize,
prepareSnapshotContent, createSnapshot.

@hardikdr hardikdr added the area/storage Storage solutions and related concerns. label Feb 7, 2026
@hardikdr hardikdr added this to Roadmap Feb 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/storage Storage solutions and related concerns. bug Something isn't working size/XS

Projects

Status: No status
Status: No status

Development

Successfully merging this pull request may close these issues.

2 participants