diff --git a/controller/dataplane/watch.go b/controller/dataplane/watch.go index 55af38902..1d017d0ee 100644 --- a/controller/dataplane/watch.go +++ b/controller/dataplane/watch.go @@ -3,11 +3,11 @@ package dataplane import ( "context" + "github.com/samber/lo" appsv1 "k8s.io/api/apps/v1" autoscalingv2 "k8s.io/api/autoscaling/v2" corev1 "k8s.io/api/core/v1" policyv1 "k8s.io/api/policy/v1" - k8stypes "k8s.io/apimachinery/pkg/types" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/builder" "sigs.k8s.io/controller-runtime/pkg/client" @@ -17,6 +17,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/source" operatorv1beta1 "github.com/kong/gateway-operator/api/v1beta1" + "github.com/kong/gateway-operator/internal/utils/index" "github.com/kong/gateway-operator/pkg/consts" ) @@ -64,30 +65,17 @@ func listDataPlanesReferencingKongPluginInstallation( if kpiToFind == "" { return nil } - var dataPlaneList operatorv1beta1.DataPlaneList - if err := c.List(ctx, &dataPlaneList); client.IgnoreNotFound(err) != nil { + if err := c.List(ctx, &dataPlaneList, client.MatchingFields{ + index.KongPluginInstallationsIndex: kpiToFind, + }); err != nil { logger.Error(err, "Failed to list DataPlanes in watch", "KongPluginInstallation", kpiToFind) return nil } - var dataPlanesToReconcile []reconcile.Request - for _, dp := range dataPlaneList.Items { - for _, ptiNN := range dp.Spec.PluginsToInstall { - kpiNN := k8stypes.NamespacedName(ptiNN) - if kpiNN.Namespace == "" { - kpiNN.Namespace = dp.Namespace - } - - if kpiNN.String() == kpiToFind { - dataPlanesToReconcile = append( - dataPlanesToReconcile, - reconcile.Request{ - NamespacedName: client.ObjectKeyFromObject(&dp), - }, - ) - } + return lo.Map(dataPlaneList.Items, func(dp operatorv1beta1.DataPlane, _ int) reconcile.Request { + return reconcile.Request{ + NamespacedName: client.ObjectKeyFromObject(&dp), } - } - return dataPlanesToReconcile + }) } } diff --git a/internal/utils/index/index.go b/internal/utils/index/index.go index 8e48b33a3..9a002799c 100644 --- a/internal/utils/index/index.go +++ b/internal/utils/index/index.go @@ -12,8 +12,12 @@ import ( ) const ( - // DataPlaneNameIndex is the key to be used to access the .spec.dataplaneName indexed values + // DataPlaneNameIndex is the key to be used to access the .spec.dataplaneName indexed values. DataPlaneNameIndex = "dataplane" + + // KongPluginInstallationsIndex is the key to be used to access the .spec.pluginsToInstall indexed values, + // in a form of list of namespace/name strings. + KongPluginInstallationsIndex = "KongPluginInstallations" ) // DataPlaneNameOnControlPlane indexes the ControlPlane .spec.dataplaneName field @@ -37,3 +41,33 @@ func DataPlaneNameOnControlPlane(ctx context.Context, c cache.Cache) error { return []string{} }) } + +// KongPluginInstallationsOnDataPlane indexes the DataPlane .spec.pluginsToInstall field +// on the "kongPluginInstallations" key. +func KongPluginInstallationsOnDataPlane(ctx context.Context, c cache.Cache) error { + if _, err := c.GetInformer(ctx, &operatorv1beta1.DataPlane{}); err != nil { + if meta.IsNoMatchError(err) { + return nil + } + return fmt.Errorf("failed to get informer for v1beta1 DataPlane: %w, disabling indexing KongPluginInstallations for DataPlanes' .spec.pluginsToInstall", err) + } + return c.IndexField( + ctx, + &operatorv1beta1.DataPlane{}, + KongPluginInstallationsIndex, + func(o client.Object) []string { + dp, ok := o.(*operatorv1beta1.DataPlane) + if !ok { + return nil + } + result := make([]string, 0, len(dp.Spec.PluginsToInstall)) + for _, kpi := range dp.Spec.PluginsToInstall { + if kpi.Namespace == "" { + kpi.Namespace = dp.Namespace + } + result = append(result, kpi.Namespace+"/"+kpi.Name) + } + return result + }, + ) +} diff --git a/modules/manager/controller_setup.go b/modules/manager/controller_setup.go index 54b49edb9..8790fb1c4 100644 --- a/modules/manager/controller_setup.go +++ b/modules/manager/controller_setup.go @@ -147,9 +147,22 @@ func (c *ControllerDef) MaybeSetupWithManager(ctx context.Context, mgr ctrl.Mana func setupIndexes(ctx context.Context, mgr manager.Manager, cfg Config) error { if cfg.ControlPlaneControllerEnabled || cfg.GatewayControllerEnabled { + log.GetLogger(ctx, "ControlPlane", cfg.DevelopmentMode).Info( + "creating index", + "indexField", index.DataPlaneNameIndex, + ) if err := index.DataPlaneNameOnControlPlane(ctx, mgr.GetCache()); err != nil { return fmt.Errorf("failed to setup index for DataPlane names on ControlPlane: %w", err) } + if cfg.KongPluginInstallationControllerEnabled { + log.GetLogger(ctx, "DataPlane", cfg.DevelopmentMode).Info( + "creating index", + "indexField", index.KongPluginInstallationsIndex, + ) + if err := index.KongPluginInstallationsOnDataPlane(ctx, mgr.GetCache()); err != nil { + return fmt.Errorf("failed to setup index for KongPluginInstallations on DataPlane: %w", err) + } + } } return nil } @@ -557,9 +570,8 @@ func setupCacheIndicesForKonnectType[ logger = log.GetLogger(ctx, entityTypeName, developmentMode) ) for _, ind := range konnect.ReconciliationIndexOptionsForEntity[TEnt]() { - logger.Info("creating index", - "entityTypeName", entityTypeName, - "indexObject", ind.IndexObject, + logger.Info( + "creating index", "indexField", ind.IndexField, ) err := mgr. diff --git a/modules/manager/run.go b/modules/manager/run.go index 053ef382a..93f7cbf08 100644 --- a/modules/manager/run.go +++ b/modules/manager/run.go @@ -199,8 +199,7 @@ func Run( secretName: cfg.ClusterCASecretName, secretNamespace: cfg.ClusterCASecretNamespace, } - err = mgr.Add(caMgr) - if err != nil { + if err = mgr.Add(caMgr); err != nil { return fmt.Errorf("unable to start manager: %w", err) }