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 (#103) (#106)
  • Loading branch information
pmalek authored Apr 22, 2024
1 parent 2160448 commit 71ab67d
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
autoscaling with `GatewayConfiguration` as the generated `DataPlane` deployment
will no longer be rejected.
[#79](https://github.com/Kong/gateway-operator/pull/79)
- Make creating a `DataPlane` index conditional based on enabling the `ControlPlane`
controller. This allows running KGO without `ControlPlane` CRD with its controller
disabled.
[#103](https://github.com/Kong/gateway-operator/pull/103)

## [v1.2.1]

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
10 changes: 8 additions & 2 deletions modules/manager/controller_setup.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package manager

import (
"context"
"errors"
"fmt"
"net/url"
Expand Down Expand Up @@ -93,8 +94,13 @@ func (c *ControllerDef) MaybeSetupWithManager(mgr ctrl.Manager) error {
return c.Controller.SetupWithManager(mgr)
}

func setupIndexes(mgr manager.Manager) error {
return index.IndexDataPlaneNameOnControlPlane(mgr.GetCache())
func setupIndexes(ctx context.Context, mgr manager.Manager, cfg Config) error {
if cfg.ControlPlaneControllerEnabled || cfg.GatewayControllerEnabled {
if err := index.DataPlaneNameOnControlPlane(ctx, mgr.GetCache()); err != nil {
return fmt.Errorf("failed to setup index for DataPlane names on ControlPlane: %w", err)
}
}
return nil
}

// SetupControllers returns a list of ControllerDefs based on config.
Expand Down
2 changes: 1 addition & 1 deletion modules/manager/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ func Run(
return fmt.Errorf("unable to start manager: %w", err)
}

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

Expand Down

0 comments on commit 71ab67d

Please sign in to comment.