Skip to content

Commit a7a4f4d

Browse files
committed
Merge remote-tracking branch 'origin/release-v2-dev' into feat/ingress_translator
2 parents 31a5dca + a7229ce commit a7a4f4d

File tree

7 files changed

+219
-53
lines changed

7 files changed

+219
-53
lines changed

internal/controller/consumer_controller.go

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,37 @@ func (r *ConsumerReconciler) SetupWithManager(mgr ctrl.Manager) error {
5959
},
6060
),
6161
).
62+
Watches(&corev1.Secret{},
63+
handler.EnqueueRequestsFromMapFunc(r.listConsumersForSecret),
64+
).
6265
Complete(r)
6366
}
6467

68+
func (r *ConsumerReconciler) listConsumersForSecret(ctx context.Context, obj client.Object) []reconcile.Request {
69+
secret, ok := obj.(*corev1.Secret)
70+
if !ok {
71+
r.Log.Error(nil, "failed to convert to Secret", "object", obj)
72+
return nil
73+
}
74+
consumerList := &v1alpha1.ConsumerList{}
75+
if err := r.List(ctx, consumerList, client.MatchingFields{
76+
indexer.SecretIndexRef: indexer.GenIndexKey(secret.GetNamespace(), secret.GetName()),
77+
}); err != nil {
78+
r.Log.Error(err, "failed to list consumers")
79+
return nil
80+
}
81+
requests := make([]reconcile.Request, 0, len(consumerList.Items))
82+
for _, consumer := range consumerList.Items {
83+
requests = append(requests, reconcile.Request{
84+
NamespacedName: client.ObjectKey{
85+
Name: consumer.Name,
86+
Namespace: consumer.Namespace,
87+
},
88+
})
89+
}
90+
return requests
91+
}
92+
6593
func (r *ConsumerReconciler) listConsumersForGateway(ctx context.Context, obj client.Object) []reconcile.Request {
6694
gateway, ok := obj.(*gatewayv1.Gateway)
6795
if !ok {
@@ -70,7 +98,7 @@ func (r *ConsumerReconciler) listConsumersForGateway(ctx context.Context, obj cl
7098
}
7199
consumerList := &v1alpha1.ConsumerList{}
72100
if err := r.List(ctx, consumerList, client.MatchingFields{
73-
indexer.ConsumerGatewayRef: indexer.GenIndexKey(gateway.Name, gateway.GetNamespace()),
101+
indexer.ConsumerGatewayRef: indexer.GenIndexKey(gateway.GetNamespace(), gateway.GetName()),
74102
}); err != nil {
75103
r.Log.Error(err, "failed to list consumers")
76104
return nil

internal/controller/indexer/indexer.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,35 @@ func setupConsumerIndexer(mgr ctrl.Manager) error {
5858
); err != nil {
5959
return err
6060
}
61+
if err := mgr.GetFieldIndexer().IndexField(
62+
context.Background(),
63+
&v1alpha1.Consumer{},
64+
SecretIndexRef,
65+
ConsumerSecretIndexFunc,
66+
); err != nil {
67+
return err
68+
}
6169
return nil
6270
}
71+
72+
func ConsumerSecretIndexFunc(rawObj client.Object) []string {
73+
consumer := rawObj.(*v1alpha1.Consumer)
74+
secretKeys := make([]string, 0)
75+
76+
for _, credential := range consumer.Spec.Credentials {
77+
if credential.SecretRef == nil {
78+
continue
79+
}
80+
ns := consumer.GetNamespace()
81+
if credential.SecretRef.Namespace != nil {
82+
ns = *credential.SecretRef.Namespace
83+
}
84+
key := GenIndexKey(ns, credential.SecretRef.Name)
85+
secretKeys = append(secretKeys, key)
86+
}
87+
return secretKeys
88+
}
89+
6390
func ConsumerGatewayRefIndexFunc(rawObj client.Object) []string {
6491
consumer := rawObj.(*v1alpha1.Consumer)
6592

internal/provider/adc/adc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ import (
99
"os/exec"
1010

1111
"go.uber.org/zap"
12-
"gopkg.in/yaml.v3"
1312
networkingv1 "k8s.io/api/networking/v1"
1413
"sigs.k8s.io/controller-runtime/pkg/client"
1514
gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"
15+
"sigs.k8s.io/yaml"
1616

1717
types "github.com/api7/api7-ingress-controller/api/adc"
1818
"github.com/api7/api7-ingress-controller/api/v1alpha1"

internal/provider/adc/translator/consumer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func (t *Translator) TranslateConsumerV1alpha1(tctx *provider.TranslateContext,
3939
}
4040
authConfig := make(map[string]any)
4141
for k, v := range secret.Data {
42-
authConfig[k] = v
42+
authConfig[k] = string(v)
4343
}
4444
credential.Config = authConfig
4545
} else {

test/e2e/crds/consumer.go

Lines changed: 117 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -110,42 +110,48 @@ spec:
110110
}
111111

112112
Context("Consumer plugins", func() {
113-
var keyAuthConsumer = `apiVersion: gateway.apisix.io/v1alpha1
113+
var limitCountConsumer = `
114+
apiVersion: gateway.apisix.io/v1alpha1
114115
kind: Consumer
115116
metadata:
116117
name: consumer-sample
117118
spec:
118119
gatewayRef:
119120
name: api7ee
120-
plugins:
121-
- name: key-auth
121+
credentials:
122+
- type: key-auth
123+
name: key-auth-sample
122124
config:
123125
key: sample-key
126+
plugins:
127+
- name: limit-count
128+
config:
129+
count: 2
130+
time_window: 60
131+
rejected_code: 503
132+
key: remote_addr
124133
`
125-
var basicAuthConsumer = `apiVersion: gateway.apisix.io/v1alpha1
134+
135+
var unlimitConsumer = `
136+
apiVersion: gateway.apisix.io/v1alpha1
126137
kind: Consumer
127138
metadata:
128-
name: consumer-sample
139+
name: consumer-sample2
129140
spec:
130141
gatewayRef:
131142
name: api7ee
132-
plugins:
133-
- name: basic-auth
143+
credentials:
144+
- type: key-auth
145+
name: key-auth-sample
134146
config:
135-
username: sample-user
136-
password: sample-password
147+
key: sample-key2
137148
`
138149

139150
BeforeEach(beforeEachHTTP)
140151

141-
It("key-auth", func() {
142-
s.ResourceApplied("Consumer", "consumer-sample", keyAuthConsumer, 1)
143-
144-
s.NewAPISIXClient().
145-
GET("/get").
146-
WithHost("httpbin.org").
147-
Expect().
148-
Status(401)
152+
It("limit-count plugin", func() {
153+
s.ResourceApplied("Consumer", "consumer-sample", limitCountConsumer, 1)
154+
s.ResourceApplied("Consumer", "consumer-sample2", unlimitConsumer, 1)
149155

150156
s.NewAPISIXClient().
151157
GET("/get").
@@ -154,46 +160,29 @@ spec:
154160
Expect().
155161
Status(200)
156162

157-
By("delete Consumer")
158-
err := s.DeleteResourceFromString(keyAuthConsumer)
159-
Expect(err).NotTo(HaveOccurred(), "deleting Consumer")
160-
time.Sleep(5 * time.Second)
161-
162163
s.NewAPISIXClient().
163164
GET("/get").
164165
WithHeader("apikey", "sample-key").
165166
WithHost("httpbin.org").
166167
Expect().
167-
Status(401)
168-
})
169-
170-
It("basic-auth", func() {
171-
s.ResourceApplied("Consumer", "consumer-sample", basicAuthConsumer, 1)
172-
173-
s.NewAPISIXClient().
174-
GET("/get").
175-
WithHost("httpbin.org").
176-
Expect().
177-
Status(401)
178-
179-
s.NewAPISIXClient().
180-
GET("/get").
181-
WithBasicAuth("sample-user", "sample-password").
182-
WithHost("httpbin.org").
183-
Expect().
184168
Status(200)
185169

186-
By("delete Consumer")
187-
err := s.DeleteResourceFromString(basicAuthConsumer)
188-
Expect(err).NotTo(HaveOccurred(), "deleting Consumer")
189-
time.Sleep(5 * time.Second)
190-
170+
By("trigger limit-count")
191171
s.NewAPISIXClient().
192172
GET("/get").
193-
WithBasicAuth("sample-user", "sample-password").
173+
WithHeader("apikey", "sample-key").
194174
WithHost("httpbin.org").
195175
Expect().
196-
Status(401)
176+
Status(503)
177+
178+
for i := 0; i < 10; i++ {
179+
s.NewAPISIXClient().
180+
GET("/get").
181+
WithHeader("apikey", "sample-key2").
182+
WithHost("httpbin.org").
183+
Expect().
184+
Status(200)
185+
}
197186
})
198187
})
199188

@@ -309,6 +298,87 @@ spec:
309298
})
310299
})
311300

312-
PContext("SecretRef", func() {
301+
Context("SecretRef", func() {
302+
var keyAuthSecret = `
303+
apiVersion: v1
304+
kind: Secret
305+
metadata:
306+
name: key-auth-secret
307+
data:
308+
key: c2FtcGxlLWtleQ==
309+
`
310+
var basicAuthSecret = `
311+
apiVersion: v1
312+
kind: Secret
313+
metadata:
314+
name: basic-auth-secret
315+
data:
316+
username: c2FtcGxlLXVzZXI=
317+
password: c2FtcGxlLXBhc3N3b3Jk
318+
`
319+
var defaultConsumer = `
320+
apiVersion: gateway.apisix.io/v1alpha1
321+
kind: Consumer
322+
metadata:
323+
name: consumer-sample
324+
spec:
325+
gatewayRef:
326+
name: api7ee
327+
credentials:
328+
- type: basic-auth
329+
name: basic-auth-sample
330+
secretRef:
331+
name: basic-auth-secret
332+
- type: key-auth
333+
name: key-auth-sample
334+
secretRef:
335+
name: key-auth-secret
336+
- type: key-auth
337+
name: key-auth-sample2
338+
config:
339+
key: sample-key2
340+
`
341+
BeforeEach(beforeEachHTTP)
342+
343+
It("Create/Update/Delete", func() {
344+
err := s.CreateResourceFromString(keyAuthSecret)
345+
Expect(err).NotTo(HaveOccurred(), "creating key-auth secret")
346+
err = s.CreateResourceFromString(basicAuthSecret)
347+
Expect(err).NotTo(HaveOccurred(), "creating basic-auth secret")
348+
s.ResourceApplied("Consumer", "consumer-sample", defaultConsumer, 1)
349+
350+
s.NewAPISIXClient().
351+
GET("/get").
352+
WithHeader("apikey", "sample-key").
353+
WithHost("httpbin.org").
354+
Expect().
355+
Status(200)
356+
357+
s.NewAPISIXClient().
358+
GET("/get").
359+
WithBasicAuth("sample-user", "sample-password").
360+
WithHost("httpbin.org").
361+
Expect().
362+
Status(200)
363+
364+
By("delete consumer")
365+
err = s.DeleteResourceFromString(defaultConsumer)
366+
Expect(err).NotTo(HaveOccurred(), "deleting consumer")
367+
time.Sleep(5 * time.Second)
368+
369+
s.NewAPISIXClient().
370+
GET("/get").
371+
WithHeader("apikey", "sample-key").
372+
WithHost("httpbin.org").
373+
Expect().
374+
Status(401)
375+
376+
s.NewAPISIXClient().
377+
GET("/get").
378+
WithBasicAuth("sample-user", "sample-password").
379+
WithHost("httpbin.org").
380+
Expect().
381+
Status(401)
382+
})
313383
})
314384
})

test/e2e/gatewayapi/httproute.go

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,11 +195,11 @@ spec:
195195

196196
BeforeEach(beforeEachHTTP)
197197

198-
It("Create/Updtea/Delete HTTPRoute", func() {
198+
It("Create/Update/Delete HTTPRoute", func() {
199199
By("create HTTPRoute")
200200
ResourceApplied("HTTPRoute", "httpbin", exactRouteByGet, 1)
201201

202-
By("access daataplane to check the HTTPRoute")
202+
By("access dataplane to check the HTTPRoute")
203203
s.NewAPISIXClient().
204204
GET("/get").
205205
Expect().
@@ -266,6 +266,29 @@ spec:
266266
- name: httpbin-service-e2e-test
267267
port: 80
268268
`
269+
var varsRoute = `
270+
apiVersion: gateway.networking.k8s.io/v1
271+
kind: HTTPRoute
272+
metadata:
273+
name: httpbin
274+
spec:
275+
parentRefs:
276+
- name: api7ee
277+
hostnames:
278+
- httpbin.example
279+
rules:
280+
- matches:
281+
- path:
282+
type: Exact
283+
value: /get
284+
headers:
285+
- type: Exact
286+
name: X-Route-Name
287+
value: httpbin
288+
backendRefs:
289+
- name: httpbin-service-e2e-test
290+
port: 80
291+
`
269292

270293
var prefixRouteByStatus = `
271294
apiVersion: gateway.networking.k8s.io/v1
@@ -372,6 +395,25 @@ spec:
372395
Expect().
373396
Status(404)
374397
})
398+
399+
It("HTTPRoute Vars Match", func() {
400+
By("create HTTPRoute")
401+
ResourceApplied("HTTPRoute", "httpbin", varsRoute, 1)
402+
403+
By("access dataplane to check the HTTPRoute")
404+
s.NewAPISIXClient().
405+
GET("/get").
406+
WithHost("httpbin.example").
407+
Expect().
408+
Status(http.StatusNotFound)
409+
410+
s.NewAPISIXClient().
411+
GET("/get").
412+
WithHost("httpbin.example").
413+
WithHeader("X-Route-Name", "httpbin").
414+
Expect().
415+
Status(http.StatusOK)
416+
})
375417
})
376418

377419
Context("HTTPRoute Filters", func() {

test/e2e/scaffold/scaffold.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,6 @@ func (s *Scaffold) GetDeploymentLogs(name string) string {
512512
Resource("pods").
513513
Namespace(s.namespace).
514514
Name(pod.Name).SubResource("log").
515-
Param("container", name).
516515
Do(context.TODO()).
517516
Raw()
518517
if err == nil {

0 commit comments

Comments
 (0)