From 9e2e008a51d62d0b6a4261b15d42ae4b3ce94bb7 Mon Sep 17 00:00:00 2001 From: pingqiu Date: Wed, 10 Jun 2026 16:56:10 -0700 Subject: [PATCH] phase39: define delete safety contract --- .../core/ops/delete_safety_contract.go | 140 +++++++++++++++++ .../core/ops/delete_safety_contract_test.go | 145 ++++++++++++++++++ seaweed-block/internal/docs/current-plan.md | 23 +-- .../finalizer-delete-safety-contract.md | 143 +++++++++++++++++ 4 files changed, 442 insertions(+), 9 deletions(-) create mode 100644 seaweed-block/core/ops/delete_safety_contract.go create mode 100644 seaweed-block/core/ops/delete_safety_contract_test.go create mode 100644 seaweed-block/internal/docs/protocol/finalizer-delete-safety-contract.md diff --git a/seaweed-block/core/ops/delete_safety_contract.go b/seaweed-block/core/ops/delete_safety_contract.go new file mode 100644 index 0000000000..9fcdab5f6f --- /dev/null +++ b/seaweed-block/core/ops/delete_safety_contract.go @@ -0,0 +1,140 @@ +package ops + +const ( + SwBlockVolumeFinalizerName = "block.seaweedfs.com/swblockvolume-protection" + + SwBlockVolumeDeleteActionReleaseFinalizer = "safe_k8s.release_swblockvolume_finalizer" + + DeleteSafetyStateNotRequested = "not_requested" + DeleteSafetyStateRequested = "requested" + DeleteSafetyStateBlocked = "blocked" + DeleteSafetyStateReleasable = "releasable" + DeleteSafetyStateReleased = "released" + + ReasonDeleteNotRequested = "delete_not_requested" + ReasonCleanupEvidenceMissing = "cleanup_evidence_missing" + ReasonDeleteFinalizerReleasable = "finalizer_releasable" +) + +type SwBlockVolumeDeleteSafetyContract struct { + FinalizerName string `json:"finalizer_name"` + OwnedKind string `json:"owned_kind"` + OwnedMutationScope []string `json:"owned_mutation_scope"` + RequiredFacts []string `json:"required_facts"` + DeleteStates []string `json:"delete_states"` + BlockingReasons []string `json:"blocking_reasons"` + ReleaseReason string `json:"release_reason"` + NonClaims []string `json:"non_claims"` + ReleaseActionType string `json:"release_action_type"` + ReleaseEvidence string `json:"release_evidence"` + BlockedSafeNextAction string `json:"blocked_safe_next_action"` +} + +type SwBlockVolumeDeleteSafetyFacts struct { + DeleteRequested bool `json:"delete_requested"` + FinalizerPresent bool `json:"finalizer_present"` + Cleanup *CleanupEvidence `json:"cleanup,omitempty"` +} + +type SwBlockVolumeDeleteSafetyDecision struct { + ActionType string `json:"action_type"` + Decision string `json:"decision"` + State string `json:"state"` + Reason string `json:"reason"` + FinalizerReleaseAllowed bool `json:"finalizer_release_allowed"` + MissingFacts []string `json:"missing_facts,omitempty"` + EvidenceRefs []string `json:"evidence_refs,omitempty"` + SafeNextAction string `json:"safe_next_action,omitempty"` +} + +func SwBlockVolumeDeleteSafetyContractDefinition() SwBlockVolumeDeleteSafetyContract { + return SwBlockVolumeDeleteSafetyContract{ + FinalizerName: SwBlockVolumeFinalizerName, + OwnedKind: SwBlockVolumeKind, + OwnedMutationScope: []string{ + "swblockvolumes.metadata.finalizers", + "swblockvolumes/status", + "events", + }, + RequiredFacts: []string{ + "identity.volume_id", + "identity.pvc_name", + "identity.pv_name", + "kubernetes.swblockvolume.deletion_timestamp", + "cleanup.status", + "cleanup.k8s_residue_count", + "cleanup.iscsi_residue_count", + "cleanup.multipath_residue_count", + "cleanup.process_residue_count", + "cleanup.hostpath_residue_count", + }, + DeleteStates: []string{ + DeleteSafetyStateNotRequested, + DeleteSafetyStateRequested, + DeleteSafetyStateBlocked, + DeleteSafetyStateReleasable, + DeleteSafetyStateReleased, + }, + BlockingReasons: []string{ + ReasonCleanupEvidenceMissing, + ReasonCleanupRequired, + "iscsi_node_records_present", + "iscsi_sessions_present", + "multipath_maps_present", + "dmsetup_devices_present", + "kubernetes_resources_present", + "hostpath_residue_present", + }, + ReleaseReason: ReasonDeleteFinalizerReleasable, + ReleaseActionType: SwBlockVolumeDeleteActionReleaseFinalizer, + ReleaseEvidence: "cleanup-summary.txt with cleanup_status=ok and all residue counts 0", + BlockedSafeNextAction: ManagedVolumeActionVerifyCleanup, + NonClaims: []string{ + "no_pvc_finalizer_ownership", + "no_automatic_cleanup_execution", + "no_pv_or_pvc_delete", + "no_pod_or_deployment_delete", + "no_iscsi_or_multipath_mutation", + "no_hostpath_delete", + }, + } +} + +func EvaluateSwBlockVolumeDeleteSafety(facts SwBlockVolumeDeleteSafetyFacts) SwBlockVolumeDeleteSafetyDecision { + if !facts.DeleteRequested { + return SwBlockVolumeDeleteSafetyDecision{ + ActionType: SwBlockVolumeDeleteActionReleaseFinalizer, + Decision: ManagedVolumeActionDecisionRejected, + State: DeleteSafetyStateNotRequested, + Reason: ReasonDeleteNotRequested, + } + } + if facts.Cleanup == nil { + return SwBlockVolumeDeleteSafetyDecision{ + ActionType: SwBlockVolumeDeleteActionReleaseFinalizer, + Decision: ManagedVolumeActionDecisionRejected, + State: DeleteSafetyStateBlocked, + Reason: ReasonCleanupEvidenceMissing, + MissingFacts: []string{"cleanup.status"}, + SafeNextAction: ManagedVolumeActionVerifyCleanup, + } + } + if cleanupRequired(facts.Cleanup) { + return SwBlockVolumeDeleteSafetyDecision{ + ActionType: SwBlockVolumeDeleteActionReleaseFinalizer, + Decision: ManagedVolumeActionDecisionRejected, + State: DeleteSafetyStateBlocked, + Reason: cleanupReason(facts.Cleanup), + EvidenceRefs: cleanupEvidenceRefs(facts.Cleanup), + SafeNextAction: ManagedVolumeActionVerifyCleanup, + } + } + return SwBlockVolumeDeleteSafetyDecision{ + ActionType: SwBlockVolumeDeleteActionReleaseFinalizer, + Decision: ManagedVolumeActionDecisionAllowed, + State: DeleteSafetyStateReleasable, + Reason: ReasonDeleteFinalizerReleasable, + FinalizerReleaseAllowed: true, + EvidenceRefs: cleanupEvidenceRefs(facts.Cleanup), + } +} diff --git a/seaweed-block/core/ops/delete_safety_contract_test.go b/seaweed-block/core/ops/delete_safety_contract_test.go new file mode 100644 index 0000000000..620ee7de41 --- /dev/null +++ b/seaweed-block/core/ops/delete_safety_contract_test.go @@ -0,0 +1,145 @@ +package ops + +import "testing" + +func TestSwBlockVolumeDeleteSafetyContract_BoundsFinalizerOwnership(t *testing.T) { + contract := SwBlockVolumeDeleteSafetyContractDefinition() + if contract.FinalizerName != SwBlockVolumeFinalizerName { + t.Fatalf("finalizer=%s", contract.FinalizerName) + } + if contract.OwnedKind != SwBlockVolumeKind { + t.Fatalf("owned kind=%s", contract.OwnedKind) + } + if contract.ReleaseActionType != SwBlockVolumeDeleteActionReleaseFinalizer { + t.Fatalf("release action=%s", contract.ReleaseActionType) + } + for _, want := range []string{ + "swblockvolumes.metadata.finalizers", + "swblockvolumes/status", + "events", + } { + if !stringSliceContains(contract.OwnedMutationScope, want) { + t.Fatalf("mutation scope missing %s: %+v", want, contract.OwnedMutationScope) + } + } + for _, forbidden := range []string{ + "persistentvolumeclaims", + "persistentvolumes", + "pods", + "deployments", + "storageclasses", + "iscsi", + "multipath", + "hostpath", + } { + if stringSliceContains(contract.OwnedMutationScope, forbidden) { + t.Fatalf("mutation scope includes forbidden target %s: %+v", forbidden, contract.OwnedMutationScope) + } + } + for _, want := range []string{ + "identity.volume_id", + "identity.pvc_name", + "identity.pv_name", + "kubernetes.swblockvolume.deletion_timestamp", + "cleanup.status", + "cleanup.iscsi_residue_count", + "cleanup.multipath_residue_count", + "cleanup.hostpath_residue_count", + } { + if !stringSliceContains(contract.RequiredFacts, want) { + t.Fatalf("required facts missing %s: %+v", want, contract.RequiredFacts) + } + } + for _, want := range []string{ + DeleteSafetyStateNotRequested, + DeleteSafetyStateRequested, + DeleteSafetyStateBlocked, + DeleteSafetyStateReleasable, + DeleteSafetyStateReleased, + } { + if !stringSliceContains(contract.DeleteStates, want) { + t.Fatalf("delete states missing %s: %+v", want, contract.DeleteStates) + } + } + for _, want := range []string{ + "no_pvc_finalizer_ownership", + "no_automatic_cleanup_execution", + "no_iscsi_or_multipath_mutation", + "no_hostpath_delete", + } { + if !stringSliceContains(contract.NonClaims, want) { + t.Fatalf("non-claims missing %s: %+v", want, contract.NonClaims) + } + } +} + +func TestEvaluateSwBlockVolumeDeleteSafety_NotRequested(t *testing.T) { + decision := EvaluateSwBlockVolumeDeleteSafety(SwBlockVolumeDeleteSafetyFacts{}) + if decision.State != DeleteSafetyStateNotRequested || + decision.Decision != ManagedVolumeActionDecisionRejected || + decision.Reason != ReasonDeleteNotRequested || + decision.FinalizerReleaseAllowed { + t.Fatalf("decision=%+v", decision) + } +} + +func TestEvaluateSwBlockVolumeDeleteSafety_BlocksWithoutCleanupEvidence(t *testing.T) { + decision := EvaluateSwBlockVolumeDeleteSafety(SwBlockVolumeDeleteSafetyFacts{ + DeleteRequested: true, + FinalizerPresent: true, + }) + if decision.State != DeleteSafetyStateBlocked || + decision.Decision != ManagedVolumeActionDecisionRejected || + decision.Reason != ReasonCleanupEvidenceMissing || + decision.FinalizerReleaseAllowed || + decision.SafeNextAction != ManagedVolumeActionVerifyCleanup { + t.Fatalf("decision=%+v", decision) + } + if !stringSliceContains(decision.MissingFacts, "cleanup.status") { + t.Fatalf("missing facts=%+v", decision.MissingFacts) + } +} + +func TestEvaluateSwBlockVolumeDeleteSafety_BlocksWithResidue(t *testing.T) { + decision := EvaluateSwBlockVolumeDeleteSafety(SwBlockVolumeDeleteSafetyFacts{ + DeleteRequested: true, + FinalizerPresent: true, + Cleanup: &CleanupEvidence{ + Status: ObservationStatusBlocked, + ISCSIResidueCount: 1, + ReasonCodes: []string{"iscsi_node_records_present"}, + EvidenceRef: "cleanup-summary.txt", + }, + }) + if decision.State != DeleteSafetyStateBlocked || + decision.Decision != ManagedVolumeActionDecisionRejected || + decision.Reason != "iscsi_node_records_present" || + decision.FinalizerReleaseAllowed || + decision.SafeNextAction != ManagedVolumeActionVerifyCleanup { + t.Fatalf("decision=%+v", decision) + } + if !stringSliceContains(decision.EvidenceRefs, "cleanup-summary.txt") { + t.Fatalf("evidence refs=%+v", decision.EvidenceRefs) + } +} + +func TestEvaluateSwBlockVolumeDeleteSafety_ReleasableWithCleanCleanupEvidence(t *testing.T) { + decision := EvaluateSwBlockVolumeDeleteSafety(SwBlockVolumeDeleteSafetyFacts{ + DeleteRequested: true, + FinalizerPresent: true, + Cleanup: &CleanupEvidence{ + Status: ObservationStatusOK, + EvidenceRef: "cleanup-summary.txt", + }, + }) + if decision.State != DeleteSafetyStateReleasable || + decision.Decision != ManagedVolumeActionDecisionAllowed || + decision.Reason != ReasonDeleteFinalizerReleasable || + !decision.FinalizerReleaseAllowed || + decision.SafeNextAction != "" { + t.Fatalf("decision=%+v", decision) + } + if !stringSliceContains(decision.EvidenceRefs, "cleanup-summary.txt") { + t.Fatalf("evidence refs=%+v", decision.EvidenceRefs) + } +} diff --git a/seaweed-block/internal/docs/current-plan.md b/seaweed-block/internal/docs/current-plan.md index a53f9671bc..d27a35f61c 100644 --- a/seaweed-block/internal/docs/current-plan.md +++ b/seaweed-block/internal/docs/current-plan.md @@ -1,6 +1,6 @@ # Current Plan: Phase 39 - Finalizer / Delete Safety -Status: active, 0% complete. Started on 2026-06-10. +Status: active, 14% complete. Started on 2026-06-10. Branch: `phase33-testops-failure-hardening` @@ -55,19 +55,19 @@ remove hostPath data, promote/fence/rebuild/failback, or mutate storage. Goal: define exactly what the finalizer owns and which facts are required before it can release deletion. -Status: pending. +Status: dev-complete; QA/internal review pending. Acceptance: ```text -[ ] SwBlockVolume finalizer name is defined -[ ] delete states are defined: not_requested, requested, blocked, releasable, +[x] SwBlockVolume finalizer name is defined +[x] delete states are defined: not_requested, requested, blocked, releasable, released -[ ] required facts are defined: volume identity, PVC/PV linkage, cleanup +[x] required facts are defined: volume identity, PVC/PV linkage, cleanup summary, active sessions, multipath/dmsetup state, generated workload residue, hostPath residue -[ ] action contract maps delete release/block to Phase 38 evaluator language -[ ] non-claims explicitly exclude automatic cleanup and PVC finalizer ownership +[x] action contract maps delete release/block to Phase 38 evaluator language +[x] non-claims explicitly exclude automatic cleanup and PVC finalizer ownership ``` Verification: @@ -229,6 +229,10 @@ QA strict rerun from clean lab - 0%: Phase 39 opened. Scope is limited to `SwBlockVolume` finalizer/delete safety as the first mutating operator path. PVC finalizers, automatic cleanup, repair/rebuild/failback, backup/restore, and NVMe remain out of scope. +- 14%: D1 dev-complete. The delete-safety contract defines the finalizer name, + owned mutation scope, required cleanup/identity facts, delete states, + non-claims, and a pure decision function that blocks missing/residue evidence + and marks clean evidence as releasable without performing mutation. ## Prerequisites / Risks @@ -241,5 +245,6 @@ QA strict rerun from clean lab ## Next Step -Start D1: write the delete-safety contract and map finalizer decisions onto the -Phase 38 action model before implementing finalizer mutation. +Run D1 review/QA, then start D2: project delete-requested, delete-blocked, and +delete-releasable states through status surfaces before enabling finalizer +mutation. diff --git a/seaweed-block/internal/docs/protocol/finalizer-delete-safety-contract.md b/seaweed-block/internal/docs/protocol/finalizer-delete-safety-contract.md new file mode 100644 index 0000000000..8f699ffc76 --- /dev/null +++ b/seaweed-block/internal/docs/protocol/finalizer-delete-safety-contract.md @@ -0,0 +1,143 @@ +# Finalizer Delete Safety Contract + +Status: Phase 39 D1 contract. + +## Goal + +Define the first bounded mutating operator behavior before implementing it: +`SwBlockVolume` finalizer/delete safety. + +The operator may eventually patch only: + +```text +SwBlockVolume.metadata.finalizers +SwBlockVolume.status +Kubernetes Events +``` + +It must not delete or mutate PVCs, PVs, Pods, Deployments, StorageClasses, +Helm releases, images, iSCSI state, multipath state, dmsetup devices, hostPath +data, replica authority, rebuild/failback state, backup, or restore state. + +## Finalizer + +```text +block.seaweedfs.com/swblockvolume-protection +``` + +The finalizer protects the Seaweed Block lifecycle object from disappearing +while evidence says deletion is unsafe or unknown. + +It does not claim ownership of the user PVC lifecycle. PVC finalizers are out of +scope for Phase 39. + +## Action Contract + +Finalizer release is represented as an action decision using the Phase 38 +vocabulary: + +```text +action_type=safe_k8s.release_swblockvolume_finalizer +decision=allowed|rejected +``` + +The action is allowed only when the delete state is `releasable`. Missing +cleanup evidence, residue, or no active delete request rejects the action. + +## Delete States + +| State | Meaning | +|---|---| +| `not_requested` | No delete request is active. | +| `requested` | Delete request exists; evidence has not yet been classified. | +| `blocked` | Delete was requested, but cleanup/evidence is missing or unsafe. | +| `releasable` | Delete was requested and cleanup evidence is clean. The operator may remove the finalizer. | +| `released` | Finalizer removal has been observed or emitted as an event. | + +## Required Facts + +The delete decision requires: + +```text +identity.volume_id +identity.pvc_name +identity.pv_name +kubernetes.swblockvolume.deletion_timestamp +cleanup.status +cleanup.k8s_residue_count +cleanup.iscsi_residue_count +cleanup.multipath_residue_count +cleanup.process_residue_count +cleanup.hostpath_residue_count +``` + +If cleanup evidence is missing, the safe decision is: + +```text +state=blocked +decision=rejected +reason=cleanup_evidence_missing +safe_next_action=observe.verify_cleanup +``` + +If residue evidence exists, the safe decision is: + +```text +state=blocked +decision=rejected +reason= +safe_next_action=observe.verify_cleanup +``` + +If cleanup evidence is clean, the safe decision is: + +```text +state=releasable +decision=allowed +reason=finalizer_releasable +``` + +## Evidence + +The release evidence is: + +```text +cleanup-summary.txt with cleanup_status=ok and all residue counts 0 +``` + +Blocking evidence may include: + +```text +cleanup-summary.txt +iscsi node/session residue +multipath maps +dmsetup devices +Kubernetes generated resources +hostPath residue +process residue +``` + +## Non-Claims + +Phase 39 D1 does not implement mutation. Later D3-D5 may implement bounded +finalizer mutation, but still do not claim: + +- PVC finalizer ownership, +- automatic cleanup execution, +- PV/PVC deletion, +- Pod/Deployment deletion, +- iSCSI or multipath mutation, +- hostPath deletion, +- promotion/fencing/rebuild/failback, +- backup/snapshot/restore, +- NVMe ANA lifecycle. + +## Review Rule + +Any future implementation must keep this invariant: + +```text +The operator can release a SwBlockVolume finalizer only after the delete-safety +decision is releasable. If evidence is missing or residue remains, deletion is +blocked with status, reason, event, evidence refs, and a non-mutating next step. +```