Repository navigation
Fix omitted fields persisting when applying Kubernetes resources - #536
Open
thokra-nav wants to merge 2 commits into
Open
thokra-nav wants to merge 2 commits into
thokra-nav wants to merge 2 commits into
Conversation
Replace server-side apply with create/update operations so submitted manifests define the entire desired object. Server-side apply retains omitted fields when another field manager owns them. This caused removed fields such as spec.resources.limits.cpu to remain on existing resources. Existing resources are now replaced using their current resourceVersion to detect concurrent updates. Strict field validation and before/after change reporting are preserved. This intentionally introduces replacement semantics for all supported resource types, including omitted metadata and data fields. Kubernetes defaults and validation still apply, and writes require create/update permissions. Adds regression coverage for field removal across ownership histories, full-object replacement, resource versions, error propagation, and idempotent reapplication.
Contributor
There was a problem hiding this comment.
Copilot review overview
🟡 Changes recommended
Raw updates can reject resources with server-assigned immutable fields and erase controller-managed metadata.
Review effort: Balanced
Findings: 1
What changed in this PR
Replaces server-side apply with create/update semantics so omitted manifest fields are removed.
Changes:
- Uses strict CREATE/UPDATE operations with resource-version conflict detection.
- Adds unit and integration coverage for replacement, removals, errors, and idempotency.
| File | Description |
|---|---|
internal/apply/client.go |
Implements create-or-replace behavior. |
internal/apply/client_test.go |
Tests replacement and concurrency behavior. |
integration_tests/apply_replace.lua |
Verifies field removal end to end. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Transfer Update ownership from kubectl-create, kubectl-client-side-apply, and deployd to nais-api so omitted fields can be removed while preserving other managers' ownership. Reject foreign-field conflicts and unsafe cross-version migrations, and retain caller resourceVersion preconditions across migration. Make the Kubernetes fake ownership-aware and cover omission, migration, controller ownership, and concurrency regressions. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Comment on lines
+84
to
+89
| if patch != nil { | ||
| migrated, err := resourceClient.Patch(ctx, name, types.JSONPatchType, patch, metav1.PatchOptions{ | ||
| FieldManager: fieldManager, | ||
| }) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("migrating field ownership for %s/%s: %w", namespace, name, err) |
Comment on lines
+91
to
+96
| if submittedVersion != "" { | ||
| if migrated == nil || migrated.GetResourceVersion() == "" { | ||
| return nil, fmt.Errorf("ownership migration for %s/%s returned no resourceVersion", namespace, name) | ||
| } | ||
| desired.SetResourceVersion(migrated.GetResourceVersion()) | ||
| } |
Comment on lines
+104
to
+108
| after, err := resourceClient.Patch(ctx, name, types.ApplyPatchType, data, metav1.PatchOptions{ | ||
| FieldManager: fieldManager, | ||
| Force: new(false), | ||
| FieldValidation: metav1.FieldValidationStrict, | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.


Replace server-side apply with create/update operations so submitted manifests define the entire desired object.
Server-side apply retains omitted fields when another field manager owns them. This caused removed fields such as spec.resources.limits.cpu to remain on existing resources.
Existing resources are now replaced using their current resourceVersion to detect concurrent updates. Strict field validation and before/after change reporting are preserved.
This intentionally introduces replacement semantics for all supported resource types, including omitted metadata and data fields. Kubernetes defaults and validation still apply, and writes require create/update permissions.
Adds regression coverage for field removal across ownership histories, full-object replacement, resource versions, error propagation, and idempotent reapplication.