@@ -4,12 +4,12 @@ import (
4
4
"context"
5
5
"errors"
6
6
"fmt"
7
+ "maps"
7
8
"strings"
8
9
9
10
corev1 "k8s.io/api/core/v1"
10
11
discoveryv1 "k8s.io/api/discovery/v1"
11
12
apiextv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
12
- kerrors "k8s.io/apimachinery/pkg/api/errors"
13
13
"k8s.io/apimachinery/pkg/api/meta"
14
14
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
15
15
"k8s.io/apimachinery/pkg/fields"
@@ -61,8 +61,8 @@ type GatewayConfig struct {
61
61
Aws * deployer.AwsInfo
62
62
63
63
Extensions extensions.K8sGatewayExtensions
64
- // CRDs defines the set of discovered Gateway API CRDs
65
- CRDs sets. Set [ string ]
64
+ // CRDs is a map of supported Gateway API CRDs with the discovered annotations for each.
65
+ CRDs * crds. CrdToAnnotation
66
66
}
67
67
68
68
func NewBaseGatewayController (ctx context.Context , cfg GatewayConfig ) error {
@@ -76,6 +76,7 @@ func NewBaseGatewayController(ctx context.Context, cfg GatewayConfig) error {
76
76
cli : cfg .Mgr .GetClient (),
77
77
scheme : cfg .Mgr .GetScheme (),
78
78
kick : cfg .Kick ,
79
+ crds : cfg .CRDs ,
79
80
},
80
81
}
81
82
@@ -135,9 +136,11 @@ func (c *controllerBuilder) addIndexes(ctx context.Context) error {
135
136
}
136
137
137
138
// Conditionally index for TCPRoute
138
- if c .cfg .CRDs .Has (wellknown .TCPRouteCRD ) {
139
- if err := c .cfg .Mgr .GetFieldIndexer ().IndexField (ctx , & apiv1a2.TCPRoute {}, query .TcpRouteTargetField , query .IndexerByObjType ); err != nil {
140
- errs = append (errs , err )
139
+ if c .cfg .CRDs != nil {
140
+ if _ , exists := (* c .cfg .CRDs )[crds .TCPRoute ]; exists {
141
+ if err := c .cfg .Mgr .GetFieldIndexer ().IndexField (ctx , & apiv1a2.TCPRoute {}, query .TcpRouteTargetField , query .IndexerByObjType ); err != nil {
142
+ errs = append (errs , err )
143
+ }
141
144
}
142
145
}
143
146
@@ -297,8 +300,8 @@ func (c *controllerBuilder) watchGwClass(_ context.Context) error {
297
300
Complete (reconcile .Func (c .reconciler .ReconcileGatewayClasses ))
298
301
}
299
302
300
- // watchCustomResourceDefinitions sets up a controller to watch for changes to specific Gateway API
301
- // CRDs and triggers GatewayClass reconciliation if generation or annotations change.
303
+ // watchCRDs sets up a controller to watch for changes to supported Gateway API CRDs
304
+ // and triggers GatewayClass reconciliation if generation or annotations change.
302
305
func (c * controllerBuilder ) watchCRDs (_ context.Context ) error {
303
306
return ctrl .NewControllerManagedBy (c .cfg .Mgr ).
304
307
For (& apiextv1.CustomResourceDefinition {}).
@@ -308,7 +311,20 @@ func (c *controllerBuilder) watchCRDs(_ context.Context) error {
308
311
return false
309
312
}
310
313
// Check if the CRD is one we care about
311
- return c .cfg .CRDs .Has (crd .Name )
314
+ if ! crds .IsSupported (crd .Name ) {
315
+ return false
316
+ }
317
+ // Maintain the set of supported CRDs
318
+ if c .cfg .CRDs != nil {
319
+ annotations , exist := (* c .cfg .CRDs )[crd .Name ]
320
+ if ! exist {
321
+ (* c .cfg .CRDs )[crd .Name ] = make (map [string ]string )
322
+ }
323
+ if ! maps .Equal (annotations , crd .Annotations ) {
324
+ (* c .cfg .CRDs )[crd .Name ] = crd .Annotations
325
+ }
326
+ }
327
+ return true
312
328
})).
313
329
WithEventFilter (predicate .Or (
314
330
predicate.AnnotationChangedPredicate {},
@@ -325,8 +341,13 @@ func (c *controllerBuilder) watchHttpRoute(_ context.Context) error {
325
341
}
326
342
327
343
func (c * controllerBuilder ) watchTcpRoute (ctx context.Context ) error {
328
- if ! c .cfg .CRDs .Has (wellknown .TCPRouteCRD ) {
329
- log .FromContext (ctx ).Info ("TCPRoute type not registered in scheme; skipping TCPRoute controller setup" )
344
+ log := log .FromContext (ctx )
345
+
346
+ if c .cfg .CRDs == nil {
347
+ log .Info ("CRD annotations map not initialized; skipping TCPRoute controller setup" )
348
+ }
349
+ if _ , exists := (* c .cfg .CRDs )[crds .TCPRoute ]; ! exists {
350
+ log .Info ("TCPRoute type not registered in scheme; skipping TCPRoute controller setup" )
330
351
return nil
331
352
}
332
353
@@ -424,6 +445,8 @@ type controllerReconciler struct {
424
445
cli client.Client
425
446
scheme * runtime.Scheme
426
447
kick func (ctx context.Context )
448
+ // crds is a map of supported Gateway API CRDs with the discovered annotations for each.
449
+ crds * crds.CrdToAnnotation
427
450
}
428
451
429
452
func (r * controllerReconciler ) ReconcileHttpListenerOptions (ctx context.Context , req ctrl.Request ) (ctrl.Result , error ) {
@@ -576,32 +599,25 @@ func (r *controllerReconciler) ReconcileGatewayClasses(ctx context.Context, req
576
599
log .Info ("Reconciling GatewayClass" )
577
600
578
601
// Initialize the status conditions. No need to set LastTransitionTime since it's handled by SetStatusCondition.
579
- msg := "Gateway API CRDs are a supported version"
580
602
acceptedCondition := metav1.Condition {
581
603
Type : string (apiv1 .GatewayClassConditionStatusAccepted ),
582
604
Status : metav1 .ConditionTrue ,
583
605
Reason : string (apiv1 .GatewayClassReasonAccepted ),
584
- Message : msg ,
606
+ Message : "All dependencies have been met" ,
585
607
ObservedGeneration : gwclass .Generation ,
586
608
}
587
609
supportedCondition := metav1.Condition {
588
610
Type : string (apiv1 .GatewayClassConditionStatusSupportedVersion ),
589
611
Status : metav1 .ConditionTrue ,
590
612
Reason : string (apiv1 .GatewayClassReasonSupportedVersion ),
591
- Message : msg ,
613
+ Message : "Gateway API CRDs are a supported version" ,
592
614
ObservedGeneration : gwclass .Generation ,
593
615
}
594
616
595
- // Check CRD versions
596
- supported , err := r .checkCRDVersions (ctx )
597
- if err != nil {
598
- log .Error (err , "Failed to check CRD versions" )
599
- return ctrl.Result {}, err
600
- }
601
-
602
- if ! supported {
617
+ // Set status conditions based on observed CRD versions
618
+ if ! r .crdsSupportedVersion (ctx ) {
603
619
// Update the values of status conditions
604
- msg = fmt .Sprintf ("Unsupported Gateway API CRDs detected. Supported versions are: %s" ,
620
+ msg : = fmt .Sprintf ("Unsupported Gateway API CRDs detected. Supported versions are: %s" ,
605
621
strings .Join (wellknown .SupportedVersions , ", " ))
606
622
acceptedCondition .Status = metav1 .ConditionFalse
607
623
acceptedCondition .Reason = string (apiv1 .GatewayClassReasonUnsupportedVersion )
@@ -625,22 +641,31 @@ func (r *controllerReconciler) ReconcileGatewayClasses(ctx context.Context, req
625
641
return ctrl.Result {}, nil
626
642
}
627
643
628
- // checkCRDVersions checks that the "gateway.networking.k8s.io/bundle-version" annotation key is set
629
- // to a supported version for each required Gateway API CRD.
630
- func (r * controllerReconciler ) checkCRDVersions (ctx context.Context ) (bool , error ) {
631
- for _ , crdName := range crds .Required {
632
- crd := & apiextv1.CustomResourceDefinition {}
633
- if err := r .cli .Get (ctx , client.ObjectKey {Name : crdName }, crd ); err != nil {
634
- if kerrors .IsNotFound (err ) {
635
- return false , nil
636
- }
637
- return false , err
644
+ // crdsSupportedVersion returns true if the "gateway.networking.k8s.io/bundle-version" annotation key is
645
+ // set to a supported version for each managed Gateway API CRD.
646
+ func (r * controllerReconciler ) crdsSupportedVersion (ctx context.Context ) bool {
647
+ log := log .FromContext (ctx )
648
+
649
+ if r .crds == nil {
650
+ return false
651
+ }
652
+
653
+ for crdName , annotations := range * r .crds {
654
+ if annotations == nil || len (annotations ) == 0 {
655
+ log .Info ("no annotations found for CRD" , "name" , crdName )
656
+ return false
638
657
}
639
658
640
- bundleVersion , exists := crd .Annotations [consts .BundleVersionAnnotation ]
641
- if ! exists || ! crds .IsSupportedVersion (bundleVersion ) {
642
- return false , nil
659
+ bundleVersion , exists := annotations [consts .BundleVersionAnnotation ]
660
+ if ! exists {
661
+ log .Info ("bundle version annotation does not exist for CRD" , "name" , crdName )
662
+ return false
663
+ }
664
+ if ! crds .IsSupportedVersion (bundleVersion ) {
665
+ log .Info ("bundle version annotation not a supported version for CRD" , "name" , crdName , "version" , bundleVersion )
666
+ return false
643
667
}
644
668
}
645
- return true , nil
669
+
670
+ return true
646
671
}
0 commit comments