Skip to content

Commit b653cd5

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

File tree

3 files changed

+42
-7
lines changed

3 files changed

+42
-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: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,15 @@ 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])
73+
74+
func WithReapplySupport[T any](supportReapply bool) ReconcilerOptionFn[T] {
75+
return func(r *Reconciler[T]) {
76+
r.supportReapply = supportReapply
77+
}
6978
}
7079

7180
type UnstructuredStateReconciler = StateHandler[unstructured.Unstructured]
@@ -74,10 +83,14 @@ type ControllerSetupBuilder = ctrlrtbuilder.TypedBuilder[reconcile.Request]
7483

7584
type SetupManagerOption func(builder *ControllerSetupBuilder) *ControllerSetupBuilder
7685

77-
func NewStateReconciler[T any](target StateHandler[T]) *Reconciler[T] {
78-
return &Reconciler[T]{
86+
func NewStateReconciler[T any](target StateHandler[T], options ...ReconcilerOptionFn[T]) *Reconciler[T] {
87+
r := &Reconciler[T]{
7988
reconciler: target,
8089
}
90+
for _, opt := range options {
91+
opt(r)
92+
}
93+
return r
8194
}
8295

8396
func NewUnstructuredStateReconciler(target UnstructuredStateReconciler, gvk schema.GroupVersionKind) *Reconciler[unstructured.Unstructured] {
@@ -287,20 +300,30 @@ func (r *Reconciler[T]) ReconcileState(ctx context.Context, t *T) (Result, error
287300
result.NextState = state.StateInitial
288301
}
289302

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

294318
if isReapplyState && result.RequeueAfter == 0 && err == nil {
295319
requeueAfter, err := PatchReapplyTimestamp(ctx, r.cluster.GetClient(), obj)
296320
if err != nil {
297-
return Result{}, err
321+
return fmt.Errorf("failed to patch reapply timestamp: %w", err)
298322
}
299323

300324
result.RequeueAfter = requeueAfter
301325
}
302-
303-
return result, err
326+
return nil
304327
}
305328

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

0 commit comments

Comments
 (0)