Skip to content

Commit 3645df3

Browse files
committed
feat(konnect): add indices for KongPluginBinding -> KonnectGatewayControlPlane and KongRoute -> KongService
1 parent f189f2d commit 3645df3

File tree

4 files changed

+83
-89
lines changed

4 files changed

+83
-89
lines changed

controller/konnect/index_kongpluginbinding.go

+41-10
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,18 @@ import (
77
)
88

99
const (
10-
// IndexFieldKongPluginBindingKongPluginReference is the index field for KongPlugin -> KongPluginBinding.
11-
IndexFieldKongPluginBindingKongPluginReference = "kongPluginRef"
12-
// IndexFieldKongPluginBindingKongServiceReference is the index field for KongService -> KongPluginBinding.
13-
IndexFieldKongPluginBindingKongServiceReference = "kongServiceRef"
14-
// IndexFieldKongPluginBindingKongRouteReference is the index field for KongRoute -> KongPluginBinding.
15-
IndexFieldKongPluginBindingKongRouteReference = "kongRouteRef"
16-
// IndexFieldKongPluginBindingKongConsumerReference is the index field for KongConsumer -> KongPluginBinding.
17-
IndexFieldKongPluginBindingKongConsumerReference = "kongConsumerRef"
18-
// IndexFieldKongPluginBindingKongConsumerGroupReference is the index field for KongConsumerGroup -> KongPluginBinding.
19-
IndexFieldKongPluginBindingKongConsumerGroupReference = "kongConsumerGroupRef"
10+
// IndexFieldKongPluginBindingKongPluginReference is the index field for KongPluginBinding -> KongPlugin.
11+
IndexFieldKongPluginBindingKongPluginReference = "kongPluginBindingPluginRef"
12+
// IndexFieldKongPluginBindingKongServiceReference is the index field for KongPluginBinding -> KongService.
13+
IndexFieldKongPluginBindingKongServiceReference = "kongPluginBindingServiceRef"
14+
// IndexFieldKongPluginBindingKongRouteReference is the index field for KongPluginBinding -> KongRoute.
15+
IndexFieldKongPluginBindingKongRouteReference = "kongPluginBindingRouteRef"
16+
// IndexFieldKongPluginBindingKongConsumerReference is the index field for KongPluginBinding -> KongConsumer.
17+
IndexFieldKongPluginBindingKongConsumerReference = "kongPluginBindingConsumerRef"
18+
// IndexFieldKongPluginBindingKongConsumerGroupReference is the index field for KongPluginBinding -> KongConsumerGroup.
19+
IndexFieldKongPluginBindingKongConsumerGroupReference = "kongPluginBindingConsumerGroupRef"
20+
// IndexFieldKongPluginBindingKonnectGatewayControlPlane is the index field for KongPluginBinding -> KonnectGatewayControlPlane.
21+
IndexFieldKongPluginBindingKonnectGatewayControlPlane = "kongPluginBindingKonnectGatewayControlPlaneRef"
2022
)
2123

2224
// IndexOptionsForKongPluginBinding returns required Index options for KongPluginBinding reconclier.
@@ -47,6 +49,11 @@ func IndexOptionsForKongPluginBinding() []ReconciliationIndexOption {
4749
IndexField: IndexFieldKongPluginBindingKongConsumerGroupReference,
4850
ExtractValue: kongConsumerGroupReferencesFromKongPluginBinding,
4951
},
52+
{
53+
IndexObject: &configurationv1alpha1.KongPluginBinding{},
54+
IndexField: IndexFieldKongPluginBindingKonnectGatewayControlPlane,
55+
ExtractValue: kongPluginBindingReferencesKonnectGatewayControlPlane,
56+
},
5057
}
5158
}
5259

@@ -113,3 +120,27 @@ func kongConsumerGroupReferencesFromKongPluginBinding(obj client.Object) []strin
113120
}
114121
return []string{binding.Spec.Targets.ConsumerGroupReference.Name}
115122
}
123+
124+
// kongPluginBindingReferencesKonnectGatewayControlPlane returns name of referenced KonnectGatewayControlPlane in KongPluginBinding spec.
125+
func kongPluginBindingReferencesKonnectGatewayControlPlane(obj client.Object) []string {
126+
binding, ok := obj.(*configurationv1alpha1.KongPluginBinding)
127+
if !ok {
128+
return nil
129+
}
130+
cpRef := binding.Spec.ControlPlaneRef
131+
if cpRef == nil ||
132+
cpRef.Type != configurationv1alpha1.ControlPlaneRefKonnectNamespacedRef ||
133+
cpRef.KonnectNamespacedRef == nil {
134+
return nil
135+
}
136+
137+
// NOTE: This provides support for setting the namespace of the KonnectGatewayControlPlane ref
138+
// but CRDs have validation rules in place which will disallow this until
139+
// cross namespace refs are allowed.
140+
namespace := binding.Namespace
141+
if cpRef.KonnectNamespacedRef.Namespace != "" {
142+
namespace = cpRef.KonnectNamespacedRef.Namespace
143+
}
144+
145+
return []string{namespace + "/" + cpRef.KonnectNamespacedRef.Name}
146+
}

controller/konnect/index_kongroute.go

+27
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111
const (
1212
// IndexFieldKongRouteOnReferencedPluginNames is the index field for KongRoute -> KongPlugin.
1313
IndexFieldKongRouteOnReferencedPluginNames = "kongRouteKongPluginRef"
14+
// IndexFieldKongRouteOnReferencedKongService is the index field for KongRoute -> KongService.
15+
IndexFieldKongRouteOnReferencedKongService = "kongRouteKongServiceRef"
1416
)
1517

1618
// IndexOptionsForKongRoute returns required Index options for KongRoute reconciler.
@@ -21,6 +23,11 @@ func IndexOptionsForKongRoute() []ReconciliationIndexOption {
2123
IndexField: IndexFieldKongRouteOnReferencedPluginNames,
2224
ExtractValue: kongRouteUsesPlugins,
2325
},
26+
{
27+
IndexObject: &configurationv1alpha1.KongRoute{},
28+
IndexField: IndexFieldKongRouteOnReferencedKongService,
29+
ExtractValue: kongRouteRefersToKongService,
30+
},
2431
}
2532
}
2633

@@ -31,3 +38,23 @@ func kongRouteUsesPlugins(object client.Object) []string {
3138
}
3239
return annotations.ExtractPluginsWithNamespaces(route)
3340
}
41+
42+
func kongRouteRefersToKongService(object client.Object) []string {
43+
route, ok := object.(*configurationv1alpha1.KongRoute)
44+
if !ok {
45+
return nil
46+
}
47+
svcRef := route.Spec.ServiceRef
48+
if svcRef == nil ||
49+
svcRef.Type != configurationv1alpha1.ServiceRefNamespacedRef ||
50+
svcRef.NamespacedRef == nil {
51+
return nil
52+
}
53+
54+
namespace := route.Namespace
55+
if svcRef.NamespacedRef.Namespace != "" {
56+
namespace = svcRef.NamespacedRef.Namespace
57+
}
58+
59+
return []string{namespace + "/" + svcRef.NamespacedRef.Name}
60+
}

controller/konnect/watch_kongpluginbinding.go

+7-39
Original file line numberDiff line numberDiff line change
@@ -194,49 +194,17 @@ func enqueueKongPluginBindingForKonnectGatewayControlPlane(
194194
return nil
195195
}
196196
var l configurationv1alpha1.KongPluginBindingList
197-
if err := cl.List(ctx, &l, &client.ListOptions{
197+
if err := cl.List(ctx, &l,
198198
// TODO: change this when cross namespace refs are allowed.
199-
Namespace: cp.GetNamespace(),
200-
}); err != nil {
199+
client.InNamespace(cp.GetNamespace()),
200+
client.MatchingFields{
201+
IndexFieldKongPluginBindingKonnectGatewayControlPlane: cp.Namespace + "/" + cp.Name,
202+
},
203+
); err != nil {
201204
return nil
202205
}
203206

204-
var ret []reconcile.Request
205-
for _, pb := range l.Items {
206-
if pb.Spec.ControlPlaneRef == nil {
207-
continue
208-
}
209-
switch pb.Spec.ControlPlaneRef.Type {
210-
case configurationv1alpha1.ControlPlaneRefKonnectNamespacedRef:
211-
// TODO: change this when cross namespace refs are allowed.
212-
if pb.Spec.ControlPlaneRef.KonnectNamespacedRef.Name != cp.Name {
213-
continue
214-
}
215-
216-
ret = append(ret, reconcile.Request{
217-
NamespacedName: types.NamespacedName{
218-
Namespace: pb.Namespace,
219-
Name: pb.Name,
220-
},
221-
})
222-
223-
case configurationv1alpha1.ControlPlaneRefKonnectID:
224-
ctrllog.FromContext(ctx).Error(
225-
fmt.Errorf("unimplemented ControlPlaneRef type %q", pb.Spec.ControlPlaneRef.Type),
226-
"unimplemented ControlPlaneRef for KongPluginBinding",
227-
"KongPluginBinding", pb, "refType", pb.Spec.ControlPlaneRef.Type,
228-
)
229-
continue
230-
231-
default:
232-
ctrllog.FromContext(ctx).V(logging.DebugLevel.Value()).Info(
233-
"unsupported ControlPlaneRef for KongPluginBinding",
234-
"KongPluginBinding", pb, "refType", pb.Spec.ControlPlaneRef.Type,
235-
)
236-
continue
237-
}
238-
}
239-
return ret
207+
return objectListToReconcileRequests(l.Items)
240208
}
241209
}
242210

controller/konnect/watch_kongroute.go

+8-40
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
"sigs.k8s.io/controller-runtime/pkg/reconcile"
1515

1616
operatorerrors "github.com/kong/gateway-operator/internal/errors"
17-
"github.com/kong/gateway-operator/modules/manager/logging"
1817

1918
configurationv1alpha1 "github.com/kong/kubernetes-configuration/api/configuration/v1alpha1"
2019
)
@@ -98,52 +97,21 @@ func enqueueKongRouteForKongService(
9897

9998
// If the KongService does not refer to a KonnectGatewayControlPlane,
10099
// we do not need to enqueue any KongRoutes bound to this KongService.
101-
cpRef := kongSvc.Spec.ControlPlaneRef
102-
if cpRef == nil || cpRef.Type != configurationv1alpha1.ControlPlaneRefKonnectNamespacedRef {
100+
if !objHasControlPlaneRefKonnectNamespacedRef(kongSvc) {
103101
return nil
104102
}
105103

106104
var l configurationv1alpha1.KongRouteList
107-
if err := cl.List(ctx, &l, &client.ListOptions{
105+
if err := cl.List(ctx, &l,
108106
// TODO: change this when cross namespace refs are allowed.
109-
Namespace: kongSvc.GetNamespace(),
110-
}); err != nil {
107+
client.InNamespace(kongSvc.GetNamespace()),
108+
client.MatchingFields{
109+
IndexFieldKongRouteOnReferencedKongService: kongSvc.Namespace + "/" + kongSvc.Name,
110+
},
111+
); err != nil {
111112
return nil
112113
}
113114

114-
var ret []reconcile.Request
115-
for _, route := range l.Items {
116-
svcRef, ok := getServiceRef(&route).Get()
117-
if !ok {
118-
continue
119-
}
120-
121-
switch svcRef.Type {
122-
case configurationv1alpha1.ServiceRefNamespacedRef:
123-
if svcRef.NamespacedRef == nil {
124-
continue
125-
}
126-
127-
// TODO: change this when cross namespace refs are allowed.
128-
if svcRef.NamespacedRef.Name != kongSvc.GetName() {
129-
continue
130-
}
131-
132-
ret = append(ret, reconcile.Request{
133-
NamespacedName: types.NamespacedName{
134-
Namespace: route.Namespace,
135-
Name: route.Name,
136-
},
137-
})
138-
139-
default:
140-
ctrllog.FromContext(ctx).V(logging.DebugLevel.Value()).Info(
141-
"unsupported ServiceRef for KongRoute",
142-
"KongRoute", route, "refType", svcRef.Type,
143-
)
144-
continue
145-
}
146-
}
147-
return ret
115+
return objectListToReconcileRequests(l.Items)
148116
}
149117
}

0 commit comments

Comments
 (0)