Skip to content

Commit f16c17b

Browse files
committed
(WIP)feat: support global rules
Signed-off-by: ashing <[email protected]>
1 parent 4a2d925 commit f16c17b

File tree

3 files changed

+64
-1
lines changed

3 files changed

+64
-1
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ require (
2929
go.uber.org/zap v1.27.0
3030
golang.org/x/net v0.28.0
3131
gopkg.in/yaml.v2 v2.4.0
32+
gopkg.in/yaml.v3 v3.0.1
3233
gorm.io/gorm v1.25.11
3334
helm.sh/helm/v3 v3.15.4
3435
k8s.io/api v0.31.0
@@ -212,7 +213,6 @@ require (
212213
gopkg.in/fsnotify.v1 v1.4.7 // indirect
213214
gopkg.in/inf.v0 v0.9.1 // indirect
214215
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
215-
gopkg.in/yaml.v3 v3.0.1 // indirect
216216
k8s.io/apiserver v0.31.0 // indirect
217217
k8s.io/cli-runtime v0.30.3 // indirect
218218
k8s.io/component-base v0.31.0 // indirect

internal/controller/gateway_controller.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ import (
55
"fmt"
66
"reflect"
77

8+
"github.com/api7/api7-ingress-controller/api/v1alpha1"
89
"github.com/api7/api7-ingress-controller/internal/controller/config"
10+
"github.com/api7/api7-ingress-controller/internal/controller/indexer"
911
"github.com/api7/api7-ingress-controller/internal/provider"
1012
"github.com/api7/gopkg/pkg/log"
1113
"github.com/go-logr/logr"
@@ -33,8 +35,23 @@ type GatewayReconciler struct { //nolint:revive
3335
Provider provider.Provider
3436
}
3537

38+
func (r *GatewayReconciler) setupIndexer(mgr ctrl.Manager) error {
39+
if err := mgr.GetFieldIndexer().IndexField(
40+
context.TODO(),
41+
&gatewayv1.Gateway{},
42+
indexer.ParametersRef,
43+
indexer.GatewayParametersRefIndexFunc,
44+
); err != nil {
45+
return err
46+
}
47+
return nil
48+
}
49+
3650
// SetupWithManager sets up the controller with the Manager.
3751
func (r *GatewayReconciler) SetupWithManager(mgr ctrl.Manager) error {
52+
if err := r.setupIndexer(mgr); err != nil {
53+
return err
54+
}
3855
return ctrl.NewControllerManagedBy(mgr).
3956
For(&gatewayv1.Gateway{}).
4057
Watches(
@@ -46,6 +63,10 @@ func (r *GatewayReconciler) SetupWithManager(mgr ctrl.Manager) error {
4663
&gatewayv1.HTTPRoute{},
4764
handler.EnqueueRequestsFromMapFunc(r.listGatewaysForHTTPRoute),
4865
).
66+
Watches(
67+
&v1alpha1.GatewayProxy{},
68+
handler.EnqueueRequestsFromMapFunc(r.listGatewaysForGatewayProxy),
69+
).
4970
Complete(r)
5071
}
5172

@@ -181,6 +202,35 @@ func (r *GatewayReconciler) checkGatewayClass(gateway *gatewayv1.Gateway) bool {
181202
return matchesController(string(gatewayClass.Spec.ControllerName))
182203
}
183204

205+
func (r *GatewayReconciler) listGatewaysForGatewayProxy(ctx context.Context, obj client.Object) []reconcile.Request {
206+
gatewayProxy, ok := obj.(*v1alpha1.GatewayProxy)
207+
if !ok {
208+
r.Log.Error(fmt.Errorf("unexpected object type"), "failed to convert object to GatewayProxy")
209+
return nil
210+
}
211+
namespace := gatewayProxy.GetNamespace()
212+
name := gatewayProxy.GetName()
213+
214+
gatewayList := &gatewayv1.GatewayList{}
215+
if err := r.List(ctx, gatewayList, client.MatchingFields{
216+
indexer.ParametersRef: indexer.GenIndexKey(namespace, name),
217+
}); err != nil {
218+
r.Log.Error(err, "failed to list gateways for gateway proxy", "gatewayproxy", gatewayProxy.GetName())
219+
return nil
220+
}
221+
222+
recs := make([]reconcile.Request, 0, len(gatewayList.Items))
223+
for _, gateway := range gatewayList.Items {
224+
recs = append(recs, reconcile.Request{
225+
NamespacedName: client.ObjectKey{
226+
Namespace: gateway.GetNamespace(),
227+
Name: gateway.GetName(),
228+
},
229+
})
230+
}
231+
return recs
232+
}
233+
184234
func (r *GatewayReconciler) listGatewaysForHTTPRoute(_ context.Context, obj client.Object) []reconcile.Request {
185235
httpRoute, ok := obj.(*gatewayv1.HTTPRoute)
186236
if !ok {

internal/controller/indexer/indexer.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
const (
99
ServiceIndexRef = "serviceRef"
1010
ExtensionRef = "extensionRef"
11+
ParametersRef = "parametersRef"
1112
)
1213

1314
func GenIndexKey(namespace, name string) string {
@@ -50,3 +51,15 @@ func HTTPRouteExtensionIndexFunc(rawObj client.Object) []string {
5051
}
5152
return keys
5253
}
54+
55+
func GatewayParametersRefIndexFunc(rawObj client.Object) []string {
56+
gw := rawObj.(*gatewayv1.Gateway)
57+
var keys []string
58+
if gw.Spec.Infrastructure != nil && gw.Spec.Infrastructure.ParametersRef != nil {
59+
// now we only case kind: GatewayProxy
60+
if gw.Spec.Infrastructure.ParametersRef.Kind == "GatewayProxy" {
61+
keys = append(keys, GenIndexKey(gw.GetNamespace(), string(gw.Spec.Infrastructure.ParametersRef.Name)))
62+
}
63+
}
64+
return keys
65+
}

0 commit comments

Comments
 (0)