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
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,50 @@ spec:
minLength: 1
maxLength: 63
x-kubernetes-list-type: set
disposedDrives:
type: array
maxItems: 16
x-kubernetes-list-type: map
x-kubernetes-list-map-keys:
- name
items:
type: object
properties:
name:
type: string
pattern: ^disk-[0-9]+$
maxLength: 12
serialNumber:
type: string
maxLength: 253
method:
type: string
enum:
- Destroyed
- Removed
reference:
type: string
minLength: 1
maxLength: 256
recovery:
type: string
minLength: 1
maxLength: 253
required:
- name
- serialNumber
- method
- reference
- recovery
required:
- type
x-kubernetes-validations:
- rule: (self.type == 'Allocation' || self.type == 'Release') == has(self.allocationRef)
message: allocationRef is set exactly for Allocation and Release
- rule: self.type == 'Release' || !has(self.openGates) || size(self.openGates) == 0
message: only Release has open gates
- rule: self.type == 'Release' || !has(self.disposedDrives) || size(self.disposedDrives) == 0
message: only Release records disposed drives
fence:
type: integer
format: int64
Expand Down Expand Up @@ -322,6 +359,31 @@ spec:
retryName:
type: string
maxLength: 253
drives:
type: array
maxItems: 16
x-kubernetes-list-type: map
x-kubernetes-list-map-keys:
- name
items:
type: object
properties:
name:
type: string
pattern: ^disk-[0-9]+$
maxLength: 12
serialNumber:
type: string
maxLength: 253
state:
type: string
enum:
- Missing
- Uncovered
required:
- name
- serialNumber
- state
waiting:
type: object
properties:
Expand Down Expand Up @@ -1365,6 +1427,7 @@ spec:
- Retry
- Retire
- Resolve
- DisposeDrive
hold:
type: object
properties:
Expand Down Expand Up @@ -1418,6 +1481,26 @@ spec:
required:
- method
- reference
drive:
type: object
properties:
name:
type: string
pattern: ^disk-[0-9]+$
maxLength: 12
method:
type: string
enum:
- Destroyed
- Removed
reference:
type: string
minLength: 1
maxLength: 256
required:
- name
- method
- reference
resolve:
type: object
properties:
Expand Down Expand Up @@ -1451,9 +1534,11 @@ spec:
- rule: (self.decision == 'Resolve') == has(self.resolve)
message: resolve is set exactly for Resolve
- rule: self.decision == 'Resolve' || has(self.hold)
message: Settle, Retry and Retire name the hold
message: Settle, Retry, Retire and DisposeDrive name the hold
- rule: '!has(self.diskDisposal) || self.decision == ''Retire'''
message: only Retire records disk disposal
- rule: (self.decision == 'DisposeDrive') == has(self.drive)
message: drive is set exactly for DisposeDrive
status:
type: object
properties:
Expand Down
45 changes: 38 additions & 7 deletions docs/development/device-custody.md
Original file line number Diff line number Diff line change
Expand Up @@ -797,6 +797,8 @@ guarded, and a test fails if its guard is removed:
| Device | Custody's `device-uid` label must match the Device, or custody stays `Unverified` until a `Resolve` | `TestARecreatedDeviceLeavesCustodyUnverified` |
| Device, against free capacity | Custody for another Device incarnation is never free | `TestCustodyForAnotherDeviceIncarnationIsNotFree` |
| Device, against a release wipe | The release controller plans a wipe only if the Device's UID is the allocation's `device-uid` label | `TestAWipeIsNeverPlannedForARecreatedDevice` |
| Wipe, against a drive disposal | `DisposeDrive` reads the held drives custody recorded with the hold, never the wipe object | `TestWipeAdmissionChecksThePlan` (a wipe recreated under the held wipe's name) |
| Wipe, after rebuild | A rebuilt hold records no drives, so `DisposeDrive` is refused | `TestARebuiltHoldCannotBeDisposed` |
| Device, against a claim | `Claim` takes the Device incarnation the allocation names, and refuses unless both the live Device and custody's label match it | `TestCustodyForAnotherDeviceIncarnationIsNeverClaimed`, `TestARecreatedDeviceIsNeverClaimedUnderItsStaleLabel`, `TestARecreatedDeviceIsNeverClaimedForTheEarlierIncarnation` |
| Device, against an operation | An operation's `device-uid` label must match the Device at admission | `TestAdmissionPreconditions` |
| Device, against an existing operation | The Metal provider and release controller require an existing same-name operation to carry the same labels | `TestAnOperationForAnotherDeviceIncarnationIsAConflict` |
Expand Down Expand Up @@ -871,12 +873,41 @@ lab's targets each have one emulated NVMe drive.
the release's three failed steps, but a release stops after 16 steps. A
retried create that finds a wipe with another plan keeps it, because
admission plans it again.
- **Missing and uncovered drives (interim).** While a planned drive is
`Missing` or `Uncovered`, the release controller creates no wipe. It sets
the allocation's `Cleaning` condition to `False` with reason `DriveMissing`
or `DriveUncovered`, and plans again when discovery changes. Admission also
refuses such a wipe. #183 instead ends it `OutcomeUnknown` under a recovery
hold that `DisposeDrive` settles; that comes with `DisposeDrive`.
- **Missing and uncovered drives.** While a planned drive is `Missing` or
`Uncovered`, the release controller sets the allocation's `Cleaning`
condition to `False` with reason `DriveMissing` or `DriveUncovered`, and
waits 10 minutes (the allocator's `CleaningGrace`) for discovery to report
the drive again. A brief gap in discovery therefore spends no wipe step and
pages nobody. After the grace it creates the wipe. Admission checks its
plan, then admits it and ends it `OutcomeUnknown` with that reason in the
same custody update (a kind's `HoldOnAdmit`), so the fence rises, nothing is
delivered, and the server is under a recovery hold. If that wipe was
settling an installation hold, the drive hold replaces it: every drive the
installation could have written is then wiped by a later step or recorded
as disposed of, which is what settling it requires. A rebuild holds the
installation again, which fails closed. Each blocked plan gets its own
grace: settling a drive hold restarts it.
- **DisposeDrive.** A `DeviceRecovery` with decision `DisposeDrive` names the
hold, the drive, and how it was destroyed or removed, with a reference. It
is admitted only from a `Release` holder under that hold, for a drive the
held wipe planned as `Missing` or `Uncovered` and that discovery no longer
reports, so an uncovered drive is pulled first. When it forms the hold, the
coordinator records in custody, with the hold, each drive the wipe could
not clean, by name, serial number and state. `DisposeDrive` reads only that
record, never the wipe object, so a wipe recreated under the same name,
which never matches custody, cannot authorize a disposal. Discovery proves only that
no drive reports the planned serial number; for a drive whose serial
changed, the recorded method and reference are the proof. Retry is refused for this
hold, because a retried wipe would plan the same drive. Like every
recovery, it applies once the sites report its fence. It clears the hold
and records the drive in custody's `holder.disposedDrives`; the planner
omits it only while its serial number remains absent. If discovery reports
the serial again, the planner marks it `Uncovered` and the normal drive
hold applies after the grace period. If every pinned drive was disposed
of and discovery reports no drives, no wipe remains and the `Cleaning` gate
closes. The record ends when the holder leaves `Release`; rebuilt custody
does not have it, so staff record the disposal again. The recovery operator
role, which creates `DeviceRecovery`, can record it.
- **Sites.** Every site cleans only by overwriting each planned drive and
reading it back, and refuses any other plan before sending anything. Sites
match planned drives to their enrolled disks by name, and fixture drives'
Expand All @@ -885,7 +916,7 @@ lab's targets each have one emulated NVMe drive.

Not implemented yet: drive inventory from commissioning, the in-band methods
other than overwrite and their evidence, the evidence check and its reasons,
the plan-derived deadline, and `DisposeDrive`.
and the plan-derived deadline.

### Cutover decisions

Expand Down
6 changes: 6 additions & 0 deletions internal/allocation/allocator.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"slices"
"sort"
"strings"
"time"

apierrors "k8s.io/apimachinery/pkg/api/errors"
apiMeta "k8s.io/apimachinery/pkg/api/meta"
Expand Down Expand Up @@ -148,6 +149,11 @@ type Allocator struct {
// in it too.
Namespace string
Quota Quota
// CleaningGrace is how long release waits for a drive it cannot clean
// before it holds the server for staff. Nil means DefaultCleaningGrace.
CleaningGrace *time.Duration
// Now is the clock; nil means time.Now.
Now func() time.Time
// Capacity decides which capacity may be offered.
Capacity Offers
// BootInterface and TargetDisks are pinned on each allocation.
Expand Down
155 changes: 155 additions & 0 deletions internal/allocation/cleaning_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package allocation

import (
"errors"
"slices"
"strings"
"testing"
"time"

apierrors "k8s.io/apimachinery/pkg/api/errors"
apiMeta "k8s.io/apimachinery/pkg/api/meta"
Expand Down Expand Up @@ -313,3 +315,156 @@ func TestAWipeIsNeverPlannedForARecreatedDevice(t *testing.T) {
t.Fatalf("no wipe may be created for a recreated Device: %v", err)
}
}

// TestAPulledDriveIsDisposedAndReleaseFinishes holds the server once a pinned
// drive stays missing past the grace, and finishes release after staff record
// the drive's disposal.
func TestAPulledDriveIsDisposedAndReleaseFinishes(t *testing.T) {
testDriveDisposal(t, false)
}

func TestReturnedDisposedDriveHoldsReleaseDespiteRetryLineage(t *testing.T) {
testDriveDisposal(t, true)
}

func testDriveDisposal(t *testing.T, returns bool) {
t.Helper()
e := startEnv(t)
device := e.server(1)
allocation, err := e.allocator.Allocate(e.ctx, e.request(1))
if err != nil {
t.Fatal(err)
}
zero := time.Duration(0)
e.allocator.CleaningGrace = &zero
pulled := e.device(device.Name)
original := pulled.Status.Discovery.Inventory.Systems[0].Storage.Drives
pulled.Status.Discovery.Inventory.Systems[0].Storage.Drives = nil
if err := e.admin.Status().Update(e.ctx, pulled); err != nil {
t.Fatal(err)
}
var hold *ops.RecoveryHold
e.until("the release holds the server for the missing drive", func() bool {
if err := e.admin.Get(e.ctx, client.ObjectKeyFromObject(allocation), allocation); err != nil {
t.Fatal(err)
}
if _, err := e.allocator.Release(e.ctx, allocation, machine(1)); err != nil && !errors.Is(err, ErrCleaningBlocked) {
t.Logf("release: %v", err)
}
hold = e.custody(device.Name).Spec.RecoveryHold
return hold != nil && hold.Reason == custody.ReasonDriveMissing
})
staff := ops.Creator{Username: "staff-example@example.com", UID: uid(803)}
recovery := &ops.DeviceRecovery{ObjectMeta: metav1.ObjectMeta{GenerateName: "dispose-", Namespace: namespace, Labels: map[string]string{ops.LabelDeviceUID: string(device.UID)}}, Spec: ops.DeviceRecoverySpec{
OperationEnvelope: ops.OperationEnvelope{Creator: staff, DeviceRef: ops.DeviceReference{Name: device.Name},
Authority: ops.Authority{ServiceRequest: &ops.ServiceRequest{Reason: "drive pulled during the allocation"}}},
Decision: ops.DecisionDisposeDrive,
Hold: &ops.HeldOperationReference{Kind: hold.Kind, Name: hold.Name, Fence: hold.Fence},
Evidence: ops.RecoveryEvidence{ObservedAt: metav1.Now(), Summary: "drive destroyed after removal"},
Drive: &ops.DriveDisposal{Name: "disk-0", Method: ops.DisposalDestroyed, Reference: "ticket-example-1"},
}}
e.create(recovery)
if returns {
e.until("disposal recorded before the next release pass", func() bool {
state := e.custody(device.Name)
return state.Spec.RecoveryHold == nil && len(state.Spec.Holder.DisposedDrives) == 1
})
current := e.device(device.Name)
current.Status.Discovery.Inventory.Systems[0].Storage.Drives = original
if err := e.admin.Status().Update(e.ctx, current); err != nil {
t.Fatal(err)
}
e.until("returned serial produces an uncovered drive hold", func() bool {
if err := e.admin.Get(e.ctx, client.ObjectKeyFromObject(allocation), allocation); err != nil {
t.Fatal(err)
}
if done, err := e.allocator.Release(e.ctx, allocation, machine(1)); done || (err != nil && !errors.Is(err, ErrCleaningBlocked)) {
t.Fatalf("release with returned drive = %v, %v", done, err)
}
newHold := e.custody(device.Name).Spec.RecoveryHold
return newHold != nil && newHold.Reason == custody.ReasonDriveUncovered
})
state := e.custody(device.Name)
if state.Spec.Holder.Type != ops.HolderRelease || !slices.Contains(state.Spec.Holder.OpenGates, ops.GateCleaning) ||
!slices.Contains(state.Spec.RecoveryHold.Drives, ops.HeldDrive{Name: "disk-0", SerialNumber: original[0].SerialNumber, State: ops.DriveUncovered}) {
t.Fatalf("wrong custody for returned drive: %+v", state.Spec)
}
state.Spec.RecoveryHold.RetryName = hold.Name
before := *allocation.Status.Wipe
if done, err := e.allocator.clean(e.ctx, state, allocation); done || err != nil || *allocation.Status.Wipe != before {
t.Fatalf("retry lineage advanced held wipe: done=%v err=%v wipe=%+v, want %+v", done, err, allocation.Status.Wipe, before)
}
return
}
e.release(allocation, machine(1))
state := e.custody(device.Name)
if state.Spec.Holder.Type != ops.HolderNone || len(state.Spec.Holder.DisposedDrives) != 0 {
t.Fatalf("after release: holder %+v", state.Spec.Holder)
}
}

// TestEachBlockedDriveGetsItsOwnGrace waits the full grace for a second drive
// that cannot be cleaned after staff disposed of the first, instead of
// holding the server at once.
func TestEachBlockedDriveGetsItsOwnGrace(t *testing.T) {
e := startEnv(t)
e.disks = []string{"disk-0", "disk-1"}
device := e.server(1)
allocation, err := e.allocator.Allocate(e.ctx, e.request(1))
if err != nil {
t.Fatal(err)
}
now := time.Now()
e.allocator.Now = func() time.Time { return now }
all := e.device(device.Name).Status.Discovery.Inventory.Systems[0].Storage.Drives
setDrives := func(drives ...inventory.ObservedDrive) {
t.Helper()
current := e.device(device.Name)
current.Status.Discovery.Inventory.Systems[0].Storage.Drives = drives
if err := e.admin.Status().Update(e.ctx, current); err != nil {
t.Fatal(err)
}
}
step := func() error {
t.Helper()
if err := e.admin.Get(e.ctx, client.ObjectKeyFromObject(allocation), allocation); err != nil {
t.Fatal(err)
}
_, err := e.allocator.Release(e.ctx, allocation, machine(1))
return err
}
// disk-1 goes missing: release waits within the grace.
setDrives(all[0])
e.until("release waits for disk-1", func() bool { return errors.Is(step(), ErrCleaningBlocked) })
// Past the grace, the server is held for disk-1.
now = now.Add(DefaultCleaningGrace + time.Minute)
var hold *ops.RecoveryHold
e.until("the server is held for disk-1", func() bool {
_ = step()
hold = e.custody(device.Name).Spec.RecoveryHold
return hold != nil && hold.Reason == custody.ReasonDriveMissing
})
e.create(&ops.DeviceRecovery{ObjectMeta: metav1.ObjectMeta{GenerateName: "dispose-", Namespace: namespace, Labels: map[string]string{ops.LabelDeviceUID: string(device.UID)}}, Spec: ops.DeviceRecoverySpec{
OperationEnvelope: ops.OperationEnvelope{Creator: ops.Creator{Username: "staff-example@example.com", UID: uid(803)}, DeviceRef: ops.DeviceReference{Name: device.Name},
Authority: ops.Authority{ServiceRequest: &ops.ServiceRequest{Reason: "disk-1 pulled"}}},
Decision: ops.DecisionDisposeDrive, Hold: &ops.HeldOperationReference{Kind: hold.Kind, Name: hold.Name, Fence: hold.Fence},
Evidence: ops.RecoveryEvidence{ObservedAt: metav1.Now(), Summary: "destroyed"},
Drive: &ops.DriveDisposal{Name: "disk-1", Method: ops.DisposalDestroyed, Reference: "ticket-example-2"},
}})
e.until("the disposal clears the hold", func() bool { return e.custody(device.Name).Spec.RecoveryHold == nil })
// Now disk-0 goes missing as well. Release must wait a fresh grace
// rather than hold the server at once.
setDrives()
e.until("release waits for disk-0", func() bool { return errors.Is(step(), ErrCleaningBlocked) })
for range 3 {
if err := step(); !errors.Is(err, ErrCleaningBlocked) {
t.Fatalf("release = %v, want it still waiting within the grace", err)
}
}
if hold := e.custody(device.Name).Spec.RecoveryHold; hold != nil {
t.Fatalf("held within the grace: %+v", hold)
}
// disk-0 returns, and release finishes with disk-1 disposed of.
setDrives(all[0])
e.release(allocation, machine(1))
}
Loading
Loading