Skip to content

Commit 9644da1

Browse files
committed
Refactor setupWithManager
1 parent ec28bdf commit 9644da1

File tree

4 files changed

+30
-31
lines changed

4 files changed

+30
-31
lines changed

internal/controller/atlasthirdpartyintegrations/reconciler.go renamed to internal/controller/atlasthirdpartyintegrations/handler.go

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ import (
1919
"fmt"
2020

2121
ctrl "sigs.k8s.io/controller-runtime"
22-
"sigs.k8s.io/controller-runtime/pkg/builder"
2322
"sigs.k8s.io/controller-runtime/pkg/client"
23+
"sigs.k8s.io/controller-runtime/pkg/reconcile"
2424

2525
"github.com/mongodb/mongodb-atlas-kubernetes/v2/internal/controller/atlas"
2626
internalbuilder "github.com/mongodb/mongodb-atlas-kubernetes/v2/internal/controller/builder"
@@ -34,7 +34,8 @@ import (
3434
"github.com/mongodb/mongodb-atlas-kubernetes/v2/pkg/state"
3535
)
3636

37-
type Reconciler struct {
37+
type AtlasThirdPartyIntegrationHandler struct {
38+
ctrlstate.StateHandler[akov2next.AtlasThirdPartyIntegration]
3839
reconciler.AtlasReconciler
3940
}
4041

@@ -44,23 +45,23 @@ type reconcileRequest struct {
4445
Service thirdpartyintegration.ThirdPartyIntegrationService
4546
}
4647

47-
func NewAtlasThirdPartyIntegrationsReconciler() *Reconciler {
48-
return &Reconciler{}
48+
func NewAtlasThirdPartyIntegrationsReconciler() *ctrlstate.Reconciler[akov2next.AtlasThirdPartyIntegration] {
49+
return ctrlstate.NewStateReconciler(&AtlasThirdPartyIntegrationHandler{})
4950
}
5051

51-
func (r *Reconciler) NewBuilderWithManager(mgr ctrl.Manager) *builder.Builder {
52-
r.Client = mgr.GetClient()
52+
func (h *AtlasThirdPartyIntegrationHandler) SetupWithManager(mgr ctrl.Manager, rec reconcile.Reconciler) error {
53+
h.Client = mgr.GetClient()
5354
obj := &akov2next.AtlasThirdPartyIntegration{}
54-
return internalbuilder.NewDefaultControllerBuilder(mgr, obj)
55+
return internalbuilder.NewDefaultSetupWithManager(mgr, rec, obj)
5556
}
5657

57-
func (r *Reconciler) HandleInitial(ctx context.Context, integration *akov2next.AtlasThirdPartyIntegration) (ctrlstate.Result, error) {
58-
req, err := r.newReconcileRequest(ctx, integration)
58+
func (h *AtlasThirdPartyIntegrationHandler) HandleInitial(ctx context.Context, integration *akov2next.AtlasThirdPartyIntegration) (ctrlstate.Result, error) {
59+
req, err := h.newReconcileRequest(ctx, integration)
5960
if err != nil {
6061
return result.Error(state.StateInitial, fmt.Errorf("failed to build reconcile request: %w", err))
6162
}
6263

63-
integrationSpec, err := r.populateIntegration(ctx, integration)
64+
integrationSpec, err := h.populateIntegration(ctx, integration)
6465
if err != nil {
6566
return result.Error(state.StateInitial, fmt.Errorf("failed to populate integration: %w", err))
6667
}
@@ -71,28 +72,29 @@ func (r *Reconciler) HandleInitial(ctx context.Context, integration *akov2next.A
7172
integration.Spec.Type, err))
7273
}
7374
integration.Status.ID = createdIntegration.ID
74-
return result.NextState(state.StateCreating,
75+
// patch the status here (think about improvements for autogen)
76+
return result.NextState(state.StateCreated,
7577
fmt.Sprintf("Creating Atlas Third Party Integration for %s", integration.Spec.Type))
7678
}
7779

78-
func (r *Reconciler) newReconcileRequest(ctx context.Context, integration *akov2next.AtlasThirdPartyIntegration) (*reconcileRequest, error) {
80+
func (h *AtlasThirdPartyIntegrationHandler) newReconcileRequest(ctx context.Context, integration *akov2next.AtlasThirdPartyIntegration) (*reconcileRequest, error) {
7981
req := reconcileRequest{}
80-
sdkClientSet, err := r.ResolveSDKClientSet(ctx, integration)
82+
sdkClientSet, err := h.ResolveSDKClientSet(ctx, integration)
8183
if err != nil {
8284
return nil, fmt.Errorf("failed to resolve connection config: %w", err)
8385
}
8486
req.ClientSet = sdkClientSet
8587
req.Service = thirdpartyintegration.NewThirdPartyIntegrationServiceFromClientSet(sdkClientSet)
86-
project, err := r.ResolveProject(ctx, sdkClientSet.SdkClient20231115008, integration)
88+
project, err := h.ResolveProject(ctx, sdkClientSet.SdkClient20231115008, integration)
8789
if err != nil {
8890
return nil, fmt.Errorf("failed to fetch referenced project: %w", err)
8991
}
9092
req.Project = project
9193
return &req, nil
9294
}
9395

94-
func (r *Reconciler) populateIntegration(ctx context.Context, integration *akov2next.AtlasThirdPartyIntegration) (*thirdpartyintegration.ThirdPartyIntegration, error) {
95-
secrets, err := fetchIntegrationSecrets(ctx, r.Client, integration)
96+
func (h *AtlasThirdPartyIntegrationHandler) populateIntegration(ctx context.Context, integration *akov2next.AtlasThirdPartyIntegration) (*thirdpartyintegration.ThirdPartyIntegration, error) {
97+
secrets, err := fetchIntegrationSecrets(ctx, h.Client, integration)
9698
if err != nil {
9799
return nil, fmt.Errorf("failed to fetch integration secrets: %w", err)
98100
}

internal/controller/builder/builder.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ package builder
1616

1717
import (
1818
controllerruntime "sigs.k8s.io/controller-runtime"
19-
builder2 "sigs.k8s.io/controller-runtime/pkg/builder"
19+
ctrlrtbuilder "sigs.k8s.io/controller-runtime/pkg/builder"
2020
"sigs.k8s.io/controller-runtime/pkg/client"
2121
"sigs.k8s.io/controller-runtime/pkg/controller"
2222
"sigs.k8s.io/controller-runtime/pkg/predicate"
@@ -26,11 +26,11 @@ import (
2626
"github.com/mongodb/mongodb-atlas-kubernetes/v2/pkg/ratelimit"
2727
)
2828

29-
func NewDefaultControllerBuilder(mgr controllerruntime.Manager, obj client.Object) *builder2.Builder {
29+
func NewDefaultSetupWithManager(mgr controllerruntime.Manager, rec reconcile.Reconciler, obj client.Object) error {
3030
return controllerruntime.NewControllerManagedBy(mgr).
3131
For(
3232
obj,
33-
builder2.WithPredicates(
33+
ctrlrtbuilder.WithPredicates(
3434
predicate.Or(
3535
mckpredicate.AnnotationChanged("mongodb.com/reapply-period"),
3636
predicate.GenerationChangedPredicate{},
@@ -40,5 +40,5 @@ func NewDefaultControllerBuilder(mgr controllerruntime.Manager, obj client.Objec
4040
).
4141
WithOptions(controller.Options{
4242
RateLimiter: ratelimit.NewRateLimiter[reconcile.Request](),
43-
})
43+
}).Complete(rec)
4444
}

pkg/controller/state/reconciler.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import (
2626
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
2727
"k8s.io/apimachinery/pkg/runtime/schema"
2828
ctrl "sigs.k8s.io/controller-runtime"
29-
"sigs.k8s.io/controller-runtime/pkg/builder"
3029
"sigs.k8s.io/controller-runtime/pkg/client"
3130
"sigs.k8s.io/controller-runtime/pkg/cluster"
3231
"sigs.k8s.io/controller-runtime/pkg/log"
@@ -42,9 +41,8 @@ type Result struct {
4241
StateMsg string
4342
}
4443

45-
type StateReconciler[T any] interface {
46-
NewBuilderWithManager(mgr ctrl.Manager) *builder.Builder
47-
44+
type StateHandler[T any] interface {
45+
SetupWithManager(ctrl.Manager, reconcile.Reconciler) error
4846
HandleInitial(context.Context, *T) (Result, error)
4947
HandleImportRequested(context.Context, *T) (Result, error)
5048
HandleImported(context.Context, *T) (Result, error)
@@ -65,13 +63,13 @@ const (
6563

6664
type Reconciler[T any] struct {
6765
cluster cluster.Cluster
68-
reconciler StateReconciler[T]
66+
reconciler StateHandler[T]
6967
unstructuredGVK schema.GroupVersionKind
7068
}
7169

72-
type UnstructuredStateReconciler = StateReconciler[unstructured.Unstructured]
70+
type UnstructuredStateReconciler = StateHandler[unstructured.Unstructured]
7371

74-
func NewStateReconciler[T any](target StateReconciler[T]) *Reconciler[T] {
72+
func NewStateReconciler[T any](target StateHandler[T]) *Reconciler[T] {
7573
return &Reconciler[T]{
7674
reconciler: target,
7775
}
@@ -86,7 +84,7 @@ func NewUnstructuredStateReconciler(target UnstructuredStateReconciler, gvk sche
8684

8785
func (r *Reconciler[T]) SetupWithManager(mgr ctrl.Manager) error {
8886
r.cluster = mgr
89-
return r.reconciler.NewBuilderWithManager(mgr).Complete(r)
87+
return r.reconciler.SetupWithManager(mgr, r)
9088
}
9189

9290
func (r *Reconciler[T]) Reconcile(ctx context.Context, req ctrl.Request) (reconcile.Result, error) {

pkg/controller/state/reconciler_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -537,9 +537,8 @@ type dummyPodReconciler struct {
537537
handleState func(context.Context, *dummyObject) (Result, error)
538538
}
539539

540-
func (d *dummyPodReconciler) NewBuilderWithManager(mgr ctrl.Manager) *ctrl.Builder {
541-
// Not used in this test
542-
return &ctrl.Builder{}
540+
func (d *dummyPodReconciler) SetupWithManager(mgr ctrl.Manager, rec reconcile.Reconciler) error {
541+
return nil
543542
}
544543
func (d *dummyPodReconciler) HandleInitial(ctx context.Context, do *dummyObject) (Result, error) {
545544
return d.handleState(ctx, do)

0 commit comments

Comments
 (0)