Skip to content
This repository was archived by the owner on Jul 5, 2025. It is now read-only.

Commit 63401b4

Browse files
authored
feat(controller): add read-only mode to LifecycleManager (#211)
* feat(controller): add read-only mode to LifecycleManager * fix(controller): prevent conditions and spread reconciles in read-only mode
1 parent a143444 commit 63401b4

File tree

1 file changed

+28
-4
lines changed

1 file changed

+28
-4
lines changed

controller/lifecycle/lifecycle.go

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ type LifecycleManager struct {
3535
controllerName string
3636
spreadReconciles bool
3737
manageConditions bool
38+
readOnly bool
3839
prepareContextFunc PrepareContextFunc
3940
}
4041

@@ -154,7 +155,9 @@ func (l *LifecycleManager) Reconcile(ctx context.Context, req ctrl.Request, inst
154155
if !retry {
155156
l.markResourceAsFinal(instance, log, conditions, v1.ConditionFalse)
156157
}
157-
_ = updateStatus(ctx, l.client, originalCopy, instance, log, generationChanged, sentryTags)
158+
if !l.readOnly {
159+
_ = updateStatus(ctx, l.client, originalCopy, instance, log, generationChanged, sentryTags)
160+
}
158161
if !retry {
159162
return ctrl.Result{}, nil
160163
}
@@ -188,9 +191,11 @@ func (l *LifecycleManager) Reconcile(ctx context.Context, req ctrl.Request, inst
188191
MustToRuntimeObjectConditionsInterface(instance, log).SetConditions(conditions)
189192
}
190193

191-
err = updateStatus(ctx, l.client, originalCopy, instance, log, generationChanged, sentryTags)
192-
if err != nil {
193-
return result, err
194+
if !l.readOnly {
195+
err = updateStatus(ctx, l.client, originalCopy, instance, log, generationChanged, sentryTags)
196+
if err != nil {
197+
return result, err
198+
}
194199
}
195200

196201
if l.spreadReconciles && instance.GetDeletionTimestamp().IsZero() {
@@ -352,6 +357,10 @@ func (l *LifecycleManager) reconcileSubroutine(ctx context.Context, instance Run
352357
}
353358

354359
func (l *LifecycleManager) addFinalizersIfNeeded(ctx context.Context, instance RuntimeObject) error {
360+
if l.readOnly {
361+
return nil
362+
}
363+
355364
if !instance.GetDeletionTimestamp().IsZero() {
356365
return nil
357366
}
@@ -386,6 +395,10 @@ func (l *LifecycleManager) addFinalizerIfNeeded(instance RuntimeObject, subrouti
386395
}
387396

388397
func (l *LifecycleManager) removeFinalizerIfNeeded(ctx context.Context, instance RuntimeObject, subroutine Subroutine, result ctrl.Result) errors.OperatorError {
398+
if l.readOnly {
399+
return nil
400+
}
401+
389402
if !result.Requeue && result.RequeueAfter == 0 {
390403
update := false
391404
for _, f := range subroutine.Finalizers() {
@@ -410,6 +423,10 @@ func (l *LifecycleManager) SetupWithManagerBuilder(mgr ctrl.Manager, maxReconcil
410423
return nil, err
411424
}
412425

426+
if (l.manageConditions || l.spreadReconciles) && l.readOnly {
427+
return nil, fmt.Errorf("cannot use conditions or spread reconciles in read-only mode")
428+
}
429+
413430
eventPredicates = append([]predicate.Predicate{filter.DebugResourcesBehaviourPredicate(debugLabelValue)}, eventPredicates...)
414431
return ctrl.NewControllerManagedBy(mgr).
415432
Named(reconcilerName).
@@ -436,3 +453,10 @@ func (l *LifecycleManager) WithPrepareContextFunc(prepareFunction PrepareContext
436453
l.prepareContextFunc = prepareFunction
437454
return l
438455
}
456+
457+
// WithReadOnly allows to set the controller to read-only mode
458+
// In read-only mode, the controller will not update the status of the instance
459+
func (l *LifecycleManager) WithReadOnly() *LifecycleManager {
460+
l.readOnly = true
461+
return l
462+
}

0 commit comments

Comments
 (0)