Skip to content

Commit e284fd7

Browse files
authored
fix: managed namespaces should not mutate the live state (#479)
Signed-off-by: Leonardo Luz Almeida <[email protected]> Signed-off-by: Leonardo Luz Almeida <[email protected]>
1 parent b371e3b commit e284fd7

File tree

2 files changed

+21
-19
lines changed

2 files changed

+21
-19
lines changed

pkg/sync/sync_context.go

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,9 @@ func WithResourceModificationChecker(enabled bool, diffResults *diff.DiffResultL
157157
// of the `namespaceModifier` function, in the case it returns `true`. If the namespace already exists, the metadata
158158
// will overwrite what is already present if `namespaceModifier` returns `true`. If `namespaceModifier` returns `false`,
159159
// this will be a no-op.
160-
func WithNamespaceModifier(namespaceModifier func(*unstructured.Unstructured) (bool, error)) SyncOpt {
160+
func WithNamespaceModifier(namespaceModifier func(*unstructured.Unstructured, *unstructured.Unstructured) (bool, error)) SyncOpt {
161161
return func(ctx *syncContext) {
162-
ctx.namespaceModifier = namespaceModifier
162+
ctx.syncNamespace = namespaceModifier
163163
}
164164
}
165165

@@ -351,7 +351,9 @@ type syncContext struct {
351351
// lock to protect concurrent updates of the result list
352352
lock sync.Mutex
353353

354-
namespaceModifier func(*unstructured.Unstructured) (bool, error)
354+
// syncNamespace is a function that will determine if the managed
355+
// namespace should be synced
356+
syncNamespace func(*unstructured.Unstructured, *unstructured.Unstructured) (bool, error)
355357

356358
syncWaveHook common.SyncWaveHook
357359

@@ -686,7 +688,7 @@ func (sc *syncContext) getSyncTasks() (_ syncTasks, successful bool) {
686688
}
687689
}
688690

689-
if sc.namespaceModifier != nil && sc.namespace != "" {
691+
if sc.syncNamespace != nil && sc.namespace != "" {
690692
tasks = sc.autoCreateNamespace(tasks)
691693
}
692694

@@ -801,24 +803,24 @@ func (sc *syncContext) autoCreateNamespace(tasks syncTasks) syncTasks {
801803

802804
if isNamespaceCreationNeeded {
803805
nsSpec := &v1.Namespace{TypeMeta: metav1.TypeMeta{APIVersion: "v1", Kind: kube.NamespaceKind}, ObjectMeta: metav1.ObjectMeta{Name: sc.namespace}}
804-
unstructuredObj, err := kube.ToUnstructured(nsSpec)
806+
managedNs, err := kube.ToUnstructured(nsSpec)
805807
if err == nil {
806-
liveObj, err := sc.kubectl.GetResource(context.TODO(), sc.config, unstructuredObj.GroupVersionKind(), unstructuredObj.GetName(), metav1.NamespaceNone)
808+
liveObj, err := sc.kubectl.GetResource(context.TODO(), sc.config, managedNs.GroupVersionKind(), managedNs.GetName(), metav1.NamespaceNone)
807809
if err == nil {
808-
nsTask := &syncTask{phase: common.SyncPhasePreSync, targetObj: unstructuredObj, liveObj: liveObj}
810+
nsTask := &syncTask{phase: common.SyncPhasePreSync, targetObj: managedNs, liveObj: liveObj}
809811
_, ok := sc.syncRes[nsTask.resultKey()]
810812
if ok {
811-
tasks = sc.appendNsTask(tasks, nsTask, unstructuredObj)
813+
tasks = sc.appendNsTask(tasks, nsTask, managedNs, liveObj)
812814
} else {
813815
if liveObj != nil {
814816
sc.log.WithValues("namespace", sc.namespace).Info("Namespace already exists")
815-
tasks = sc.appendNsTask(tasks, &syncTask{phase: common.SyncPhasePreSync, targetObj: unstructuredObj, liveObj: liveObj}, unstructuredObj)
817+
tasks = sc.appendNsTask(tasks, &syncTask{phase: common.SyncPhasePreSync, targetObj: managedNs, liveObj: liveObj}, managedNs, liveObj)
816818
}
817819
}
818820
} else if apierr.IsNotFound(err) {
819-
tasks = sc.appendNsTask(tasks, &syncTask{phase: common.SyncPhasePreSync, targetObj: unstructuredObj, liveObj: nil}, unstructuredObj)
821+
tasks = sc.appendNsTask(tasks, &syncTask{phase: common.SyncPhasePreSync, targetObj: managedNs, liveObj: nil}, managedNs, nil)
820822
} else {
821-
tasks = sc.appendFailedNsTask(tasks, unstructuredObj, fmt.Errorf("Namespace auto creation failed: %s", err))
823+
tasks = sc.appendFailedNsTask(tasks, managedNs, fmt.Errorf("Namespace auto creation failed: %s", err))
822824
}
823825
} else {
824826
sc.setOperationPhase(common.OperationFailed, fmt.Sprintf("Namespace auto creation failed: %s", err))
@@ -827,10 +829,10 @@ func (sc *syncContext) autoCreateNamespace(tasks syncTasks) syncTasks {
827829
return tasks
828830
}
829831

830-
func (sc *syncContext) appendNsTask(tasks syncTasks, preTask *syncTask, unstructuredObj *unstructured.Unstructured) syncTasks {
831-
modified, err := sc.namespaceModifier(unstructuredObj)
832+
func (sc *syncContext) appendNsTask(tasks syncTasks, preTask *syncTask, managedNs, liveNs *unstructured.Unstructured) syncTasks {
833+
modified, err := sc.syncNamespace(managedNs, liveNs)
832834
if err != nil {
833-
tasks = sc.appendFailedNsTask(tasks, unstructuredObj, fmt.Errorf("namespaceModifier error: %s", err))
835+
tasks = sc.appendFailedNsTask(tasks, managedNs, fmt.Errorf("namespaceModifier error: %s", err))
834836
} else if modified {
835837
tasks = append(tasks, preTask)
836838
}

pkg/sync/sync_context_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -922,7 +922,7 @@ func TestNamespaceAutoCreation(t *testing.T) {
922922
namespace := NewNamespace()
923923
syncCtx := newTestSyncCtx(nil)
924924
syncCtx.namespace = FakeArgoCDNamespace
925-
syncCtx.namespaceModifier = func(u *unstructured.Unstructured) (bool, error) {
925+
syncCtx.syncNamespace = func(m, l *unstructured.Unstructured) (bool, error) {
926926
return true, nil
927927
}
928928
namespace.SetName(FakeArgoCDNamespace)
@@ -992,7 +992,7 @@ func TestNamespaceAutoCreation(t *testing.T) {
992992
Live: []*unstructured.Unstructured{nil},
993993
Target: []*unstructured.Unstructured{pod},
994994
})
995-
syncCtx.namespaceModifier = nil
995+
syncCtx.syncNamespace = nil
996996

997997
res := synccommon.ResourceSyncResult{
998998
ResourceKey: kube.GetResourceKey(task.obj()),
@@ -1031,7 +1031,7 @@ func TestNamespaceAutoCreationForNonExistingNs(t *testing.T) {
10311031
Target: []*unstructured.Unstructured{pod},
10321032
})
10331033
creatorCalled := false
1034-
syncCtx.namespaceModifier = func(*unstructured.Unstructured) (bool, error) {
1034+
syncCtx.syncNamespace = func(m, l *unstructured.Unstructured) (bool, error) {
10351035
creatorCalled = true
10361036
return true, nil
10371037
}
@@ -1048,7 +1048,7 @@ func TestNamespaceAutoCreationForNonExistingNs(t *testing.T) {
10481048
Target: []*unstructured.Unstructured{pod},
10491049
})
10501050
creatorCalled := false
1051-
syncCtx.namespaceModifier = func(*unstructured.Unstructured) (bool, error) {
1051+
syncCtx.syncNamespace = func(m, l *unstructured.Unstructured) (bool, error) {
10521052
creatorCalled = true
10531053
return false, nil
10541054
}
@@ -1065,7 +1065,7 @@ func TestNamespaceAutoCreationForNonExistingNs(t *testing.T) {
10651065
Target: []*unstructured.Unstructured{pod},
10661066
})
10671067
creatorCalled := false
1068-
syncCtx.namespaceModifier = func(*unstructured.Unstructured) (bool, error) {
1068+
syncCtx.syncNamespace = func(m, l *unstructured.Unstructured) (bool, error) {
10691069
creatorCalled = true
10701070
return false, errors.New("some error")
10711071
}

0 commit comments

Comments
 (0)