@@ -10,7 +10,6 @@ import (
1010 "sigs.k8s.io/controller-runtime/pkg/cache"
1111 "sigs.k8s.io/controller-runtime/pkg/client"
1212 "sigs.k8s.io/controller-runtime/pkg/controller"
13- "sigs.k8s.io/controller-runtime/pkg/event"
1413 "sigs.k8s.io/controller-runtime/pkg/handler"
1514 "sigs.k8s.io/controller-runtime/pkg/manager"
1615 "sigs.k8s.io/controller-runtime/pkg/predicate"
3029)
3130
3231// New function initializes the controller and sets up the watch for the ConfigMap
33- func New (mgr manager.Manager , config Config ) (controller.Controller , error ) {
34- operatorCache := mgr .GetCache ()
32+ func New (mgr manager.Manager ) (controller.Controller , error ) {
3533 reconciler := & reconciler {
36- cache : config .Cache ,
37- client : mgr .GetClient (),
34+ cache : mgr .GetCache (), // Directly using mgr.GetCache()
3835 }
3936
4037 //Create a new controller with given reconciler
@@ -46,50 +43,33 @@ func New(mgr manager.Manager, config Config) (controller.Controller, error) {
4643 }
4744
4845 // Define a predicate filter to watch for specific changes in the ConfigMap
49- adminGateFilter := predicate.Funcs {
50- CreateFunc : func (e event.CreateEvent ) bool {
51- // Watch for creation of the "admin-gates" ConfigMap in "openshift-config-managed" namespace
52- configmap := e .Object .(* corev1.ConfigMap )
53- return configmap .Name == operatorcontroller .AdminGatesConfigMapName ().Name && configmap .Namespace == operatorcontroller .AdminGatesConfigMapName ().Namespace
54- },
55- DeleteFunc : func (deleteEvent event.DeleteEvent ) bool { return false }, // Ignore delete events
56- UpdateFunc : func (e event.UpdateEvent ) bool {
57- // Watch for updates to the "admin-gates" ConfigMap to check if the admin key changes
58- configmapOld := e .ObjectOld .(* corev1.ConfigMap )
59- configmapNew := e .ObjectNew .(* corev1.ConfigMap )
60- if configmapOld .Name != operatorcontroller .AdminGatesConfigMapName ().Name || configmapOld .Namespace != operatorcontroller .AdminGatesConfigMapName ().Namespace {
61- return false
62- }
63- oldVal , oldExists := configmapOld .Data [GatewayAPIAdminKey ]
64- newVal , newExists := configmapNew .Data [GatewayAPIAdminKey ]
65- return oldExists != newExists || oldVal != newVal
66- },
67- GenericFunc : func (genericEvent event.GenericEvent ) bool { return false }, // Ignore generic events
68- }
46+ // Check that ConfigMap's name and namespace match expected values
47+ adminGatePredicate := predicate .NewPredicateFuncs (func (o client.Object ) bool {
48+ cm := o .(* corev1.ConfigMap )
49+ return cm .Name == operatorcontroller .AdminGatesConfigMapName ().Name && cm .Namespace == operatorcontroller .AdminGatesConfigMapName ().Namespace
50+ })
6951
52+ //Map events to the admin gate ConfigMap
7053 toAdminGateConfigMap := func (_ context.Context , _ client.Object ) []reconcile.Request {
7154 return []reconcile.Request {{
7255 NamespacedName : operatorcontroller .AdminGatesConfigMapName (),
7356 }}
7457 }
58+
7559 // Define the CRD predicate
7660 crdPredicate := predicate .NewPredicateFuncs (func (o client.Object ) bool {
7761 return o .(* apiextensionsv1.CustomResourceDefinition ).Spec .Group == gatewayapiv1 .GroupName
7862 })
7963
80- //TODO: too many args to c.watch and not enough to source.Kind
81- if err := c .Watch (source .Kind (operatorCache , & corev1.ConfigMap {}),
82- handler .EnqueueRequestsFromMapFunc (toAdminGateConfigMap ),
83- adminGateFilter ); err != nil {
64+ // Set up a single watch for CRD events and map to admin gate ConfigMap
65+ if err := c .Watch (source .Kind [client.Object ](mgr .GetCache (), & apiextensionsv1.CustomResourceDefinition {}, handler .EnqueueRequestsFromMapFunc (toAdminGateConfigMap ), crdPredicate )); err != nil {
8466 return nil , err
8567 }
8668
87- // Watch for CRD updates using the reconciler cache and apply the CRD predicate
88- if err := c .Watch (source .Kind (reconciler .cache , & apiextensionsv1.CustomResourceDefinition {}),
89- & handler.EnqueueRequestForObject {},
90- crdPredicate ); err != nil {
69+ if err := c .Watch (source .Kind [client.Object ](mgr .GetCache (), & corev1.ConfigMap {}, & handler.EnqueueRequestForObject {}, adminGatePredicate )); err != nil {
9170 return nil , err
9271 }
72+
9373 return c , nil
9474}
9575
@@ -99,21 +79,16 @@ type reconciler struct {
9979 cache cache.Cache
10080}
10181
102- // Config struct holds the cache information
103- type Config struct {
104- //namespace referes to namespace where admin gates controller resides
105- Namespace string
106- //cache should be a namespace global cache
107- Cache cache.Cache
108- }
109-
11082// Reconcile function implements the logic to check conditions and manage the admin gate
11183func (r * reconciler ) Reconcile (ctx context.Context , request reconcile.Request ) (reconcile.Result , error ) {
11284 log .Info ("reconciling" , "request" , request )
85+
86+ // Check if the admin gate should be added or removed
11387 adminGateConditionExists , err := r .adminGateConditionExists (ctx )
11488 if err != nil {
115- return reconcile.Result {}, fmt .Errorf ("failed to determine if UnservableInFutureVersionsRoute routes exist: %w" , err )
89+ return reconcile.Result {}, fmt .Errorf ("failed to determine if admin gate exist: %w" , err )
11690 }
91+
11792 if adminGateConditionExists {
11893 if err := r .addAdminGate (ctx ); err != nil {
11994 return reconcile.Result {}, fmt .Errorf ("failed to add admin gate: %w" , err )
@@ -127,15 +102,31 @@ func (r *reconciler) Reconcile(ctx context.Context, request reconcile.Request) (
127102 return reconcile.Result {}, nil
128103}
129104
130- // adminGateConditionExists is a placeholder for now,
131- // TODO: adminGatesConditionExists needs to be implemented....
132- func (r * reconciler ) adminGateConditionExists (ctx context.Context , condition string ) (bool , error ) {
105+ // adminGateConditionExists checks if the admin gate condition exists based on both ConfigMap and CRDs
106+ func (r * reconciler ) adminGateConditionExists (ctx context.Context ) (bool , error ) {
107+ adminGateConfigMap := & corev1.ConfigMap {}
108+ if err := r .cache .Get (ctx , operatorcontroller .AdminGatesConfigMapName (), adminGateConfigMap ); err != nil {
109+ return false , fmt .Errorf ("failed to get configmap %s: %w" , operatorcontroller .AdminGatesConfigMapName (), err )
110+ }
111+
112+ // Check if the admin key exists and is set to the expected message
113+ if val , ok := adminGateConfigMap .Data [GatewayAPIAdminKey ]; ok && val == GatewayAPIAdminMsg {
114+ return true , nil // Exists as expected
115+ }
133116
134- // Placeholder: Attempt to list ConfigMaps
135- // Placeholder: Iterate through ConfigMap list and check for the condition (to be implemented).
117+ // Check for the presence of Gateway API CRDs
118+ crds := & apiextensionsv1.CustomResourceDefinitionList {}
119+ if err := r .client .List (ctx , crds ); err != nil {
120+ return false , fmt .Errorf ("failed to list CRDs: %w" , err )
121+ }
122+
123+ for _ , crd := range crds .Items {
124+ if crd .Spec .Group == gatewayapiv1 .GroupName {
125+ return true , nil
126+ }
127+ }
136128
137- // Placeholder return statement, to be replaced with actual logic.
138- return false , nil
129+ return false , nil // No Gateway API CRDs found
139130}
140131
141132// addAdminGate function adds the admin gate to the ConfigMap
0 commit comments