Skip to content
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

chore: introduce index for DataPlanes referencing KongPluginInstallations #666

Merged
merged 1 commit into from
Sep 29, 2024
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
30 changes: 9 additions & 21 deletions controller/dataplane/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
)

Expand Down Expand Up @@ -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
})
}
}
36 changes: 35 additions & 1 deletion internal/utils/index/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
},
)
}
18 changes: 15 additions & 3 deletions modules/manager/controller_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
programmer04 marked this conversation as resolved.
Show resolved Hide resolved
"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
}
Expand Down Expand Up @@ -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.
Expand Down
3 changes: 1 addition & 2 deletions modules/manager/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
Loading