Skip to content

Commit b0ba08c

Browse files
authored
feat: support global rules (#62)
Signed-off-by: ashing <[email protected]>
1 parent 7f955b9 commit b0ba08c

File tree

15 files changed

+444
-37
lines changed

15 files changed

+444
-37
lines changed

.dockerignore

Lines changed: 0 additions & 3 deletions
This file was deleted.

Dockerfile.dev

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,12 @@ RUN apt update \
2121
&& mv ./node-binary/linux-${TARGETARCH} /bin/adc \
2222
&& rm -rf /app
2323

24-
FROM golang:1.22 AS builder
25-
WORKDIR /workspace
26-
COPY go.* ./
27-
28-
RUN if [ "$ENABLE_PROXY" = "true" ] ; then go env -w GOPROXY=https://goproxy.cn,direct ; fi \
29-
&& go mod download
30-
31-
COPY . .
32-
33-
RUN --mount=type=cache,target=/root/.cache/go-build make build && mv bin/api7-ingress-controller /bin && rm -rf /workspace
34-
3524
FROM debian:bullseye-slim
3625
WORKDIR /app
3726

38-
COPY --from=builder /bin/api7-ingress-controller .
3927
COPY --from=node_builder /bin/adc /bin/adc
40-
COPY config/samples/config.yaml ./conf/config.yaml
28+
COPY ./bin/api7-ingress-controller .
29+
COPY ./config/samples/config.yaml ./conf/config.yaml
4130

4231
ENTRYPOINT ["/app/api7-ingress-controller"]
4332
CMD ["-c", "/app/conf/config.yaml"]

Makefile

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,10 @@ kind-load-images: pull-infra-images
127127
@kind load docker-image $(IMG) --name $(KIND_NAME)
128128
@kind load docker-image jmalloc/echo-server:latest --name $(KIND_NAME)
129129

130+
.PHONY: kind-load-ingress-image
131+
kind-load-ingress-image:
132+
@kind load docker-image $(IMG) --name $(KIND_NAME)
133+
130134
.PHONY: pull-infra-images
131135
pull-infra-images:
132136
@docker pull hkccr.ccs.tencentyun.com/api7-dev/api7-ee-3-gateway:dev
@@ -146,6 +150,8 @@ kind-load-image:
146150
build: manifests generate fmt vet ## Build manager binary.
147151
CGO_ENABLED=0 go build -o bin/api7-ingress-controller -ldflags $(GO_LDFLAGS) cmd/main.go
148152

153+
linux-build:
154+
GOOS=linux GOARCH=arm64 CGO_ENABLED=0 go build -o bin/api7-ingress-controller -ldflags $(GO_LDFLAGS) cmd/main.go
149155

150156
.PHONY: build-image
151157
build-image: docker-build
@@ -158,7 +164,7 @@ run: manifests generate fmt vet ## Run a controller from your host.
158164
# (i.e. docker build --platform linux/arm64). However, you must enable docker buildKit for it.
159165
# More info: https://docs.docker.com/develop/develop-images/build_enhancements/
160166
.PHONY: docker-build
161-
docker-build: ## Build docker image with the manager.
167+
docker-build: build ## Build docker image with the manager.
162168
$(CONTAINER_TOOL) build -t ${IMG} -f Dockerfile.dev .
163169

164170
.PHONY: docker-push

config/rbac/role.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,14 @@ rules:
5555
- get
5656
- list
5757
- watch
58+
- apiGroups:
59+
- gateway.apisix.io
60+
resources:
61+
- gatewayproxies
62+
verbs:
63+
- get
64+
- list
65+
- watch
5866
- apiGroups:
5967
- gateway.apisix.io
6068
resources:

internal/controller/gateway_controller.go

Lines changed: 84 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,10 +35,26 @@ 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.Background(),
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{}).
57+
WithEventFilter(predicate.GenerationChangedPredicate{}).
4058
Watches(
4159
&gatewayv1.GatewayClass{},
4260
handler.EnqueueRequestsFromMapFunc(r.listGatewayForGatewayClass),
@@ -46,6 +64,10 @@ func (r *GatewayReconciler) SetupWithManager(mgr ctrl.Manager) error {
4664
&gatewayv1.HTTPRoute{},
4765
handler.EnqueueRequestsFromMapFunc(r.listGatewaysForHTTPRoute),
4866
).
67+
Watches(
68+
&v1alpha1.GatewayProxy{},
69+
handler.EnqueueRequestsFromMapFunc(r.listGatewaysForGatewayProxy),
70+
).
4971
Complete(r)
5072
}
5173

@@ -99,6 +121,13 @@ func (r *GatewayReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
99121
Secrets: make(map[types.NamespacedName]*corev1.Secret),
100122
}
101123
r.processListenerConfig(tctx, gateway, ns)
124+
if err := r.processInfrastructure(tctx, gateway, ns); err != nil {
125+
acceptStatus = status{
126+
status: false,
127+
msg: err.Error(),
128+
}
129+
}
130+
102131
if err := r.Provider.Update(ctx, tctx, gateway); err != nil {
103132
acceptStatus = status{
104133
status: false,
@@ -108,6 +137,7 @@ func (r *GatewayReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
108137

109138
ListenerStatuses, err := getListenerStatus(ctx, r.Client, gateway)
110139
if err != nil {
140+
log.Error(err, "failed to get listener status", "gateway", gateway.GetName())
111141
return ctrl.Result{}, err
112142
}
113143

@@ -123,6 +153,7 @@ func (r *GatewayReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
123153

124154
return ctrl.Result{}, r.Status().Update(ctx, gateway)
125155
}
156+
126157
return ctrl.Result{}, nil
127158
}
128159

@@ -181,6 +212,35 @@ func (r *GatewayReconciler) checkGatewayClass(gateway *gatewayv1.Gateway) bool {
181212
return matchesController(string(gatewayClass.Spec.ControllerName))
182213
}
183214

215+
func (r *GatewayReconciler) listGatewaysForGatewayProxy(ctx context.Context, obj client.Object) []reconcile.Request {
216+
gatewayProxy, ok := obj.(*v1alpha1.GatewayProxy)
217+
if !ok {
218+
r.Log.Error(fmt.Errorf("unexpected object type"), "failed to convert object to GatewayProxy")
219+
return nil
220+
}
221+
namespace := gatewayProxy.GetNamespace()
222+
name := gatewayProxy.GetName()
223+
224+
gatewayList := &gatewayv1.GatewayList{}
225+
if err := r.List(ctx, gatewayList, client.MatchingFields{
226+
indexer.ParametersRef: indexer.GenIndexKey(namespace, name),
227+
}); err != nil {
228+
r.Log.Error(err, "failed to list gateways for gateway proxy", "gatewayproxy", gatewayProxy.GetName())
229+
return nil
230+
}
231+
232+
recs := make([]reconcile.Request, 0, len(gatewayList.Items))
233+
for _, gateway := range gatewayList.Items {
234+
recs = append(recs, reconcile.Request{
235+
NamespacedName: client.ObjectKey{
236+
Namespace: gateway.GetNamespace(),
237+
Name: gateway.GetName(),
238+
},
239+
})
240+
}
241+
return recs
242+
}
243+
184244
func (r *GatewayReconciler) listGatewaysForHTTPRoute(_ context.Context, obj client.Object) []reconcile.Request {
185245
httpRoute, ok := obj.(*gatewayv1.HTTPRoute)
186246
if !ok {
@@ -215,6 +275,30 @@ func (r *GatewayReconciler) listGatewaysForHTTPRoute(_ context.Context, obj clie
215275
return recs
216276
}
217277

278+
func (r *GatewayReconciler) processInfrastructure(tctx *provider.TranslateContext, gateway *gatewayv1.Gateway, ns string) error {
279+
infra := gateway.Spec.Infrastructure
280+
if infra == nil || infra.ParametersRef == nil {
281+
return nil
282+
}
283+
284+
paramRef := infra.ParametersRef
285+
if string(paramRef.Group) == v1alpha1.GroupVersion.Group && string(paramRef.Kind) == "GatewayProxy" {
286+
gatewayProxy := &v1alpha1.GatewayProxy{}
287+
if err := r.Get(context.Background(), client.ObjectKey{
288+
Namespace: ns,
289+
Name: paramRef.Name,
290+
}, gatewayProxy); err != nil {
291+
log.Error(err, "failed to get GatewayProxy", "namespace", ns, "name", paramRef.Name)
292+
return err
293+
} else {
294+
log.Info("found GatewayProxy for Gateway", "gateway", gateway.Name, "gatewayproxy", gatewayProxy.Name)
295+
tctx.GatewayProxy = gatewayProxy
296+
}
297+
}
298+
299+
return nil
300+
}
301+
218302
func (r *GatewayReconciler) processListenerConfig(tctx *provider.TranslateContext, gateway *gatewayv1.Gateway, ns string) {
219303
listeners := gateway.Spec.Listeners
220304
for _, listener := range listeners {

internal/controller/httproute_controller.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
ctrl "sigs.k8s.io/controller-runtime"
1515
"sigs.k8s.io/controller-runtime/pkg/client"
1616
"sigs.k8s.io/controller-runtime/pkg/handler"
17+
"sigs.k8s.io/controller-runtime/pkg/predicate"
1718
"sigs.k8s.io/controller-runtime/pkg/reconcile"
1819
gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"
1920

@@ -42,6 +43,7 @@ func (r *HTTPRouteReconciler) SetupWithManager(mgr ctrl.Manager) error {
4243
}
4344
return ctrl.NewControllerManagedBy(mgr).
4445
For(&gatewayv1.HTTPRoute{}).
46+
WithEventFilter(predicate.GenerationChangedPredicate{}).
4547
Watches(&discoveryv1.EndpointSlice{},
4648
handler.EnqueueRequestsFromMapFunc(r.listHTTPRoutesByServiceBef),
4749
).
@@ -96,6 +98,7 @@ func (r *HTTPRouteReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
9698
}
9799

98100
tctx := provider.NewDefaultTranslateContext()
101+
99102
if err := r.processHTTPRoute(tctx, hr); err != nil {
100103
acceptStatus.status = false
101104
acceptStatus.msg = err.Error()
@@ -113,8 +116,6 @@ func (r *HTTPRouteReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
113116
acceptStatus.msg = err.Error()
114117
}
115118

116-
// process the HTTPRoute
117-
118119
// TODO: diff the old and new status
119120
hr.Status.Parents = make([]gatewayv1.RouteParentStatus, 0, len(gateways))
120121
for _, gateway := range gateways {
@@ -139,15 +140,15 @@ func (r *HTTPRouteReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
139140
// SetupWithManager sets up the controller with the Manager.
140141
func (r *HTTPRouteReconciler) setupIndexer(mgr ctrl.Manager) error {
141142
if err := mgr.GetFieldIndexer().IndexField(
142-
context.TODO(),
143+
context.Background(),
143144
&gatewayv1.HTTPRoute{},
144145
indexer.ExtensionRef,
145146
indexer.HTTPRouteExtensionIndexFunc,
146147
); err != nil {
147148
return err
148149
}
149150
if err := mgr.GetFieldIndexer().IndexField(
150-
context.TODO(),
151+
context.Background(),
151152
&gatewayv1.HTTPRoute{},
152153
indexer.ServiceIndexRef,
153154
indexer.HTTPRouteServiceIndexFunc,

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+
if gw.Spec.Infrastructure != nil && gw.Spec.Infrastructure.ParametersRef != nil {
58+
// now we only care about kind: GatewayProxy
59+
if gw.Spec.Infrastructure.ParametersRef.Kind == "GatewayProxy" {
60+
name := gw.Spec.Infrastructure.ParametersRef.Name
61+
return []string{GenIndexKey(gw.GetNamespace(), name)}
62+
}
63+
}
64+
return nil
65+
}

internal/manager/controllers.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
// +kubebuilder:rbac:groups="",resources=secrets,verbs=get;list;watch
1818
// +kubebuilder:rbac:groups="",resources=namespaces,verbs=get;list;watch
1919
// +kubebuilder:rbac:groups=gateway.apisix.io,resources=pluginconfigs,verbs=get;list;watch
20+
// +kubebuilder:rbac:groups=gateway.apisix.io,resources=gatewayproxies,verbs=get;list;watch
2021

2122
type Controller interface {
2223
SetupWithManager(mgr manager.Manager) error

0 commit comments

Comments
 (0)