Skip to content

Commit db6042e

Browse files
committed
Do not resolve or auto-claim into a deleted IPAddressClass
The class-protection finalizer keeps a deleted class until its addresses are gone, and while it waited, default-class resolution and auto-claim still chose it, minting addresses that kept it from draining. Both now skip a class that is being deleted. Assisted-By: LLM Signed-off-by: Timofei Larkin <[email protected]>
1 parent 80f930d commit db6042e

4 files changed

Lines changed: 39 additions & 0 deletions

File tree

‎internal/controller/ipaddressclaim_controller.go‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,9 @@ func (r *IPAddressClaimReconciler) defaultClassName(ctx context.Context, claim *
236236
if err := r.List(ctx, classes); err != nil {
237237
return "", err
238238
}
239+
// A deleted class is held only until its addresses are gone; drawing
240+
// new claims into it would keep it from draining.
241+
classes.Items = slices.DeleteFunc(classes.Items, classDeleting)
239242
var defaults []string
240243
for _, c := range classes.Items {
241244
if c.Annotations[localv1alpha1.IsDefaultClassAnnotation] == "true" {

‎internal/controller/ipaddressclass_controller.go‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,11 @@ func (r *IPAddressClassReconciler) Reconcile(ctx context.Context, req ctrl.Reque
8181
return ctrl.Result{}, r.Update(ctx, class)
8282
}
8383

84+
// classDeleting reports whether a class is being deleted.
85+
func classDeleting(c localv1alpha1.IPAddressClass) bool {
86+
return !c.DeletionTimestamp.IsZero()
87+
}
88+
8489
// classForAddress maps an IPAddress event to the address's class.
8590
func (r *IPAddressClassReconciler) classForAddress(_ context.Context, o client.Object) []reconcile.Request {
8691
addr := o.(*localv1alpha1.IPAddress)

‎internal/controller/ipaddressclass_controller_test.go‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,31 @@ func TestDeletedClassIsReleasedWithoutAddresses(t *testing.T) {
9393
t.Errorf("class still present with no addresses of its own: %v", err)
9494
}
9595
}
96+
97+
func TestDeletedClassIsNotChosenByDefault(t *testing.T) {
98+
class := defaultClass()
99+
class.Finalizers = []string{localv1alpha1.ClassProtectionFinalizer}
100+
c := testClient(t, class, pendingClaim(""))
101+
if err := c.Delete(context.Background(), class); err != nil {
102+
t.Fatal(err)
103+
}
104+
reconcileClaim(t, claimReconciler(c))
105+
106+
if got := getClaim(t, c); got.Status.ClassName != "" {
107+
t.Errorf("claim resolved to the deleted default class %q", got.Status.ClassName)
108+
}
109+
}
110+
111+
func TestDeletedClassIsNotChosenForAutoClaim(t *testing.T) {
112+
class := autoClaimClass(testClassName)
113+
class.Finalizers = []string{localv1alpha1.ClassProtectionFinalizer}
114+
c := testClient(t, class, loadBalancerService())
115+
if err := c.Delete(context.Background(), class); err != nil {
116+
t.Fatal(err)
117+
}
118+
reconcileService(t, serviceReconciler(c))
119+
120+
if claim := getAutoClaim(t, c); claim != nil {
121+
t.Errorf("claim auto-provisioned from the deleted class %q", claim.Spec.ClassName)
122+
}
123+
}

‎internal/controller/service_controller.go‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,9 @@ func (r *ServiceReconciler) autoClaimClassName(ctx context.Context, svc *corev1.
159159
if err := r.List(ctx, classes); err != nil {
160160
return "", err
161161
}
162+
// A deleted class is held only until its addresses are gone; drawing
163+
// new claims into it would keep it from draining.
164+
classes.Items = slices.DeleteFunc(classes.Items, classDeleting)
162165
var marked []string
163166
for _, c := range classes.Items {
164167
if c.Annotations[localv1alpha1.IsAutoClaimClassAnnotation] == "true" {

0 commit comments

Comments
 (0)