Skip to content
This repository was archived by the owner on Dec 17, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 31 additions & 15 deletions pkg/sync/sync_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,12 @@ func WithResourceModificationChecker(enabled bool, diffResults *diff.DiffResultL
}
}

// WithNamespaceCreation will create non-exist namespace
func WithNamespaceCreation(createNamespace bool, namespaceModifier func(*unstructured.Unstructured) bool) SyncOpt {
// WithNamespaceModifier will create a namespace with the metadata passed in the `*unstructured.Unstructured` argument
// of the `namespaceModifier` function, in the case it returns `true`. If the namespace already exists, the metadata
// will overwrite what is already present if `namespaceModifier` returns `true`. If `namespaceModifier` returns `false`,
// this will be a no-op.
func WithNamespaceModifier(namespaceModifier func(*unstructured.Unstructured) (bool, error)) SyncOpt {
return func(ctx *syncContext) {
ctx.createNamespace = createNamespace
ctx.namespaceModifier = namespaceModifier
}
}
Expand Down Expand Up @@ -349,8 +351,7 @@ type syncContext struct {
// lock to protect concurrent updates of the result list
lock sync.Mutex

createNamespace bool
namespaceModifier func(*unstructured.Unstructured) bool
namespaceModifier func(*unstructured.Unstructured) (bool, error)

syncWaveHook common.SyncWaveHook

Expand Down Expand Up @@ -685,7 +686,7 @@ func (sc *syncContext) getSyncTasks() (_ syncTasks, successful bool) {
}
}

if sc.createNamespace && sc.namespace != "" {
if sc.namespaceModifier != nil && sc.namespace != "" {
tasks = sc.autoCreateNamespace(tasks)
}

Expand Down Expand Up @@ -807,20 +808,17 @@ func (sc *syncContext) autoCreateNamespace(tasks syncTasks) syncTasks {
nsTask := &syncTask{phase: common.SyncPhasePreSync, targetObj: unstructuredObj, liveObj: liveObj}
_, ok := sc.syncRes[nsTask.resultKey()]
if ok {
tasks = append(tasks, nsTask)
tasks = sc.appendNsTask(tasks, nsTask, unstructuredObj)
} else {
sc.log.WithValues("namespace", sc.namespace).Info("Namespace already exists")
liveObjCopy := liveObj.DeepCopy()
if sc.namespaceModifier(liveObjCopy) {
tasks = append(tasks, &syncTask{phase: common.SyncPhasePreSync, targetObj: liveObjCopy, liveObj: liveObj})
if liveObj != nil {
sc.log.WithValues("namespace", sc.namespace).Info("Namespace already exists")
tasks = sc.appendNsTask(tasks, &syncTask{phase: common.SyncPhasePreSync, targetObj: unstructuredObj, liveObj: liveObj}, unstructuredObj)
}
}
} else if apierr.IsNotFound(err) {
tasks = append(tasks, &syncTask{phase: common.SyncPhasePreSync, targetObj: unstructuredObj, liveObj: nil})
tasks = sc.appendNsTask(tasks, &syncTask{phase: common.SyncPhasePreSync, targetObj: unstructuredObj, liveObj: nil}, unstructuredObj)
} else {
task := &syncTask{phase: common.SyncPhasePreSync, targetObj: unstructuredObj}
sc.setResourceResult(task, common.ResultCodeSyncFailed, common.OperationError, fmt.Sprintf("Namespace auto creation failed: %s", err))
tasks = append(tasks, task)
tasks = sc.appendFailedNsTask(tasks, unstructuredObj, fmt.Errorf("Namespace auto creation failed: %s", err))
}
} else {
sc.setOperationPhase(common.OperationFailed, fmt.Sprintf("Namespace auto creation failed: %s", err))
Expand All @@ -829,6 +827,24 @@ func (sc *syncContext) autoCreateNamespace(tasks syncTasks) syncTasks {
return tasks
}

func (sc *syncContext) appendNsTask(tasks syncTasks, preTask *syncTask, unstructuredObj *unstructured.Unstructured) syncTasks {
modified, err := sc.namespaceModifier(unstructuredObj)
if err != nil {
tasks = sc.appendFailedNsTask(tasks, unstructuredObj, fmt.Errorf("namespaceModifier error: %s", err))
} else if modified {
tasks = append(tasks, preTask)
}

return tasks
}

func (sc *syncContext) appendFailedNsTask(tasks syncTasks, unstructuredObj *unstructured.Unstructured, err error) syncTasks {
task := &syncTask{phase: common.SyncPhasePreSync, targetObj: unstructuredObj}
sc.setResourceResult(task, common.ResultCodeSyncFailed, common.OperationError, err.Error())
tasks = append(tasks, task)
return tasks
}

func isNamespaceWithName(res *unstructured.Unstructured, ns string) bool {
return isNamespaceKind(res) &&
res.GetName() == ns
Expand Down
Loading