Skip to content

Commit 9ba875c

Browse files
committed
fix: review
Signed-off-by: Ashing Zheng <[email protected]>
1 parent d71c521 commit 9ba875c

File tree

6 files changed

+70
-71
lines changed

6 files changed

+70
-71
lines changed

internal/controller/apisixroute_controller.go

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -69,16 +69,19 @@ func (r *ApisixRouteReconciler) SetupWithManager(mgr ctrl.Manager) error {
6969
// Check and store EndpointSlice API support
7070
r.supportsEndpointSlice = pkgutils.HasAPIResource(mgr, &discoveryv1.EndpointSlice{})
7171

72+
eventFilters := []predicate.Predicate{
73+
predicate.GenerationChangedPredicate{},
74+
predicate.AnnotationChangedPredicate{},
75+
predicate.NewPredicateFuncs(TypePredicate[*corev1.Secret]()),
76+
}
77+
78+
if !r.supportsEndpointSlice {
79+
eventFilters = append(eventFilters, predicate.NewPredicateFuncs(TypePredicate[*corev1.Endpoints]()))
80+
}
81+
7282
bdr := ctrl.NewControllerManagedBy(mgr).
7383
For(&apiv2.ApisixRoute{}).
74-
WithEventFilter(
75-
predicate.Or(
76-
predicate.GenerationChangedPredicate{},
77-
predicate.AnnotationChangedPredicate{},
78-
predicate.NewPredicateFuncs(TypePredicate[*corev1.Endpoints]()),
79-
predicate.NewPredicateFuncs(TypePredicate[*corev1.Secret]()),
80-
),
81-
).
84+
WithEventFilter(predicate.Or(eventFilters...)).
8285
Watches(
8386
&networkingv1.IngressClass{},
8487
handler.EnqueueRequestsFromMapFunc(r.listApisixRouteForIngressClass),
@@ -91,16 +94,10 @@ func (r *ApisixRouteReconciler) SetupWithManager(mgr ctrl.Manager) error {
9194
)
9295

9396
// Conditionally watch EndpointSlice or Endpoints based on cluster API support
94-
if r.supportsEndpointSlice {
95-
bdr = bdr.Watches(&discoveryv1.EndpointSlice{},
96-
handler.EnqueueRequestsFromMapFunc(r.listApisixRoutesForService),
97-
)
98-
} else {
99-
r.Log.Info("EndpointSlice API not available, falling back to Endpoints API for service discovery")
100-
bdr = bdr.Watches(&corev1.Endpoints{},
101-
handler.EnqueueRequestsFromMapFunc(r.listApisixRoutesForEndpoints),
102-
)
103-
}
97+
bdr = watchEndpointSliceOrEndpoints(bdr, r.supportsEndpointSlice,
98+
r.listApisixRoutesForService,
99+
r.listApisixRoutesForEndpoints,
100+
r.Log)
104101

105102
return bdr.
106103
Watches(&corev1.Secret{},

internal/controller/gatewayproxy_controller.go

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -57,30 +57,27 @@ func (r *GatewayProxyController) SetupWithManager(mrg ctrl.Manager) error {
5757
// Check and store EndpointSlice API support
5858
r.supportsEndpointSlice = pkgutils.HasAPIResource(mrg, &discoveryv1.EndpointSlice{})
5959

60+
eventFilters := []predicate.Predicate{
61+
predicate.GenerationChangedPredicate{},
62+
predicate.NewPredicateFuncs(TypePredicate[*corev1.Secret]()),
63+
}
64+
65+
if !r.supportsEndpointSlice {
66+
eventFilters = append(eventFilters, predicate.NewPredicateFuncs(TypePredicate[*corev1.Endpoints]()))
67+
}
68+
6069
bdr := ctrl.NewControllerManagedBy(mrg).
6170
For(&v1alpha1.GatewayProxy{}).
62-
WithEventFilter(
63-
predicate.Or(
64-
predicate.GenerationChangedPredicate{},
65-
predicate.NewPredicateFuncs(TypePredicate[*corev1.Endpoints]()),
66-
predicate.NewPredicateFuncs(TypePredicate[*corev1.Secret]()),
67-
),
68-
).
71+
WithEventFilter(predicate.Or(eventFilters...)).
6972
Watches(&corev1.Service{},
7073
handler.EnqueueRequestsFromMapFunc(r.listGatewayProxiesForProviderService),
7174
)
7275

7376
// Conditionally watch EndpointSlice or Endpoints based on cluster API support
74-
if r.supportsEndpointSlice {
75-
bdr = bdr.Watches(&discoveryv1.EndpointSlice{},
76-
handler.EnqueueRequestsFromMapFunc(r.listGatewayProxiesForProviderEndpointSlice),
77-
)
78-
} else {
79-
r.Log.Info("EndpointSlice API not available, falling back to Endpoints API for provider service discovery")
80-
bdr = bdr.Watches(&corev1.Endpoints{},
81-
handler.EnqueueRequestsFromMapFunc(r.listGatewayProxiesForProviderEndpoints),
82-
)
83-
}
77+
bdr = watchEndpointSliceOrEndpoints(bdr, r.supportsEndpointSlice,
78+
r.listGatewayProxiesForProviderEndpointSlice,
79+
r.listGatewayProxiesForProviderEndpoints,
80+
r.Log)
8481

8582
return bdr.
8683
Watches(&corev1.Secret{},

internal/controller/httproute_controller.go

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -80,25 +80,23 @@ func (r *HTTPRouteReconciler) SetupWithManager(mgr ctrl.Manager) error {
8080
// Check and store EndpointSlice API support
8181
r.supportsEndpointSlice = pkgutils.HasAPIResource(mgr, &discoveryv1.EndpointSlice{})
8282

83+
eventFilters := []predicate.Predicate{
84+
predicate.GenerationChangedPredicate{},
85+
}
86+
87+
if !r.supportsEndpointSlice {
88+
eventFilters = append(eventFilters, predicate.NewPredicateFuncs(TypePredicate[*corev1.Endpoints]()))
89+
}
90+
8391
bdr := ctrl.NewControllerManagedBy(mgr).
8492
For(&gatewayv1.HTTPRoute{}).
85-
WithEventFilter(
86-
predicate.Or(
87-
predicate.GenerationChangedPredicate{},
88-
predicate.NewPredicateFuncs(TypePredicate[*corev1.Endpoints]()),
89-
))
93+
WithEventFilter(predicate.Or(eventFilters...))
9094

9195
// Conditionally watch EndpointSlice or Endpoints based on cluster API support
92-
if r.supportsEndpointSlice {
93-
bdr = bdr.Watches(&discoveryv1.EndpointSlice{},
94-
handler.EnqueueRequestsFromMapFunc(r.listHTTPRoutesByServiceBef),
95-
)
96-
} else {
97-
r.Log.Info("EndpointSlice API not available, falling back to Endpoints API for service discovery")
98-
bdr = bdr.Watches(&corev1.Endpoints{},
99-
handler.EnqueueRequestsFromMapFunc(r.listHTTPRoutesByServiceForEndpoints),
100-
)
101-
}
96+
bdr = watchEndpointSliceOrEndpoints(bdr, r.supportsEndpointSlice,
97+
r.listHTTPRoutesByServiceBef,
98+
r.listHTTPRoutesByServiceForEndpoints,
99+
r.Log)
102100

103101
bdr = bdr.
104102
Watches(&v1alpha1.PluginConfig{},

internal/controller/ingress_controller.go

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -74,20 +74,23 @@ func (r *IngressReconciler) SetupWithManager(mgr ctrl.Manager) error {
7474
// Check and store EndpointSlice API support
7575
r.supportsEndpointSlice = pkgutils.HasAPIResource(mgr, &discoveryv1.EndpointSlice{})
7676

77+
eventFilters := []predicate.Predicate{
78+
predicate.GenerationChangedPredicate{},
79+
predicate.AnnotationChangedPredicate{},
80+
predicate.NewPredicateFuncs(TypePredicate[*corev1.Secret]()),
81+
}
82+
83+
if !r.supportsEndpointSlice {
84+
eventFilters = append(eventFilters, predicate.NewPredicateFuncs(TypePredicate[*corev1.Endpoints]()))
85+
}
86+
7787
bdr := ctrl.NewControllerManagedBy(mgr).
7888
For(&networkingv1.Ingress{},
7989
builder.WithPredicates(
8090
predicate.NewPredicateFuncs(r.checkIngressClass),
8191
),
8292
).
83-
WithEventFilter(
84-
predicate.Or(
85-
predicate.GenerationChangedPredicate{},
86-
predicate.AnnotationChangedPredicate{},
87-
predicate.NewPredicateFuncs(TypePredicate[*corev1.Endpoints]()),
88-
predicate.NewPredicateFuncs(TypePredicate[*corev1.Secret]()),
89-
),
90-
).
93+
WithEventFilter(predicate.Or(eventFilters...)).
9194
Watches(
9295
&networkingv1.IngressClass{},
9396
handler.EnqueueRequestsFromMapFunc(r.listIngressForIngressClass),
@@ -97,18 +100,10 @@ func (r *IngressReconciler) SetupWithManager(mgr ctrl.Manager) error {
97100
)
98101

99102
// Conditionally watch EndpointSlice or Endpoints based on cluster API support
100-
if r.supportsEndpointSlice {
101-
bdr = bdr.Watches(
102-
&discoveryv1.EndpointSlice{},
103-
handler.EnqueueRequestsFromMapFunc(r.listIngressesByService),
104-
)
105-
} else {
106-
r.Log.Info("EndpointSlice API not available, falling back to Endpoints API for service discovery")
107-
bdr = bdr.Watches(
108-
&corev1.Endpoints{},
109-
handler.EnqueueRequestsFromMapFunc(r.listIngressesByEndpoints),
110-
)
111-
}
103+
bdr = watchEndpointSliceOrEndpoints(bdr, r.supportsEndpointSlice,
104+
r.listIngressesByService,
105+
r.listIngressesByEndpoints,
106+
r.Log)
112107

113108
return bdr.
114109
Watches(

internal/controller/utils.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,10 @@ import (
4040
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
4141
"k8s.io/apimachinery/pkg/labels"
4242
k8stypes "k8s.io/apimachinery/pkg/types"
43+
ctrl "sigs.k8s.io/controller-runtime"
4344
"sigs.k8s.io/controller-runtime/pkg/client"
4445
"sigs.k8s.io/controller-runtime/pkg/event"
46+
"sigs.k8s.io/controller-runtime/pkg/handler"
4547
"sigs.k8s.io/controller-runtime/pkg/predicate"
4648
"sigs.k8s.io/controller-runtime/pkg/reconcile"
4749
gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"
@@ -1506,3 +1508,13 @@ func MatchConsumerGatewayRef(ctx context.Context, c client.Client, log logr.Logg
15061508
}
15071509
return matchesController(string(gatewayClass.Spec.ControllerName))
15081510
}
1511+
1512+
// watchEndpointSliceOrEndpoints adds watcher for EndpointSlice or Endpoints based on cluster API support
1513+
func watchEndpointSliceOrEndpoints(bdr *ctrl.Builder, supportsEndpointSlice bool, endpointSliceMapFunc, endpointsMapFunc handler.MapFunc, log logr.Logger) *ctrl.Builder {
1514+
if supportsEndpointSlice {
1515+
return bdr.Watches(&discoveryv1.EndpointSlice{}, handler.EnqueueRequestsFromMapFunc(endpointSliceMapFunc))
1516+
} else {
1517+
log.Info("EndpointSlice API not available, falling back to Endpoints API for service discovery")
1518+
return bdr.Watches(&corev1.Endpoints{}, handler.EnqueueRequestsFromMapFunc(endpointsMapFunc))
1519+
}
1520+
}

pkg/utils/endpoints.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func ConvertEndpointsToEndpointSlice(ep *corev1.Endpoints) []discoveryv1.Endpoin
6767

6868
for i, subset := range ep.Subsets {
6969
// Create ports array
70-
var ports []discoveryv1.EndpointPort
70+
ports := make([]discoveryv1.EndpointPort, 0, len(subset.Ports))
7171
for _, p := range subset.Ports {
7272
epPort := discoveryv1.EndpointPort{
7373
Port: &p.Port,

0 commit comments

Comments
 (0)