|
| 1 | +/* |
| 2 | +Copyright 2026 The Cozystack Authors. |
| 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 controller |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "sort" |
| 22 | + |
| 23 | + "k8s.io/apimachinery/pkg/runtime" |
| 24 | + "k8s.io/apimachinery/pkg/types" |
| 25 | + "k8s.io/client-go/tools/record" |
| 26 | + ctrl "sigs.k8s.io/controller-runtime" |
| 27 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 28 | + "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" |
| 29 | + "sigs.k8s.io/controller-runtime/pkg/handler" |
| 30 | + "sigs.k8s.io/controller-runtime/pkg/reconcile" |
| 31 | + |
| 32 | + localv1alpha1 "github.com/cozystack/address-controller/api/v1alpha1" |
| 33 | +) |
| 34 | + |
| 35 | +// IPAddressClassReconciler guards class deletion: a deleted class is held |
| 36 | +// by the class-protection finalizer until no IPAddress of the class is left. |
| 37 | +type IPAddressClassReconciler struct { |
| 38 | + client.Client |
| 39 | + Scheme *runtime.Scheme |
| 40 | + Recorder record.EventRecorder |
| 41 | +} |
| 42 | + |
| 43 | +// +kubebuilder:rbac:groups=local.sdn.cozystack.io,resources=ipaddressclasses,verbs=get;list;watch;update;patch |
| 44 | +// +kubebuilder:rbac:groups=local.sdn.cozystack.io,resources=ipaddressclasses/finalizers,verbs=update |
| 45 | +// +kubebuilder:rbac:groups=local.sdn.cozystack.io,resources=ipaddresses,verbs=get;list;watch |
| 46 | +// +kubebuilder:rbac:groups="",resources=events,verbs=create;patch |
| 47 | + |
| 48 | +// Reconcile adds the protection finalizer, or releases a deleted class once |
| 49 | +// its addresses are gone. |
| 50 | +func (r *IPAddressClassReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { |
| 51 | + class := &localv1alpha1.IPAddressClass{} |
| 52 | + if err := r.Get(ctx, req.NamespacedName, class); err != nil { |
| 53 | + return ctrl.Result{}, client.IgnoreNotFound(err) |
| 54 | + } |
| 55 | + |
| 56 | + if class.DeletionTimestamp.IsZero() { |
| 57 | + if controllerutil.AddFinalizer(class, localv1alpha1.ClassProtectionFinalizer) { |
| 58 | + return ctrl.Result{}, r.Update(ctx, class) |
| 59 | + } |
| 60 | + return ctrl.Result{}, nil |
| 61 | + } |
| 62 | + if !controllerutil.ContainsFinalizer(class, localv1alpha1.ClassProtectionFinalizer) { |
| 63 | + return ctrl.Result{}, nil |
| 64 | + } |
| 65 | + |
| 66 | + addrs := &localv1alpha1.IPAddressList{} |
| 67 | + if err := r.List(ctx, addrs, client.MatchingFields{IPAddressClassNameIndex: class.Name}); err != nil { |
| 68 | + return ctrl.Result{}, err |
| 69 | + } |
| 70 | + if len(addrs.Items) > 0 { |
| 71 | + names := make([]string, 0, len(addrs.Items)) |
| 72 | + for _, addr := range addrs.Items { |
| 73 | + names = append(names, addr.Name) |
| 74 | + } |
| 75 | + sort.Strings(names) |
| 76 | + r.Recorder.Eventf(class, "Warning", localv1alpha1.ReasonClassInUse, |
| 77 | + "deletion waits for %d IPAddress(es) of this class: %v", len(names), names) |
| 78 | + return ctrl.Result{}, nil |
| 79 | + } |
| 80 | + controllerutil.RemoveFinalizer(class, localv1alpha1.ClassProtectionFinalizer) |
| 81 | + return ctrl.Result{}, r.Update(ctx, class) |
| 82 | +} |
| 83 | + |
| 84 | +// classDeleting reports whether a class is being deleted. |
| 85 | +func classDeleting(c localv1alpha1.IPAddressClass) bool { |
| 86 | + return !c.DeletionTimestamp.IsZero() |
| 87 | +} |
| 88 | + |
| 89 | +// classForAddress maps an IPAddress event to the address's class. |
| 90 | +func (r *IPAddressClassReconciler) classForAddress(_ context.Context, o client.Object) []reconcile.Request { |
| 91 | + addr := o.(*localv1alpha1.IPAddress) |
| 92 | + if addr.Spec.ClassName == "" { |
| 93 | + return nil |
| 94 | + } |
| 95 | + return []reconcile.Request{{NamespacedName: types.NamespacedName{Name: addr.Spec.ClassName}}} |
| 96 | +} |
| 97 | + |
| 98 | +// SetupWithManager sets up the controller with the Manager. |
| 99 | +func (r *IPAddressClassReconciler) SetupWithManager(mgr ctrl.Manager) error { |
| 100 | + return ctrl.NewControllerManagedBy(mgr). |
| 101 | + For(&localv1alpha1.IPAddressClass{}). |
| 102 | + Watches(&localv1alpha1.IPAddress{}, handler.EnqueueRequestsFromMapFunc(r.classForAddress)). |
| 103 | + Named("ipaddressclass"). |
| 104 | + Complete(r) |
| 105 | +} |
0 commit comments