From e16f72fc7779449124623dff0048ece99f6a9d09 Mon Sep 17 00:00:00 2001 From: pingqiu Date: Tue, 2 Jun 2026 15:23:06 -0700 Subject: [PATCH] phase35: add read-only operator CRD contract --- .../swblockclusters.block.seaweedfs.com.yaml | 130 ++++++++++ .../swblockvolumes.block.seaweedfs.com.yaml | 152 ++++++++++++ .../templates/operator-status-rbac.yaml | 41 ++++ .../charts/seaweed-block/values.schema.json | 12 + .../charts/seaweed-block/values.yaml | 7 + .../core/ops/kubernetes_crd_manifests_test.go | 227 ++++++++++++++++++ seaweed-block/internal/docs/current-plan.md | 40 +-- .../protocol/operator-readiness-contract.md | 41 +++- 8 files changed, 632 insertions(+), 18 deletions(-) create mode 100644 seaweed-block/charts/seaweed-block/crds/swblockclusters.block.seaweedfs.com.yaml create mode 100644 seaweed-block/charts/seaweed-block/crds/swblockvolumes.block.seaweedfs.com.yaml create mode 100644 seaweed-block/charts/seaweed-block/templates/operator-status-rbac.yaml create mode 100644 seaweed-block/core/ops/kubernetes_crd_manifests_test.go diff --git a/seaweed-block/charts/seaweed-block/crds/swblockclusters.block.seaweedfs.com.yaml b/seaweed-block/charts/seaweed-block/crds/swblockclusters.block.seaweedfs.com.yaml new file mode 100644 index 0000000000..9145ad4420 --- /dev/null +++ b/seaweed-block/charts/seaweed-block/crds/swblockclusters.block.seaweedfs.com.yaml @@ -0,0 +1,130 @@ +apiVersion: apiextensions.k8s.io/v1 +kind: CustomResourceDefinition +metadata: + name: swblockclusters.block.seaweedfs.com +spec: + group: block.seaweedfs.com + scope: Namespaced + names: + plural: swblockclusters + singular: swblockcluster + kind: SwBlockCluster + shortNames: + - swbc + versions: + - name: v1alpha1 + served: true + storage: true + subresources: + status: {} + additionalPrinterColumns: + - name: Ready + type: integer + jsonPath: .status.readyVolumeCount + - name: Blocked + type: integer + jsonPath: .status.blockedVolumeCount + - name: Stale + type: integer + jsonPath: .status.staleVolumeCount + - name: Age + type: date + jsonPath: .metadata.creationTimestamp + schema: + openAPIV3Schema: + type: object + description: Read-only Seaweed Block cluster status projection. + properties: + spec: + type: object + description: Declarative install metadata. Phase 35 does not reconcile spec into storage mutation. + properties: + image: + type: string + csiImage: + type: string + storageClass: + type: string + ackProfile: + type: string + protocol: + type: string + blockNodes: + type: array + items: + type: object + properties: + name: + type: string + internalIP: + type: string + status: + type: object + properties: + observedAt: + type: string + format: date-time + observedGeneration: + type: integer + format: int64 + nodeCount: + type: integer + minimum: 0 + volumeCount: + type: integer + minimum: 0 + readyVolumeCount: + type: integer + minimum: 0 + blockedVolumeCount: + type: integer + minimum: 0 + staleVolumeCount: + type: integer + minimum: 0 + conditions: + type: array + items: + x-kubernetes-preserve-unknown-fields: true + type: object + required: + - type + - status + properties: + type: + type: string + enum: + - Ready + - Recovered + - Recovering + - Blocked + - Invalid + - CleanupRequired + - EvidenceStale + status: + type: string + enum: + - "True" + - "False" + - Unknown + reason: + type: string + severity: + type: string + enum: + - info + - warning + - error + message: + type: string + lastTransitionTime: + type: string + format: date-time + evidenceRefs: + type: array + items: + type: string + evidenceRefs: + type: array + items: + type: string diff --git a/seaweed-block/charts/seaweed-block/crds/swblockvolumes.block.seaweedfs.com.yaml b/seaweed-block/charts/seaweed-block/crds/swblockvolumes.block.seaweedfs.com.yaml new file mode 100644 index 0000000000..6cd7654357 --- /dev/null +++ b/seaweed-block/charts/seaweed-block/crds/swblockvolumes.block.seaweedfs.com.yaml @@ -0,0 +1,152 @@ +apiVersion: apiextensions.k8s.io/v1 +kind: CustomResourceDefinition +metadata: + name: swblockvolumes.block.seaweedfs.com +spec: + group: block.seaweedfs.com + scope: Namespaced + names: + plural: swblockvolumes + singular: swblockvolume + kind: SwBlockVolume + shortNames: + - swbv + versions: + - name: v1alpha1 + served: true + storage: true + subresources: + status: {} + additionalPrinterColumns: + - name: Status + type: string + jsonPath: .status.status + - name: Reason + type: string + jsonPath: .status.reasonCode + - name: PVC + type: string + jsonPath: .status.pvcName + - name: Age + type: date + jsonPath: .metadata.creationTimestamp + schema: + openAPIV3Schema: + type: object + description: Read-only Seaweed Block volume status projection. + properties: + spec: + type: object + description: PVC binding metadata. Phase 35 does not reconcile spec into storage mutation. + properties: + pvcName: + type: string + storageClass: + type: string + status: + type: object + properties: + volumeID: + type: string + pvcName: + type: string + status: + type: string + enum: + - ready + - recovered + - recovering + - degraded + - blocked + - invalid + - unsafe + - unknown + reasonCode: + type: string + observedAt: + type: string + format: date-time + conditions: + type: array + items: + x-kubernetes-preserve-unknown-fields: true + type: object + required: + - type + - status + properties: + type: + type: string + enum: + - Ready + - Recovered + - Recovering + - Blocked + - Invalid + - CleanupRequired + - EvidenceStale + status: + type: string + enum: + - "True" + - "False" + - Unknown + reason: + type: string + severity: + type: string + enum: + - info + - warning + - error + message: + type: string + lastTransitionTime: + type: string + format: date-time + evidenceRefs: + type: array + items: + type: string + nonClaims: + type: array + items: + type: string + evidenceRefs: + type: array + items: + type: string + allowedActions: + type: array + items: + type: object + required: + - type + - mode + - mutationAllowed + properties: + type: + type: string + mode: + type: string + enum: + - read_only + - dry_run + sideEffectClass: + type: string + ownerExecutor: + type: string + mutationAllowed: + type: boolean + preconditions: + type: array + items: + type: string + invariantRefs: + type: array + items: + type: string + evidenceRefs: + type: array + items: + type: string diff --git a/seaweed-block/charts/seaweed-block/templates/operator-status-rbac.yaml b/seaweed-block/charts/seaweed-block/templates/operator-status-rbac.yaml new file mode 100644 index 0000000000..7056003c0c --- /dev/null +++ b/seaweed-block/charts/seaweed-block/templates/operator-status-rbac.yaml @@ -0,0 +1,41 @@ +{{- if and .Values.operatorStatus.create .Values.operatorStatus.rbac.create }} +apiVersion: v1 +kind: ServiceAccount +metadata: + name: {{ include "seaweed-block.fullname" . }}-operator-status + namespace: {{ .Release.Namespace }} + labels: + {{- include "seaweed-block.labels" . | nindent 4 }} +--- +apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRole +metadata: + name: {{ include "seaweed-block.fullname" . }}-operator-status + labels: + {{- include "seaweed-block.labels" . | nindent 4 }} +rules: + - apiGroups: ["block.seaweedfs.com"] + resources: ["swblockclusters", "swblockvolumes"] + verbs: ["get", "list", "watch"] + - apiGroups: ["block.seaweedfs.com"] + resources: ["swblockclusters/status", "swblockvolumes/status"] + verbs: ["get", "update", "patch"] + - apiGroups: [""] + resources: ["events"] + verbs: ["create"] +--- +apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRoleBinding +metadata: + name: {{ include "seaweed-block.fullname" . }}-operator-status + labels: + {{- include "seaweed-block.labels" . | nindent 4 }} +subjects: + - kind: ServiceAccount + name: {{ include "seaweed-block.fullname" . }}-operator-status + namespace: {{ .Release.Namespace }} +roleRef: + apiGroup: rbac.authorization.k8s.io + kind: ClusterRole + name: {{ include "seaweed-block.fullname" . }}-operator-status +{{- end }} diff --git a/seaweed-block/charts/seaweed-block/values.schema.json b/seaweed-block/charts/seaweed-block/values.schema.json index 935339e1fd..e09156827a 100644 --- a/seaweed-block/charts/seaweed-block/values.schema.json +++ b/seaweed-block/charts/seaweed-block/values.schema.json @@ -85,6 +85,18 @@ "poolID": { "type": "string" } } } + }, + "operatorStatus": { + "type": "object", + "properties": { + "create": { "type": "boolean" }, + "rbac": { + "type": "object", + "properties": { + "create": { "type": "boolean" } + } + } + } } } } diff --git a/seaweed-block/charts/seaweed-block/values.yaml b/seaweed-block/charts/seaweed-block/values.yaml index b90d9dc2ec..6861be711c 100644 --- a/seaweed-block/charts/seaweed-block/values.yaml +++ b/seaweed-block/charts/seaweed-block/values.yaml @@ -113,6 +113,13 @@ blockNodes: rbac: create: true +operatorStatus: + # Phase 35 read-only operator foundation. This only creates the status + # service account and RBAC; it does not install a controller by itself. + create: false + rbac: + create: true + csiController: replicas: 1 diff --git a/seaweed-block/core/ops/kubernetes_crd_manifests_test.go b/seaweed-block/core/ops/kubernetes_crd_manifests_test.go new file mode 100644 index 0000000000..8d330868d9 --- /dev/null +++ b/seaweed-block/core/ops/kubernetes_crd_manifests_test.go @@ -0,0 +1,227 @@ +package ops + +import ( + "os" + "path/filepath" + "strings" + "testing" + + "gopkg.in/yaml.v3" +) + +func TestPhase35D1CRDManifestsMatchManagedVolumeContract(t *testing.T) { + contract := ManagedVolumeCRDContractDefinition() + for _, tc := range []struct { + path string + name string + kind string + plural string + singular string + }{ + { + path: "charts/seaweed-block/crds/swblockclusters.block.seaweedfs.com.yaml", + name: "swblockclusters.block.seaweedfs.com", + kind: SwBlockClusterKind, + plural: "swblockclusters", + singular: "swblockcluster", + }, + { + path: "charts/seaweed-block/crds/swblockvolumes.block.seaweedfs.com.yaml", + name: "swblockvolumes.block.seaweedfs.com", + kind: SwBlockVolumeKind, + plural: "swblockvolumes", + singular: "swblockvolume", + }, + } { + t.Run(tc.kind, func(t *testing.T) { + doc := readYAMLMap(t, tc.path) + assertYAMLString(t, doc, "apiVersion", "apiextensions.k8s.io/v1") + assertYAMLString(t, doc, "kind", "CustomResourceDefinition") + + metadata := yamlMap(t, doc, "metadata") + assertYAMLString(t, metadata, "name", tc.name) + + spec := yamlMap(t, doc, "spec") + assertYAMLString(t, spec, "group", contract.Group) + assertYAMLString(t, spec, "scope", "Namespaced") + + names := yamlMap(t, spec, "names") + assertYAMLString(t, names, "kind", tc.kind) + assertYAMLString(t, names, "plural", tc.plural) + assertYAMLString(t, names, "singular", tc.singular) + + versions := yamlSlice(t, spec, "versions") + if len(versions) != 1 { + t.Fatalf("%s versions=%d want 1", tc.kind, len(versions)) + } + version := yamlMapFromValue(t, versions[0]) + assertYAMLString(t, version, "name", contract.Version) + assertYAMLBool(t, version, "served", true) + assertYAMLBool(t, version, "storage", true) + if _, ok := yamlMap(t, version, "subresources")["status"]; !ok { + t.Fatalf("%s missing status subresource", tc.kind) + } + + statusSchema := yamlMap(t, + yamlMap(t, + yamlMap(t, + yamlMap(t, version, "schema"), + "openAPIV3Schema"), + "properties"), + "status") + properties := yamlMap(t, statusSchema, "properties") + if _, ok := properties["conditions"]; !ok { + t.Fatalf("%s status schema missing conditions", tc.kind) + } + if _, ok := properties["evidenceRefs"]; !ok { + t.Fatalf("%s status schema missing evidenceRefs", tc.kind) + } + }) + } +} + +func TestPhase35D1SwBlockVolumeConditionEnumCoversContract(t *testing.T) { + doc := readYAMLMap(t, "charts/seaweed-block/crds/swblockvolumes.block.seaweedfs.com.yaml") + conditionType := yamlMap(t, + yamlMap(t, + yamlMap(t, + yamlMap(t, + yamlMap(t, + yamlMap(t, + yamlMap(t, + yamlMap(t, + yamlMap(t, + yamlSlice(t, yamlMap(t, doc, "spec"), "versions")[0].(map[string]any), + "schema"), + "openAPIV3Schema"), + "properties"), + "status"), + "properties"), + "conditions"), + "items"), + "properties"), + "type") + enum := yamlStringSet(t, conditionType, "enum") + for _, want := range []string{ + ConditionReady, + ConditionRecovered, + ConditionRecovering, + ConditionBlocked, + ConditionInvalid, + ConditionCleanupRequired, + ConditionEvidenceStale, + } { + if !enum[want] { + t.Fatalf("condition enum missing %s: %+v", want, enum) + } + } +} + +func TestPhase35D1OperatorStatusRBACIsStatusOnly(t *testing.T) { + raw := readRepoFile(t, "charts/seaweed-block/templates/operator-status-rbac.yaml") + required := []string{ + `resources: ["swblockclusters", "swblockvolumes"]`, + `verbs: ["get", "list", "watch"]`, + `resources: ["swblockclusters/status", "swblockvolumes/status"]`, + `verbs: ["get", "update", "patch"]`, + `resources: ["events"]`, + `verbs: ["create"]`, + } + for _, want := range required { + if !strings.Contains(raw, want) { + t.Fatalf("operator RBAC missing %q\n%s", want, raw) + } + } + for _, forbidden := range []string{ + `resources: ["persistentvolumes"`, + `resources: ["persistentvolumeclaims"`, + `resources: ["deployments"`, + `resources: ["secrets"`, + `resources: ["storageclasses"`, + `"delete"`, + } { + if strings.Contains(raw, forbidden) { + t.Fatalf("operator status RBAC contains forbidden fragment %q\n%s", forbidden, raw) + } + } +} + +func TestPhase35D1OperatorStatusRBACDefaultDisabled(t *testing.T) { + values := readYAMLMap(t, "charts/seaweed-block/values.yaml") + operatorStatus := yamlMap(t, values, "operatorStatus") + assertYAMLBool(t, operatorStatus, "create", false) + rbac := yamlMap(t, operatorStatus, "rbac") + assertYAMLBool(t, rbac, "create", true) +} + +func readRepoFile(t *testing.T, repoPath string) string { + t.Helper() + raw, err := os.ReadFile(filepath.Join("..", "..", filepath.FromSlash(repoPath))) + if err != nil { + t.Fatalf("read %s: %v", repoPath, err) + } + return string(raw) +} + +func readYAMLMap(t *testing.T, repoPath string) map[string]any { + t.Helper() + var out map[string]any + if err := yaml.Unmarshal([]byte(readRepoFile(t, repoPath)), &out); err != nil { + t.Fatalf("unmarshal %s: %v", repoPath, err) + } + return out +} + +func yamlMap(t *testing.T, in map[string]any, key string) map[string]any { + t.Helper() + out, ok := in[key].(map[string]any) + if !ok { + t.Fatalf("key %q is %T, want map", key, in[key]) + } + return out +} + +func yamlMapFromValue(t *testing.T, in any) map[string]any { + t.Helper() + out, ok := in.(map[string]any) + if !ok { + t.Fatalf("value is %T, want map", in) + } + return out +} + +func yamlSlice(t *testing.T, in map[string]any, key string) []any { + t.Helper() + out, ok := in[key].([]any) + if !ok { + t.Fatalf("key %q is %T, want slice", key, in[key]) + } + return out +} + +func assertYAMLString(t *testing.T, in map[string]any, key, want string) { + t.Helper() + if got, _ := in[key].(string); got != want { + t.Fatalf("key %q=%q want %q", key, got, want) + } +} + +func assertYAMLBool(t *testing.T, in map[string]any, key string, want bool) { + t.Helper() + if got, _ := in[key].(bool); got != want { + t.Fatalf("key %q=%t want %t", key, got, want) + } +} + +func yamlStringSet(t *testing.T, in map[string]any, key string) map[string]bool { + t.Helper() + out := map[string]bool{} + for _, item := range yamlSlice(t, in, key) { + s, ok := item.(string) + if !ok { + t.Fatalf("enum item is %T, want string", item) + } + out[s] = true + } + return out +} diff --git a/seaweed-block/internal/docs/current-plan.md b/seaweed-block/internal/docs/current-plan.md index b3d9935886..8737ba8a17 100644 --- a/seaweed-block/internal/docs/current-plan.md +++ b/seaweed-block/internal/docs/current-plan.md @@ -50,21 +50,27 @@ StorageClasses, Helm releases, iSCSI sessions, multipath maps, or hostPath data. Goal: define the Kubernetes API shape without starting mutating lifecycle. +Status: local PASS; live k3s apply/dry-run pending QA. + Deliverables: -- `SwBlockCluster` CRD manifest. -- `SwBlockVolume` CRD manifest. -- read-only/status-only RBAC manifest. -- schema docs that map existing ManagedVolume fields to CRD status fields. +- `SwBlockCluster` CRD manifest: + `charts/seaweed-block/crds/swblockclusters.block.seaweedfs.com.yaml` +- `SwBlockVolume` CRD manifest: + `charts/seaweed-block/crds/swblockvolumes.block.seaweedfs.com.yaml` +- read-only/status-only RBAC manifest: + `charts/seaweed-block/templates/operator-status-rbac.yaml` +- schema docs that map existing ManagedVolume fields to CRD status fields: + `internal/docs/protocol/operator-readiness-contract.md` Acceptance: ```text -CRDs apply cleanly on k3s -status subresource is enabled -RBAC permits get/list/watch and status/event writes only -RBAC does not permit storage mutation verbs -unit/schema tests cover required condition vocabulary +[pending QA] CRDs apply cleanly on k3s +[done] status subresource is enabled +[done] RBAC permits get/list/watch and status/event writes only +[done] RBAC does not permit storage mutation verbs +[done] unit/schema tests cover required condition vocabulary ``` ## D2: Status-Only Controller Skeleton @@ -211,14 +217,20 @@ finished plan moved under internal/docs/finished-plans/ ## Current Progress - 0%: Phase 35 plan opened. +- 12%: D1 CRD/RBAC contract manifests landed locally. `go test ./core/ops + ./cmd/sw-block`, `helm lint charts/seaweed-block`, and `helm template + --include-crds --set operatorStatus.create=true` pass. Live k3s + apply/dry-run remains the QA validation item. ## Next Step -Start D1 with TDD: +Ask QA to validate D1 on the k3s lab, then start D2 controller skeleton. ```text -define CRD/status/RBAC contract -write schema/RBAC tests -add manifests -verify apply/dry-run on k3s +helm template sw-block charts/seaweed-block \ + --namespace kube-system \ + --set operatorStatus.create=true \ + --include-crds > /tmp/sw-block-phase35-d1.yaml + +kubectl apply --dry-run=server -f /tmp/sw-block-phase35-d1.yaml ``` diff --git a/seaweed-block/internal/docs/protocol/operator-readiness-contract.md b/seaweed-block/internal/docs/protocol/operator-readiness-contract.md index 7e126a52a4..09a61bbd8a 100644 --- a/seaweed-block/internal/docs/protocol/operator-readiness-contract.md +++ b/seaweed-block/internal/docs/protocol/operator-readiness-contract.md @@ -1,6 +1,7 @@ # Operator Readiness Contract -Status: Phase 23 seed. Read-only contract only; no operator implementation. +Status: Phase 35 D1 seed. Read-only CRD/status contract only; no controller +implementation yet. ## Purpose @@ -20,11 +21,19 @@ ManagedVolumeProjection It is not: -- a CRD schema, - a controller loop, - a mutating admin workflow, - a repair/rebuild/failback implementation. +The Phase 35 D1 CRD schema lives in: + +- `charts/seaweed-block/crds/swblockclusters.block.seaweedfs.com.yaml` +- `charts/seaweed-block/crds/swblockvolumes.block.seaweedfs.com.yaml` + +The status-only RBAC seed lives in: + +- `charts/seaweed-block/templates/operator-status-rbac.yaml` + ## Source Of Truth The future operator must consume `ManagedVolumeProjection` or the equivalent @@ -65,6 +74,29 @@ The contract contains: - `events[]` - `allowed_actions[]` +The Kubernetes CRD projection keeps the same vocabulary: + +| ManagedVolume Contract | CRD Status Path | +|---|---| +| `status.volume_id` | `SwBlockVolume.status.volumeID` | +| `status.pvc_name` | `SwBlockVolume.status.pvcName` | +| `status.status` | `SwBlockVolume.status.status` | +| `status.reason_code` | `SwBlockVolume.status.reasonCode` | +| `status.conditions[]` | `SwBlockVolume.status.conditions[]` | +| `status.non_claims[]` | `SwBlockVolume.status.nonClaims[]` | +| `status.evidence_refs[]` | `SwBlockVolume.status.evidenceRefs[]` | +| `allowed_actions[]` | `SwBlockVolume.status.allowedActions[]` | + +Cluster-level aggregate fields map to `SwBlockCluster.status.*`: + +- `nodeCount` +- `volumeCount` +- `readyVolumeCount` +- `blockedVolumeCount` +- `staleVolumeCount` +- `conditions[]` +- `evidenceRefs[]` + All Phase 23 actions are non-executing: ```text @@ -127,7 +159,6 @@ Examples: This contract does not deliver: -- CRDs, - controller reconciliation, - mutating actions, - automatic repair/rebuild, @@ -141,9 +172,11 @@ It exists so those future pieces use the same facts and safety boundaries. Pinned by: - `core/ops/managed_volume_operator_contract_test.go` +- `core/ops/managed_volume_crd_contract_test.go` +- `core/ops/kubernetes_crd_manifests_test.go` Regression: ```text -go test ./core/ops -run "ManagedVolumeOperatorContract" -count=1 +go test ./core/ops -run "ManagedVolumeOperatorContract|ManagedVolumeCRDContract|Phase35D1" -count=1 ```