Skip to content

Commit 5cf5ec7

Browse files
committed
fix: r
Signed-off-by: ashing <[email protected]>
1 parent e59fb85 commit 5cf5ec7

File tree

8 files changed

+176
-98
lines changed

8 files changed

+176
-98
lines changed

internal/controller/consumer_controller.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,17 @@ func (r *ConsumerReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
138138
var statusErr error
139139
tctx := provider.NewDefaultTranslateContext()
140140

141+
gateway, err := r.getGateway(ctx, consumer)
142+
if err != nil {
143+
r.Log.Error(err, "failed to get gateway", "consumer", consumer)
144+
statusErr = err
145+
}
146+
147+
if err := ProcessGatewayProxy(r.Client, tctx, gateway); err != nil {
148+
r.Log.Error(err, "failed to process gateway proxy", "gateway", gateway)
149+
statusErr = err
150+
}
151+
141152
if err := r.processSpec(ctx, tctx, consumer); err != nil {
142153
r.Log.Error(err, "failed to process consumer spec", "consumer", consumer)
143154
statusErr = err
@@ -201,6 +212,22 @@ func (r *ConsumerReconciler) updateStatus(ctx context.Context, consumer *v1alpha
201212
return nil
202213
}
203214

215+
func (r *ConsumerReconciler) getGateway(ctx context.Context, consumer *v1alpha1.Consumer) (*gatewayv1.Gateway, error) {
216+
ns := consumer.GetNamespace()
217+
if consumer.Spec.GatewayRef.Namespace != nil {
218+
ns = *consumer.Spec.GatewayRef.Namespace
219+
}
220+
gateway := &gatewayv1.Gateway{}
221+
if err := r.Get(ctx, client.ObjectKey{
222+
Name: consumer.Spec.GatewayRef.Name,
223+
Namespace: ns,
224+
}, gateway); err != nil {
225+
r.Log.Error(err, "failed to get gateway", "gateway", consumer.Spec.GatewayRef.Name)
226+
return nil, err
227+
}
228+
return gateway, nil
229+
}
230+
204231
func (r *ConsumerReconciler) checkGatewayRef(object client.Object) bool {
205232
consumer, ok := object.(*v1alpha1.Consumer)
206233
if !ok {

internal/controller/gateway_controller.go

Lines changed: 1 addition & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -267,60 +267,7 @@ func (r *GatewayReconciler) listGatewaysForHTTPRoute(_ context.Context, obj clie
267267
}
268268

269269
func (r *GatewayReconciler) processInfrastructure(tctx *provider.TranslateContext, gateway *gatewayv1.Gateway) error {
270-
infra := gateway.Spec.Infrastructure
271-
if infra == nil || infra.ParametersRef == nil {
272-
return nil
273-
}
274-
275-
ns := gateway.GetNamespace()
276-
paramRef := infra.ParametersRef
277-
if string(paramRef.Group) == v1alpha1.GroupVersion.Group && string(paramRef.Kind) == "GatewayProxy" {
278-
gatewayProxy := &v1alpha1.GatewayProxy{}
279-
if err := r.Get(context.Background(), client.ObjectKey{
280-
Namespace: ns,
281-
Name: paramRef.Name,
282-
}, gatewayProxy); err != nil {
283-
log.Error(err, "failed to get GatewayProxy", "namespace", ns, "name", paramRef.Name)
284-
return err
285-
} else {
286-
log.Info("found GatewayProxy for Gateway", "gateway", gateway.Name, "gatewayproxy", gatewayProxy.Name)
287-
tctx.GatewayProxy = gatewayProxy
288-
289-
// Process provider secrets if provider exists
290-
if gatewayProxy.Spec.Provider != nil && gatewayProxy.Spec.Provider.Type == v1alpha1.ProviderTypeControlPlane {
291-
if gatewayProxy.Spec.Provider.ControlPlane != nil &&
292-
gatewayProxy.Spec.Provider.ControlPlane.Auth.Type == v1alpha1.AuthTypeAdminKey &&
293-
gatewayProxy.Spec.Provider.ControlPlane.Auth.AdminKey != nil &&
294-
gatewayProxy.Spec.Provider.ControlPlane.Auth.AdminKey.ValueFrom != nil &&
295-
gatewayProxy.Spec.Provider.ControlPlane.Auth.AdminKey.ValueFrom.SecretKeyRef != nil {
296-
297-
secretRef := gatewayProxy.Spec.Provider.ControlPlane.Auth.AdminKey.ValueFrom.SecretKeyRef
298-
secret := &corev1.Secret{}
299-
if err := r.Get(context.Background(), client.ObjectKey{
300-
Namespace: ns,
301-
Name: secretRef.Name,
302-
}, secret); err != nil {
303-
log.Error(err, "failed to get secret for GatewayProxy provider",
304-
"namespace", ns,
305-
"name", secretRef.Name)
306-
return err
307-
}
308-
309-
log.Info("found secret for GatewayProxy provider",
310-
"gateway", gateway.Name,
311-
"gatewayproxy", gatewayProxy.Name,
312-
"secret", secretRef.Name)
313-
314-
tctx.Secrets[types.NamespacedName{
315-
Namespace: ns,
316-
Name: secretRef.Name,
317-
}] = secret
318-
}
319-
}
320-
}
321-
}
322-
323-
return nil
270+
return ProcessGatewayProxy(r.Client, tctx, gateway)
324271
}
325272

326273
func (r *GatewayReconciler) processListenerConfig(tctx *provider.TranslateContext, gateway *gatewayv1.Gateway) {

internal/controller/httproute_controller.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,12 @@ func (r *HTTPRouteReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
114114

115115
tctx := provider.NewDefaultTranslateContext()
116116

117+
for _, gateway := range gateways {
118+
if err := ProcessGatewayProxy(r.Client, tctx, gateway.Gateway); err != nil {
119+
return ctrl.Result{}, err
120+
}
121+
}
122+
117123
if err := r.processHTTPRoute(tctx, hr); err != nil {
118124
acceptStatus.status = false
119125
acceptStatus.msg = err.Error()

internal/controller/ingress_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,7 @@ func (r *IngressReconciler) processIngressClassParameters(ctx context.Context, t
548548
}
549549

550550
r.Log.Info("found GatewayProxy for IngressClass", "ingressClass", ingressClass.Name, "gatewayproxy", gatewayProxy.Name)
551-
tctx.GatewayProxy = gatewayProxy
551+
tctx.GatewayProxies = append(tctx.GatewayProxies, *gatewayProxy)
552552

553553
// check if the provider field references a secret
554554
if gatewayProxy.Spec.Provider != nil && gatewayProxy.Spec.Provider.Type == v1alpha1.ProviderTypeControlPlane {

internal/controller/utils.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,16 @@ import (
55
"fmt"
66
"strings"
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/provider"
11+
"github.com/api7/gopkg/pkg/log"
912
"github.com/samber/lo"
1013
corev1 "k8s.io/api/core/v1"
1114
networkingv1 "k8s.io/api/networking/v1"
1215
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1316
"k8s.io/apimachinery/pkg/labels"
17+
"k8s.io/apimachinery/pkg/types"
1418
"sigs.k8s.io/controller-runtime/pkg/client"
1519
"sigs.k8s.io/controller-runtime/pkg/reconcile"
1620
gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"
@@ -759,3 +763,63 @@ func SplitMetaNamespaceKey(key string) (namespace, name string, err error) {
759763

760764
return "", "", fmt.Errorf("unexpected key format: %q", key)
761765
}
766+
767+
func ProcessGatewayProxy(r client.Client, tctx *provider.TranslateContext, gateway *gatewayv1.Gateway) error {
768+
if gateway == nil {
769+
return nil
770+
}
771+
infra := gateway.Spec.Infrastructure
772+
if infra == nil || infra.ParametersRef == nil {
773+
return nil
774+
}
775+
776+
ns := gateway.GetNamespace()
777+
paramRef := infra.ParametersRef
778+
if string(paramRef.Group) == v1alpha1.GroupVersion.Group && string(paramRef.Kind) == "GatewayProxy" {
779+
gatewayProxy := &v1alpha1.GatewayProxy{}
780+
if err := r.Get(context.Background(), client.ObjectKey{
781+
Namespace: ns,
782+
Name: paramRef.Name,
783+
}, gatewayProxy); err != nil {
784+
log.Error(err, "failed to get GatewayProxy", "namespace", ns, "name", paramRef.Name)
785+
return err
786+
} else {
787+
log.Info("found GatewayProxy for Gateway", "gateway", gateway.Name, "gatewayproxy", gatewayProxy.Name)
788+
tctx.GatewayProxies = append(tctx.GatewayProxies, *gatewayProxy)
789+
790+
// Process provider secrets if provider exists
791+
if gatewayProxy.Spec.Provider != nil && gatewayProxy.Spec.Provider.Type == v1alpha1.ProviderTypeControlPlane {
792+
if gatewayProxy.Spec.Provider.ControlPlane != nil &&
793+
gatewayProxy.Spec.Provider.ControlPlane.Auth.Type == v1alpha1.AuthTypeAdminKey &&
794+
gatewayProxy.Spec.Provider.ControlPlane.Auth.AdminKey != nil &&
795+
gatewayProxy.Spec.Provider.ControlPlane.Auth.AdminKey.ValueFrom != nil &&
796+
gatewayProxy.Spec.Provider.ControlPlane.Auth.AdminKey.ValueFrom.SecretKeyRef != nil {
797+
798+
secretRef := gatewayProxy.Spec.Provider.ControlPlane.Auth.AdminKey.ValueFrom.SecretKeyRef
799+
secret := &corev1.Secret{}
800+
if err := r.Get(context.Background(), client.ObjectKey{
801+
Namespace: ns,
802+
Name: secretRef.Name,
803+
}, secret); err != nil {
804+
log.Error(err, "failed to get secret for GatewayProxy provider",
805+
"namespace", ns,
806+
"name", secretRef.Name)
807+
return err
808+
}
809+
810+
log.Info("found secret for GatewayProxy provider",
811+
"gateway", gateway.Name,
812+
"gatewayproxy", gatewayProxy.Name,
813+
"secret", secretRef.Name)
814+
815+
tctx.Secrets[types.NamespacedName{
816+
Namespace: ns,
817+
Name: secretRef.Name,
818+
}] = secret
819+
}
820+
}
821+
}
822+
}
823+
824+
return nil
825+
}

internal/provider/adc/adc.go

Lines changed: 69 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"github.com/api7/api7-ingress-controller/internal/provider"
2121
"github.com/api7/api7-ingress-controller/internal/provider/adc/translator"
2222
"github.com/api7/gopkg/pkg/log"
23+
"k8s.io/apimachinery/pkg/types"
2324
)
2425

2526
type ResourceKind struct {
@@ -64,22 +65,64 @@ func (d *adcClient) getConfigs(rk ResourceKind) []adcConfig {
6465
return d.configs[rk]
6566
}
6667

67-
func (d *adcClient) updateGatewayConfigs(rk ResourceKind, tctx *provider.TranslateContext) ([]adcConfig, error) {
68-
// get gateway proxy from tctx
69-
return nil, nil
70-
}
68+
func (d *adcClient) getConfigsForGatewayProxy(rk ResourceKind, tctx *provider.TranslateContext, gatewayProxy *v1alpha1.GatewayProxy) (*adcConfig, error) {
69+
if gatewayProxy == nil || gatewayProxy.Spec.Provider == nil {
70+
return nil, nil
71+
}
7172

72-
func (d *adcClient) updateIngressConfigs(rk ResourceKind, tctx *provider.TranslateContext) ([]adcConfig, error) {
73-
// get gateway proxy from tctx
74-
return nil, nil
75-
}
73+
provider := gatewayProxy.Spec.Provider
74+
if provider.Type != v1alpha1.ProviderTypeControlPlane || provider.ControlPlane == nil {
75+
return nil, nil
76+
}
77+
78+
endpoints := provider.ControlPlane.Endpoints
79+
if len(endpoints) == 0 {
80+
return nil, errors.New("no endpoints found")
81+
}
82+
83+
endpoint := endpoints[0]
84+
config := adcConfig{
85+
ServerAddr: endpoint,
86+
}
87+
88+
if provider.ControlPlane.Auth.Type == v1alpha1.AuthTypeAdminKey && provider.ControlPlane.Auth.AdminKey != nil {
89+
if provider.ControlPlane.Auth.AdminKey.ValueFrom != nil && provider.ControlPlane.Auth.AdminKey.ValueFrom.SecretKeyRef != nil {
90+
secretRef := provider.ControlPlane.Auth.AdminKey.ValueFrom.SecretKeyRef
91+
secret, ok := tctx.Secrets[types.NamespacedName{
92+
Namespace: rk.Namespace,
93+
Name: secretRef.Name,
94+
}]
95+
if ok {
96+
if token, ok := secret.Data[secretRef.Key]; ok {
97+
config.Token = string(token)
98+
}
99+
}
100+
} else if provider.ControlPlane.Auth.AdminKey.Value != "" {
101+
config.Token = provider.ControlPlane.Auth.AdminKey.Value
102+
}
103+
}
76104

77-
func (d *adcClient) updateHTTPRouteConfigs(rk ResourceKind, tctx *provider.TranslateContext) ([]adcConfig, error) {
78-
return nil, nil
105+
if config.Token == "" {
106+
return nil, errors.New("no token found")
107+
}
108+
109+
return &config, nil
79110
}
80111

81-
func (d *adcClient) updateConsumerConfigs(rk ResourceKind, tctx *provider.TranslateContext) ([]adcConfig, error) {
82-
return nil, nil
112+
func (d *adcClient) updateConfigs(rk ResourceKind, tctx *provider.TranslateContext) error {
113+
var configs []adcConfig
114+
for _, gatewayProxy := range tctx.GatewayProxies {
115+
config, err := d.getConfigsForGatewayProxy(rk, tctx, &gatewayProxy)
116+
if err != nil {
117+
return err
118+
}
119+
if config != nil {
120+
configs = append(configs, *config)
121+
}
122+
}
123+
124+
d.configs[rk] = configs
125+
return nil
83126
}
84127

85128
func (d *adcClient) Update(ctx context.Context, tctx *provider.TranslateContext, obj client.Object) error {
@@ -90,60 +133,47 @@ func (d *adcClient) Update(ctx context.Context, tctx *provider.TranslateContext,
90133
err error
91134
)
92135

93-
var configs []adcConfig
94-
95-
rk := ResourceKind{
96-
Kind: obj.GetObjectKind().GroupVersionKind().Kind,
97-
Namespace: obj.GetNamespace(),
98-
Name: obj.GetName(),
99-
}
100-
101136
switch t := obj.(type) {
102137
case *gatewayv1.HTTPRoute:
103138
result, err = d.translator.TranslateHTTPRoute(tctx, t.DeepCopy())
104-
if err != nil {
105-
return err
106-
}
107139
resourceTypes = append(resourceTypes, "service")
108-
configs, err = d.updateHTTPRouteConfigs(rk, tctx)
109-
if err != nil {
110-
return err
111-
}
112140
case *gatewayv1.Gateway:
113141
result, err = d.translator.TranslateGateway(tctx, t.DeepCopy())
114142
if err != nil {
115143
return err
116144
}
117145
resourceTypes = append(resourceTypes, "global_rule", "ssl", "plugin_metadata")
118-
configs, err = d.updateGatewayConfigs(rk, tctx)
119-
if err != nil {
120-
return err
121-
}
122146
case *networkingv1.Ingress:
123147
result, err = d.translator.TranslateIngress(tctx, t.DeepCopy())
124148
if err != nil {
125149
return err
126150
}
127151
resourceTypes = append(resourceTypes, "service", "ssl")
128-
configs, err = d.updateIngressConfigs(rk, tctx)
129-
if err != nil {
130-
return err
131-
}
132152
case *v1alpha1.Consumer:
133153
result, err = d.translator.TranslateConsumerV1alpha1(tctx, t.DeepCopy())
134154
if err != nil {
135155
return err
136156
}
137157
resourceTypes = append(resourceTypes, "consumer")
138-
configs, err = d.updateConsumerConfigs(rk, tctx)
139-
if err != nil {
140-
return err
141-
}
158+
}
159+
if err != nil {
160+
return err
142161
}
143162
if result == nil {
144163
return nil
145164
}
146165

166+
// update adc configs
167+
rk := ResourceKind{
168+
Kind: obj.GetObjectKind().GroupVersionKind().Kind,
169+
Namespace: obj.GetNamespace(),
170+
Name: obj.GetName(),
171+
}
172+
if err := d.updateConfigs(rk, tctx); err != nil {
173+
return err
174+
}
175+
configs := d.getConfigs(rk)
176+
147177
return d.sync(Task{
148178
Name: obj.GetName(),
149179
Labels: label.GenLabel(obj),

internal/provider/adc/translator/gateway.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,18 @@ func (t *Translator) TranslateGateway(tctx *provider.TranslateContext, obj *gate
3232
result.SSL = append(result.SSL, ssl...)
3333
}
3434
}
35-
if tctx.GatewayProxy != nil {
35+
var gatewayProxy *v1alpha1.GatewayProxy
36+
if len(tctx.GatewayProxies) > 0 {
37+
gatewayProxy = &tctx.GatewayProxies[0]
38+
}
39+
if gatewayProxy != nil {
3640
var (
3741
globalRules = adctypes.Plugins{}
3842
pluginMetadata = adctypes.Plugins{}
3943
)
4044
// apply plugins from GatewayProxy to global rules
41-
t.fillPluginsFromGatewayProxy(globalRules, tctx.GatewayProxy)
42-
t.fillPluginMetadataFromGatewayProxy(pluginMetadata, tctx.GatewayProxy)
45+
t.fillPluginsFromGatewayProxy(globalRules, gatewayProxy)
46+
t.fillPluginMetadataFromGatewayProxy(pluginMetadata, gatewayProxy)
4347
result.GlobalRules = globalRules
4448
result.PluginMetadata = pluginMetadata
4549
}

internal/provider/provider.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ type Provider interface {
2020
type TranslateContext struct {
2121
BackendRefs []gatewayv1.BackendRef
2222
GatewayTLSConfig []gatewayv1.GatewayTLSConfig
23-
GatewayProxy *v1alpha1.GatewayProxy
23+
GatewayProxies []v1alpha1.GatewayProxy
2424
Credentials []v1alpha1.Credential
2525
Gateways []gatewayv1.Gateway
2626
EndpointSlices map[types.NamespacedName][]discoveryv1.EndpointSlice

0 commit comments

Comments
 (0)