Skip to content

CLOUDP-316083: Fix initialisation breaking rate limiting #2401

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions internal/controller/atlasthirdpartyintegrations/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ import (
"github.com/mongodb/mongodb-atlas-kubernetes/v2/internal/translation/thirdpartyintegration"
ctrlstate "github.com/mongodb/mongodb-atlas-kubernetes/v2/pkg/controller/state"
mckpredicate "github.com/mongodb/mongodb-atlas-kubernetes/v2/pkg/predicate"
"github.com/mongodb/mongodb-atlas-kubernetes/v2/pkg/ratelimit"
)

type serviceBuilderFunc func(*atlas.ClientSet) thirdpartyintegration.ThirdPartyIntegrationService
Expand Down Expand Up @@ -83,10 +82,10 @@ func (r *AtlasThirdPartyIntegrationHandler) For() (client.Object, builder.Predic
return &akov2next.AtlasThirdPartyIntegration{}, builder.WithPredicates(predicate.GenerationChangedPredicate{})
}

func (h *AtlasThirdPartyIntegrationHandler) SetupWithManager(mgr ctrl.Manager, rec reconcile.Reconciler, opts ...ctrlstate.SetupManagerOption) error {
func (h *AtlasThirdPartyIntegrationHandler) SetupWithManager(mgr ctrl.Manager, rec reconcile.Reconciler, defaultOptions controller.Options) error {
h.Client = mgr.GetClient()
obj := &akov2next.AtlasThirdPartyIntegration{}
builder := controllerruntime.NewControllerManagedBy(mgr).
return controllerruntime.NewControllerManagedBy(mgr).
For(
obj,
ctrlrtbuilder.WithPredicates(
Expand All @@ -107,10 +106,7 @@ func (h *AtlasThirdPartyIntegrationHandler) SetupWithManager(mgr ctrl.Manager, r
handler.EnqueueRequestsFromMapFunc(h.integrationForSecretMapFunc()),
builder.WithPredicates(predicate.ResourceVersionChangedPredicate{}),
).
WithOptions(controller.Options{
RateLimiter: ratelimit.NewRateLimiter[reconcile.Request](),
})
return ctrlstate.ApplyOptions(builder, opts...).Complete(rec)
WithOptions(defaultOptions).Complete(rec)
}

func (h *AtlasThirdPartyIntegrationHandler) integrationForProjectMapFunc() handler.MapFunc {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
"sigs.k8s.io/controller-runtime/pkg/cluster"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

Expand Down Expand Up @@ -122,7 +123,7 @@ func TestSetupWithManager(t *testing.T) {
deletionProtection: false,
}

require.NoError(t, handler.SetupWithManager(fakeMgr, &fakeReconciler{}))
require.NoError(t, handler.SetupWithManager(fakeMgr, &fakeReconciler{}, controller.Options{}))
}

func TestNewReconcileRequest(t *testing.T) {
Expand Down
18 changes: 8 additions & 10 deletions internal/controller/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import (
"github.com/mongodb/mongodb-atlas-kubernetes/v2/internal/pointer"
"github.com/mongodb/mongodb-atlas-kubernetes/v2/internal/version"
ctrlstate "github.com/mongodb/mongodb-atlas-kubernetes/v2/pkg/controller/state"
"github.com/mongodb/mongodb-atlas-kubernetes/v2/pkg/ratelimit"
)

const DefaultReapplySupport = true
Expand Down Expand Up @@ -133,7 +134,7 @@ func (r *Registry) registerControllers(c cluster.Cluster, ap atlas.Provider) {
r.globalSecretRef,
r.reapplySupport,
)
compatibleIntegrationsReconciler := newCtrlStateReconciler(*integrationsReconciler)
compatibleIntegrationsReconciler := newCtrlStateReconciler(integrationsReconciler)
reconcilers = append(reconcilers, compatibleIntegrationsReconciler)
}
r.reconcilers = reconcilers
Expand All @@ -151,20 +152,17 @@ func (r *Registry) defaultPredicates() []predicate.Predicate {
}

type ctrlStateReconciler[T any] struct {
ctrlstate.Reconciler[T]
*ctrlstate.Reconciler[T]
}

func newCtrlStateReconciler[T any](r ctrlstate.Reconciler[T]) *ctrlStateReconciler[T] {
func newCtrlStateReconciler[T any](r *ctrlstate.Reconciler[T]) *ctrlStateReconciler[T] {
return &ctrlStateReconciler[T]{Reconciler: r}
}

func (nr *ctrlStateReconciler[T]) SetupWithManager(mgr ctrl.Manager, skipNameValidation bool) error {
skipNameOptionFn := func(skipNameValidation bool) ctrlstate.SetupManagerOption {
return func(builder *ctrlstate.ControllerSetupBuilder) *ctrlstate.ControllerSetupBuilder {
return builder.WithOptions(controller.TypedOptions[reconcile.Request]{
SkipNameValidation: pointer.MakePtr(skipNameValidation),
})
}
defaultReconcilerOptions := controller.TypedOptions[reconcile.Request]{
RateLimiter: ratelimit.NewRateLimiter[reconcile.Request](),
SkipNameValidation: pointer.MakePtr(skipNameValidation),
}
return nr.Reconciler.SetupWithManager(mgr, skipNameOptionFn(skipNameValidation))
return nr.Reconciler.SetupWithManager(mgr, defaultReconcilerOptions)
}
61 changes: 61 additions & 0 deletions internal/controller/registry_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright 2025 MongoDB Inc
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package controller

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

"github.com/mongodb/mongodb-atlas-kubernetes/v2/internal/pointer"
ctrlstate "github.com/mongodb/mongodb-atlas-kubernetes/v2/pkg/controller/state"
"github.com/mongodb/mongodb-atlas-kubernetes/v2/pkg/ratelimit"
)

func TestCtrlStateReconciler_SetupWithManager_NoGomock(t *testing.T) {
fakeMgr := &fakeManager{}
mock := mockStateReconciler{}
fakeReconciler := ctrlstate.NewStateReconciler(&mock)
skipNameValidation := true

r := newCtrlStateReconciler(fakeReconciler)
require.NoError(t, r.SetupWithManager(fakeMgr, skipNameValidation))
require.Equal(t, fakeMgr, mock.ReceivedMgr)
wantOpts := controller.TypedOptions[reconcile.Request]{
SkipNameValidation: pointer.MakePtr(skipNameValidation),
RateLimiter: ratelimit.NewRateLimiter[reconcile.Request](),
}
assert.Equal(t, wantOpts, mock.ReceivedOpts)
}

type fakeManager struct {
ctrl.Manager
}

type mockStateReconciler struct {
ctrlstate.StateHandler[mockStateReconciler]
ReceivedMgr ctrl.Manager
ReceivedOpts controller.TypedOptions[reconcile.Request]
}

func (m *mockStateReconciler) SetupWithManager(mgr ctrl.Manager, reconciler reconcile.Reconciler, opts controller.Options) error {
m.ReceivedMgr = mgr
m.ReceivedOpts = opts
return nil
}
16 changes: 4 additions & 12 deletions pkg/controller/state/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
ctrlrtbuilder "sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/cluster"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

Expand All @@ -44,7 +45,7 @@ type Result struct {
}

type StateHandler[T any] interface {
SetupWithManager(ctrl.Manager, reconcile.Reconciler, ...SetupManagerOption) error
SetupWithManager(ctrl.Manager, reconcile.Reconciler, controller.Options) error
For() (client.Object, builder.Predicates)
HandleInitial(context.Context, *T) (Result, error)
HandleImportRequested(context.Context, *T) (Result, error)
Expand Down Expand Up @@ -89,8 +90,6 @@ type UnstructuredStateReconciler = StateHandler[unstructured.Unstructured]

type ControllerSetupBuilder = ctrlrtbuilder.TypedBuilder[reconcile.Request]

type SetupManagerOption func(builder *ControllerSetupBuilder) *ControllerSetupBuilder

func NewStateReconciler[T any](target StateHandler[T], options ...ReconcilerOptionFn[T]) *Reconciler[T] {
r := &Reconciler[T]{
reconciler: target,
Expand All @@ -108,22 +107,15 @@ func NewUnstructuredStateReconciler(target UnstructuredStateReconciler, gvk sche
}
}

func (r *Reconciler[T]) SetupWithManager(mgr ctrl.Manager, options ...SetupManagerOption) error {
func (r *Reconciler[T]) SetupWithManager(mgr ctrl.Manager, defaultOptions controller.Options) error {
r.cluster = mgr
return r.reconciler.SetupWithManager(mgr, r, options...)
return r.reconciler.SetupWithManager(mgr, r, defaultOptions)
}

func (r *Reconciler[T]) For() (client.Object, builder.Predicates) {
return r.reconciler.For()
}

func ApplyOptions(builder *ControllerSetupBuilder, options ...SetupManagerOption) *ControllerSetupBuilder {
for _, optionFn := range options {
builder = optionFn(builder)
}
return builder
}

func (r *Reconciler[T]) Reconcile(ctx context.Context, req ctrl.Request) (reconcile.Result, error) {
logger := log.FromContext(ctx).WithName("state")
logger.Info("reconcile started", "req", req)
Expand Down
3 changes: 2 additions & 1 deletion pkg/controller/state/reconciler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client/fake"
"sigs.k8s.io/controller-runtime/pkg/client/interceptor"
"sigs.k8s.io/controller-runtime/pkg/cluster"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

"github.com/mongodb/mongodb-atlas-kubernetes/v2/pkg/state"
Expand Down Expand Up @@ -538,7 +539,7 @@ type dummyPodReconciler struct {
handleState func(context.Context, *dummyObject) (Result, error)
}

func (d *dummyPodReconciler) SetupWithManager(_ ctrl.Manager, _ reconcile.Reconciler, _ ...SetupManagerOption) error {
func (d *dummyPodReconciler) SetupWithManager(_ ctrl.Manager, _ reconcile.Reconciler, _ controller.Options) error {
return nil
}
func (d *dummyPodReconciler) For() (client.Object, builder.Predicates) {
Expand Down
Loading