Skip to content

Commit

Permalink
chore: change fmt.Print to logger call and add context param to Setup…
Browse files Browse the repository at this point in the history
…WithManager() (#564)
  • Loading branch information
pmalek authored Sep 9, 2024
1 parent 74ba82d commit dcb56ad
Show file tree
Hide file tree
Showing 14 changed files with 26 additions and 22 deletions.
2 changes: 1 addition & 1 deletion controller/controlplane/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ type Reconciler struct {
const requeueWithoutBackoff = time.Millisecond * 200

// SetupWithManager sets up the controller with the Manager.
func (r *Reconciler) SetupWithManager(mgr ctrl.Manager) error {
func (r *Reconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager) error {
// for owned objects we need to check if updates to the objects resulted in the
// removal of an OwnerReference to the parent object, and if so we need to
// enqueue the parent object so that reconciliation can create a replacement.
Expand Down
2 changes: 1 addition & 1 deletion controller/dataplane/bluegreen_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ type BlueGreenReconciler struct {
}

// SetupWithManager sets up the controller with the Manager.
func (r *BlueGreenReconciler) SetupWithManager(mgr ctrl.Manager) error {
func (r *BlueGreenReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager) error {
delegate, ok := r.DataPlaneController.(*Reconciler)
if !ok {
return fmt.Errorf("incorrect delegate controller type: %T", r.DataPlaneController)
Expand Down
2 changes: 1 addition & 1 deletion controller/dataplane/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ type Reconciler struct {
}

// SetupWithManager sets up the controller with the Manager.
func (r *Reconciler) SetupWithManager(mgr ctrl.Manager) error {
func (r *Reconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager) error {
r.eventRecorder = mgr.GetEventRecorderFor("dataplane")

return DataPlaneWatchBuilder(mgr).
Expand Down
2 changes: 1 addition & 1 deletion controller/dataplane/owned_finalizer_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func NewDataPlaneOwnedResourceFinalizerReconciler[T DataPlaneOwnedResource, PT D
}

// SetupWithManager sets up the controller with the Manager.
func (r *DataPlaneOwnedResourceFinalizerReconciler[T, PT]) SetupWithManager(mgr ctrl.Manager) error {
func (r *DataPlaneOwnedResourceFinalizerReconciler[T, PT]) SetupWithManager(ctx context.Context, mgr ctrl.Manager) error {
objectIsBeingDeleted := func(obj client.Object) bool {
return obj.GetDeletionTimestamp() != nil
}
Expand Down
2 changes: 1 addition & 1 deletion controller/gateway/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ type Reconciler struct {
const provisionDataPlaneFailRetryAfter = 5 * time.Second

// SetupWithManager sets up the controller with the Manager.
func (r *Reconciler) SetupWithManager(mgr ctrl.Manager) error {
func (r *Reconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
// watch Gateway objects, filtering out any Gateways which are not configured with
// a supported GatewayClass controller name.
Expand Down
2 changes: 1 addition & 1 deletion controller/gatewayclass/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type Reconciler struct {
}

// SetupWithManager sets up the controller with the Manager.
func (r *Reconciler) SetupWithManager(mgr ctrl.Manager) error {
func (r *Reconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&gatewayv1.GatewayClass{},
builder.WithPredicates(predicate.NewPredicateFuncs(r.gatewayClassMatches))).
Expand Down
2 changes: 1 addition & 1 deletion controller/kongplugininstallation/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type Reconciler struct {
}

// SetupWithManager sets up the controller with the Manager.
func (r *Reconciler) SetupWithManager(mgr ctrl.Manager) error {
func (r *Reconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&v1alpha1.KongPluginInstallation{}).
WithEventFilter(predicate.GenerationChangedPredicate{}).
Expand Down
8 changes: 5 additions & 3 deletions controller/konnect/reconciler_generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ const (
)

// SetupWithManager sets up the controller with the given manager.
func (r *KonnectEntityReconciler[T, TEnt]) SetupWithManager(mgr ctrl.Manager) error {
func (r *KonnectEntityReconciler[T, TEnt]) SetupWithManager(ctx context.Context, mgr ctrl.Manager) error {
var (
e T
ent = TEnt(&e)
Expand All @@ -99,11 +99,13 @@ func (r *KonnectEntityReconciler[T, TEnt]) SetupWithManager(mgr ctrl.Manager) er
WithOptions(controller.Options{
MaxConcurrentReconciles: MaxConcurrentReconciles,
})
entityTypeName = constraints.EntityTypeName[T]()
logger = log.GetLogger(ctx, entityTypeName, r.DevelopmentMode)
)

for _, ind := range ReconciliationIndexOptionsForEntity(ent) {
if err := mgr.GetCache().IndexField(context.Background(), ind.IndexObject, ind.IndexField, ind.ExtractValue); err != nil {
fmt.Println("Create index:", err)
logger.Info("creating index", "entityTypeName", entityTypeName, "indexObject", ind.IndexObject, "indexField", ind.IndexField)
if err := mgr.GetCache().IndexField(ctx, ind.IndexObject, ind.IndexField, ind.ExtractValue); err != nil {
return err
}
}
Expand Down
4 changes: 2 additions & 2 deletions controller/konnect/reconciler_generic_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package konnect

import (
"context"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -45,12 +46,11 @@ func testNewKonnectEntityReconciler[
t.Run(ent.GetTypeName(), func(t *testing.T) {
cl := fakectrlruntimeclient.NewFakeClient()
mgr, err := ctrl.NewManager(&rest.Config{}, ctrl.Options{

Scheme: scheme.Get(),
})
require.NoError(t, err)

reconciler := NewKonnectEntityReconciler[T, TEnt](sdkFactory, false, cl)
require.NoError(t, reconciler.SetupWithManager(mgr))
require.NoError(t, reconciler.SetupWithManager(context.Background(), mgr))
})
}
2 changes: 1 addition & 1 deletion controller/konnect/reconciler_konnectapiauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func NewKonnectAPIAuthConfigurationReconciler(
}

// SetupWithManager sets up the controller with the Manager.
func (r *KonnectAPIAuthConfigurationReconciler) SetupWithManager(mgr ctrl.Manager) error {
func (r *KonnectAPIAuthConfigurationReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager) error {
secretLabelPredicate, err := predicate.LabelSelectorPredicate(
metav1.LabelSelector{
MatchLabels: map[string]string{
Expand Down
2 changes: 1 addition & 1 deletion controller/specialized/aigateway_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type AIGatewayReconciler struct {
}

// SetupWithManager sets up the controller with the Manager.
func (r *AIGatewayReconciler) SetupWithManager(mgr ctrl.Manager) error {
func (r *AIGatewayReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
// watch AIGateway objects, filtering out any Gateways which are not
// configured with a supported GatewayClass controller name.
Expand Down
6 changes: 3 additions & 3 deletions modules/manager/controller_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func SetupControllersShim(mgr manager.Manager, c *Config) ([]ControllerDef, erro

// Controller is a Kubernetes controller that can be plugged into Manager.
type Controller interface {
SetupWithManager(ctrl.Manager) error
SetupWithManager(context.Context, ctrl.Manager) error
}

// AutoHandler decides whether the specific controller shall be enabled (true) or disabled (false).
Expand All @@ -109,12 +109,12 @@ func (c *ControllerDef) Name() string {

// MaybeSetupWithManager runs SetupWithManager on the controller if it is enabled
// and its AutoHandler (if any) indicates that it can load.
func (c *ControllerDef) MaybeSetupWithManager(mgr ctrl.Manager) error {
func (c *ControllerDef) MaybeSetupWithManager(ctx context.Context, mgr ctrl.Manager) error {
if !c.Enabled {
return nil
}

return c.Controller.SetupWithManager(mgr)
return c.Controller.SetupWithManager(ctx, mgr)
}

func setupIndexes(ctx context.Context, mgr manager.Manager, cfg Config) error {
Expand Down
10 changes: 6 additions & 4 deletions modules/manager/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,9 @@ func Run(
return fmt.Errorf("unable to start manager: %w", err)
}

if err := setupIndexes(context.Background(), mgr, cfg); err != nil {
ctx := context.Background()

if err := setupIndexes(ctx, mgr, cfg); err != nil {
return err
}

Expand All @@ -218,7 +220,7 @@ func Run(
logger: ctrl.Log.WithName("webhook_manager"),
cfg: &cfg,
}
if err := webhookMgr.PrepareWebhookServerWithControllers(context.Background(), setupControllers, admissionRequestHandler); err != nil {
if err := webhookMgr.PrepareWebhookServerWithControllers(ctx, setupControllers, admissionRequestHandler); err != nil {
return fmt.Errorf("unable to create webhook server: %w", err)
}

Expand All @@ -239,7 +241,7 @@ func Run(
return err
}
for _, c := range controllers {
if err := c.MaybeSetupWithManager(mgr); err != nil {
if err := c.MaybeSetupWithManager(ctx, mgr); err != nil {
return fmt.Errorf("unable to create controller %q: %w", c.Name(), err)
}
}
Expand All @@ -261,7 +263,7 @@ func Run(
// Enable anonnymous reporting when configured but not for development builds
// to reduce the noise.
if cfg.AnonymousReports && !cfg.DevelopmentMode {
stopAnonymousReports, err := setupAnonymousReports(context.Background(), restCfg, setupLog, metadata)
stopAnonymousReports, err := setupAnonymousReports(ctx, restCfg, setupLog, metadata)
if err != nil {
setupLog.Error(err, "failed setting up anonymous reports")
} else {
Expand Down
2 changes: 1 addition & 1 deletion modules/manager/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func (m *webhookManager) Start(ctx context.Context) error {
}

for _, c := range controllers {
if err := c.MaybeSetupWithManager(m.mgr); err != nil {
if err := c.MaybeSetupWithManager(ctx, m.mgr); err != nil {
return fmt.Errorf("unable to create controller %q: %w", c.Name(), err)
}
}
Expand Down

0 comments on commit dcb56ad

Please sign in to comment.