Skip to content
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

fix(server): Application sync fails when CreateNamespace and ServerSideApply flags are set in the sync options #563

Closed
wants to merge 4 commits into from
Closed
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 10 additions & 11 deletions pkg/sync/sync_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -921,45 +921,44 @@ func (sc *syncContext) applyObject(t *syncTask, dryRun, force, validate bool) (c
var err error
var message string
shouldReplace := sc.replace || resourceutil.HasAnnotationOption(t.targetObj, common.AnnotationSyncOptions, common.SyncOptionReplace)
applyFn := func(dryRunStrategy cmdutil.DryRunStrategy) (string, error) {
applyFn := func(dryRunOption cmdutil.DryRunStrategy, serverSideApplyFlag bool) (string, error) {
sc.log.Info(fmt.Sprintf("calling applyFn using dry run strategy: %v and server side apply %v", dryRunOption, serverSideApplyFlag))
if !shouldReplace {
return sc.resourceOps.ApplyResource(context.TODO(), t.targetObj, dryRunStrategy, force, validate, serverSideApply, sc.serverSideApplyManager, false)
return sc.resourceOps.ApplyResource(context.TODO(), t.targetObj, dryRunOption, force, validate, serverSideApplyFlag, sc.serverSideApplyManager, false)
}
if t.liveObj == nil {
return sc.resourceOps.CreateResource(context.TODO(), t.targetObj, dryRunStrategy, validate)
return sc.resourceOps.CreateResource(context.TODO(), t.targetObj, dryRunOption, validate)
}
// Avoid using `kubectl replace` for CRDs since 'replace' might recreate resource and so delete all CRD instances.
// The same thing applies for namespaces, which would delete the namespace as well as everything within it,
// so we want to avoid using `kubectl replace` in that case as well.
if kube.IsCRD(t.targetObj) || t.targetObj.GetKind() == kubeutil.NamespaceKind {
update := t.targetObj.DeepCopy()
update.SetResourceVersion(t.liveObj.GetResourceVersion())
_, err = sc.resourceOps.UpdateResource(context.TODO(), update, dryRunStrategy)
_, err := sc.resourceOps.UpdateResource(context.TODO(), update, dryRunOption)
if err != nil {
return fmt.Sprintf("error when updating: %v", err.Error()), err
}
return fmt.Sprintf("%s/%s updated", t.targetObj.GetKind(), t.targetObj.GetName()), nil

}
return sc.resourceOps.ReplaceResource(context.TODO(), t.targetObj, dryRunStrategy, force)
return sc.resourceOps.ReplaceResource(context.TODO(), t.targetObj, dryRunOption, force)
}

message, err = applyFn(dryRunStrategy)

message, err = applyFn(dryRunStrategy, serverSideApply)
// DryRunServer fails with "Kind does not support fieldValidation" error for kubernetes server < 1.25
// it fails inside apply.go , o.DryRunVerifier.HasSupport(info.Mapping.GroupVersionKind) line
// so we retry with DryRunClient that works for all cases, but cause issues with hooks
// Details: https://github.com/argoproj/argo-cd/issues/16177
if dryRunStrategy == cmdutil.DryRunServer && err != nil {
message, err = applyFn(cmdutil.DryRunClient)
sc.log.Info("Reapplying dry run with client strategy and server side apply false")
message, err = applyFn(cmdutil.DryRunClient, false)
}

if err != nil {
return common.ResultCodeSyncFailed, err.Error()
}
if kube.IsCRD(t.targetObj) && !dryRun {
crdName := t.targetObj.GetName()
if err = sc.ensureCRDReady(crdName); err != nil {
if err := sc.ensureCRDReady(crdName); err != nil {
sc.log.Error(err, fmt.Sprintf("failed to ensure that CRD %s is ready", crdName))
}
}
Expand Down