From 72845d92ce9b46e2ee7ef27a7c8dac1f0dea5e26 Mon Sep 17 00:00:00 2001 From: Scot Wells Date: Wed, 25 Nov 2026 09:48:41 -0600 Subject: [PATCH 1/4] feat(quota): Hold each Machine's quota claim until release Create the Milo quota claim with Fleet's quota custody finalizer. Milo keeps counting a deleting claim while a finalizer holds it, so a claim deleted by another actor or by namespace purge keeps its quota until Fleet releases it. Release deletes the claim, then removes the finalizer with a conditional update, and reports the quota free only once the claim is gone. Acquire restores a finalizer removed from a live claim. Co-Authored-By: Claude Opus 5.5 --- docs/development/README.md | 9 ++++++ internal/milo/quota.go | 38 +++++++++++++++++++---- internal/milo/quota_test.go | 61 +++++++++++++++++++++++++++++++++++-- 3 files changed, 99 insertions(+), 9 deletions(-) diff --git a/docs/development/README.md b/docs/development/README.md index 4592604b..e51b5c77 100644 --- a/docs/development/README.md +++ b/docs/development/README.md @@ -65,6 +65,15 @@ real API server verify it. No handler reads the creator yet, and no Machine without one is refused. See the [production admission proposal](https://github.com/datum-cloud/fleet/pull/187). +The Milo quota claim is created with Fleet's quota custody finalizer, so a +claim deleted by anyone else, or by namespace purge, keeps its quota until +release. Release deletes the claim, removes the finalizer with a conditional +update, and reports the quota free only once the claim is gone. A finalizer +removed from a live claim is restored on the next acquire. No mode wires the +quota claim into allocation yet; synthetic staging still reserves no quota. +See the proposed [quota +custody](../enhancements/production-admission/README.md#quota-custody). + Production Kustomize roots now package the implemented manager, admission, API, Milo IAM, service, and quota boundaries; packaging is not runtime readiness. diff --git a/internal/milo/quota.go b/internal/milo/quota.go index 0f0235df..2da6a6bf 100644 --- a/internal/milo/quota.go +++ b/internal/milo/quota.go @@ -6,6 +6,7 @@ import ( "encoding/hex" "errors" "fmt" + "slices" quotav1alpha1 "go.miloapis.com/milo/pkg/apis/quota/v1alpha1" apierrors "k8s.io/apimachinery/pkg/api/errors" @@ -20,6 +21,12 @@ import ( var _ allocation.Quota = Quota{} +// QuotaFinalizer holds a Machine's quota claim until Fleet releases it. +// Milo keeps counting a deleting claim while a finalizer holds it, so a +// claim deleted by anyone else, or by namespace purge, keeps its quota until +// Fleet removes this finalizer. Its removal is the release acknowledgment. +const QuotaFinalizer = "fleet.datumapis.com/quota-custody" + // Quota reserves one Machine's quota as a Milo ResourceClaim in the Machine's // namespace. The claim is named from the Machine UID within its consumer // project, as its allocation is, so a retried create collides. Only a Milo @@ -37,7 +44,7 @@ func ClaimName(machine allocation.Machine) string { func desired(machine allocation.Machine) *quotav1alpha1.ResourceClaim { return "av1alpha1.ResourceClaim{ - ObjectMeta: metav1.ObjectMeta{Name: ClaimName(machine), Namespace: machine.Namespace, Labels: map[string]string{ + ObjectMeta: metav1.ObjectMeta{Name: ClaimName(machine), Namespace: machine.Namespace, Finalizers: []string{QuotaFinalizer}, Labels: map[string]string{ "fleet.datumapis.com/quota-claim": "machine", fleetv1.LabelMachineUID: string(machine.UID), fleetv1.LabelConsumerUID: string(machine.ConsumerUID), }}, Spec: quotav1alpha1.ResourceClaimSpec{ @@ -77,6 +84,14 @@ func (q Quota) Acquire(ctx context.Context, machine allocation.Machine) (string, if claim.DeletionTimestamp != nil { return "", ErrQuotaUnknown } + // Someone removed Fleet's finalizer from a live claim. Restore the hold + // before relying on the grant; a conflict is retried. + if !slices.Contains(claim.Finalizers, QuotaFinalizer) { + claim.Finalizers = append(claim.Finalizers, QuotaFinalizer) + if err := api.Update(ctx, claim); err != nil { + return "", fmt.Errorf("%w: restore the quota finalizer: %v", ErrQuotaUnknown, err) + } + } granted := apiMeta.FindStatusCondition(claim.Status.Conditions, quotav1alpha1.ResourceClaimGranted) switch { case granted == nil || claim.Status.ObservedGeneration != claim.Generation: @@ -89,8 +104,8 @@ func (q Quota) Acquire(ctx context.Context, machine allocation.Machine) (string, return claim.Name, nil } -// Release deletes the Machine's claim. It returns nil once the claim is -// gone. +// Release deletes the Machine's claim, then removes Fleet's finalizer. It +// returns nil once the claim is gone. func (q Quota) Release(ctx context.Context, machine allocation.Machine) error { name := ClaimName(machine) api, err := q.client(ctx, machine) @@ -109,9 +124,20 @@ func (q Quota) Release(ctx context.Context, machine allocation.Machine) error { if !matches(machine, claim) { return ErrQuotaInvalid } - uid := claim.UID - if err := api.Delete(ctx, claim, client.Preconditions{UID: &uid}); err != nil && !apierrors.IsNotFound(err) { - return fmt.Errorf("%w: delete ResourceClaim: %v", ErrQuotaUnknown, err) + if claim.DeletionTimestamp == nil { + uid := claim.UID + if err := api.Delete(ctx, claim, client.Preconditions{UID: &uid}); err != nil && !apierrors.IsNotFound(err) { + return fmt.Errorf("%w: delete ResourceClaim: %v", ErrQuotaUnknown, err) + } + return fmt.Errorf("%w: the claim is being deleted", ErrQuotaPending) + } + // The claim is deleting and still counted. Removing the finalizer frees + // the quota; the update is conditional on the version read. + if index := slices.Index(claim.Finalizers, QuotaFinalizer); index >= 0 { + claim.Finalizers = slices.Delete(claim.Finalizers, index, index+1) + if err := api.Update(ctx, claim); err != nil && !apierrors.IsNotFound(err) { + return fmt.Errorf("%w: remove the quota finalizer: %v", ErrQuotaUnknown, err) + } } // Deletion is observed, not assumed: the next call sees the claim gone. return fmt.Errorf("%w: the claim is being deleted", ErrQuotaPending) diff --git a/internal/milo/quota_test.go b/internal/milo/quota_test.go index 401a52f4..692bbb5a 100644 --- a/internal/milo/quota_test.go +++ b/internal/milo/quota_test.go @@ -3,6 +3,7 @@ package milo import ( "context" "errors" + "slices" "testing" quotav1alpha1 "go.miloapis.com/milo/pkg/apis/quota/v1alpha1" @@ -82,21 +83,75 @@ func TestTheQuotaClaimIsNamedFromTheMachineAndGrantedOnce(t *testing.T) { } } -// TestReleaseIsObservedNotAssumed deletes the claim and reports it released -// only once it is gone. +// TestReleaseIsObservedNotAssumed deletes the claim, then removes Fleet's +// finalizer, and reports it released only once it is gone. func TestReleaseIsObservedNotAssumed(t *testing.T) { ctx := context.Background() api := quotaClient(t) quota := Quota{Consumers: consumers(api, machine.ConsumerUID)} _, _ = quota.Acquire(ctx, machine) if err := quota.Release(ctx, machine); !errors.Is(err, ErrQuotaPending) { - t.Fatalf("release while deleting = %v", err) + t.Fatalf("release requesting deletion = %v", err) + } + // The deleting claim is still held, and so still counted, until Fleet + // removes its finalizer. + claim := "av1alpha1.ResourceClaim{} + if err := api.Get(ctx, client.ObjectKey{Namespace: machine.Namespace, Name: ClaimName(machine)}, claim); err != nil || claim.DeletionTimestamp == nil || !slices.Contains(claim.Finalizers, QuotaFinalizer) { + t.Fatalf("claim after deletion request = %+v, %v", claim.ObjectMeta, err) + } + if err := quota.Release(ctx, machine); !errors.Is(err, ErrQuotaPending) { + t.Fatalf("release removing the finalizer = %v", err) } if err := quota.Release(ctx, machine); err != nil { t.Fatalf("release after deletion = %v", err) } } +// TestTheClaimIsHeldUntilFleetReleasesIt keeps the quota of a claim that +// someone else deletes, and restores a finalizer removed from a live claim. +func TestTheClaimIsHeldUntilFleetReleasesIt(t *testing.T) { + ctx := context.Background() + api := quotaClient(t) + quota := Quota{Consumers: consumers(api, machine.ConsumerUID)} + _, _ = quota.Acquire(ctx, machine) + key := client.ObjectKey{Namespace: machine.Namespace, Name: ClaimName(machine)} + claim := "av1alpha1.ResourceClaim{} + if err := api.Get(ctx, key, claim); err != nil || !slices.Contains(claim.Finalizers, QuotaFinalizer) { + t.Fatalf("created claim = %+v, %v", claim.ObjectMeta, err) + } + + // A finalizer removed from a live claim is restored on the next acquire. + claim.Finalizers = nil + if err := api.Update(ctx, claim); err != nil { + t.Fatal(err) + } + if _, err := quota.Acquire(ctx, machine); !errors.Is(err, ErrQuotaPending) { + t.Fatalf("acquire restoring the finalizer = %v", err) + } + if err := api.Get(ctx, key, claim); err != nil || !slices.Contains(claim.Finalizers, QuotaFinalizer) { + t.Fatalf("restored claim = %+v, %v", claim.ObjectMeta, err) + } + + // A delete by someone else, such as a namespace purge, leaves the claim + // held and counted. Acquire reports it unknown rather than granted. + if err := api.Delete(ctx, claim); err != nil { + t.Fatal(err) + } + if err := api.Get(ctx, key, claim); err != nil || claim.DeletionTimestamp == nil { + t.Fatalf("claim deleted by another actor = %+v, %v", claim.ObjectMeta, err) + } + if _, err := quota.Acquire(ctx, machine); !errors.Is(err, ErrQuotaUnknown) { + t.Fatalf("acquire of a deleting claim = %v", err) + } + // Release finishes what the other actor started. + if err := quota.Release(ctx, machine); !errors.Is(err, ErrQuotaPending) { + t.Fatalf("release of a deleting claim = %v", err) + } + if err := quota.Release(ctx, machine); err != nil { + t.Fatalf("release after the finalizer = %v", err) + } +} + // TestQuotaServesOnlyTheMachinesProjectIncarnation never claims quota in a // project recreated under the Machine's project name, or for a consumer that // is not a Milo Project, and finds nothing left to release in a recreated From be0355abcfc18067733dec8af6c8111c4796b596 Mon Sep 17 00:00:00 2001 From: Scot Wells Date: Wed, 25 Nov 2026 09:50:37 -0600 Subject: [PATCH 2/4] test(quota): Keep a claim another finalizer holds Release removes only Fleet's finalizer and stays pending while another actor still holds the claim, because Milo still counts it. Co-Authored-By: Claude Opus 5.5 --- internal/milo/quota_test.go | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/internal/milo/quota_test.go b/internal/milo/quota_test.go index 692bbb5a..af875af6 100644 --- a/internal/milo/quota_test.go +++ b/internal/milo/quota_test.go @@ -176,3 +176,37 @@ func TestQuotaServesOnlyTheMachinesProjectIncarnation(t *testing.T) { t.Fatalf("claims = %+v, %v", claims.Items, err) } } + +// TestAnotherFinalizerKeepsTheClaimCounted removes only Fleet's finalizer. +// A claim another actor still holds exists, so Milo still counts it, and +// release stays pending until that actor lets go. +func TestAnotherFinalizerKeepsTheClaimCounted(t *testing.T) { + ctx := context.Background() + api := quotaClient(t) + quota := Quota{Consumers: consumers(api, machine.ConsumerUID)} + _, _ = quota.Acquire(ctx, machine) + key := client.ObjectKey{Namespace: machine.Namespace, Name: ClaimName(machine)} + claim := "av1alpha1.ResourceClaim{} + if err := api.Get(ctx, key, claim); err != nil { + t.Fatal(err) + } + claim.Finalizers = append(claim.Finalizers, "example.com/other") + if err := api.Update(ctx, claim); err != nil { + t.Fatal(err) + } + for range 3 { + if err := quota.Release(ctx, machine); !errors.Is(err, ErrQuotaPending) { + t.Fatalf("release while another finalizer holds the claim = %v", err) + } + } + if err := api.Get(ctx, key, claim); err != nil || !slices.Equal(claim.Finalizers, []string{"example.com/other"}) { + t.Fatalf("claim = %+v, %v", claim.ObjectMeta, err) + } + claim.Finalizers = nil + if err := api.Update(ctx, claim); err != nil { + t.Fatal(err) + } + if err := quota.Release(ctx, machine); err != nil { + t.Fatalf("release after the other finalizer = %v", err) + } +} From 6671309208afb800d4e45f7a7a40b0bbaf4dee85 Mon Sep 17 00:00:00 2001 From: Scot Wells Date: Wed, 25 Nov 2026 16:07:39 -0600 Subject: [PATCH 3/4] test(quota): Keep a recreated Machine from inheriting its claim Recreate a Machine with the same project, namespace and name. It gets its own claim and cannot acquire or release the predecessor's granted claim, because the claim name hashes the Machine UID and the machine-uid label must match. The test fails when either guard is removed. Co-Authored-By: Claude Opus 5.5 --- internal/milo/quota_test.go | 50 +++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/internal/milo/quota_test.go b/internal/milo/quota_test.go index af875af6..baee6e40 100644 --- a/internal/milo/quota_test.go +++ b/internal/milo/quota_test.go @@ -210,3 +210,53 @@ func TestAnotherFinalizerKeepsTheClaimCounted(t *testing.T) { t.Fatalf("release after the other finalizer = %v", err) } } + +// TestARecreatedMachineGetsItsOwnClaim recreates a Machine with the same +// consumer, namespace and name. The claim name hashes the Machine UID and the +// claim carries it as a label, so the new Machine neither finds nor releases +// the old Machine's granted claim. +func TestARecreatedMachineGetsItsOwnClaim(t *testing.T) { + ctx := context.Background() + api := quotaClient(t) + quota := Quota{Consumers: consumers(api, machine.ConsumerUID)} + _, _ = quota.Acquire(ctx, machine) + old := "av1alpha1.ResourceClaim{} + if err := api.Get(ctx, client.ObjectKey{Namespace: machine.Namespace, Name: ClaimName(machine)}, old); err != nil { + t.Fatal(err) + } + old.Status.ObservedGeneration = old.Generation + old.Status.Conditions = []metav1.Condition{{Type: quotav1alpha1.ResourceClaimGranted, Status: metav1.ConditionTrue}} + if err := api.Update(ctx, old); err != nil { + t.Fatal(err) + } + + recreated := machine + recreated.UID = "00000000-0000-4000-8000-000000000602" + if ClaimName(recreated) == ClaimName(machine) { + t.Fatal("a recreated Machine shares its predecessor's claim name") + } + if _, err := quota.Acquire(ctx, recreated); !errors.Is(err, ErrQuotaPending) { + t.Fatalf("recreated acquire = %v; the predecessor's grant must not carry over", err) + } + // Releasing the new Machine leaves the predecessor's claim held. + for range 3 { + _ = quota.Release(ctx, recreated) + } + if err := api.Get(ctx, client.ObjectKeyFromObject(old), old); err != nil || old.DeletionTimestamp != nil { + t.Fatalf("predecessor claim after the new Machine's release = %+v, %v", old.ObjectMeta, err) + } + + // A claim at the new Machine's name that records another Machine is not + // the new Machine's claim: acquire and release both refuse it. + forged := desired(recreated) + forged.Labels[fleetv1.LabelMachineUID] = string(machine.UID) + if err := api.Create(ctx, forged); err != nil { + t.Fatal(err) + } + if _, err := quota.Acquire(ctx, recreated); !errors.Is(err, ErrQuotaInvalid) { + t.Fatalf("acquire of a claim recording another Machine = %v", err) + } + if err := quota.Release(ctx, recreated); !errors.Is(err, ErrQuotaInvalid) { + t.Fatalf("release of a claim recording another Machine = %v", err) + } +} From 7aad3d324396494fef64221c704396191af1d840 Mon Sep 17 00:00:00 2001 From: Scot Wells Date: Wed, 25 Nov 2026 18:51:27 -0600 Subject: [PATCH 4/4] docs(quota): Describe the custody finalizer in the quota section State the finalizer, its release order and the acknowledgment once, in the quota claim section, and drop the overview paragraph that repeated it. Say that Milo still supplies neither a custody anchor nor an acknowledgment and does not protect Fleet's finalizer. Co-Authored-By: Claude Opus 5.5 --- docs/development/README.md | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/docs/development/README.md b/docs/development/README.md index e51b5c77..477f4efd 100644 --- a/docs/development/README.md +++ b/docs/development/README.md @@ -65,15 +65,6 @@ real API server verify it. No handler reads the creator yet, and no Machine without one is refused. See the [production admission proposal](https://github.com/datum-cloud/fleet/pull/187). -The Milo quota claim is created with Fleet's quota custody finalizer, so a -claim deleted by anyone else, or by namespace purge, keeps its quota until -release. Release deletes the claim, removes the finalizer with a conditional -update, and reports the quota free only once the claim is gone. A finalizer -removed from a live claim is restored on the next acquire. No mode wires the -quota claim into allocation yet; synthetic staging still reserves no quota. -See the proposed [quota -custody](../enhancements/production-admission/README.md#quota-custody). - Production Kustomize roots now package the implemented manager, admission, API, Milo IAM, service, and quota boundaries; packaging is not runtime readiness. @@ -632,12 +623,19 @@ SHA-256 of the consumer project UID and the Machine UID, in the Machine's namespace, before it creates the MachineAllocation. A retried create finds the claim by name. Only a granted claim reserves quota, so a pending claim returns `ErrQuotaPending` and the allocator retries. A claim that is not the claim Fleet -would create for that Machine is `ErrQuotaInvalid`. Release deletes the claim and -reports it released only once it is gone (`TestReleaseIsObservedNotAssumed`). - -Milo v0.32.5 supplies ResourceClaim evidence but no service-owned quota custody -anchor or explicit release acknowledgment, so deletion is observed, not -acknowledged. +would create for that Machine is `ErrQuotaInvalid`. + +Fleet holds each claim with its quota custody finalizer from the moment it +creates it, so a claim deleted by anyone else, or by namespace purge, keeps its +quota until release. Milo still counts a deleting claim while a finalizer +holds it. Release deletes the claim, removes the finalizer with a conditional +update, and reports the quota free only once the claim is gone +(`TestReleaseIsObservedNotAssumed`). Removing the finalizer is the release +acknowledgment. A finalizer removed from a live claim is restored on the next +acquire. Milo itself supplies neither a custody anchor nor an acknowledgment, +and it lets anyone who can update the claim remove the finalizer. See the +[production admission +proposal](https://github.com/datum-cloud/fleet/pull/187). ## Simulator site @@ -793,7 +791,8 @@ acceptance inventory. authorization. The network adapter remains deliberately unwired until production location, authorization, and object-guard owners are available. - Milo v0.32.5 supplies ResourceClaim evidence but no service-owned quota custody - anchor or explicit release acknowledgment. It also does not supply Fleet's + anchor or explicit release acknowledgment; Fleet's own finalizer stands in + for both and is not protected. It also does not supply Fleet's caller-preserving source/provenance receipt, atomic invalidation-versus-bind fence, consumer-project deletion recovery, or provider-project purge guard. Production cross-project allocation therefore fails closed, and the quota claim