Skip to content

Commit

Permalink
use indices
Browse files Browse the repository at this point in the history
  • Loading branch information
czeslavo committed Oct 2, 2024
1 parent 18b1e95 commit 11c59a4
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 67 deletions.
56 changes: 56 additions & 0 deletions controller/konnect/index_kongkey.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package konnect

import (
"sigs.k8s.io/controller-runtime/pkg/client"

configurationv1alpha1 "github.com/kong/kubernetes-configuration/api/configuration/v1alpha1"
)

const (
// IndexFieldKongKeyOnKongKeySetReference is the index field for KongKey-> KongKeySet.
IndexFieldKongKeyOnKongKeySetReference = "kongKeySetRef"

// IndexFieldKongKeyOnKonnectGatewayControlPlane is the index field for KongKey -> KonnectGatewayControlPlane.
IndexFieldKongKeyOnKonnectGatewayControlPlane = "kongKeyKonnectGatewayControlPlaneRef"
)

// IndexOptionsForKongKey returns required Index options for KongKey reconclier.
func IndexOptionsForKongKey() []ReconciliationIndexOption {
return []ReconciliationIndexOption{
{
IndexObject: &configurationv1alpha1.KongKey{},
IndexField: IndexFieldKongKeyOnKongKeySetReference,
ExtractValue: kongKeySetRefFromKongKey,
},
{
IndexObject: &configurationv1alpha1.KongKey{},
IndexField: IndexFieldKongKeyOnKonnectGatewayControlPlane,
ExtractValue: konnectGatewayControlPlaneRefFromKongKey,
},
}
}

// kongKeySetRefFromKongKey returns namespace/name of referenced KongKeySet in KongKey spec.
func kongKeySetRefFromKongKey(obj client.Object) []string {
key, ok := obj.(*configurationv1alpha1.KongKey)
if !ok {
return nil
}

if key.Spec.KeySetRef == nil ||
key.Spec.KeySetRef.Type != configurationv1alpha1.KeySetRefNamespacedRef ||
key.Spec.KeySetRef.NamespacedRef == nil {
return nil
}

return []string{key.GetNamespace() + "/" + key.Spec.KeySetRef.NamespacedRef.Name}
}

// kongPluginReferencesFromKongKey returns namespace/name of referenced KonnectGatewayControlPlane in KongKey spec.
func konnectGatewayControlPlaneRefFromKongKey(obj client.Object) []string {
key, ok := obj.(*configurationv1alpha1.KongKey)
if !ok {
return nil
}
return controlPlaneKonnectNamespacedRefAsSlice(key)
}
81 changes: 14 additions & 67 deletions controller/konnect/watch_kongkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,50 +138,17 @@ func enqueueKongKeyForKonnectControlPlane(
return nil
}
var l configurationv1alpha1.KongKeyList
if err := cl.List(ctx, &l, &client.ListOptions{
if err := cl.List(ctx, &l,
// TODO: change this when cross namespace refs are allowed.
Namespace: cp.GetNamespace(),
}); err != nil {
client.InNamespace(cp.GetNamespace()),
client.MatchingFields{
IndexFieldKongKeyOnKonnectGatewayControlPlane: cp.GetNamespace() + "/" + cp.GetName(),
},
); err != nil {
return nil
}

var ret []reconcile.Request
for _, key := range l.Items {
cpRef, ok := getControlPlaneRef(&key).Get()
if !ok {
continue
}
switch cpRef.Type {
case configurationv1alpha1.ControlPlaneRefKonnectNamespacedRef:
// TODO: change this when cross namespace refs are allowed.
if cpRef.KonnectNamespacedRef.Name != cp.Name {
continue
}

ret = append(ret, reconcile.Request{
NamespacedName: types.NamespacedName{
Namespace: key.Namespace,
Name: key.Name,
},
})

case configurationv1alpha1.ControlPlaneRefKonnectID:
ctrllog.FromContext(ctx).Error(
fmt.Errorf("unimplemented ControlPlaneRef type %q", cpRef.Type),
"unimplemented ControlPlaneRef for KongKey",
"KongKey", key, "refType", cpRef.Type,
)
continue

default:
ctrllog.FromContext(ctx).V(logging.DebugLevel.Value()).Info(
"unsupported ControlPlaneRef for KongKey",
"KongKey", key, "refType", cpRef.Type,
)
continue
}
}
return ret
return objectListToReconcileRequests(l.Items)
}
}

Expand All @@ -192,35 +159,15 @@ func enqueueKongKeyForKongKeySet(cl client.Client) handler.MapFunc {
return nil
}
var l configurationv1alpha1.KongKeyList
if err := cl.List(ctx, &l, &client.ListOptions{}); err != nil {
if err := cl.List(ctx, &l,
client.InNamespace(keySet.GetNamespace()),
client.MatchingFields{
IndexFieldKongKeyOnKongKeySetReference: keySet.GetNamespace() + "/" + keySet.GetName(),
},
); err != nil {
return nil
}

var ret []reconcile.Request
for _, key := range l.Items {
keySetRef := getKeySetRef(&key)
ref, ok := keySetRef.Get()
if !ok {
continue
}
if ref.Type != configurationv1alpha1.KeySetRefNamespacedRef {
ctrllog.FromContext(ctx).V(logging.DebugLevel.Value()).Info(
"unsupported KongKeySetRef for KongKey",
"KongKey", key, "refType", ref.Type,
)
continue
}
if ref.NamespacedRef == nil || ref.NamespacedRef.Name != keySet.Name {
continue
}
ret = append(ret, reconcile.Request{
NamespacedName: types.NamespacedName{
Namespace: key.Namespace,
Name: key.Name,
},
})
}

return ret
return objectListToReconcileRequests(l.Items)
}
}
4 changes: 4 additions & 0 deletions modules/manager/controller_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,10 @@ func SetupCacheIndicesForKonnectTypes(ctx context.Context, mgr manager.Manager,
Object: &configurationv1alpha1.KongSNI{},
IndexOptions: konnect.IndexOptionsForKongSNI(),
},
{
Object: &configurationv1alpha1.KongKey{},
IndexOptions: konnect.IndexOptionsForKongKey(),
},
}

for _, t := range types {
Expand Down

0 comments on commit 11c59a4

Please sign in to comment.