Skip to content

Commit

Permalink
fix: make creating DataPlanes index conditional on enabling the Contr…
Browse files Browse the repository at this point in the history
…olPlane controller
  • Loading branch information
pmalek committed Apr 19, 2024
1 parent e8a7e61 commit 3403874
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 12 deletions.
5 changes: 5 additions & 0 deletions controller/controlplane/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/kong/gateway-operator/controller/pkg/log"
"github.com/kong/gateway-operator/controller/pkg/op"
operatorerrors "github.com/kong/gateway-operator/internal/errors"
"github.com/kong/gateway-operator/internal/utils/index"
"github.com/kong/gateway-operator/internal/versions"
"github.com/kong/gateway-operator/pkg/consts"
gatewayutils "github.com/kong/gateway-operator/pkg/utils/gateway"
Expand All @@ -48,6 +49,10 @@ const requeueWithoutBackoff = time.Millisecond * 200

// SetupWithManager sets up the controller with the Manager.
func (r *Reconciler) SetupWithManager(mgr ctrl.Manager) error {
if err := index.DataPlaneNameOnControlPlane(context.Background(), mgr.GetCache()); err != nil {
return fmt.Errorf("failed to set up DataPlanes index: %w", err)
}

// 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
15 changes: 12 additions & 3 deletions internal/utils/index/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package index

import (
"context"
"fmt"

"k8s.io/apimachinery/pkg/api/meta"
"sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/client"

Expand All @@ -14,10 +16,17 @@ const (
DataPlaneNameIndex = "dataplane"
)

// IndexDataPlaneNameOnControlPlane indexes the ControlPlane .spec.dataplaneName field
// DataPlaneNameOnControlPlane indexes the ControlPlane .spec.dataplaneName field
// on the "dataplane" key.
func IndexDataPlaneNameOnControlPlane(c cache.Cache) error {
return c.IndexField(context.Background(), &operatorv1beta1.ControlPlane{}, DataPlaneNameIndex, func(o client.Object) []string {
func DataPlaneNameOnControlPlane(ctx context.Context, c cache.Cache) error {
if _, err := c.GetInformer(ctx, &operatorv1beta1.ControlPlane{}); err != nil {
if meta.IsNoMatchError(err) {
return nil
}
return fmt.Errorf("failed to get informer for v1beta1 ControlPlane: %w, disabling indexing DataPlanes for ControlPlanes' .spec.dataplaneName", err)
}

return c.IndexField(ctx, &operatorv1beta1.ControlPlane{}, DataPlaneNameIndex, func(o client.Object) []string {
controlPlane, ok := o.(*operatorv1beta1.ControlPlane)
if !ok {
return []string{}
Expand Down
5 changes: 0 additions & 5 deletions modules/manager/controller_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"github.com/kong/gateway-operator/controller/gateway"
"github.com/kong/gateway-operator/controller/gatewayclass"
"github.com/kong/gateway-operator/controller/specialized"
"github.com/kong/gateway-operator/internal/utils/index"
dataplanevalidator "github.com/kong/gateway-operator/internal/validation/dataplane"
"github.com/kong/gateway-operator/pkg/consts"
)
Expand Down Expand Up @@ -93,10 +92,6 @@ func (c *ControllerDef) MaybeSetupWithManager(mgr ctrl.Manager) error {
return c.Controller.SetupWithManager(mgr)
}

func setupIndexes(mgr manager.Manager) error {
return index.IndexDataPlaneNameOnControlPlane(mgr.GetCache())
}

// SetupControllers returns a list of ControllerDefs based on config.
func SetupControllers(mgr manager.Manager, c *Config) (map[string]ControllerDef, error) {
// These checks prevent controller-runtime spamming in logs about failing
Expand Down
4 changes: 0 additions & 4 deletions modules/manager/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,6 @@ func Run(
return fmt.Errorf("unable to start manager: %w", err)
}

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

if cfg.ValidatingWebhookEnabled {
// if the validatingWebhook is enabled, we don't need to setup the Gateway API controllers
// here, as they will be set up by the webhook manager once all the webhook resources will be created
Expand Down

0 comments on commit 3403874

Please sign in to comment.