Skip to content

Commit 64a2fce

Browse files
committed
fix: add translator
Signed-off-by: ashing <[email protected]>
1 parent 6ae96a8 commit 64a2fce

File tree

5 files changed

+81
-4
lines changed

5 files changed

+81
-4
lines changed

internal/controller/gateway_controller.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ func (r *GatewayReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
120120
Secrets: make(map[types.NamespacedName]*corev1.Secret),
121121
}
122122
r.processListenerConfig(tctx, gateway, ns)
123+
r.processGatewayProxy(tctx, gateway, ns)
124+
123125
if err := r.Provider.Update(ctx, tctx, gateway); err != nil {
124126
acceptStatus = status{
125127
status: false,
@@ -265,6 +267,25 @@ func (r *GatewayReconciler) listGatewaysForHTTPRoute(_ context.Context, obj clie
265267
return recs
266268
}
267269

270+
func (r *GatewayReconciler) processGatewayProxy(tctx *provider.TranslateContext, gateway *gatewayv1.Gateway, ns string) {
271+
infra := gateway.Spec.Infrastructure
272+
if infra != nil && infra.ParametersRef != nil {
273+
paramRef := infra.ParametersRef
274+
if string(paramRef.Group) == v1alpha1.GroupVersion.Group && string(paramRef.Kind) == "GatewayProxy" {
275+
gatewayProxy := &v1alpha1.GatewayProxy{}
276+
if err := r.Get(context.Background(), client.ObjectKey{
277+
Namespace: ns,
278+
Name: string(paramRef.Name),
279+
}, gatewayProxy); err != nil {
280+
log.Error(err, "failed to get GatewayProxy", "namespace", ns, "name", string(paramRef.Name))
281+
} else {
282+
log.Info("found GatewayProxy for Gateway", "gateway", gateway.Name, "gatewayproxy", gatewayProxy.Name)
283+
tctx.GatewayProxy = gatewayProxy
284+
}
285+
}
286+
}
287+
}
288+
268289
func (r *GatewayReconciler) processListenerConfig(tctx *provider.TranslateContext, gateway *gatewayv1.Gateway, ns string) {
269290
listeners := gateway.Spec.Listeners
270291
for _, listener := range listeners {

internal/provider/adc/adc.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ func (d *adcClient) Update(ctx context.Context, tctx *provider.TranslateContext,
4949
switch obj := obj.(type) {
5050
case *gatewayv1.HTTPRoute:
5151
result, err = d.translator.TranslateHTTPRoute(tctx, obj.DeepCopy())
52+
case *gatewayv1.Gateway:
53+
result, err = d.translator.TranslateGateway(tctx, obj.DeepCopy())
5254
}
5355
if err != nil {
5456
return err
@@ -58,7 +60,8 @@ func (d *adcClient) Update(ctx context.Context, tctx *provider.TranslateContext,
5860
}
5961

6062
resources := types.Resources{
61-
Services: result.Services,
63+
Services: result.Services,
64+
GlobalRules: result.GlobalRules,
6265
}
6366

6467
return d.sync(Task{
@@ -101,6 +104,7 @@ func (d *adcClient) sync(task Task) error {
101104
"sync",
102105
"-f", tmpFile.Name(),
103106
"--include-resource-type", "service",
107+
"--include-resource-type", "global_rule",
104108
"--tls-skip-verify",
105109
}
106110

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,51 @@
11
package translator
2+
3+
import (
4+
"encoding/json"
5+
6+
adctypes "github.com/api7/api7-ingress-controller/api/adc"
7+
"github.com/api7/api7-ingress-controller/api/v1alpha1"
8+
"github.com/api7/api7-ingress-controller/internal/provider"
9+
"github.com/api7/gopkg/pkg/log"
10+
"go.uber.org/zap"
11+
gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"
12+
)
13+
14+
// FillPluginsFromGatewayProxy fill plugins from GatewayProxy to given plugins
15+
func (t *Translator) FillPluginsFromGatewayProxy(plugins adctypes.Plugins, gatewayProxy *v1alpha1.GatewayProxy) {
16+
if gatewayProxy == nil {
17+
return
18+
}
19+
20+
for _, plugin := range gatewayProxy.Spec.Plugins {
21+
// only apply enabled plugins
22+
if !plugin.Enabled {
23+
continue
24+
}
25+
26+
pluginName := plugin.Name
27+
var pluginConfig map[string]any
28+
if err := json.Unmarshal(plugin.Config.Raw, &pluginConfig); err != nil {
29+
log.Errorw("gateway proxy plugin config unmarshal failed", zap.Error(err), zap.String("plugin", pluginName))
30+
continue
31+
}
32+
33+
log.Debugw("fill plugin from gateway proxy", zap.String("plugin", pluginName), zap.Any("config", pluginConfig))
34+
plugins[pluginName] = pluginConfig
35+
}
36+
}
37+
38+
// TranslateGateway translate gateway to adc resources
39+
func (t *Translator) TranslateGateway(tctx *provider.TranslateContext, gateway *gatewayv1.Gateway) (*TranslateResult, error) {
40+
result := &TranslateResult{}
41+
42+
// if gateway has a GatewayProxy resource, create a global service and apply plugins
43+
if tctx.GatewayProxy != nil {
44+
globalRules := adctypes.Plugins{}
45+
// apply plugins from GatewayProxy to global rules
46+
t.FillPluginsFromGatewayProxy(globalRules, tctx.GatewayProxy)
47+
result.GlobalRules = globalRules
48+
}
49+
50+
return result, nil
51+
}

internal/provider/adc/translator/translator.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ type Translator struct {
1010
Log logr.Logger
1111
}
1212
type TranslateResult struct {
13-
Routes []*adctypes.Route
14-
Services []*adctypes.Service
15-
SSL []*adctypes.SSL
13+
Routes []*adctypes.Route
14+
Services []*adctypes.Service
15+
SSL []*adctypes.SSL
16+
GlobalRules adctypes.Plugins
1617
}

internal/provider/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ type Provider interface {
2020
type TranslateContext struct {
2121
BackendRefs []gatewayv1.BackendRef
2222
GatewayTLSConfig []gatewayv1.GatewayTLSConfig
23+
GatewayProxy *v1alpha1.GatewayProxy
2324
EndpointSlices map[types.NamespacedName][]discoveryv1.EndpointSlice
2425
Secrets map[types.NamespacedName]*corev1.Secret
2526
PluginConfigs map[types.NamespacedName]*v1alpha1.PluginConfig

0 commit comments

Comments
 (0)