Skip to content

Commit e339529

Browse files
author
Ali Syed
committed
Addressed the review comments. Included tidying up the code up to
standard and refactoring some fucntions.
1 parent a3a6ff0 commit e339529

File tree

4 files changed

+44
-58
lines changed

4 files changed

+44
-58
lines changed

pkg/operator/controller/gatewayapi-upgradeable/controller.go

Lines changed: 40 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -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"
@@ -30,11 +29,9 @@ var (
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
11183
func (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

pkg/operator/controller/gatewayapi/controller.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,6 @@ type Config struct {
8383
// resources. The gatewayapi controller starts these controllers once
8484
// the Gateway API CRDs have been created.
8585
DependentControllers []controller.Controller
86-
87-
//AdminConsentRequired indicates
88-
AdminConsentRequired bool
8986
}
9087

9188
// reconciler reconciles gatewayclasses.

pkg/operator/controller/names.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ const (
5252
func AdminGatesConfigMapName() types.NamespacedName {
5353
return types.NamespacedName{
5454
Name: "admin-gates",
55-
Namespace: "openshift-config",
55+
Namespace: GlobalMachineSpecifiedConfigNamespace,
5656
}
5757
}
5858

pkg/operator/operator.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package operator
33
import (
44
"context"
55
"fmt"
6-
gatewayapi_upgradeable "github.com/openshift/cluster-ingress-operator/pkg/operator/controller/gatewayapi-upgradeable"
76
"time"
87

98
configclient "github.com/openshift/client-go/config/clientset/versioned"
@@ -36,6 +35,7 @@ import (
3635
dnscontroller "github.com/openshift/cluster-ingress-operator/pkg/operator/controller/dns"
3736
gatewayservicednscontroller "github.com/openshift/cluster-ingress-operator/pkg/operator/controller/gateway-service-dns"
3837
gatewayapicontroller "github.com/openshift/cluster-ingress-operator/pkg/operator/controller/gatewayapi"
38+
gatewayapi_upgradeable "github.com/openshift/cluster-ingress-operator/pkg/operator/controller/gatewayapi-upgradeable"
3939
gatewayclasscontroller "github.com/openshift/cluster-ingress-operator/pkg/operator/controller/gatewayclass"
4040
ingress "github.com/openshift/cluster-ingress-operator/pkg/operator/controller/ingress"
4141
ingresscontroller "github.com/openshift/cluster-ingress-operator/pkg/operator/controller/ingress"
@@ -320,10 +320,8 @@ func New(config operatorconfig.Config, kubeConfig *rest.Config) (*Operator, erro
320320
return nil, fmt.Errorf("failed to create gatewayapi controller: %w", err)
321321
}
322322

323-
//set up the gateway api upgradeable controller
324-
if _, err := gatewayapi_upgradeable.New(mgr, gatewayapi_upgradeable.Config{
325-
Cache: mgr.GetCache(),
326-
}); err != nil {
323+
// Set up the gateway api upgradeable controller
324+
if _, err := gatewayapi_upgradeable.New(mgr); err != nil {
327325
return nil, fmt.Errorf("failed to create gatewayapi upgradeable controller: %w", err)
328326
}
329327

0 commit comments

Comments
 (0)