Skip to content

Commit bf07565

Browse files
authored
feat: Add ReferenceGrant support for HTTPRoute (#149)
1 parent ecebace commit bf07565

File tree

9 files changed

+265
-220
lines changed

9 files changed

+265
-220
lines changed

api/v1alpha1/backendtrafficpolicy_types.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ type BackendTrafficPolicy struct {
2222
metav1.TypeMeta `json:",inline"`
2323
metav1.ObjectMeta `json:"metadata,omitempty"`
2424

25-
// BackendTrafficPolicySpec defines traffic handling policies applied to backend services,
26-
// such as load balancing strategy, connection settings, and failover behavior.
25+
// BackendTrafficPolicySpec defines traffic handling policies applied to backend services,
26+
// such as load balancing strategy, connection settings, and failover behavior.
2727
Spec BackendTrafficPolicySpec `json:"spec,omitempty"`
2828
Status PolicyStatus `json:"status,omitempty"`
2929
}

api/v1alpha1/consumer_types.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,19 @@ type Consumer struct {
2323
metav1.TypeMeta `json:",inline"`
2424
metav1.ObjectMeta `json:"metadata,omitempty"`
2525

26-
// ConsumerSpec defines the configuration for a consumer, including consumer name,
26+
// ConsumerSpec defines the configuration for a consumer, including consumer name,
2727
// authentication credentials, and plugin settings.
2828
Spec ConsumerSpec `json:"spec,omitempty"`
2929
Status Status `json:"status,omitempty"`
3030
}
3131

3232
type ConsumerSpec struct {
3333
// GatewayRef specifies the gateway details.
34-
GatewayRef GatewayRef `json:"gatewayRef,omitempty"`
34+
GatewayRef GatewayRef `json:"gatewayRef,omitempty"`
3535
// Credentials specifies the credential details of a consumer.
3636
Credentials []Credential `json:"credentials,omitempty"`
3737
// Plugins define the plugins associated with a consumer.
38-
Plugins []Plugin `json:"plugins,omitempty"`
38+
Plugins []Plugin `json:"plugins,omitempty"`
3939
}
4040

4141
type GatewayRef struct {
@@ -48,7 +48,7 @@ type GatewayRef struct {
4848
Kind *string `json:"kind,omitempty"`
4949
// Group is the API group the resource belongs to. Default is `gateway.networking.k8s.io`.
5050
// +kubebuilder:default=gateway.networking.k8s.io
51-
Group *string `json:"group,omitempty"`
51+
Group *string `json:"group,omitempty"`
5252
// Namespace is namespace of the resource.
5353
Namespace *string `json:"namespace,omitempty"`
5454
}
@@ -58,18 +58,18 @@ type Credential struct {
5858
// +kubebuilder:validation:Enum=jwt-auth;basic-auth;key-auth;hmac-auth;
5959
// Type specifies the type of authentication to configure credentials for.
6060
// Can be one of `jwt-auth`, `basic-auth`, `key-auth`, or `hmac-auth`.
61-
Type string `json:"type"`
61+
Type string `json:"type"`
6262
// Config specifies the credential details for authentication.
63-
Config apiextensionsv1.JSON `json:"config,omitempty"`
63+
Config apiextensionsv1.JSON `json:"config,omitempty"`
6464
// SecretRef references to the Secret that contains the credentials.
65-
SecretRef *SecretReference `json:"secretRef,omitempty"`
65+
SecretRef *SecretReference `json:"secretRef,omitempty"`
6666
// Name is the name of the credential.
67-
Name string `json:"name,omitempty"`
67+
Name string `json:"name,omitempty"`
6868
}
6969

7070
type SecretReference struct {
7171
// Name is the name of the secret.
72-
Name string `json:"name"`
72+
Name string `json:"name"`
7373
// Namespace is the namespace of the secret.
7474
Namespace *string `json:"namespace,omitempty"`
7575
}

api/v1alpha1/gatewayproxy_types.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ type GatewayProxySpec struct {
2727

2828
// PublishService specifies the LoadBalancer-type Service whose external address the controller uses to
2929
// update the status of Ingress resources.
30-
PublishService string `json:"publishService,omitempty"`
30+
PublishService string `json:"publishService,omitempty"`
3131
// StatusAddress specifies the external IP addresses that the controller uses to populate the status field
3232
// of GatewayProxy or Ingress resources for developers to access.
33-
StatusAddress []string `json:"statusAddress,omitempty"`
33+
StatusAddress []string `json:"statusAddress,omitempty"`
3434
// Provider configures the provider details.
35-
Provider *GatewayProxyProvider `json:"provider,omitempty"`
35+
Provider *GatewayProxyProvider `json:"provider,omitempty"`
3636
// Plugins configure global plugins.
37-
Plugins []GatewayProxyPlugin `json:"plugins,omitempty"`
37+
Plugins []GatewayProxyPlugin `json:"plugins,omitempty"`
3838
// PluginMetadata configures common configurations shared by all plugin instances of the same name.
3939
PluginMetadata map[string]apiextensionsv1.JSON `json:"pluginMetadata,omitempty"`
4040
}
@@ -132,8 +132,8 @@ type GatewayProxy struct {
132132
metav1.TypeMeta `json:",inline"`
133133
metav1.ObjectMeta `json:"metadata,omitempty"`
134134

135-
// GatewayProxySpec defines the desired state and configuration of a GatewayProxy,
136-
// including networking settings, global plugins, and plugin metadata.
135+
// GatewayProxySpec defines the desired state and configuration of a GatewayProxy,
136+
// including networking settings, global plugins, and plugin metadata.
137137
Spec GatewayProxySpec `json:"spec,omitempty"`
138138
}
139139

@@ -148,11 +148,11 @@ type GatewayProxyList struct {
148148
// GatewayProxyPlugin contains plugin configurations.
149149
type GatewayProxyPlugin struct {
150150
// Name is the name of the plugin.
151-
Name string `json:"name,omitempty"`
152-
// Enabled defines whether the plugin is enabled.
153-
Enabled bool `json:"enabled,omitempty"`
151+
Name string `json:"name,omitempty"`
152+
// Enabled defines whether the plugin is enabled.
153+
Enabled bool `json:"enabled,omitempty"`
154154
// Config defines the plugin's configuration details.
155-
Config apiextensionsv1.JSON `json:"config,omitempty"`
155+
Config apiextensionsv1.JSON `json:"config,omitempty"`
156156
}
157157

158158
func init() {

api/v1alpha1/httproutepolicy_types.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ type HTTPRoutePolicySpec struct {
2525
// +kubebuilder:validation:MaxItems=16
2626
TargetRefs []gatewayv1alpha2.LocalPolicyTargetReferenceWithSectionName `json:"targetRefs"`
2727
// Priority sets the priority for route. A higher value sets a higher priority in route matching.
28-
Priority *int64 `json:"priority,omitempty" yaml:"priority,omitempty"`
28+
Priority *int64 `json:"priority,omitempty" yaml:"priority,omitempty"`
2929
// Vars sets the request matching conditions.
30-
Vars []apiextensionsv1.JSON `json:"vars,omitempty" yaml:"vars,omitempty"`
30+
Vars []apiextensionsv1.JSON `json:"vars,omitempty" yaml:"vars,omitempty"`
3131
}
3232

3333
// +kubebuilder:object:root=true

internal/controller/gateway_controller.go

Lines changed: 18 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"fmt"
1919
"reflect"
2020

21+
"github.com/api7/gopkg/pkg/log"
2122
"github.com/go-logr/logr"
2223
corev1 "k8s.io/api/core/v1"
2324
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -26,15 +27,12 @@ import (
2627
ctrl "sigs.k8s.io/controller-runtime"
2728
"sigs.k8s.io/controller-runtime/pkg/builder"
2829
"sigs.k8s.io/controller-runtime/pkg/client"
29-
"sigs.k8s.io/controller-runtime/pkg/event"
3030
"sigs.k8s.io/controller-runtime/pkg/handler"
3131
"sigs.k8s.io/controller-runtime/pkg/predicate"
3232
"sigs.k8s.io/controller-runtime/pkg/reconcile"
3333
gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"
3434
"sigs.k8s.io/gateway-api/apis/v1beta1"
3535

36-
"github.com/api7/gopkg/pkg/log"
37-
3836
"github.com/apache/apisix-ingress-controller/api/v1alpha1"
3937
"github.com/apache/apisix-ingress-controller/internal/controller/indexer"
4038
"github.com/apache/apisix-ingress-controller/internal/provider"
@@ -51,7 +49,7 @@ type GatewayReconciler struct { //nolint:revive
5149

5250
// SetupWithManager sets up the controller with the Manager.
5351
func (r *GatewayReconciler) SetupWithManager(mgr ctrl.Manager) error {
54-
return ctrl.NewControllerManagedBy(mgr).
52+
bdr := ctrl.NewControllerManagedBy(mgr).
5553
For(
5654
&gatewayv1.Gateway{},
5755
builder.WithPredicates(
@@ -85,25 +83,16 @@ func (r *GatewayReconciler) SetupWithManager(mgr ctrl.Manager) error {
8583
Watches(
8684
&corev1.Secret{},
8785
handler.EnqueueRequestsFromMapFunc(r.listGatewaysForSecret),
88-
).
89-
Watches(&v1beta1.ReferenceGrant{},
86+
)
87+
88+
if GetEnableReferenceGrant() {
89+
bdr.Watches(&v1beta1.ReferenceGrant{},
9090
handler.EnqueueRequestsFromMapFunc(r.listReferenceGrantsForGateway),
91-
builder.WithPredicates(predicate.Funcs{
92-
CreateFunc: func(e event.CreateEvent) bool {
93-
return referenceGrantHasGatewayFrom(e.Object)
94-
},
95-
UpdateFunc: func(e event.UpdateEvent) bool {
96-
return referenceGrantHasGatewayFrom(e.ObjectOld) || referenceGrantHasGatewayFrom(e.ObjectNew)
97-
},
98-
DeleteFunc: func(e event.DeleteEvent) bool {
99-
return referenceGrantHasGatewayFrom(e.Object)
100-
},
101-
GenericFunc: func(e event.GenericEvent) bool {
102-
return referenceGrantHasGatewayFrom(e.Object)
103-
},
104-
}),
105-
).
106-
Complete(r)
91+
builder.WithPredicates(referenceGrantPredicates(KindGateway)),
92+
)
93+
}
94+
95+
return bdr.Complete(r)
10796
}
10897

10998
func (r *GatewayReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
@@ -184,14 +173,9 @@ func (r *GatewayReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
184173
}
185174
}
186175

187-
var referenceGrantList v1beta1.ReferenceGrantList
188-
if err := r.List(ctx, &referenceGrantList); err != nil {
189-
r.Log.Error(err, "failed to list reference grants")
190-
return ctrl.Result{}, err
191-
}
192-
listenerStatuses, err := getListenerStatus(ctx, r.Client, gateway, referenceGrantList.Items)
176+
listenerStatuses, err := getListenerStatus(ctx, r.Client, gateway)
193177
if err != nil {
194-
r.Log.Error(err, "failed to get listener status", "gateway", types.NamespacedName{Namespace: gateway.GetNamespace(), Name: gateway.GetName()})
178+
r.Log.Error(err, "failed to get listener status", "gateway", req.NamespacedName)
195179
return ctrl.Result{}, err
196180
}
197181

@@ -391,12 +375,12 @@ func (r *GatewayReconciler) listReferenceGrantsForGateway(ctx context.Context, o
391375
}
392376

393377
for _, gateway := range gatewayList.Items {
378+
gw := v1beta1.ReferenceGrantFrom{
379+
Group: gatewayv1.GroupName,
380+
Kind: KindGateway,
381+
Namespace: v1beta1.Namespace(gateway.GetNamespace()),
382+
}
394383
for _, from := range grant.Spec.From {
395-
gw := v1beta1.ReferenceGrantFrom{
396-
Group: gatewayv1.GroupName,
397-
Kind: KindGateway,
398-
Namespace: v1beta1.Namespace(gateway.GetNamespace()),
399-
}
400384
if from == gw {
401385
requests = append(requests, reconcile.Request{
402386
NamespacedName: types.NamespacedName{
@@ -410,19 +394,6 @@ func (r *GatewayReconciler) listReferenceGrantsForGateway(ctx context.Context, o
410394
return requests
411395
}
412396

413-
func referenceGrantHasGatewayFrom(obj client.Object) bool {
414-
grant, ok := obj.(*v1beta1.ReferenceGrant)
415-
if !ok {
416-
return false
417-
}
418-
for _, from := range grant.Spec.From {
419-
if from.Kind == KindGateway && string(from.Group) == gatewayv1.GroupName {
420-
return true
421-
}
422-
}
423-
return false
424-
}
425-
426397
func (r *GatewayReconciler) processInfrastructure(tctx *provider.TranslateContext, gateway *gatewayv1.Gateway) error {
427398
rk := provider.ResourceKind{
428399
Kind: gateway.Kind,

0 commit comments

Comments
 (0)