Skip to content

Commit ae6afdb

Browse files
committed
fix: review
Signed-off-by: ashing <axingfly@gmail.com>
1 parent 6645993 commit ae6afdb

2 files changed

Lines changed: 53 additions & 4 deletions

File tree

internal/controller/apisixroute_controller.go

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -352,19 +352,45 @@ func (r *ApisixRouteReconciler) listApisixRoutesForSecret(ctx context.Context, o
352352
}
353353

354354
var (
355-
arList apiv2.ApisixRouteList
355+
arList apiv2.ApisixRouteList
356+
pcList apiv2.ApisixPluginConfigList
357+
allRequests = make([]reconcile.Request, 0)
356358
)
359+
360+
// First, find ApisixRoutes that directly reference this secret
357361
if err := r.List(ctx, &arList, client.MatchingFields{
358362
indexer.SecretIndexRef: indexer.GenIndexKey(secret.GetNamespace(), secret.GetName()),
359363
}); err != nil {
360364
r.Log.Error(err, "failed to list apisixroutes by secret", "secret", secret.Name)
361365
return nil
362366
}
363-
requests := make([]reconcile.Request, 0, len(arList.Items))
364367
for _, ar := range arList.Items {
365-
requests = append(requests, reconcile.Request{NamespacedName: utils.NamespacedName(&ar)})
368+
allRequests = append(allRequests, reconcile.Request{NamespacedName: utils.NamespacedName(&ar)})
366369
}
367-
return pkgutils.DedupComparable(requests)
370+
371+
// Second, find ApisixPluginConfigs that reference this secret
372+
if err := r.List(ctx, &pcList, client.MatchingFields{
373+
indexer.SecretIndexRef: indexer.GenIndexKey(secret.GetNamespace(), secret.GetName()),
374+
}); err != nil {
375+
r.Log.Error(err, "failed to list apisixpluginconfigs by secret", "secret", secret.Name)
376+
return nil
377+
}
378+
379+
// Then find ApisixRoutes that reference these PluginConfigs
380+
for _, pc := range pcList.Items {
381+
var arListForPC apiv2.ApisixRouteList
382+
if err := r.List(ctx, &arListForPC, client.MatchingFields{
383+
indexer.PluginConfigIndexRef: indexer.GenIndexKey(pc.GetNamespace(), pc.GetName()),
384+
}); err != nil {
385+
r.Log.Error(err, "failed to list apisixroutes by plugin config", "pluginconfig", pc.Name)
386+
continue
387+
}
388+
for _, ar := range arListForPC.Items {
389+
allRequests = append(allRequests, reconcile.Request{NamespacedName: utils.NamespacedName(&ar)})
390+
}
391+
}
392+
393+
return pkgutils.DedupComparable(allRequests)
368394
}
369395

370396
func (r *ApisixRouteReconciler) listApiRouteForIngressClass(ctx context.Context, object client.Object) (requests []reconcile.Request) {

internal/controller/indexer/indexer.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ func SetupIndexer(mgr ctrl.Manager) error {
5353
setupGatewaySecretIndex,
5454
setupGatewayClassIndexer,
5555
setupApisixRouteIndexer,
56+
setupApisixPluginConfigIndexer,
5657
} {
5758
if err := setup(mgr); err != nil {
5859
return err
@@ -108,6 +109,18 @@ func setupApisixRouteIndexer(mgr ctrl.Manager) error {
108109
return nil
109110
}
110111

112+
func setupApisixPluginConfigIndexer(mgr ctrl.Manager) error {
113+
if err := mgr.GetFieldIndexer().IndexField(
114+
context.Background(),
115+
&apiv2.ApisixPluginConfig{},
116+
SecretIndexRef,
117+
ApisixPluginConfigSecretIndexFunc,
118+
); err != nil {
119+
return err
120+
}
121+
return nil
122+
}
123+
111124
func ConsumerSecretIndexFunc(rawObj client.Object) []string {
112125
consumer := rawObj.(*v1alpha1.Consumer)
113126
secretKeys := make([]string, 0)
@@ -554,3 +567,13 @@ func IngressClassParametersRefIndexFunc(rawObj client.Object) []string {
554567
}
555568
return nil
556569
}
570+
571+
func ApisixPluginConfigSecretIndexFunc(obj client.Object) (keys []string) {
572+
pc := obj.(*apiv2.ApisixPluginConfig)
573+
for _, plugin := range pc.Spec.Plugins {
574+
if plugin.Enable && plugin.SecretRef != "" {
575+
keys = append(keys, GenIndexKey(pc.GetNamespace(), plugin.SecretRef))
576+
}
577+
}
578+
return
579+
}

0 commit comments

Comments
 (0)