Skip to content

Fix WithOptimisticLock on object creation#118

Merged
kdorosh merged 14 commits into
mainfrom
fix_optimistic_lock_opts
Jun 4, 2026
Merged

Fix WithOptimisticLock on object creation#118
kdorosh merged 14 commits into
mainfrom
fix_optimistic_lock_opts

Conversation

@kdorosh

@kdorosh kdorosh commented Jun 2, 2026

Copy link
Copy Markdown
Contributor

💸 TL;DR

There was a regression in #113 for SDK users of the WithOptimisticLock() option on object creation. This PR fixes the regression.

📜 Details

The problem: WithOptimisticLock() is only valid for PUT requests, it doesn't make sense for server side apply. In #113 the logic was changed:

v0.13.13 flow (correct):

  1. Get object
  2. If NotFound → createNewObject → apply opts (ignore ResourceVersionMissing) → Create
  3. If exists → apply opts on desired → diff → patch

v0.13.14 flow (broken):

  1. Apply opts immediately → WithOptimisticLock() errors because RV is empty
  2. Never reaches Get or createNewObject

This PR restores the v0.13.13 flow for the Get + Create codepath but also keeps the early options apply for the SSA codepath.

🧪 Testing Steps / Validation

Validated against failing internal envtests as well as the new test coverage added in this PR.

✅ Checks

  • CI tests (if present) are passing
  • Adheres to code style for repo
  • Contributor License Agreement (CLA) completed if not a Reddit employee (Reddit employee)

@kdorosh kdorosh force-pushed the fix_optimistic_lock_opts branch from 47af5b2 to fe1c22d Compare June 2, 2026 19:13
@kdorosh kdorosh requested a review from harveyxia June 2, 2026 19:20
@kdorosh kdorosh marked this pull request as ready for review June 2, 2026 19:20
@kdorosh kdorosh requested a review from a team as a code owner June 2, 2026 19:20
@kdorosh kdorosh changed the title Fix optimistic lock opts Fix WithOptimisticLock on object creation Jun 2, 2026
@kdorosh kdorosh force-pushed the fix_optimistic_lock_opts branch 2 times, most recently from df602b3 to 6d47c24 Compare June 2, 2026 21:00
@kdorosh kdorosh force-pushed the fix_optimistic_lock_opts branch from 6d47c24 to a4f6ad3 Compare June 3, 2026 19:38
kdorosh added 3 commits June 3, 2026 16:42
This reverts commit a4f6ad3.
This reverts commit ba6d8b0.

@harveyxia harveyxia left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Approach LGTM, a few comments

Comment thread pkg/io/applicator.go Outdated
}

// apply options to desired after confirming the object exists (or on the create path above).
if err := applyOpts(ctx, desired, requestOpts, opts); err != nil {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Can this, L132, and L246 apply only the object options so it's clear when the options need to be applied in the logical flow, and we don't redundantly apply request options?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

🔕 good call, implemented in 56b387f

Comment thread pkg/io/applicator_test.go Outdated
Comment thread pkg/io/options.go Outdated
Comment on lines 121 to 127
func requestOption(fn func(*RequestOptions) error) ApplyOption {
return ApplyOption{configureRequest: fn}
}

func objectOption(fn func(context.Context, client.Object, *RequestOptions) error) ApplyOption {
return ApplyOption{configureObject: fn}
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Move these to the top of the file, add a concise but descriptive docstring explaining the contract

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

🔕 done in 3223fda

Comment thread pkg/io/applicator.go Outdated
@kdorosh kdorosh requested a review from harveyxia June 4, 2026 14:30
Comment thread pkg/io/options.go Outdated
Comment thread pkg/io/options.go Outdated
kdorosh and others added 2 commits June 4, 2026 10:36
Co-authored-by: Harvey Xia <harvey.xia@reddit.com>
Co-authored-by: Harvey Xia <harvey.xia@reddit.com>
@kdorosh kdorosh merged commit 214b14a into main Jun 4, 2026
1 check passed
@kdorosh kdorosh deleted the fix_optimistic_lock_opts branch June 4, 2026 14:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants