Skip to content

Commit

Permalink
refactor(konnect): add indices for objects that reference KonnectGate…
Browse files Browse the repository at this point in the history
…wayControlPlane
  • Loading branch information
pmalek committed Oct 2, 2024
1 parent 820f2c8 commit 519cfb7
Show file tree
Hide file tree
Showing 10 changed files with 158 additions and 131 deletions.
21 changes: 21 additions & 0 deletions controller/konnect/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package konnect

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

"github.com/kong/gateway-operator/controller/konnect/constraints"
)

// ReconciliationIndexOption contains required options of index for a kind of object required for reconciliation.
Expand All @@ -10,3 +12,22 @@ type ReconciliationIndexOption struct {
IndexField string
ExtractValue client.IndexerFunc
}

// controlPlaneKonnectNamespacedRefRefAsSlice returns a slice of strings representing
// the KonnectNamespacedRef of the object.
func controlPlaneKonnectNamespacedRefRefAsSlice[
T constraints.SupportedKonnectEntityType,
TEnt constraints.EntityType[T],
](ent TEnt) []string {
cpRef, ok := controlPlaneIsRefKonnectNamespacedRef(ent)
if !ok {
return nil
}

konnectRef := cpRef.KonnectNamespacedRef
if konnectRef == nil {
return nil
}

return []string{konnectRef.Namespace + "/" + konnectRef.Name}
}
16 changes: 16 additions & 0 deletions controller/konnect/index_kongconsumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ const (
IndexFieldKongConsumerOnKongConsumerGroup = "consumerGroupRef"
// IndexFieldKongConsumerOnPlugin is the index field for KongConsumer -> KongPlugin.
IndexFieldKongConsumerOnPlugin = "consumerPluginRef"
// IndexFieldKongConsumerOnKonnectGatewayControlPlane is the index field for KongConsumer -> KonnectGatewayControlPlane.
IndexFieldKongConsumerOnKonnectGatewayControlPlane = "consumerKonnectGatewayControlPlaneRef"
)

// IndexOptionsForKongConsumer returns required Index options for KongConsumer reconciler.
Expand All @@ -28,6 +30,11 @@ func IndexOptionsForKongConsumer() []ReconciliationIndexOption {
IndexField: IndexFieldKongConsumerOnPlugin,
ExtractValue: kongConsumerReferencesKongPluginsViaAnnotation,
},
{
IndexObject: &configurationv1.KongConsumer{},
IndexField: IndexFieldKongConsumerOnKonnectGatewayControlPlane,
ExtractValue: kongConsumerReferencesKonnectGatewayControlPlane,
},
}
}

Expand All @@ -46,3 +53,12 @@ func kongConsumerReferencesKongPluginsViaAnnotation(object client.Object) []stri
}
return annotations.ExtractPluginsWithNamespaces(consumer)
}

func kongConsumerReferencesKonnectGatewayControlPlane(object client.Object) []string {
consumer, ok := object.(*configurationv1.KongConsumer)
if !ok {
return nil
}

return controlPlaneKonnectNamespacedRefRefAsSlice(consumer)
}
16 changes: 16 additions & 0 deletions controller/konnect/index_kongservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
const (
// IndexFieldKongServiceOnReferencedPluginNames is the index field for KongService -> KongPlugin.
IndexFieldKongServiceOnReferencedPluginNames = "kongServiceKongPluginRef"
// IndexFieldKongServiceOnKonnectGatewayControlPlane is the index field for KongService -> KonnectGatewayControlPlane.
IndexFieldKongServiceOnKonnectGatewayControlPlane = "kongServiceKonnectGatewayControlPlaneRef"
)

// IndexOptionsForKongService returns required Index options for KongService reconciler.
Expand All @@ -21,6 +23,11 @@ func IndexOptionsForKongService() []ReconciliationIndexOption {
IndexField: IndexFieldKongServiceOnReferencedPluginNames,
ExtractValue: kongServiceUsesPlugins,
},
{
IndexObject: &configurationv1alpha1.KongService{},
IndexField: IndexFieldKongServiceOnKonnectGatewayControlPlane,
ExtractValue: kongServiceReferencesKonnectGatewayControlPlane,
},
}
}

Expand All @@ -32,3 +39,12 @@ func kongServiceUsesPlugins(object client.Object) []string {

return annotations.ExtractPluginsWithNamespaces(svc)
}

func kongServiceReferencesKonnectGatewayControlPlane(object client.Object) []string {
svc, ok := object.(*configurationv1alpha1.KongService)
if !ok {
return nil
}

return controlPlaneKonnectNamespacedRefRefAsSlice(svc)
}
32 changes: 32 additions & 0 deletions controller/konnect/index_kongtarget.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package konnect

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

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

const (
// IndexFieldKongTargetOnReferencedUpstream is the index field for KongTarget -> KongUpstream.
IndexFieldKongTargetOnReferencedUpstream = "kongTargetUpstreamRef"
)

// IndexOptionsForKongTarget returns required Index options for KongTarget reconciler.
func IndexOptionsForKongTarget() []ReconciliationIndexOption {
return []ReconciliationIndexOption{
{
IndexObject: &configurationv1alpha1.KongTarget{},
IndexField: IndexFieldKongTargetOnReferencedUpstream,
ExtractValue: kongTargetReferencesKongUpstream,
},
}
}

func kongTargetReferencesKongUpstream(object client.Object) []string {
target, ok := object.(*configurationv1alpha1.KongTarget)
if !ok {
return nil
}

return []string{target.Spec.UpstreamRef.Name}
}
32 changes: 32 additions & 0 deletions controller/konnect/index_kongupstream.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package konnect

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

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

const (
// IndexFieldKongUpstreamOnKonnectGatewayControlPlane is the index field for KongUpstream -> KonnectGatewayControlPlane.
IndexFieldKongUpstreamOnKonnectGatewayControlPlane = "kongUpstreamKonnectGatewayControlPlaneRef"
)

// IndexOptionsForKongUpstream returns required Index options for KongUpstream reconciler.
func IndexOptionsForKongUpstream() []ReconciliationIndexOption {
return []ReconciliationIndexOption{
{
IndexObject: &configurationv1alpha1.KongUpstream{},
IndexField: IndexFieldKongUpstreamOnKonnectGatewayControlPlane,
ExtractValue: kongUpstreamReferencesKonnectGatewayControlPlane,
},
}
}

func kongUpstreamReferencesKonnectGatewayControlPlane(object client.Object) []string {
upstream, ok := object.(*configurationv1alpha1.KongUpstream)
if !ok {
return nil
}

return controlPlaneKonnectNamespacedRefRefAsSlice(upstream)
}
47 changes: 7 additions & 40 deletions controller/konnect/watch_kongconsumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,50 +153,17 @@ func enqueueKongConsumerForKonnectGatewayControlPlane(
return nil
}
var l configurationv1.KongConsumerList
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{
IndexFieldKongConsumerOnKonnectGatewayControlPlane: cp.Namespace + "/" + cp.Name,
},
); err != nil {
return nil
}

var ret []reconcile.Request
for _, consumer := range l.Items {
cpRef, ok := getControlPlaneRef(&consumer).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: consumer.Namespace,
Name: consumer.Name,
},
})

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

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

Expand Down
46 changes: 7 additions & 39 deletions controller/konnect/watch_kongservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,48 +143,16 @@ func enqueueKongServiceForKonnectGatewayControlPlane(
return nil
}
var l configurationv1alpha1.KongServiceList
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{
IndexFieldKongServiceOnKonnectGatewayControlPlane: cp.Namespace + "/" + cp.Name,
},
); err != nil {
return nil
}

var ret []reconcile.Request
for _, svc := range l.Items {
if svc.Spec.ControlPlaneRef == nil {
continue
}
switch svc.Spec.ControlPlaneRef.Type {
case configurationv1alpha1.ControlPlaneRefKonnectNamespacedRef:
// TODO: change this when cross namespace refs are allowed.
if svc.Spec.ControlPlaneRef.KonnectNamespacedRef.Name != cp.Name {
continue
}

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

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

default:
ctrllog.FromContext(ctx).V(logging.DebugLevel.Value()).Info(
"unsupported ControlPlaneRef for KongService",
"KongService", svc, "refType", svc.Spec.ControlPlaneRef.Type,
)
continue
}
}
return ret
return objectListToReconcileRequests(l.Items)
}
}
21 changes: 8 additions & 13 deletions controller/konnect/watch_kongtarget.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,22 +77,17 @@ func enqueueKongTargetForKongUpstream(cl client.Client,
return nil
}

var targetList configurationv1alpha1.KongTargetList
if err := cl.List(ctx, &targetList, &client.ListOptions{
var l configurationv1alpha1.KongTargetList
if err := cl.List(ctx, &l,
// TODO: change this when cross namespace refs are allowed.
Namespace: kongUpstream.GetNamespace(),
}); err != nil {
client.InNamespace(kongUpstream.GetNamespace()),
client.MatchingFields{
IndexFieldKongTargetOnReferencedUpstream: kongUpstream.Name,
},
); err != nil {
return nil
}

var ret []reconcile.Request
for _, target := range targetList.Items {
if target.Spec.UpstreamRef.Name == kongUpstream.Name {
ret = append(ret, reconcile.Request{
NamespacedName: client.ObjectKeyFromObject(&target),
})
}
}
return ret
return objectListToReconcileRequests(l.Items)
}
}
46 changes: 7 additions & 39 deletions controller/konnect/watch_kongupstream.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,48 +143,16 @@ func enqueueKongUpstreamForKonnectGatewayControlPlane(
return nil
}
var l configurationv1alpha1.KongUpstreamList
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{
IndexFieldKongUpstreamOnKonnectGatewayControlPlane: cp.Namespace + "/" + cp.Name,
},
); err != nil {
return nil
}

var ret []reconcile.Request
for _, upstream := range l.Items {
if upstream.Spec.ControlPlaneRef == nil {
continue
}
switch upstream.Spec.ControlPlaneRef.Type {
case configurationv1alpha1.ControlPlaneRefKonnectNamespacedRef:
// TODO: change this when cross namespace refs are allowed.
if upstream.Spec.ControlPlaneRef.KonnectNamespacedRef.Name != cp.Name {
continue
}

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

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

default:
ctrllog.FromContext(ctx).V(logging.DebugLevel.Value()).Info(
"unsupported ControlPlaneRef for KongUpstream",
"KongUpstream", upstream, "refType", upstream.Spec.ControlPlaneRef.Type,
)
continue
}
}
return ret
return objectListToReconcileRequests(l.Items)
}
}
12 changes: 12 additions & 0 deletions modules/manager/controller_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,10 @@ func SetupCacheIndicesForKonnectTypes(ctx context.Context, mgr manager.Manager,
Object: &configurationv1alpha1.KongCredentialJWT{},
IndexOptions: konnect.IndexOptionsForCredentialsJWT(),
},
{
Object: &configurationv1alpha1.KongCredentialAPIKey{},
IndexOptions: konnect.IndexOptionsForCredentialsAPIKey(),
},
{
Object: &configurationv1.KongConsumer{},
IndexOptions: konnect.IndexOptionsForKongConsumer(),
Expand All @@ -600,6 +604,14 @@ func SetupCacheIndicesForKonnectTypes(ctx context.Context, mgr manager.Manager,
Object: &configurationv1alpha1.KongRoute{},
IndexOptions: konnect.IndexOptionsForKongRoute(),
},
{
Object: &configurationv1alpha1.KongUpstream{},
IndexOptions: konnect.IndexOptionsForKongUpstream(),
},
{
Object: &configurationv1alpha1.KongTarget{},
IndexOptions: konnect.IndexOptionsForKongTarget(),
},
{
Object: &configurationv1alpha1.KongSNI{},
IndexOptions: konnect.IndexOptionsForKongSNI(),
Expand Down

0 comments on commit 519cfb7

Please sign in to comment.