-
Notifications
You must be signed in to change notification settings - Fork 7.4k
fix: use csapgrade to patch managedFields for client-side apply migration #26289
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
reggie-k
merged 9 commits into
argoproj:master
from
pjiang-dev:pjiang/fix-csa-migration
Feb 19, 2026
Merged
Changes from 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
6c86afe
fallback to csupgrade when annotation large
pjiang-dev 122edf3
refactor
pjiang-dev 8e4bfc3
no need to remove last-app-annotation
pjiang-dev e806958
refactor to always use csaupgrade
pjiang-dev a0247e0
add unit tests for csamigration
pjiang-dev cd569c1
update docs
pjiang-dev d186ec1
handle race condition
pjiang-dev e3e34fb
Update tests and needsClientSideApplyMigration
pjiang-dev 2fbd162
address comments
pjiang-dev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,10 +17,13 @@ import ( | |
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
| "k8s.io/apimachinery/pkg/runtime/schema" | ||
| "k8s.io/apimachinery/pkg/types" | ||
| "k8s.io/apimachinery/pkg/util/sets" | ||
| "k8s.io/apimachinery/pkg/util/wait" | ||
| "k8s.io/client-go/discovery" | ||
| "k8s.io/client-go/dynamic" | ||
| "k8s.io/client-go/rest" | ||
| "k8s.io/client-go/util/csaupgrade" | ||
| "k8s.io/client-go/util/retry" | ||
| "k8s.io/klog/v2/textlogger" | ||
| cmdutil "k8s.io/kubectl/pkg/cmd/util" | ||
|
|
@@ -1234,29 +1237,69 @@ func (sc *syncContext) needsClientSideApplyMigration(liveObj *unstructured.Unstr | |
| return false | ||
| } | ||
|
|
||
| // performClientSideApplyMigration performs a client-side-apply using the specified field manager. | ||
| // This moves the 'last-applied-configuration' field to be managed by the specified manager. | ||
| // The next time server-side apply is performed, kubernetes automatically migrates all fields from the manager | ||
| // that owns 'last-applied-configuration' to the manager that uses server-side apply. This will remove the | ||
| // specified manager from the resources managed fields. 'kubectl-client-side-apply' is used as the default manager. | ||
| func (sc *syncContext) performClientSideApplyMigration(targetObj *unstructured.Unstructured, fieldManager string) error { | ||
| sc.log.WithValues("resource", kubeutil.GetResourceKey(targetObj)).V(1).Info("Performing client-side apply migration step") | ||
|
|
||
| // Apply with the specified manager to set up the migration | ||
| _, err := sc.resourceOps.ApplyResource( | ||
| context.TODO(), | ||
| targetObj, | ||
| cmdutil.DryRunNone, | ||
| false, | ||
| false, | ||
| false, | ||
| fieldManager, | ||
| ) | ||
| // performCSAUpgradeMigration uses the csaupgrade package to migrate managed fields | ||
| // from a client-side apply manager (operation: Update) to the server-side apply manager. | ||
| // This directly patches the managedFields to transfer field ownership, avoiding the need | ||
| // to write the last-applied-configuration annotation (which has a 262KB size limit). | ||
| // This is the primary method for CSA to SSA migration in ArgoCD. | ||
| func (sc *syncContext) performCSAUpgradeMigration(liveObj *unstructured.Unstructured, csaFieldManager string) error { | ||
| sc.log.WithValues("resource", kubeutil.GetResourceKey(liveObj)).V(1).Info( | ||
| "Performing csaupgrade-based migration") | ||
|
|
||
| // Get the dynamic resource interface for the live object | ||
| gvk := liveObj.GroupVersionKind() | ||
| apiResource, err := kubeutil.ServerResourceForGroupVersionKind(sc.disco, gvk, "patch") | ||
| if err != nil { | ||
| return fmt.Errorf("failed to perform client-side apply migration on manager %s: %w", fieldManager, err) | ||
| return fmt.Errorf("failed to get api resource for %s: %w", gvk, err) | ||
| } | ||
| res := kubeutil.ToGroupVersionResource(gvk.GroupVersion().String(), apiResource) | ||
| resIf := kubeutil.ToResourceInterface(sc.dynamicIf, apiResource, res, liveObj.GetNamespace()) | ||
|
|
||
| return nil | ||
| // Use retry to handle conflicts if managed fields changed between reconciliation and now | ||
| //nolint:wrapcheck // error is wrapped inside the retry function | ||
| return retry.RetryOnConflict(retry.DefaultRetry, func() error { | ||
|
pjiang-dev marked this conversation as resolved.
|
||
| // Fetch fresh object to get current managed fields state | ||
| freshObj, getErr := resIf.Get(context.TODO(), liveObj.GetName(), metav1.GetOptions{}) | ||
| if getErr != nil { | ||
| return fmt.Errorf("failed to get fresh object for CSA migration: %w", getErr) | ||
| } | ||
|
|
||
| // Check if migration is still needed with fresh state | ||
| if !sc.needsClientSideApplyMigration(freshObj, csaFieldManager) { | ||
| sc.log.WithValues("resource", kubeutil.GetResourceKey(liveObj)).V(1).Info( | ||
| "CSA migration no longer needed") | ||
| return nil | ||
|
pjiang-dev marked this conversation as resolved.
|
||
| } | ||
|
|
||
| // Generate the migration patch using the csaupgrade package | ||
| // This unions the CSA manager's fields into the SSA manager and removes the CSA manager entry | ||
| patchData, patchErr := csaupgrade.UpgradeManagedFieldsPatch( | ||
| freshObj, | ||
| sets.New(csaFieldManager), | ||
| sc.serverSideApplyManager, | ||
| ) | ||
| if patchErr != nil { | ||
| return fmt.Errorf("failed to generate csaupgrade migration patch: %w", patchErr) | ||
| } | ||
| if patchData == nil { | ||
| // No migration needed | ||
| return nil | ||
| } | ||
|
|
||
| // Apply the migration patch to transfer field ownership | ||
| _, patchErr = resIf.Patch(context.TODO(), liveObj.GetName(), types.JSONPatchType, patchData, metav1.PatchOptions{}) | ||
| if patchErr != nil { | ||
| if apierrors.IsConflict(patchErr) { | ||
|
pjiang-dev marked this conversation as resolved.
|
||
| sc.log.WithValues("resource", kubeutil.GetResourceKey(liveObj)).V(1).Info( | ||
| "Retrying CSA migration due to conflict") | ||
| } | ||
| return fmt.Errorf("failed to apply csaupgrade migration patch: %w", patchErr) | ||
| } | ||
|
|
||
| sc.log.WithValues("resource", kubeutil.GetResourceKey(liveObj)).Info( | ||
|
pjiang-dev marked this conversation as resolved.
Outdated
|
||
| "Successfully migrated managed fields using csaupgrade") | ||
| return nil | ||
| }) | ||
| } | ||
|
|
||
| func (sc *syncContext) applyObject(t *syncTask, dryRun, validate bool) (common.ResultCode, string) { | ||
|
|
@@ -1277,9 +1320,12 @@ func (sc *syncContext) applyObject(t *syncTask, dryRun, validate bool) (common.R | |
| serverSideApply := sc.shouldUseServerSideApply(t.targetObj, dryRun) | ||
|
|
||
| // Check if we need to perform client-side apply migration for server-side apply | ||
| // Perform client-side apply migration for server-side apply | ||
| // This uses csaupgrade to directly patch managedFields, transferring ownership | ||
| // from CSA managers (operation: Update) to the SSA manager (argocd-controller) | ||
| if serverSideApply && !dryRun && sc.enableClientSideApplyMigration { | ||
| if sc.needsClientSideApplyMigration(t.liveObj, sc.clientSideApplyMigrationManager) { | ||
| err = sc.performClientSideApplyMigration(t.targetObj, sc.clientSideApplyMigrationManager) | ||
| err = sc.performCSAUpgradeMigration(t.liveObj, sc.clientSideApplyMigrationManager) | ||
| if err != nil { | ||
| return common.ResultCodeSyncFailed, fmt.Sprintf("Failed to perform client-side apply migration: %v", err) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i know not your code but should add the resource name here? |
||
| } | ||
|
|
||
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.