@@ -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 }
0 commit comments