Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions docs/development/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -623,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

Expand Down Expand Up @@ -784,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
Expand Down
38 changes: 32 additions & 6 deletions internal/milo/quota.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand All @@ -37,7 +44,7 @@ func ClaimName(machine allocation.Machine) string {

func desired(machine allocation.Machine) *quotav1alpha1.ResourceClaim {
return &quotav1alpha1.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{
Expand Down Expand Up @@ -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:
Expand All @@ -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)
Expand All @@ -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)
Expand Down
145 changes: 142 additions & 3 deletions internal/milo/quota_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package milo
import (
"context"
"errors"
"slices"
"testing"

quotav1alpha1 "go.miloapis.com/milo/pkg/apis/quota/v1alpha1"
Expand Down Expand Up @@ -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 := &quotav1alpha1.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 := &quotav1alpha1.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
Expand All @@ -121,3 +176,87 @@ 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 := &quotav1alpha1.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)
}
}

// 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 := &quotav1alpha1.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)
}
}
Loading