@@ -17,8 +17,10 @@ limitations under the License.
1717package controller
1818
1919import (
20+ "cmp"
2021 "context"
2122 "fmt"
23+ "slices"
2224 "sort"
2325
2426 apierrors "k8s.io/apimachinery/pkg/api/errors"
@@ -82,18 +84,28 @@ func (r *IPAddressClaimReconciler) Reconcile(ctx context.Context, req ctrl.Reque
8284 if err != nil {
8385 return ctrl.Result {}, err
8486 }
85- if err := r .completePreBindings (ctx , claim , bound ); err != nil {
87+
88+ // Accepting a pre-binding needs the claim's class, so a claim that has
89+ // not recorded one resolves it first.
90+ className , resolved := "" , false
91+ if knownClassName (claim ) == "" && hasPendingPreBinding (bound ) {
92+ if className , err = r .resolveClass (ctx , claim ); err != nil {
93+ return ctrl.Result {}, err
94+ }
95+ resolved = true
96+ }
97+ if bound , err = r .acceptPreBindings (ctx , claim , cmp .Or (knownClassName (claim ), className ), bound ); err != nil {
8698 return ctrl.Result {}, err
8799 }
88100
89- className := ""
90- if len ( missingFamilies ( claim , bound )) > 0 {
91- className , err = r .resolveClass (ctx , claim )
92- if err != nil {
93- return ctrl. Result {}, err
101+ if unbound := unboundFamilies ( claim , bound ); len ( unbound ) > 0 {
102+ if ! resolved {
103+ if className , err = r .resolveClass (ctx , claim ); err != nil {
104+ return ctrl. Result {}, err
105+ }
94106 }
95107 if className != "" {
96- for _ , family := range missingFamilies ( claim , bound ) {
108+ for _ , family := range unbound {
97109 matched , err := r .bindAvailableAddress (ctx , claim , className , family )
98110 if err != nil {
99111 return ctrl.Result {}, err
@@ -123,29 +135,36 @@ func (r *IPAddressClaimReconciler) finalizeClaim(ctx context.Context, claim *loc
123135 if err != nil {
124136 return err
125137 }
126- // A claim still controlled by a Service lives and dies with it, so its
138+ // A claim with a controller owner lives and dies with it, so its
127139 // addresses are deleted whatever their reclaimPolicy. Removing the
128140 // controller reference is how such a claim is retained.
129- ownedByService := controlledByService (claim )
141+ owner := metav1 . GetControllerOf (claim )
130142 for i := range addrs {
131143 addr := & addrs [i ]
132144 if ! addr .DeletionTimestamp .IsZero () {
133145 continue
134146 }
147+ // A pre-binding the claim never accepted is not the claim's to
148+ // reclaim; the address controller handles it once the claim is gone.
149+ if addr .Spec .ClaimRef .UID == "" && preBindingMismatch (claim , knownClassName (claim ), nil , addr ) != "" {
150+ continue
151+ }
135152 switch {
136- case ownedByService || addr .Spec .ReclaimPolicy == localv1alpha1 .ReclaimDelete :
153+ case owner != nil || addr .Spec .ReclaimPolicy == localv1alpha1 .ReclaimDelete :
137154 // The driver's own finalizer tears down the backend allocation
138155 // before the object goes away.
139156 if err := r .Delete (ctx , addr ); err != nil && ! apierrors .IsNotFound (err ) {
140157 return err
141158 }
142- if ownedByService {
159+ if owner != nil {
143160 r .Recorder .Eventf (claim , "Normal" , "AddressDeleted" ,
144- "IPAddress %s deleted with the claim's owning Service " , addr .Name )
161+ "IPAddress %s deleted with the claim's owner %s %s " , addr . Name , owner . Kind , owner .Name )
145162 } else {
146163 r .Recorder .Eventf (claim , "Normal" , "AddressDeleted" ,
147164 "IPAddress %s deleted per reclaimPolicy Delete" , addr .Name )
148165 }
166+ case addr .Status .Phase == localv1alpha1 .IPAddressConflict || addr .Status .Phase == localv1alpha1 .IPAddressLost :
167+ // Conflict and Lost belong to the driver and stay as recorded.
149168 default : // Retain
150169 if addr .Status .Phase != localv1alpha1 .IPAddressReleased {
151170 addr .Status .Phase = localv1alpha1 .IPAddressReleased
@@ -161,10 +180,10 @@ func (r *IPAddressClaimReconciler) finalizeClaim(ctx context.Context, claim *loc
161180 return r .Update (ctx , claim )
162181}
163182
164- // controlledByService reports whether a claim's controller is a Service.
165- func controlledByService ( claim * localv1alpha1. IPAddressClaim ) bool {
166- ref := metav1 . GetControllerOf (claim )
167- return ref != nil && ref . APIVersion == "v1" && ref . Kind == serviceKind
183+ // knownClassName is the class the claim names or has already resolved to,
184+ // or "" if neither is recorded yet.
185+ func knownClassName (claim * localv1alpha1. IPAddressClaim ) string {
186+ return cmp . Or ( claim . Spec . ClassName , claim . Status . ClassName )
168187}
169188
170189// resolveClass resolves the claim's class name — spec first, then the sticky
@@ -251,19 +270,67 @@ func (r *IPAddressClaimReconciler) markUnresolved(claim *localv1alpha1.IPAddress
251270 })
252271}
253272
254- // completePreBindings accepts driver pre-bound addresses by writing the
255- // claim's UID into claimRefs that carry none yet.
256- func (r * IPAddressClaimReconciler ) completePreBindings (ctx context.Context , claim * localv1alpha1.IPAddressClaim , addrs []localv1alpha1.IPAddress ) error {
273+ // hasPendingPreBinding reports whether any address is pre-bound to the claim
274+ // by name only, without the claim's UID.
275+ func hasPendingPreBinding (addrs []localv1alpha1.IPAddress ) bool {
276+ for _ , addr := range addrs {
277+ if addr .Spec .ClaimRef .UID == "" {
278+ return true
279+ }
280+ }
281+ return false
282+ }
283+
284+ // acceptPreBindings completes pre-bindings — addresses whose claimRef names
285+ // the claim but carries no UID yet — by writing the claim's UID, as the PV
286+ // controller does for a pre-bound volume that satisfies its claim. A
287+ // pre-binding of another class, of a family the claim does not request, or
288+ // of a family the claim already holds is refused with an event and left
289+ // out of the returned list. Without a resolved class nothing is accepted.
290+ func (r * IPAddressClaimReconciler ) acceptPreBindings (ctx context.Context , claim * localv1alpha1.IPAddressClaim , className string , addrs []localv1alpha1.IPAddress ) ([]localv1alpha1.IPAddress , error ) {
291+ accepted := make ([]localv1alpha1.IPAddress , 0 , len (addrs ))
292+ for _ , addr := range addrs {
293+ if addr .Spec .ClaimRef .UID != "" {
294+ accepted = append (accepted , addr )
295+ }
296+ }
257297 for i := range addrs {
258298 addr := & addrs [i ]
259- if addr .Spec .ClaimRef .UID == "" {
260- addr .Spec .ClaimRef .UID = claim .UID
261- if err := r .Update (ctx , addr ); err != nil {
262- return err
263- }
299+ if addr .Spec .ClaimRef .UID != "" || className == "" {
300+ continue
264301 }
302+ if reason := preBindingMismatch (claim , className , accepted , addr ); reason != "" {
303+ r .Recorder .Eventf (claim , "Warning" , "PreBindingRefused" ,
304+ "IPAddress %s is pre-bound to this claim but %s" , addr .Name , reason )
305+ continue
306+ }
307+ addr .Spec .ClaimRef .UID = claim .UID
308+ if err := r .Update (ctx , addr ); err != nil {
309+ return nil , err
310+ }
311+ accepted = append (accepted , * addr )
265312 }
266- return nil
313+ return accepted , nil
314+ }
315+
316+ // preBindingMismatch reports why a pre-bound address cannot bind to the
317+ // claim, or "" if it can. held lists the addresses the claim already holds.
318+ func preBindingMismatch (claim * localv1alpha1.IPAddressClaim , className string , held []localv1alpha1.IPAddress , addr * localv1alpha1.IPAddress ) string {
319+ family := familyOf (addr .Spec .Address )
320+ switch {
321+ case className == "" :
322+ return "the claim's class is not resolved"
323+ case addr .Spec .ClassName != className :
324+ return fmt .Sprintf ("its class %q is not the claim's class %q" , addr .Spec .ClassName , className )
325+ case ! slices .Contains (requestedFamilies (claim ), family ):
326+ return fmt .Sprintf ("the claim does not request family %q" , family )
327+ }
328+ for _ , h := range held {
329+ if familyOf (h .Spec .Address ) == family {
330+ return fmt .Sprintf ("the claim already holds %s address %s" , family , h .Name )
331+ }
332+ }
333+ return ""
267334}
268335
269336// addressesBoundTo lists live addresses whose claimRef names this claim and
@@ -302,6 +369,21 @@ func requestedFamilies(claim *localv1alpha1.IPAddressClaim) []localv1alpha1.Addr
302369 }
303370}
304371
372+ // unboundFamilies reports which requested families no bound address covers,
373+ // in any phase. A Lost address still covers its family: the claim keeps it
374+ // and waits for it to be restored or deleted rather than binding another.
375+ func unboundFamilies (claim * localv1alpha1.IPAddressClaim , addrs []localv1alpha1.IPAddress ) []localv1alpha1.AddressFamily {
376+ var unbound []localv1alpha1.AddressFamily
377+ for _ , family := range requestedFamilies (claim ) {
378+ if ! slices .ContainsFunc (addrs , func (addr localv1alpha1.IPAddress ) bool {
379+ return familyOf (addr .Spec .Address ) == family
380+ }) {
381+ unbound = append (unbound , family )
382+ }
383+ }
384+ return unbound
385+ }
386+
305387// missingFamilies reports which requested families no bound address
306388// satisfies. An address in phase Lost does not satisfy its family.
307389func missingFamilies (claim * localv1alpha1.IPAddressClaim , addrs []localv1alpha1.IPAddress ) []localv1alpha1.AddressFamily {
0 commit comments