|
| 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 | + |
| 9 | + corev1 "k8s.io/api/core/v1" |
| 10 | + |
| 11 | + apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" |
| 12 | + |
| 13 | + "sigs.k8s.io/controller-runtime/pkg/cache" |
| 14 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 15 | + "sigs.k8s.io/controller-runtime/pkg/controller" |
| 16 | + "sigs.k8s.io/controller-runtime/pkg/handler" |
| 17 | + "sigs.k8s.io/controller-runtime/pkg/manager" |
| 18 | + "sigs.k8s.io/controller-runtime/pkg/predicate" |
| 19 | + "sigs.k8s.io/controller-runtime/pkg/reconcile" |
| 20 | + "sigs.k8s.io/controller-runtime/pkg/source" |
| 21 | + gatewayapiv1 "sigs.k8s.io/gateway-api/apis/v1" |
| 22 | +) |
| 23 | + |
| 24 | +const ( |
| 25 | + controllerName = "gatewayapi_upgradeable_controller" |
| 26 | + gatewayAPIAdminKey = "ack-gateway-api-management" |
| 27 | + gatewayAPIAdminMsg = "Gateway API CRDs have been detected. OCP fully manages the life-cycle of Gateway API CRDs. External management is unsupported and will be prevented. The cluster administrator is responsible for the safety of existing Gateway API implementations and must acknowledge their responsibilities via the admin gate to proceed with upgrades. See https://docs.redhat.com/en/documentation/openshift_container_platform/4.19/html/release_notes/ocp-4-19-release-notes#ocp-4-19-networking-gateway-api-crd-lifecycle_release-notes for details. Failure to read and understand the documentation for this and the implications can result in outages and data loss." |
| 28 | +) |
| 29 | + |
| 30 | +var ( |
| 31 | + log = logf.Logger.WithName(controllerName) |
| 32 | +) |
| 33 | + |
| 34 | +// The New function initializes the controller and sets up the watch for the ConfigMap. |
| 35 | +func New(mgr manager.Manager) (controller.Controller, error) { |
| 36 | + c, err := controller.New(controllerName, mgr, controller.Options{ |
| 37 | + Reconciler: &reconciler{ |
| 38 | + client: mgr.GetClient(), |
| 39 | + cache: mgr.GetCache(), |
| 40 | + }, |
| 41 | + }) |
| 42 | + if err != nil { |
| 43 | + return nil, err |
| 44 | + } |
| 45 | + |
| 46 | + // Mapping the events to the admin gate ConfigMap. |
| 47 | + toAdminGatesConfigMap := func(_ context.Context, _ client.Object) []reconcile.Request { |
| 48 | + return []reconcile.Request{{ |
| 49 | + NamespacedName: operatorcontroller.AdminGatesConfigMapName(), |
| 50 | + }} |
| 51 | + } |
| 52 | + |
| 53 | + // Defining the CRD predicate. |
| 54 | + crdPredicate := predicate.NewPredicateFuncs(func(o client.Object) bool { |
| 55 | + group := o.(*apiextensionsv1.CustomResourceDefinition).Spec.Group |
| 56 | + return group == gatewayapiv1.GroupName || group == "gateway.networking.x-k8s.io" |
| 57 | + }) |
| 58 | + |
| 59 | + // Setting up a watch for CRD events. |
| 60 | + if err := c.Watch(source.Kind[client.Object](mgr.GetCache(), &apiextensionsv1.CustomResourceDefinition{}, handler.EnqueueRequestsFromMapFunc(toAdminGatesConfigMap), crdPredicate)); err != nil { |
| 61 | + return nil, err |
| 62 | + } |
| 63 | + |
| 64 | + // A predicate filter to watch for specific changes in the ConfigMap. |
| 65 | + // Verify that the ConfigMap's name and namespace match the expected values. |
| 66 | + adminGatePredicate := predicate.NewPredicateFuncs(func(o client.Object) bool { |
| 67 | + return o.GetNamespace() == operatorcontroller.AdminGatesConfigMapName().Namespace && |
| 68 | + o.GetName() == operatorcontroller.AdminGatesConfigMapName().Name |
| 69 | + }) |
| 70 | + |
| 71 | + if err := c.Watch(source.Kind[client.Object](mgr.GetCache(), &corev1.ConfigMap{}, &handler.EnqueueRequestForObject{}, adminGatePredicate)); err != nil { |
| 72 | + return nil, err |
| 73 | + } |
| 74 | + |
| 75 | + return c, nil |
| 76 | +} |
| 77 | + |
| 78 | +// Reconciler struct holds the client and cache attributes. |
| 79 | +type reconciler struct { |
| 80 | + client client.Client |
| 81 | + cache cache.Cache |
| 82 | +} |
| 83 | + |
| 84 | +// Reconcile function implements the logic to check conditions and manage the admin gate. |
| 85 | +func (r *reconciler) Reconcile(ctx context.Context, request reconcile.Request) (reconcile.Result, error) { |
| 86 | + log.Info("reconciling", "request", request) |
| 87 | + |
| 88 | + adminGateConditionExists, err := r.adminGateConditionExists(ctx) |
| 89 | + if err != nil { |
| 90 | + return reconcile.Result{}, fmt.Errorf("failed to determine if admin gate condition exists: %w", err) |
| 91 | + } |
| 92 | + |
| 93 | + if adminGateConditionExists { |
| 94 | + if err := r.addAdminGate(ctx); err != nil { |
| 95 | + return reconcile.Result{}, fmt.Errorf("failed to add admin gate: %w", err) |
| 96 | + } |
| 97 | + } else { |
| 98 | + if err := r.removeAdminGate(ctx); err != nil { |
| 99 | + return reconcile.Result{}, fmt.Errorf("failed to remove admin gate: %w", err) |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + return reconcile.Result{}, nil |
| 104 | +} |
| 105 | + |
| 106 | +// adminGateConditionExists checks if the admin gate condition exists based on both ConfigMap and CRDs. |
| 107 | +func (r *reconciler) adminGateConditionExists(ctx context.Context) (bool, error) { |
| 108 | + crds := &apiextensionsv1.CustomResourceDefinitionList{} |
| 109 | + if err := r.cache.List(ctx, crds); err != nil { |
| 110 | + return false, fmt.Errorf("failed to list CRDs: %w", err) |
| 111 | + } |
| 112 | + |
| 113 | + for _, crd := range crds.Items { |
| 114 | + if crd.Spec.Group == gatewayapiv1.GroupName || crd.Spec.Group == "gateway.networking.x-k8s.io" { |
| 115 | + return true, nil |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + return false, nil |
| 120 | +} |
| 121 | + |
| 122 | +// The addAdminGate function is responsible for adding the admin gate to the ConfigMap. |
| 123 | +func (r *reconciler) addAdminGate(ctx context.Context) error { |
| 124 | + adminGatesConfigMap := &corev1.ConfigMap{} |
| 125 | + if err := r.cache.Get(ctx, operatorcontroller.AdminGatesConfigMapName(), adminGatesConfigMap); err != nil { |
| 126 | + return fmt.Errorf("failed to get configmap %s: %w", operatorcontroller.AdminGatesConfigMapName(), err) |
| 127 | + } |
| 128 | + |
| 129 | + if adminGatesConfigMap.Data == nil { |
| 130 | + adminGatesConfigMap.Data = map[string]string{} |
| 131 | + } |
| 132 | + |
| 133 | + // The function checks if the admin key exists and if it is set to the expected message. |
| 134 | + if val, ok := adminGatesConfigMap.Data[gatewayAPIAdminKey]; ok && val == gatewayAPIAdminMsg { |
| 135 | + return nil |
| 136 | + } |
| 137 | + adminGatesConfigMap.Data[gatewayAPIAdminKey] = gatewayAPIAdminMsg |
| 138 | + |
| 139 | + log.Info("Adding admin gate for Gateway API management") |
| 140 | + if err := r.client.Update(ctx, adminGatesConfigMap); err != nil { |
| 141 | + return fmt.Errorf("failed to update configmap %s: %w", operatorcontroller.AdminGatesConfigMapName(), err) |
| 142 | + } |
| 143 | + return nil |
| 144 | +} |
| 145 | + |
| 146 | +// The removeAdminGate function is responsible for removing the admin gate from the ConfigMap. |
| 147 | +func (r *reconciler) removeAdminGate(ctx context.Context) error { |
| 148 | + adminGatesConfigMap := &corev1.ConfigMap{} |
| 149 | + if err := r.cache.Get(ctx, operatorcontroller.AdminGatesConfigMapName(), adminGatesConfigMap); err != nil { |
| 150 | + return fmt.Errorf("failed to get configmap %s: %w", operatorcontroller.AdminGatesConfigMapName(), err) |
| 151 | + } |
| 152 | + |
| 153 | + if _, ok := adminGatesConfigMap.Data[gatewayAPIAdminKey]; !ok { |
| 154 | + return nil |
| 155 | + } |
| 156 | + |
| 157 | + log.Info("Removing admin gate for Gateway API management") |
| 158 | + delete(adminGatesConfigMap.Data, gatewayAPIAdminKey) |
| 159 | + if err := r.client.Update(ctx, adminGatesConfigMap); err != nil { |
| 160 | + return fmt.Errorf("failed to update configmap %s: %w", operatorcontroller.AdminGatesConfigMapName(), err) |
| 161 | + } |
| 162 | + return nil |
| 163 | +} |
0 commit comments