|
| 1 | +/* |
| 2 | +Copyright 2026 Cozystack contributors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package controllers |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + |
| 22 | + "github.com/cockroachdb/errors" |
| 23 | + apierrors "k8s.io/apimachinery/pkg/api/errors" |
| 24 | + ctrl "sigs.k8s.io/controller-runtime" |
| 25 | + "sigs.k8s.io/controller-runtime/pkg/builder" |
| 26 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 27 | + "sigs.k8s.io/controller-runtime/pkg/event" |
| 28 | + "sigs.k8s.io/controller-runtime/pkg/log" |
| 29 | + "sigs.k8s.io/controller-runtime/pkg/predicate" |
| 30 | + |
| 31 | + blockstoriov1alpha1 "github.com/cozystack/blockstor/api/v1alpha1" |
| 32 | +) |
| 33 | + |
| 34 | +// ResourceReconciler watches Resource CRDs filtered to those |
| 35 | +// placed on this satellite's node and translates them into the |
| 36 | +// existing apply chain. Phase 10.1: replaces the gRPC |
| 37 | +// `ApplyResources` consumer. |
| 38 | +// |
| 39 | +// The reconciler is intentionally minimal in this initial |
| 40 | +// commit — it logs what it sees and exits. Subsequent commits |
| 41 | +// fill in the desired-state-builder + apply path by delegating |
| 42 | +// to `Config.Apply` (the pre-existing satellite reconciler that |
| 43 | +// owns storage + DRBD + LUKS). |
| 44 | +type ResourceReconciler struct { |
| 45 | + client.Client |
| 46 | + |
| 47 | + Config Config |
| 48 | +} |
| 49 | + |
| 50 | +// Reconcile reads a Resource and drives the satellite-side |
| 51 | +// apply chain when the Resource is placed on this node. |
| 52 | +// Deletion is handled via finalizer in a follow-up commit. |
| 53 | +func (r *ResourceReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { |
| 54 | + logger := log.FromContext(ctx).WithValues("resource", req.Name) |
| 55 | + |
| 56 | + var res blockstoriov1alpha1.Resource |
| 57 | + |
| 58 | + err := r.Get(ctx, req.NamespacedName, &res) |
| 59 | + if err != nil { |
| 60 | + if apierrors.IsNotFound(err) { |
| 61 | + return ctrl.Result{}, nil |
| 62 | + } |
| 63 | + |
| 64 | + return ctrl.Result{}, errors.Wrap(err, "get Resource") |
| 65 | + } |
| 66 | + |
| 67 | + if res.Spec.NodeName != r.Config.NodeName { |
| 68 | + // Predicate should have filtered this out — defensive |
| 69 | + // check in case the watch cache is mid-resync. |
| 70 | + return ctrl.Result{}, nil |
| 71 | + } |
| 72 | + |
| 73 | + logger.V(1).Info("observed Resource", |
| 74 | + "rd", res.Spec.ResourceDefinitionName, |
| 75 | + "flags", res.Spec.Flags, |
| 76 | + "deletionTimestamp", res.DeletionTimestamp) |
| 77 | + |
| 78 | + // Phase 10.1 follow-up: translate to DesiredResource + |
| 79 | + // call r.Config.Apply.Apply(...). Until then the gRPC |
| 80 | + // `ApplyResources` consumer continues to drive applies. |
| 81 | + return ctrl.Result{}, nil |
| 82 | +} |
| 83 | + |
| 84 | +// SetupWithManager wires this reconciler with a node-name |
| 85 | +// predicate so only Resources placed on this satellite trigger |
| 86 | +// reconciles. The predicate also fires on Delete events (the |
| 87 | +// CRD finalizer dance still needs us to clean up local state). |
| 88 | +func (r *ResourceReconciler) SetupWithManager(mgr ctrl.Manager) error { |
| 89 | + err := ctrl.NewControllerManagedBy(mgr). |
| 90 | + For(&blockstoriov1alpha1.Resource{}, |
| 91 | + builder.WithPredicates(nodeNamePredicate(r.Config.NodeName))). |
| 92 | + Named("satellite-resource"). |
| 93 | + Complete(r) |
| 94 | + if err != nil { |
| 95 | + return errors.Wrap(err, "register ResourceReconciler") |
| 96 | + } |
| 97 | + |
| 98 | + return nil |
| 99 | +} |
| 100 | + |
| 101 | +// nodeNamePredicate filters events down to objects whose |
| 102 | +// `Spec.NodeName` matches the given node. Centralised so the |
| 103 | +// StoragePool reconciler can reuse the same shape. |
| 104 | +func nodeNamePredicate(nodeName string) predicate.Predicate { |
| 105 | + matches := func(obj client.Object) bool { |
| 106 | + if r, ok := obj.(*blockstoriov1alpha1.Resource); ok { |
| 107 | + return r.Spec.NodeName == nodeName |
| 108 | + } |
| 109 | + |
| 110 | + if sp, ok := obj.(*blockstoriov1alpha1.StoragePool); ok { |
| 111 | + return sp.Spec.NodeName == nodeName |
| 112 | + } |
| 113 | + |
| 114 | + return false |
| 115 | + } |
| 116 | + |
| 117 | + return predicate.Funcs{ |
| 118 | + CreateFunc: func(e event.CreateEvent) bool { return matches(e.Object) }, |
| 119 | + UpdateFunc: func(e event.UpdateEvent) bool { |
| 120 | + return matches(e.ObjectNew) || matches(e.ObjectOld) |
| 121 | + }, |
| 122 | + DeleteFunc: func(e event.DeleteEvent) bool { return matches(e.Object) }, |
| 123 | + GenericFunc: func(e event.GenericEvent) bool { return matches(e.Object) }, |
| 124 | + } |
| 125 | +} |
0 commit comments