@@ -157,9 +157,9 @@ func WithResourceModificationChecker(enabled bool, diffResults *diff.DiffResultL
157
157
// of the `namespaceModifier` function, in the case it returns `true`. If the namespace already exists, the metadata
158
158
// will overwrite what is already present if `namespaceModifier` returns `true`. If `namespaceModifier` returns `false`,
159
159
// 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 {
161
161
return func (ctx * syncContext ) {
162
- ctx .namespaceModifier = namespaceModifier
162
+ ctx .syncNamespace = namespaceModifier
163
163
}
164
164
}
165
165
@@ -351,7 +351,9 @@ type syncContext struct {
351
351
// lock to protect concurrent updates of the result list
352
352
lock sync.Mutex
353
353
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 )
355
357
356
358
syncWaveHook common.SyncWaveHook
357
359
@@ -686,7 +688,7 @@ func (sc *syncContext) getSyncTasks() (_ syncTasks, successful bool) {
686
688
}
687
689
}
688
690
689
- if sc .namespaceModifier != nil && sc .namespace != "" {
691
+ if sc .syncNamespace != nil && sc .namespace != "" {
690
692
tasks = sc .autoCreateNamespace (tasks )
691
693
}
692
694
@@ -801,24 +803,24 @@ func (sc *syncContext) autoCreateNamespace(tasks syncTasks) syncTasks {
801
803
802
804
if isNamespaceCreationNeeded {
803
805
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 )
805
807
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 )
807
809
if err == nil {
808
- nsTask := & syncTask {phase : common .SyncPhasePreSync , targetObj : unstructuredObj , liveObj : liveObj }
810
+ nsTask := & syncTask {phase : common .SyncPhasePreSync , targetObj : managedNs , liveObj : liveObj }
809
811
_ , ok := sc .syncRes [nsTask .resultKey ()]
810
812
if ok {
811
- tasks = sc .appendNsTask (tasks , nsTask , unstructuredObj )
813
+ tasks = sc .appendNsTask (tasks , nsTask , managedNs , liveObj )
812
814
} else {
813
815
if liveObj != nil {
814
816
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 )
816
818
}
817
819
}
818
820
} 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 )
820
822
} 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 ))
822
824
}
823
825
} else {
824
826
sc .setOperationPhase (common .OperationFailed , fmt .Sprintf ("Namespace auto creation failed: %s" , err ))
@@ -827,10 +829,10 @@ func (sc *syncContext) autoCreateNamespace(tasks syncTasks) syncTasks {
827
829
return tasks
828
830
}
829
831
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 )
832
834
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 ))
834
836
} else if modified {
835
837
tasks = append (tasks , preTask )
836
838
}
0 commit comments