Skip to content

Commit b371e3b

Browse files
Update namespace v2 (#465)
* Revert "Revert "feat: Ability to create custom labels for namespaces created … (#455)" This reverts commit ce2fb70. Signed-off-by: Blake Pettersson <[email protected]> * feat: enable namespace to be updated Rename `WithNamespaceCreation` to `WithNamespaceModifier`, since this method is also used for modifying existing namespaces. This method takes a single argument for the actual updating, and unless this method gets invoked by its caller no updating will take place (fulfilling what the `createNamespace` argument used to do). Within `autoCreateNamespace`, everywhere where we previously added tasks we'll now need to check whether the namespace should be created (or modified), which is now delegated to the `appendNsTask` and `appendFailedNsTask` methods. Signed-off-by: Blake Pettersson <[email protected]> Signed-off-by: Blake Pettersson <[email protected]>
1 parent 9664cf8 commit b371e3b

File tree

2 files changed

+176
-55
lines changed

2 files changed

+176
-55
lines changed

pkg/sync/sync_context.go

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -153,10 +153,12 @@ func WithResourceModificationChecker(enabled bool, diffResults *diff.DiffResultL
153153
}
154154
}
155155

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

352-
createNamespace bool
353-
namespaceModifier func(*unstructured.Unstructured) bool
354+
namespaceModifier func(*unstructured.Unstructured) (bool, error)
354355

355356
syncWaveHook common.SyncWaveHook
356357

@@ -685,7 +686,7 @@ func (sc *syncContext) getSyncTasks() (_ syncTasks, successful bool) {
685686
}
686687
}
687688

688-
if sc.createNamespace && sc.namespace != "" {
689+
if sc.namespaceModifier != nil && sc.namespace != "" {
689690
tasks = sc.autoCreateNamespace(tasks)
690691
}
691692

@@ -807,20 +808,17 @@ func (sc *syncContext) autoCreateNamespace(tasks syncTasks) syncTasks {
807808
nsTask := &syncTask{phase: common.SyncPhasePreSync, targetObj: unstructuredObj, liveObj: liveObj}
808809
_, ok := sc.syncRes[nsTask.resultKey()]
809810
if ok {
810-
tasks = append(tasks, nsTask)
811+
tasks = sc.appendNsTask(tasks, nsTask, unstructuredObj)
811812
} else {
812-
sc.log.WithValues("namespace", sc.namespace).Info("Namespace already exists")
813-
liveObjCopy := liveObj.DeepCopy()
814-
if sc.namespaceModifier(liveObjCopy) {
815-
tasks = append(tasks, &syncTask{phase: common.SyncPhasePreSync, targetObj: liveObjCopy, liveObj: liveObj})
813+
if liveObj != nil {
814+
sc.log.WithValues("namespace", sc.namespace).Info("Namespace already exists")
815+
tasks = sc.appendNsTask(tasks, &syncTask{phase: common.SyncPhasePreSync, targetObj: unstructuredObj, liveObj: liveObj}, unstructuredObj)
816816
}
817817
}
818818
} else if apierr.IsNotFound(err) {
819-
tasks = append(tasks, &syncTask{phase: common.SyncPhasePreSync, targetObj: unstructuredObj, liveObj: nil})
819+
tasks = sc.appendNsTask(tasks, &syncTask{phase: common.SyncPhasePreSync, targetObj: unstructuredObj, liveObj: nil}, unstructuredObj)
820820
} else {
821-
task := &syncTask{phase: common.SyncPhasePreSync, targetObj: unstructuredObj}
822-
sc.setResourceResult(task, common.ResultCodeSyncFailed, common.OperationError, fmt.Sprintf("Namespace auto creation failed: %s", err))
823-
tasks = append(tasks, task)
821+
tasks = sc.appendFailedNsTask(tasks, unstructuredObj, fmt.Errorf("Namespace auto creation failed: %s", err))
824822
}
825823
} else {
826824
sc.setOperationPhase(common.OperationFailed, fmt.Sprintf("Namespace auto creation failed: %s", err))
@@ -829,6 +827,24 @@ func (sc *syncContext) autoCreateNamespace(tasks syncTasks) syncTasks {
829827
return tasks
830828
}
831829

830+
func (sc *syncContext) appendNsTask(tasks syncTasks, preTask *syncTask, unstructuredObj *unstructured.Unstructured) syncTasks {
831+
modified, err := sc.namespaceModifier(unstructuredObj)
832+
if err != nil {
833+
tasks = sc.appendFailedNsTask(tasks, unstructuredObj, fmt.Errorf("namespaceModifier error: %s", err))
834+
} else if modified {
835+
tasks = append(tasks, preTask)
836+
}
837+
838+
return tasks
839+
}
840+
841+
func (sc *syncContext) appendFailedNsTask(tasks syncTasks, unstructuredObj *unstructured.Unstructured, err error) syncTasks {
842+
task := &syncTask{phase: common.SyncPhasePreSync, targetObj: unstructuredObj}
843+
sc.setResourceResult(task, common.ResultCodeSyncFailed, common.OperationError, err.Error())
844+
tasks = append(tasks, task)
845+
return tasks
846+
}
847+
832848
func isNamespaceWithName(res *unstructured.Unstructured, ns string) bool {
833849
return isNamespaceKind(res) &&
834850
res.GetName() == ns

0 commit comments

Comments
 (0)