|
| 1 | +package gatewayapi_upgradeable |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + logf "github.com/openshift/cluster-ingress-operator/pkg/log" |
| 8 | + operatorcontroller "github.com/openshift/cluster-ingress-operator/pkg/operator/controller" |
| 9 | + |
| 10 | + corev1 "k8s.io/api/core/v1" |
| 11 | + |
| 12 | + apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" |
| 13 | + |
| 14 | + "sigs.k8s.io/controller-runtime/pkg/cache" |
| 15 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 16 | + "sigs.k8s.io/controller-runtime/pkg/controller" |
| 17 | + "sigs.k8s.io/controller-runtime/pkg/handler" |
| 18 | + "sigs.k8s.io/controller-runtime/pkg/manager" |
| 19 | + "sigs.k8s.io/controller-runtime/pkg/predicate" |
| 20 | + "sigs.k8s.io/controller-runtime/pkg/reconcile" |
| 21 | + "sigs.k8s.io/controller-runtime/pkg/source" |
| 22 | + gatewayapiv1 "sigs.k8s.io/gateway-api/apis/v1" |
| 23 | +) |
| 24 | + |
| 25 | +const ( |
| 26 | + controllerName = "gatewayapi_upgradeable_controller" |
| 27 | + GatewayAPIAdminKey = "ack-gateway-api-management" |
| 28 | + GatewayAPIAdminMsg = "This message was generated due to the presence of the Gateway API CRDs in the cluster. Going forward OCP will fully manage the lifecycle of these CRDs and we strongly advise against any external entity attempting to manage them, as this will be blocked and result in failure. Only the platform itself will support management of these CRDs. Any tampering will cause the cluster to enter a 'Degraded' state. The cluster administrator is responsible for the safety of existing Gateway API implementations. An admin gate will inform the administrator of their responsibilities and prevent upgrades until they acknowledge and accept them. For more information, please refer to the [link to doc here pending]." |
| 29 | +) |
| 30 | + |
| 31 | +var ( |
| 32 | + log = logf.Logger.WithName(controllerName) |
| 33 | +) |
| 34 | + |
| 35 | +// The New function initializes the controller and sets up the watch for the ConfigMap. |
| 36 | +func New(mgr manager.Manager) (controller.Controller, error) { |
| 37 | + reconciler := &reconciler{ |
| 38 | + client: mgr.GetClient(), |
| 39 | + cache: mgr.GetCache(), |
| 40 | + } |
| 41 | + |
| 42 | + c, err := controller.New(controllerName, mgr, controller.Options{ |
| 43 | + Reconciler: reconciler, |
| 44 | + }) |
| 45 | + if err != nil { |
| 46 | + return nil, err |
| 47 | + } |
| 48 | + |
| 49 | + // A predicate filter to watch for specific changes in the ConfigMap. |
| 50 | + // Verify that the ConfigMap's name and namespace match the expected values. |
| 51 | + adminGatePredicate := predicate.NewPredicateFuncs(func(o client.Object) bool { |
| 52 | + cm := o.(*corev1.ConfigMap) |
| 53 | + return cm.Name == operatorcontroller.AdminGatesConfigMapName().Name && cm.Namespace == operatorcontroller.AdminGatesConfigMapName().Namespace |
| 54 | + }) |
| 55 | + |
| 56 | + // Mapping the events to the admin gate ConfigMap. |
| 57 | + toAdminGateConfigMap := func(_ context.Context, _ client.Object) []reconcile.Request { |
| 58 | + return []reconcile.Request{{ |
| 59 | + NamespacedName: operatorcontroller.AdminGatesConfigMapName(), |
| 60 | + }} |
| 61 | + } |
| 62 | + |
| 63 | + // Defining the CRD predicate. |
| 64 | + crdPredicate := predicate.NewPredicateFuncs(func(o client.Object) bool { |
| 65 | + return o.(*apiextensionsv1.CustomResourceDefinition).Spec.Group == gatewayapiv1.GroupName |
| 66 | + }) |
| 67 | + |
| 68 | + // Setting up a watch for CRD events. |
| 69 | + if err := c.Watch(source.Kind[client.Object](mgr.GetCache(), &apiextensionsv1.CustomResourceDefinition{}, handler.EnqueueRequestsFromMapFunc(toAdminGateConfigMap), crdPredicate)); err != nil { |
| 70 | + return nil, err |
| 71 | + } |
| 72 | + |
| 73 | + if err := c.Watch(source.Kind[client.Object](mgr.GetCache(), &corev1.ConfigMap{}, &handler.EnqueueRequestForObject{}, adminGatePredicate)); err != nil { |
| 74 | + return nil, err |
| 75 | + } |
| 76 | + |
| 77 | + return c, nil |
| 78 | +} |
| 79 | + |
| 80 | +// Reconciler struct holds the client and cache attributes. |
| 81 | +type reconciler struct { |
| 82 | + client client.Client |
| 83 | + cache cache.Cache |
| 84 | +} |
| 85 | + |
| 86 | +// Reconcile function implements the logic to check conditions and manage the admin gate. |
| 87 | +func (r *reconciler) Reconcile(ctx context.Context, request reconcile.Request) (reconcile.Result, error) { |
| 88 | + log.Info("reconciling", "request", request) |
| 89 | + |
| 90 | + adminGateConditionExists, err := r.adminGateConditionExists(ctx) |
| 91 | + if err != nil { |
| 92 | + return reconcile.Result{}, fmt.Errorf("failed to determine if admin gate condition exists: %v", err) |
| 93 | + } |
| 94 | + |
| 95 | + if adminGateConditionExists { |
| 96 | + if err := r.addAdminGate(ctx); err != nil { |
| 97 | + return reconcile.Result{}, fmt.Errorf("failed to add admin gate: %w", err) |
| 98 | + } |
| 99 | + } else { |
| 100 | + if err := r.removeAdminGate(ctx); err != nil { |
| 101 | + return reconcile.Result{}, fmt.Errorf("failed to remove admin gate: %w", err) |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + return reconcile.Result{}, nil |
| 106 | +} |
| 107 | + |
| 108 | +// adminGateConditionExists checks if the admin gate condition exists based on both ConfigMap and CRDs. |
| 109 | +func (r *reconciler) adminGateConditionExists(ctx context.Context) (bool, error) { |
| 110 | + crds := &apiextensionsv1.CustomResourceDefinitionList{} |
| 111 | + if err := r.client.List(ctx, crds); err != nil { |
| 112 | + return false, fmt.Errorf("failed to list CRDs: %w", err) |
| 113 | + } |
| 114 | + |
| 115 | + for _, crd := range crds.Items { |
| 116 | + if crd.Spec.Group == gatewayapiv1.GroupName { |
| 117 | + return true, nil |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + return false, nil |
| 122 | +} |
| 123 | + |
| 124 | +// The addAdminGate function is responsible for adding the admin gate to the ConfigMap. |
| 125 | +func (r *reconciler) addAdminGate(ctx context.Context) error { |
| 126 | + adminGateConfigMap := &corev1.ConfigMap{} |
| 127 | + if err := r.cache.Get(ctx, operatorcontroller.AdminGatesConfigMapName(), adminGateConfigMap); err != nil { |
| 128 | + return fmt.Errorf("failed to get configmap %s: %w", operatorcontroller.AdminGatesConfigMapName(), err) |
| 129 | + } |
| 130 | + |
| 131 | + if adminGateConfigMap.Data == nil { |
| 132 | + adminGateConfigMap.Data = map[string]string{} |
| 133 | + } |
| 134 | + |
| 135 | + // The function checks if the admin key exists and if it is set to the expected message. |
| 136 | + if val, ok := adminGateConfigMap.Data[GatewayAPIAdminKey]; ok && val == GatewayAPIAdminMsg { |
| 137 | + return nil |
| 138 | + } |
| 139 | + adminGateConfigMap.Data[GatewayAPIAdminKey] = GatewayAPIAdminMsg |
| 140 | + |
| 141 | + log.Info("Adding admin gate for Gateway API management") |
| 142 | + if err := r.client.Update(ctx, adminGateConfigMap); err != nil { |
| 143 | + return fmt.Errorf("failed to update configmap %s: %w", operatorcontroller.AdminGatesConfigMapName(), err) |
| 144 | + } |
| 145 | + return nil |
| 146 | +} |
| 147 | + |
| 148 | +// The removeAdminGate function is responsible for removing the admin gate from the ConfigMap. |
| 149 | +func (r *reconciler) removeAdminGate(ctx context.Context) error { |
| 150 | + adminGateConfigMap := &corev1.ConfigMap{} |
| 151 | + if err := r.cache.Get(ctx, operatorcontroller.AdminGatesConfigMapName(), adminGateConfigMap); err != nil { |
| 152 | + return fmt.Errorf("failed to get configmap %s: %w", operatorcontroller.AdminGatesConfigMapName(), err) |
| 153 | + } |
| 154 | + |
| 155 | + // The function checks if the admin key exists in the ConfigMap. |
| 156 | + if _, ok := adminGateConfigMap.Data[GatewayAPIAdminKey]; !ok { |
| 157 | + return nil // Nothing to do if the key doesn't exist |
| 158 | + } |
| 159 | + |
| 160 | + log.Info("Removing admin gate for Gateway API management") |
| 161 | + delete(adminGateConfigMap.Data, GatewayAPIAdminKey) |
| 162 | + if err := r.client.Update(ctx, adminGateConfigMap); err != nil { |
| 163 | + return fmt.Errorf("failed to update configmap %s: %w", operatorcontroller.AdminGatesConfigMapName(), err) |
| 164 | + } |
| 165 | + return nil |
| 166 | +} |
0 commit comments