Skip to content

Commit 65cda84

Browse files
committed
fix: gateway proxy
Signed-off-by: Ashing Zheng <[email protected]>
1 parent 77b3734 commit 65cda84

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed

internal/controller/gatewayproxy_controller.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,13 @@ func (r *GatewayProxyController) SetupWithManager(mrg ctrl.Manager) error {
6262
// Check and store EndpointSlice API support
6363
r.supportsEndpointSlice = pkgutils.HasAPIResource(mrg, &discoveryv1.EndpointSlice{})
6464
r.supportsGateway = pkgutils.HasAPIResource(mrg, &gatewayv1.Gateway{})
65+
var icWatch client.Object
66+
switch r.ICGV.String() {
67+
case networkingv1beta1.SchemeGroupVersion.String():
68+
icWatch = &networkingv1beta1.IngressClass{}
69+
default:
70+
icWatch = &networkingv1.IngressClass{}
71+
}
6572

6673
eventFilters := []predicate.Predicate{
6774
predicate.GenerationChangedPredicate{},
@@ -77,6 +84,10 @@ func (r *GatewayProxyController) SetupWithManager(mrg ctrl.Manager) error {
7784
WithEventFilter(predicate.Or(eventFilters...)).
7885
Watches(&corev1.Service{},
7986
handler.EnqueueRequestsFromMapFunc(r.listGatewayProxiesForProviderService),
87+
).
88+
Watches(
89+
icWatch,
90+
handler.EnqueueRequestsFromMapFunc(r.listGatewayProxiesForIngressClass),
8091
)
8192

8293
// Conditionally watch EndpointSlice or Endpoints based on cluster API support
@@ -85,6 +96,12 @@ func (r *GatewayProxyController) SetupWithManager(mrg ctrl.Manager) error {
8596
r.listGatewayProxiesForProviderEndpoints,
8697
r.Log)
8798

99+
if r.supportsGateway {
100+
bdr = bdr.Watches(&gatewayv1.Gateway{},
101+
handler.EnqueueRequestsFromMapFunc(r.listGatewayProxiesByGateway),
102+
)
103+
}
104+
88105
return bdr.
89106
Watches(&corev1.Secret{},
90107
handler.EnqueueRequestsFromMapFunc(r.listGatewayProxiesForSecret),
@@ -265,3 +282,32 @@ func (r *GatewayProxyController) listGatewayProxiesForSecret(ctx context.Context
265282
indexer.SecretIndexRef: indexer.GenIndexKey(secret.GetNamespace(), secret.GetName()),
266283
})
267284
}
285+
286+
func (r *GatewayProxyController) listGatewayProxiesForIngressClass(ctx context.Context, object client.Object) []reconcile.Request {
287+
ingressClass := pkgutils.ConvertToIngressClassV1(object)
288+
289+
reqs := []reconcile.Request{}
290+
gp, _ := GetGatewayProxyByIngressClass(ctx, r.Client, ingressClass)
291+
if gp != nil {
292+
reqs = append(reqs, reconcile.Request{
293+
NamespacedName: utils.NamespacedName(gp),
294+
})
295+
}
296+
return reqs
297+
}
298+
299+
func (r *GatewayProxyController) listGatewayProxiesByGateway(ctx context.Context, object client.Object) []reconcile.Request {
300+
gateway, ok := object.(*gatewayv1.Gateway)
301+
if !ok {
302+
r.Log.Error(errors.New("unexpected object type"), "failed to convert object to IngressClass")
303+
return nil
304+
}
305+
reqs := []reconcile.Request{}
306+
gp, _ := GetGatewayProxyByGateway(ctx, r.Client, gateway)
307+
if gp != nil {
308+
reqs = append(reqs, reconcile.Request{
309+
NamespacedName: utils.NamespacedName(gp),
310+
})
311+
}
312+
return reqs
313+
}

internal/controller/utils.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1679,3 +1679,53 @@ func ExtractIngressClass(obj client.Object) string {
16791679
panic(fmt.Errorf("unhandled object type %T for extracting ingress class", obj))
16801680
}
16811681
}
1682+
1683+
func GetGatewayProxyByIngressClass(ctx context.Context, r client.Client, ingressClass *networkingv1.IngressClass) (*v1alpha1.GatewayProxy, error) {
1684+
if ingressClass.Spec.Parameters == nil {
1685+
return nil, nil
1686+
}
1687+
1688+
if ingressClass.Spec.Parameters.APIGroup == nil ||
1689+
*ingressClass.Spec.Parameters.APIGroup != v1alpha1.GroupVersion.Group ||
1690+
ingressClass.Spec.Parameters.Kind != KindGatewayProxy {
1691+
return nil, nil
1692+
}
1693+
1694+
namespace := ingressClass.Namespace
1695+
if ingressClass.Spec.Parameters.Namespace != nil {
1696+
namespace = *ingressClass.Spec.Parameters.Namespace
1697+
}
1698+
1699+
gatewayProxy := new(v1alpha1.GatewayProxy)
1700+
if err := r.Get(ctx, client.ObjectKey{
1701+
Namespace: namespace,
1702+
Name: ingressClass.Spec.Parameters.Name,
1703+
}, gatewayProxy); err != nil {
1704+
return nil, fmt.Errorf("failed to get gateway proxy: %w", err)
1705+
}
1706+
return gatewayProxy, nil
1707+
}
1708+
1709+
func GetGatewayProxyByGateway(ctx context.Context, r client.Client, gateway *gatewayv1.Gateway) (*v1alpha1.GatewayProxy, error) {
1710+
if gateway == nil {
1711+
return nil, nil
1712+
}
1713+
infra := gateway.Spec.Infrastructure
1714+
if infra == nil || infra.ParametersRef == nil {
1715+
return nil, nil
1716+
}
1717+
1718+
ns := gateway.GetNamespace()
1719+
paramRef := infra.ParametersRef
1720+
if string(paramRef.Group) != v1alpha1.GroupVersion.Group || string(paramRef.Kind) != KindGatewayProxy {
1721+
return nil, nil
1722+
}
1723+
gatewayProxy := &v1alpha1.GatewayProxy{}
1724+
if err := r.Get(context.Background(), client.ObjectKey{
1725+
Namespace: ns,
1726+
Name: paramRef.Name,
1727+
}, gatewayProxy); err != nil {
1728+
return nil, fmt.Errorf("failed to get GatewayProxy: %w", err)
1729+
}
1730+
return gatewayProxy, nil
1731+
}

0 commit comments

Comments
 (0)