Skip to content

Commit 38ac42f

Browse files
committed
Add configurability to reapply support
1 parent 289e7b3 commit 38ac42f

File tree

3 files changed

+43
-7
lines changed

3 files changed

+43
-7
lines changed

internal/controller/atlasthirdpartyintegrations/setup.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ func NewAtlasThirdPartyIntegrationsReconciler(
5555
deletionProtection bool,
5656
logger *zap.Logger,
5757
globalSecretRef client.ObjectKey,
58+
reapplySupport bool,
5859
) *ctrlstate.Reconciler[akov2next.AtlasThirdPartyIntegration] {
5960
return ctrlstate.NewStateReconciler(&AtlasThirdPartyIntegrationHandler{
6061
AtlasReconciler: reconciler.AtlasReconciler{
@@ -63,7 +64,7 @@ func NewAtlasThirdPartyIntegrationsReconciler(
6364
GlobalSecretRef: globalSecretRef,
6465
},
6566
deletionProtection: deletionProtection,
66-
})
67+
}, ctrlstate.WithReapplySupport[akov2next.AtlasThirdPartyIntegration](reapplySupport))
6768
}
6869

6970
func (h *AtlasThirdPartyIntegrationHandler) SetupWithManager(mgr ctrl.Manager, rec reconcile.Reconciler, opts ...ctrlstate.SetupManagerOption) error {

internal/controller/registry.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ import (
4949
ctrlstate "github.com/mongodb/mongodb-atlas-kubernetes/v2/pkg/controller/state"
5050
)
5151

52+
const DefaultReapplySupport = true
53+
5254
type Reconciler interface {
5355
reconcile.Reconciler
5456
For() (client.Object, builder.Predicates)
@@ -65,6 +67,8 @@ type Registry struct {
6567
reconcilers []Reconciler
6668
globalSecretRef client.ObjectKey
6769
experimentalReconcilers bool
70+
71+
reapplySupport bool
6872
}
6973

7074
func NewRegistry(predicates []predicate.Predicate, deletionProtection bool, logger *zap.Logger, independentSyncPeriod time.Duration, featureFlags *featureflags.FeatureFlags, globalSecretRef client.ObjectKey, experimentalReconcilers bool) *Registry {
@@ -76,6 +80,7 @@ func NewRegistry(predicates []predicate.Predicate, deletionProtection bool, logg
7680
featureFlags: featureFlags,
7781
globalSecretRef: globalSecretRef,
7882
experimentalReconcilers: experimentalReconcilers,
83+
reapplySupport: DefaultReapplySupport,
7984
}
8085
}
8186

@@ -128,7 +133,13 @@ func (r *Registry) registerControllers(c cluster.Cluster, ap atlas.Provider) {
128133

129134
func (r *Registry) appendExperimentalReconcilers(reconcilers []Reconciler, _ cluster.Cluster, ap atlas.Provider) []Reconciler {
130135
// TODO cluster.Cluster needed in initialization
131-
integrationsReconciler := integrations.NewAtlasThirdPartyIntegrationsReconciler(ap, r.deletionProtection, r.logger, r.globalSecretRef)
136+
integrationsReconciler := integrations.NewAtlasThirdPartyIntegrationsReconciler(
137+
ap,
138+
r.deletionProtection,
139+
r.logger,
140+
r.globalSecretRef,
141+
r.reapplySupport,
142+
)
132143
compatibleIntegrationsReconciler := newCtrlStateReconciler(*integrationsReconciler)
133144
reconcilers = append(reconcilers, compatibleIntegrationsReconciler)
134145
return reconcilers

pkg/controller/state/reconciler.go

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,16 @@ type Reconciler[T any] struct {
6666
cluster cluster.Cluster
6767
reconciler StateHandler[T]
6868
unstructuredGVK schema.GroupVersionKind
69+
supportReapply bool
70+
}
71+
72+
type ReconcilerOptionFn[T any] func(*Reconciler[T]) *Reconciler[T]
73+
74+
func WithReapplySupport[T any](supportReapply bool) ReconcilerOptionFn[T] {
75+
return func(r *Reconciler[T]) *Reconciler[T] {
76+
r.supportReapply = supportReapply
77+
return r
78+
}
6979
}
7080

7181
type UnstructuredStateReconciler = StateHandler[unstructured.Unstructured]
@@ -74,10 +84,14 @@ type ControllerSetupBuilder = ctrlrtbuilder.TypedBuilder[reconcile.Request]
7484

7585
type SetupManagerOption func(builder *ControllerSetupBuilder) *ControllerSetupBuilder
7686

77-
func NewStateReconciler[T any](target StateHandler[T]) *Reconciler[T] {
78-
return &Reconciler[T]{
87+
func NewStateReconciler[T any](target StateHandler[T], options ...ReconcilerOptionFn[T]) *Reconciler[T] {
88+
r := &Reconciler[T]{
7989
reconciler: target,
8090
}
91+
for _, opt := range options {
92+
r = opt(r)
93+
}
94+
return r
8195
}
8296

8397
func NewUnstructuredStateReconciler(target UnstructuredStateReconciler, gvk schema.GroupVersionKind) *Reconciler[unstructured.Unstructured] {
@@ -287,20 +301,30 @@ func (r *Reconciler[T]) ReconcileState(ctx context.Context, t *T) (Result, error
287301
result.NextState = state.StateInitial
288302
}
289303

304+
if r.supportReapply {
305+
err := r.reconcileReapply(ctx, obj, result, err)
306+
if err != nil {
307+
return Result{}, fmt.Errorf("failed to reconcile reapply: %w", err)
308+
}
309+
}
310+
311+
return result, err
312+
}
313+
314+
func (r *Reconciler[T]) reconcileReapply(ctx context.Context, obj client.Object, result Result, err error) error {
290315
isReapplyState := result.NextState == state.StateImported ||
291316
result.NextState == state.StateCreated ||
292317
result.NextState == state.StateUpdated
293318

294319
if isReapplyState && result.RequeueAfter == 0 && err == nil {
295320
requeueAfter, err := PatchReapplyTimestamp(ctx, r.cluster.GetClient(), obj)
296321
if err != nil {
297-
return Result{}, err
322+
return fmt.Errorf("failed to patch reapply timestamp: %w", err)
298323
}
299324

300325
result.RequeueAfter = requeueAfter
301326
}
302-
303-
return result, err
327+
return nil
304328
}
305329

306330
func getObservedGeneration(obj client.Object, prevStatusConditions []metav1.Condition, nextState state.ResourceState) int64 {

0 commit comments

Comments
 (0)