Skip to content

ssa: fix dry-run failure when upgrading API version#1131

Open
mehrdadbn9 wants to merge 2 commits intofluxcd:mainfrom
mehrdadbn9:fix/api-version-migration-dry-run
Open

ssa: fix dry-run failure when upgrading API version#1131
mehrdadbn9 wants to merge 2 commits intofluxcd:mainfrom
mehrdadbn9:fix/api-version-migration-dry-run

Conversation

@mehrdadbn9
Copy link

Summary

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.

Fix

This PR introduces a new migrateAPIVersion function that:

  1. Detects when the desired object has a different API version than the existing object
  2. Uses the existing PatchMigrateToVersion function to update the API version in managed fields
  3. Applies the patch before the dry-run to ensure proper schema validation

The fix is applied in:

  • Apply() - single object apply
  • ApplyAll() - batch object apply
  • Diff() - drift detection

Testing

  • Added unit tests for PatchMigrateToVersion with various scenarios
  • Added integration test for API version migration workflow
  • Code compiles and passes go vet

Fixes: fluxcd/flux2#5715

Reproduction

The issue can be reproduced with a CRD like Linkerd's Server resource:

  1. Apply a resource with policy.linkerd.io/v1beta1
  2. Update the manifest to use policy.linkerd.io/v1beta3
  3. Flux dry-run fails with schema validation error for default fields

With this fix, the API version migration is handled automatically before dry-run validation.

@mehrdadbn9 mehrdadbn9 force-pushed the fix/api-version-migration-dry-run branch 2 times, most recently from 542ce68 to 0aacf8b Compare February 23, 2026 22:03
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

"dry-run failed" when upgrading API version

2 participants