Skip to content

Commit ad9a694

Browse files
fix: do not replace namespaces (#524)
When doing `kubectl replace`, namespaces should not be affected. Fixes argoproj/argo-cd#12810 and argoproj/argo-cd#12539. Signed-off-by: Blake Pettersson <[email protected]>
1 parent b4dd8b8 commit ad9a694

File tree

2 files changed

+5
-2
lines changed

2 files changed

+5
-2
lines changed

pkg/sync/sync_context.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -915,8 +915,10 @@ func (sc *syncContext) applyObject(t *syncTask, dryRun, force, validate bool) (c
915915
serverSideApply := sc.serverSideApply || resourceutil.HasAnnotationOption(t.targetObj, common.AnnotationSyncOptions, common.SyncOptionServerSideApply)
916916
if shouldReplace {
917917
if t.liveObj != nil {
918-
// Avoid using `kubectl replace` for CRDs since 'replace' might recreate resource and so delete all CRD instances
919-
if kube.IsCRD(t.targetObj) {
918+
// Avoid using `kubectl replace` for CRDs since 'replace' might recreate resource and so delete all CRD instances.
919+
// The same thing applies for namespaces, which would delete the namespace as well as everything within it,
920+
// so we want to avoid using `kubectl replace` in that case as well.
921+
if kube.IsCRD(t.targetObj) || t.targetObj.GetKind() == kubeutil.NamespaceKind {
920922
update := t.targetObj.DeepCopy()
921923
update.SetResourceVersion(t.liveObj.GetResourceVersion())
922924
_, err = sc.resourceOps.UpdateResource(context.TODO(), update, dryRunStrategy)

pkg/sync/sync_context_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -784,6 +784,7 @@ func TestSync_ServerSideApply(t *testing.T) {
784784
{"NoAnnotation", NewPod(), NewPod(), "apply", false, "managerA"},
785785
{"ServerSideApplyAnnotationIsSet", withServerSideApplyAnnotation(NewPod()), NewPod(), "apply", true, "managerB"},
786786
{"ServerSideApplyAndReplaceAnnotationsAreSet", withReplaceAndServerSideApplyAnnotations(NewPod()), NewPod(), "replace", false, ""},
787+
{"ServerSideApplyAndReplaceAnnotationsAreSetNamespace", withReplaceAndServerSideApplyAnnotations(NewNamespace()), NewNamespace(), "update", false, ""},
787788
{"LiveObjectMissing", withReplaceAnnotation(NewPod()), nil, "create", false, ""},
788789
}
789790

0 commit comments

Comments
 (0)