|
| 1 | +package gatewayapi_upgradeable |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + logf "github.com/openshift/cluster-ingress-operator/pkg/log" |
| 7 | + operatorcontroller "github.com/openshift/cluster-ingress-operator/pkg/operator/controller" |
| 8 | + corev1 "k8s.io/api/core/v1" |
| 9 | + apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" |
| 10 | + "sigs.k8s.io/controller-runtime/pkg/cache" |
| 11 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 12 | + "sigs.k8s.io/controller-runtime/pkg/controller" |
| 13 | + "sigs.k8s.io/controller-runtime/pkg/event" |
| 14 | + "sigs.k8s.io/controller-runtime/pkg/handler" |
| 15 | + "sigs.k8s.io/controller-runtime/pkg/manager" |
| 16 | + "sigs.k8s.io/controller-runtime/pkg/predicate" |
| 17 | + "sigs.k8s.io/controller-runtime/pkg/reconcile" |
| 18 | + "sigs.k8s.io/controller-runtime/pkg/source" |
| 19 | + gatewayapiv1 "sigs.k8s.io/gateway-api/apis/v1" |
| 20 | +) |
| 21 | + |
| 22 | +const ( |
| 23 | + controllerName = "gatewayapi_upgradeable_controller" |
| 24 | + GatewayAPIAdminKey = "ack-gateway-api-management" |
| 25 | + GatewayAPIAdminMsg = "You are now acknowledging that you are taking control of the Gateway API CRD Management from here on out. Please review the documentation for implications." |
| 26 | +) |
| 27 | + |
| 28 | +var ( |
| 29 | + log = logf.Logger.WithName(controllerName) |
| 30 | +) |
| 31 | + |
| 32 | +// 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() |
| 35 | + reconciler := &reconciler{ |
| 36 | + cache: config.Cache, |
| 37 | + client: mgr.GetClient(), |
| 38 | + } |
| 39 | + |
| 40 | + //Create a new controller with given reconciler |
| 41 | + c, err := controller.New(controllerName, mgr, controller.Options{ |
| 42 | + Reconciler: reconciler, |
| 43 | + }) |
| 44 | + if err != nil { |
| 45 | + return nil, err |
| 46 | + } |
| 47 | + |
| 48 | + // 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 | + } |
| 69 | + |
| 70 | + toAdminGateConfigMap := func(_ context.Context, _ client.Object) []reconcile.Request { |
| 71 | + return []reconcile.Request{{ |
| 72 | + NamespacedName: operatorcontroller.AdminGatesConfigMapName(), |
| 73 | + }} |
| 74 | + } |
| 75 | + // Define the CRD predicate |
| 76 | + crdPredicate := predicate.NewPredicateFuncs(func(o client.Object) bool { |
| 77 | + return o.(*apiextensionsv1.CustomResourceDefinition).Spec.Group == gatewayapiv1.GroupName |
| 78 | + }) |
| 79 | + |
| 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 { |
| 84 | + return nil, err |
| 85 | + } |
| 86 | + |
| 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 { |
| 91 | + return nil, err |
| 92 | + } |
| 93 | + return c, nil |
| 94 | +} |
| 95 | + |
| 96 | +// reconciler struct holds the client and cache attributes |
| 97 | +type reconciler struct { |
| 98 | + client client.Client |
| 99 | + cache cache.Cache |
| 100 | +} |
| 101 | + |
| 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 | + |
| 110 | +// Reconcile function implements the logic to check conditions and manage the admin gate |
| 111 | +func (r *reconciler) Reconcile(ctx context.Context, request reconcile.Request) (reconcile.Result, error) { |
| 112 | + log.Info("reconciling", "request", request) |
| 113 | + adminGateConditionExists, err := r.adminGateConditionExists(ctx) |
| 114 | + if err != nil { |
| 115 | + return reconcile.Result{}, fmt.Errorf("failed to determine if UnservableInFutureVersionsRoute routes exist: %w", err) |
| 116 | + } |
| 117 | + if adminGateConditionExists { |
| 118 | + if err := r.addAdminGate(ctx); err != nil { |
| 119 | + return reconcile.Result{}, fmt.Errorf("failed to add admin gate: %w", err) |
| 120 | + } |
| 121 | + } else { |
| 122 | + if err := r.removeAdminGate(ctx); err != nil { |
| 123 | + return reconcile.Result{}, fmt.Errorf("failed to remove admin gate: %w", err) |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + return reconcile.Result{}, nil |
| 128 | +} |
| 129 | + |
| 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) { |
| 133 | + |
| 134 | + // Placeholder: Attempt to list ConfigMaps |
| 135 | + // Placeholder: Iterate through ConfigMap list and check for the condition (to be implemented). |
| 136 | + |
| 137 | + // Placeholder return statement, to be replaced with actual logic. |
| 138 | + return false, nil |
| 139 | +} |
| 140 | + |
| 141 | +// addAdminGate function adds the admin gate to the ConfigMap |
| 142 | +func (r *reconciler) addAdminGate(ctx context.Context) error { |
| 143 | + adminGateConfigMap := &corev1.ConfigMap{} |
| 144 | + if err := r.client.Get(ctx, operatorcontroller.AdminGatesConfigMapName(), adminGateConfigMap); err != nil { |
| 145 | + return fmt.Errorf("failed to get configmap %s: %w", operatorcontroller.AdminGatesConfigMapName(), err) |
| 146 | + } |
| 147 | + |
| 148 | + if adminGateConfigMap.Data == nil { |
| 149 | + adminGateConfigMap.Data = map[string]string{} |
| 150 | + } |
| 151 | + |
| 152 | + // Check if the admin key exists and is set to the expected message |
| 153 | + if val, ok := adminGateConfigMap.Data[GatewayAPIAdminKey]; ok && val == GatewayAPIAdminMsg { |
| 154 | + return nil // Exists as expected |
| 155 | + } |
| 156 | + adminGateConfigMap.Data[GatewayAPIAdminKey] = GatewayAPIAdminMsg |
| 157 | + |
| 158 | + log.Info("Adding admin gate for Gateway API management") |
| 159 | + if err := r.client.Update(ctx, adminGateConfigMap); err != nil { |
| 160 | + return fmt.Errorf("failed to update configmap %s: %w", operatorcontroller.AdminGatesConfigMapName(), err) |
| 161 | + } |
| 162 | + return nil |
| 163 | +} |
| 164 | + |
| 165 | +// removeAdminGate function removes the admin gate from the ConfigMap |
| 166 | +func (r *reconciler) removeAdminGate(ctx context.Context) error { |
| 167 | + adminGateConfigMap := &corev1.ConfigMap{} |
| 168 | + if err := r.client.Get(ctx, operatorcontroller.AdminGatesConfigMapName(), adminGateConfigMap); err != nil { |
| 169 | + return fmt.Errorf("failed to get configmap %s: %w", operatorcontroller.AdminGatesConfigMapName(), err) |
| 170 | + } |
| 171 | + |
| 172 | + // Check if the admin key exists |
| 173 | + if _, ok := adminGateConfigMap.Data[GatewayAPIAdminKey]; !ok { |
| 174 | + return nil // Nothing to do if the key doesn't exist |
| 175 | + } |
| 176 | + |
| 177 | + log.Info("Removing admin gate for Gateway API management") |
| 178 | + delete(adminGateConfigMap.Data, GatewayAPIAdminKey) |
| 179 | + if err := r.client.Update(ctx, adminGateConfigMap); err != nil { |
| 180 | + return fmt.Errorf("failed to update configmap %s: %w", operatorcontroller.AdminGatesConfigMapName(), err) |
| 181 | + } |
| 182 | + return nil |
| 183 | +} |
0 commit comments