Skip to content

Commit a56a803

Browse files
feat: Ability to create custom labels for namespaces created with syncOptions CreateNamespace (#443)
* namespace labels hook Signed-off-by: pashavictorovich <[email protected]> * add tests Signed-off-by: pashavictorovich <[email protected]> * fix test Signed-off-by: pashavictorovich <[email protected]> * rename import Signed-off-by: pashavictorovich <[email protected]> * remove deep copy Signed-off-by: pashavictorovich <[email protected]> Signed-off-by: pashavictorovich <[email protected]>
1 parent 2bc3fef commit a56a803

File tree

2 files changed

+96
-38
lines changed

2 files changed

+96
-38
lines changed

pkg/sync/sync_context.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,11 @@ func WithResourceModificationChecker(enabled bool, diffResults *diff.DiffResultL
152152
}
153153

154154
// WithNamespaceCreation will create non-exist namespace
155-
func WithNamespaceCreation(createNamespace bool, namespaceModifier func(*unstructured.Unstructured) bool) SyncOpt {
155+
func WithNamespaceCreation(createNamespace bool, namespaceModifier func(*unstructured.Unstructured) bool, namespaceCreator func(*unstructured.Unstructured) bool) SyncOpt {
156156
return func(ctx *syncContext) {
157157
ctx.createNamespace = createNamespace
158158
ctx.namespaceModifier = namespaceModifier
159+
ctx.namespaceCreator = namespaceCreator
159160
}
160161
}
161162

@@ -349,6 +350,7 @@ type syncContext struct {
349350

350351
createNamespace bool
351352
namespaceModifier func(*unstructured.Unstructured) bool
353+
namespaceCreator func(*unstructured.Unstructured) bool
352354

353355
syncWaveHook common.SyncWaveHook
354356

@@ -792,7 +794,9 @@ func (sc *syncContext) autoCreateNamespace(tasks syncTasks) syncTasks {
792794
}
793795
}
794796
} else if apierr.IsNotFound(err) {
795-
tasks = append(tasks, &syncTask{phase: common.SyncPhasePreSync, targetObj: unstructuredObj, liveObj: nil})
797+
if sc.namespaceCreator(unstructuredObj) {
798+
tasks = append(tasks, &syncTask{phase: common.SyncPhasePreSync, targetObj: unstructuredObj, liveObj: nil})
799+
}
796800
} else {
797801
task := &syncTask{phase: common.SyncPhasePreSync, targetObj: unstructuredObj}
798802
sc.setResourceResult(task, common.ResultCodeSyncFailed, common.OperationError, fmt.Sprintf("Namespace auto creation failed: %s", err))

0 commit comments

Comments
 (0)