@@ -18,8 +18,11 @@ package controller
1818
1919import (
2020 "context"
21+ "time"
2122
2223 apierrors "k8s.io/apimachinery/pkg/api/errors"
24+ "k8s.io/apimachinery/pkg/api/meta"
25+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2326 "k8s.io/apimachinery/pkg/runtime"
2427 "k8s.io/apimachinery/pkg/types"
2528 "k8s.io/client-go/tools/record"
@@ -42,12 +45,15 @@ type IPAddressReconciler struct {
4245 client.Client
4346 Scheme * runtime.Scheme
4447 Recorder record.EventRecorder
48+ // Now returns the current time; nil means time.Now.
49+ Now func () time.Time
4550}
4651
4752// +kubebuilder:rbac:groups=local.sdn.cozystack.io,resources=ipaddresses,verbs=get;list;watch;update;patch;delete
4853// +kubebuilder:rbac:groups=local.sdn.cozystack.io,resources=ipaddresses/status,verbs=get;update;patch
4954// +kubebuilder:rbac:groups=local.sdn.cozystack.io,resources=ipaddresses/finalizers,verbs=update
5055// +kubebuilder:rbac:groups=local.sdn.cozystack.io,resources=ipaddressclaims,verbs=get;list;watch
56+ // +kubebuilder:rbac:groups=local.sdn.cozystack.io,resources=ipaddressclasses,verbs=get;list;watch
5157// +kubebuilder:rbac:groups="",resources=events,verbs=create;patch
5258
5359// Reconcile drives an IPAddress's phase bookkeeping.
@@ -95,9 +101,13 @@ func (r *IPAddressReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
95101
96102 switch {
97103 case claim == nil :
98- // The claim is gone but the claim-side reclaim never ran (stale
99- // UID, or the claim disappeared without the finalizer flow).
100- return ctrl.Result {}, r .reclaim (ctx , addr )
104+ // The claim is gone: reclaim it, unless the claim-side flow already
105+ // did (a stale UID, or a claim that vanished without its
106+ // finalizer), and then count down a Released address's delay.
107+ if err := r .reclaim (ctx , addr ); err != nil {
108+ return ctrl.Result {}, err
109+ }
110+ return r .releaseAfterDelay (ctx , addr )
101111 case ! claim .DeletionTimestamp .IsZero ():
102112 // The claim-side flow owns the transition; nothing to do here.
103113 return ctrl.Result {}, nil
@@ -170,13 +180,88 @@ func (r *IPAddressReconciler) reclaim(ctx context.Context, addr *localv1alpha1.I
170180}
171181
172182func (r * IPAddressReconciler ) setPhase (ctx context.Context , addr * localv1alpha1.IPAddress , phase localv1alpha1.IPAddressPhase ) error {
173- if addr . Status . Phase == phase {
183+ if ! markPhase ( addr , phase ) {
174184 return nil
175185 }
176- addr .Status .Phase = phase
177186 return r .Status ().Update (ctx , addr )
178187}
179188
189+ // releaseAfterDelay returns a Released address to the pool once its
190+ // class's releaseDelay has passed since it was Released, by clearing its
191+ // claimRef; the next pass makes it Available. An address annotated held,
192+ // or of a class without a delay, is left alone.
193+ func (r * IPAddressReconciler ) releaseAfterDelay (ctx context.Context , addr * localv1alpha1.IPAddress ) (ctrl.Result , error ) {
194+ if addr .Status .Phase != localv1alpha1 .IPAddressReleased || addr .Annotations [localv1alpha1 .HeldAnnotation ] == localv1alpha1 .AnnotationTrue {
195+ return ctrl.Result {}, nil
196+ }
197+ class := & localv1alpha1.IPAddressClass {}
198+ if err := r .Get (ctx , types.NamespacedName {Name : addr .Spec .ClassName }, class ); err != nil {
199+ return ctrl.Result {}, client .IgnoreNotFound (err )
200+ }
201+ if class .Spec .ReleaseDelay == nil || class .Spec .ReleaseDelay .Duration <= 0 {
202+ return ctrl.Result {}, nil
203+ }
204+ released := meta .FindStatusCondition (addr .Status .Conditions , localv1alpha1 .ConditionBound )
205+ if released == nil || released .Reason != localv1alpha1 .ReasonReleased {
206+ return ctrl.Result {}, nil
207+ }
208+ now := time .Now
209+ if r .Now != nil {
210+ now = r .Now
211+ }
212+ if remaining := released .LastTransitionTime .Add (class .Spec .ReleaseDelay .Duration ).Sub (now ()); remaining > 0 {
213+ return ctrl.Result {RequeueAfter : remaining }, nil
214+ }
215+ ref := addr .Spec .ClaimRef
216+ addr .Spec .ClaimRef = nil
217+ if err := r .Update (ctx , addr ); err != nil {
218+ return ctrl.Result {}, err
219+ }
220+ r .Recorder .Eventf (addr , "Normal" , "ReleaseDelayElapsed" ,
221+ "released by claim %s/%s for %s; returned to the pool" , ref .Namespace , ref .Name , class .Spec .ReleaseDelay .Duration )
222+ return ctrl.Result {}, nil
223+ }
224+
225+ // markPhase sets a core-owned phase on the in-memory address together with
226+ // its Bound condition, whose lastTransitionTime then dates the change. It
227+ // reports whether anything changed.
228+ func markPhase (addr * localv1alpha1.IPAddress , phase localv1alpha1.IPAddressPhase ) bool {
229+ cond := metav1.Condition {
230+ Type : localv1alpha1 .ConditionBound ,
231+ Status : metav1 .ConditionFalse ,
232+ ObservedGeneration : addr .Generation ,
233+ }
234+ switch phase {
235+ case localv1alpha1 .IPAddressBound :
236+ cond .Status , cond .Reason , cond .Message = metav1 .ConditionTrue , localv1alpha1 .ReasonBound , "bound to its claim"
237+ case localv1alpha1 .IPAddressReleased :
238+ cond .Reason , cond .Message = localv1alpha1 .ReasonReleased , "its claim was deleted under reclaimPolicy Retain"
239+ case localv1alpha1 .IPAddressAvailable :
240+ cond .Reason , cond .Message = localv1alpha1 .ReasonAvailable , "bound to no claim"
241+ default :
242+ cond .Reason , cond .Message = localv1alpha1 .ReasonPending , "spec.address does not parse"
243+ }
244+ changed := addr .Status .Phase != phase
245+ addr .Status .Phase = phase
246+ return meta .SetStatusCondition (& addr .Status .Conditions , cond ) || changed
247+ }
248+
249+ // releasedAddressesForClass maps a class event to its Released addresses,
250+ // so a changed releaseDelay takes effect.
251+ func (r * IPAddressReconciler ) releasedAddressesForClass (ctx context.Context , o client.Object ) []reconcile.Request {
252+ list := & localv1alpha1.IPAddressList {}
253+ if err := r .List (ctx , list , client.MatchingFields {IPAddressClassNameIndex : o .GetName ()}); err != nil {
254+ return nil
255+ }
256+ var reqs []reconcile.Request
257+ for _ , addr := range list .Items {
258+ if addr .Status .Phase == localv1alpha1 .IPAddressReleased {
259+ reqs = append (reqs , reconcile.Request {NamespacedName : types.NamespacedName {Name : addr .Name }})
260+ }
261+ }
262+ return reqs
263+ }
264+
180265// addressesForClaim maps a claim event to the addresses bound to it, so
181266// claim deletion and rebinding propagate promptly.
182267func (r * IPAddressReconciler ) addressesForClaim (ctx context.Context , o client.Object ) []reconcile.Request {
@@ -198,6 +283,7 @@ func (r *IPAddressReconciler) SetupWithManager(mgr ctrl.Manager) error {
198283 return ctrl .NewControllerManagedBy (mgr ).
199284 For (& localv1alpha1.IPAddress {}).
200285 Watches (& localv1alpha1.IPAddressClaim {}, handler .EnqueueRequestsFromMapFunc (r .addressesForClaim )).
286+ Watches (& localv1alpha1.IPAddressClass {}, handler .EnqueueRequestsFromMapFunc (r .releasedAddressesForClass )).
201287 Named ("ipaddress" ).
202288 Complete (r )
203289}
0 commit comments