ssa: fix dry-run failure when upgrading API version#1131
Open
mehrdadbn9 wants to merge 2 commits intofluxcd:mainfrom
Open
ssa: fix dry-run failure when upgrading API version#1131mehrdadbn9 wants to merge 2 commits intofluxcd:mainfrom
mehrdadbn9 wants to merge 2 commits intofluxcd:mainfrom
Conversation
542ce68 to
0aacf8b
Compare
When upgrading a custom resource to a new API version (e.g., v1beta1 to v1beta3), the server-side apply dry-run fails with an error like: 'dry-run failed: .spec.accessPolicy: field not declared in schema' This happens because the managed fields in the existing object still reference the old API version, and Kubernetes validates those fields against the new schema during dry-run. Fields that have default values in the new version but didn't exist in the old version cause schema validation errors. The fix introduces a new function that: 1. Detects when the desired object has a different API version than existing 2. Uses the existing function to update managed fields 3. Applies the patch before the dry-run to ensure proper schema validation This enables seamless API version upgrades without manual intervention. Fixes: fluxcd/flux2#5715 Signed-off-by: mehrdadbn9 <mehrdadbiukian@gmail.com>
- Remove API version migration from Diff() - Diff should not alter objects - Refactor migrateAPIVersion to not require getError parameter - Check object existence conditionally before calling migrateAPIVersion - Remove inadequate TestApply_APIVersionMigration test Fixes: fluxcd/flux2#5715 Signed-off-by: mehrdadbn9 <mehrdadbiukian@gmail.com>
ab51cc4 to
a03da8d
Compare
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.
Summary
When upgrading a custom resource to a new API version (e.g.,
v1beta1tov1beta3), the server-side apply dry-run fails with an error like:This happens because the managed fields in the existing object still reference the old API version, and Kubernetes validates those fields against the new schema during dry-run. Fields that have default values in the new version but didn't exist in the old version cause schema validation errors.
Fix
This PR introduces a new
migrateAPIVersionfunction that:PatchMigrateToVersionfunction to update the API version in managed fieldsThe fix is applied in:
Apply()- single object applyApplyAll()- batch object applyDiff()- drift detectionTesting
PatchMigrateToVersionwith various scenariosgo vetFixes: fluxcd/flux2#5715
Reproduction
The issue can be reproduced with a CRD like Linkerd's
Serverresource:policy.linkerd.io/v1beta1policy.linkerd.io/v1beta3With this fix, the API version migration is handled automatically before dry-run validation.