Skip to content

Commit 847cfc9

Browse files
fix: Ability to disable Server Side Apply on individual resource level (#634)
* fix: Ability to disable Server Side Apply on individual resource level Signed-off-by: pashakostohrys <[email protected]> * fix: Ability to disable Server Side Apply on individual resource level Signed-off-by: pashakostohrys <[email protected]> --------- Signed-off-by: pashakostohrys <[email protected]>
1 parent 9ab0b2e commit 847cfc9

File tree

3 files changed

+26
-5
lines changed

3 files changed

+26
-5
lines changed

pkg/sync/common/types.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ const (
3232
SyncOptionForce = "Force=true"
3333
// Sync option that enables use of --server-side flag instead of client-side
3434
SyncOptionServerSideApply = "ServerSideApply=true"
35+
// Sync option that disables use of --server-side flag instead of client-side
36+
SyncOptionDisableServerSideApply = "ServerSideApply=false"
3537
// Sync option that disables resource deletion
3638
SyncOptionDisableDeletion = "Delete=false"
3739
// Sync option that sync only out of sync resources

pkg/sync/sync_context.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -948,6 +948,23 @@ func (sc *syncContext) ensureCRDReady(name string) error {
948948
})
949949
}
950950

951+
func (sc *syncContext) shouldUseServerSideApply(targetObj *unstructured.Unstructured) bool {
952+
// if it is a dry run, disable server side apply, as the goal is to validate only the
953+
// yaml correctness of the rendered manifests.
954+
// running dry-run in server mode breaks the auto create namespace feature
955+
// https://github.com/argoproj/argo-cd/issues/13874
956+
if sc.dryRun {
957+
return false
958+
}
959+
960+
resourceHasDisableSSAAnnotation := resourceutil.HasAnnotationOption(targetObj, common.AnnotationSyncOptions, common.SyncOptionDisableServerSideApply)
961+
if resourceHasDisableSSAAnnotation {
962+
return false
963+
}
964+
965+
return sc.serverSideApply || resourceutil.HasAnnotationOption(targetObj, common.AnnotationSyncOptions, common.SyncOptionServerSideApply)
966+
}
967+
951968
func (sc *syncContext) applyObject(t *syncTask, dryRun, validate bool) (common.ResultCode, string) {
952969
dryRunStrategy := cmdutil.DryRunNone
953970
if dryRun {
@@ -963,11 +980,7 @@ func (sc *syncContext) applyObject(t *syncTask, dryRun, validate bool) (common.R
963980
var message string
964981
shouldReplace := sc.replace || resourceutil.HasAnnotationOption(t.targetObj, common.AnnotationSyncOptions, common.SyncOptionReplace)
965982
force := sc.force || resourceutil.HasAnnotationOption(t.targetObj, common.AnnotationSyncOptions, common.SyncOptionForce)
966-
// if it is a dry run, disable server side apply, as the goal is to validate only the
967-
// yaml correctness of the rendered manifests.
968-
// running dry-run in server mode breaks the auto create namespace feature
969-
// https://github.com/argoproj/argo-cd/issues/13874
970-
serverSideApply := !dryRun && (sc.serverSideApply || resourceutil.HasAnnotationOption(t.targetObj, common.AnnotationSyncOptions, common.SyncOptionServerSideApply))
983+
serverSideApply := sc.shouldUseServerSideApply(t.targetObj)
971984
if shouldReplace {
972985
if t.liveObj != nil {
973986
// Avoid using `kubectl replace` for CRDs since 'replace' might recreate resource and so delete all CRD instances.

pkg/sync/sync_context_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -792,6 +792,11 @@ func withServerSideApplyAnnotation(un *unstructured.Unstructured) *unstructured.
792792
return un
793793
}
794794

795+
func withDisableServerSideApplyAnnotation(un *unstructured.Unstructured) *unstructured.Unstructured {
796+
un.SetAnnotations(map[string]string{synccommon.AnnotationSyncOptions: synccommon.SyncOptionDisableServerSideApply})
797+
return un
798+
}
799+
795800
func withReplaceAndServerSideApplyAnnotations(un *unstructured.Unstructured) *unstructured.Unstructured {
796801
un.SetAnnotations(map[string]string{synccommon.AnnotationSyncOptions: "Replace=true,ServerSideApply=true"})
797802
return un
@@ -808,6 +813,7 @@ func TestSync_ServerSideApply(t *testing.T) {
808813
}{
809814
{"NoAnnotation", NewPod(), NewPod(), "apply", false, "managerA"},
810815
{"ServerSideApplyAnnotationIsSet", withServerSideApplyAnnotation(NewPod()), NewPod(), "apply", true, "managerB"},
816+
{"DisableServerSideApplyAnnotationIsSet", withDisableServerSideApplyAnnotation(NewPod()), NewPod(), "apply", false, "managerB"},
811817
{"ServerSideApplyAndReplaceAnnotationsAreSet", withReplaceAndServerSideApplyAnnotations(NewPod()), NewPod(), "replace", false, ""},
812818
{"ServerSideApplyAndReplaceAnnotationsAreSetNamespace", withReplaceAndServerSideApplyAnnotations(NewNamespace()), NewNamespace(), "update", false, ""},
813819
{"LiveObjectMissing", withReplaceAnnotation(NewPod()), nil, "create", false, ""},

0 commit comments

Comments
 (0)