diff --git a/packages/system/linstor/templates/_backend.tpl b/packages/system/linstor/templates/_backend.tpl new file mode 100644 index 000000000..04b2952d5 --- /dev/null +++ b/packages/system/linstor/templates/_backend.tpl @@ -0,0 +1,12 @@ +{{- /* +Whether piraeus-operator runs in external mode against blockstor-apiserver. +That is backend blockstor once blockstorMigration is cleared, and not before: +while blockstor adopts a LINSTOR cluster's volumes, every piraeus object +(LinstorCluster, LinstorSatelliteConfiguration and what they reference) +renders exactly as under backend linstor, so the operator, which keeps +running, finds nothing to change and keeps linstor-csi on LINSTOR. Templates +of blockstor's own components follow .Values.backend instead. +*/ -}} +{{- define "linstor.piraeusExternal" -}} +{{- if and (eq .Values.backend "blockstor") (not .Values.blockstorMigration) }}true{{ end -}} +{{- end -}} diff --git a/packages/system/linstor/templates/backend-guard.yaml b/packages/system/linstor/templates/backend-guard.yaml index 75f1dc98e..c2b5422a8 100644 --- a/packages/system/linstor/templates/backend-guard.yaml +++ b/packages/system/linstor/templates/backend-guard.yaml @@ -8,6 +8,18 @@ any other way, a hand-set component value for one, is checked here too. {{- fail (printf "linstor: backend must be \"linstor\" or \"blockstor\", got %q" (toString .Values.backend)) }} {{- end }} {{- /* +blockstorMigration holds piraeus in the LINSTOR shape. A value the chart +does not know would render it as complete, and with backend linstor there +is nothing to migrate to: both are refused rather than read as off. +*/ -}} +{{- $migration := .Values.blockstorMigration | default "" }} +{{- if not (has $migration (list "" "hold" "adopt")) }} +{{- fail (printf "linstor: blockstorMigration must be \"\", \"hold\" or \"adopt\", got %q" (toString $migration)) }} +{{- end }} +{{- if and $migration (ne .Values.backend "blockstor") }} +{{- fail (printf "linstor: blockstorMigration %q is only valid with backend blockstor; clear it together with switching the backend back" $migration) }} +{{- end }} +{{- /* blockstor's three images are one release: the controller, apiserver and satellite share a wire protocol and the CRDs under definitions/, which `make update-blockstor-crds` fetches for the controller's tag. Pinning one diff --git a/packages/system/linstor/templates/blockstor-controller.yaml b/packages/system/linstor/templates/blockstor-controller.yaml index e4a9b1383..993225541 100644 --- a/packages/system/linstor/templates/blockstor-controller.yaml +++ b/packages/system/linstor/templates/blockstor-controller.yaml @@ -116,7 +116,13 @@ metadata: app: blockstor-controller app.kubernetes.io/component: blockstor-controller spec: - replicas: 1 + {{- /* + Stopped in the hold step of a migration: started before the converted + manifests are applied, the controller would place replicas of its own + under definitions whose adopted Resources have not arrived yet. The + release owns the count, so an upgrade or a retry does not bring the + pod back while the manifests go in, as a scale by hand would. */}} + replicas: {{ eq .Values.blockstorMigration "hold" | ternary 0 1 }} {{- /* One reconciler at a time. The binary runs without leader election, so a rolling update would run the old and the new pod side by side against the diff --git a/packages/system/linstor/templates/cluster.yaml b/packages/system/linstor/templates/cluster.yaml index 271611b26..f5b1c387b 100644 --- a/packages/system/linstor/templates/cluster.yaml +++ b/packages/system/linstor/templates/cluster.yaml @@ -1,3 +1,6 @@ +{{- /* See linstor.piraeusExternal in _backend.tpl for why this follows it + rather than .Values.backend. */ -}} +{{- $external := include "linstor.piraeusExternal" . -}} apiVersion: piraeus.io/v1 kind: LinstorCluster metadata: @@ -7,7 +10,7 @@ metadata: spec: #nodeSelector: # node-role.kubernetes.io/linstor: "" - {{- if eq .Values.backend "blockstor" }} + {{- if $external }} {{- /* External mode: piraeus-operator runs no linstor-controller of its own. linstor-csi talks to blockstor-apiserver, which serves the LINSTOR REST API on the mTLS port :3371 only. The URL has to match @@ -25,7 +28,7 @@ spec: {{- end }} apiTLS: certManager: - {{- if eq .Values.backend "blockstor" }} + {{- if $external }} {{- /* The operator issues linstor-csi-{controller,node}-tls from this Issuer, so the CSI pods present a client certificate chained to the CA blockstor-apiserver verifies. */}} @@ -34,7 +37,7 @@ spec: name: linstor-api-ca {{- end }} kind: Issuer - {{- if eq .Values.backend "blockstor" }} + {{- if $external }} {{- /* The operator is a client of the API itself: it probes /v1/controller/version before every LinstorCluster and LinstorSatellite reconcile. In external mode it issues no @@ -64,7 +67,7 @@ spec: value: "20" - name: DrbdOptions/Net/timeout value: "100" - {{- if eq .Values.backend "linstor" }} + {{- if not $external }} controller: enabled: true podTemplate: @@ -182,7 +185,7 @@ spec: - name: linstor-wait-node-online image: {{ .Values.linstorCSI.image.repository }}:{{ .Values.linstorCSI.image.tag }} patches: - {{- if eq .Values.backend "linstor" }} + {{- if not $external }} - target: kind: Deployment name: linstor-controller diff --git a/packages/system/linstor/templates/linstor-api-tls.yaml b/packages/system/linstor/templates/linstor-api-tls.yaml index c178b1a67..a66c8234f 100644 --- a/packages/system/linstor/templates/linstor-api-tls.yaml +++ b/packages/system/linstor/templates/linstor-api-tls.yaml @@ -1,4 +1,4 @@ -{{- if eq .Values.backend "linstor" }} +{{- if not (include "linstor.piraeusExternal" .) }} --- apiVersion: cert-manager.io/v1 kind: Certificate diff --git a/packages/system/linstor/templates/plunger/configmap-scripts.yaml b/packages/system/linstor/templates/plunger/configmap-scripts.yaml index 871a554d2..388e861a0 100644 --- a/packages/system/linstor/templates/plunger/configmap-scripts.yaml +++ b/packages/system/linstor/templates/plunger/configmap-scripts.yaml @@ -1,4 +1,4 @@ -{{- if eq .Values.backend "linstor" }} +{{- if not (include "linstor.piraeusExternal" .) }} {{- $files := .Files.Glob "hack/plunger/*.sh" -}} {{/* TODO Add checksum of scripts to the pod selectors */}} --- diff --git a/packages/system/linstor/templates/podscrape.yaml b/packages/system/linstor/templates/podscrape.yaml index 2ed584d38..f19cd7aa3 100644 --- a/packages/system/linstor/templates/podscrape.yaml +++ b/packages/system/linstor/templates/podscrape.yaml @@ -20,7 +20,7 @@ spec: selector: matchLabels: app.kubernetes.io/component: linstor-satellite -{{- if eq .Values.backend "linstor" }} +{{- if not (include "linstor.piraeusExternal" .) }} --- apiVersion: operator.victoriametrics.com/v1beta1 kind: VMPodScrape diff --git a/packages/system/linstor/templates/satellites-plunger.yaml b/packages/system/linstor/templates/satellites-plunger.yaml index 8a0fc603a..f193b19aa 100644 --- a/packages/system/linstor/templates/satellites-plunger.yaml +++ b/packages/system/linstor/templates/satellites-plunger.yaml @@ -1,4 +1,4 @@ -{{- if eq .Values.backend "linstor" }} +{{- if not (include "linstor.piraeusExternal" .) }} apiVersion: piraeus.io/v1 kind: LinstorSatelliteConfiguration metadata: diff --git a/packages/system/linstor/tests/blockstor_migration_test.yaml b/packages/system/linstor/tests/blockstor_migration_test.yaml new file mode 100644 index 000000000..d0da1a457 --- /dev/null +++ b/packages/system/linstor/tests/blockstor_migration_test.yaml @@ -0,0 +1,196 @@ +suite: blockstor adopting a LINSTOR cluster + +# The runbook freezes CSI before blockstor adopts, and repoints CSI only once +# adoption has converged, while piraeus-operator keeps running. So hold and +# adopt render every piraeus object byte for byte as backend linstor does, +# which leaves the operator nothing to change, and only clearing the value +# moves piraeus to external mode. + +release: + name: linstor + namespace: cozy-linstor + +tests: + - it: "keeps LinstorCluster on the LINSTOR controller while holding" + set: + backend: blockstor + blockstorMigration: hold + templates: + - templates/cluster.yaml + asserts: + - notExists: + path: spec.externalController + - equal: + path: spec.internalTLS.certManager.name + value: linstor-internal-ca + - equal: + path: spec.apiTLS.certManager.name + value: linstor-api-ca + - notExists: + path: spec.apiTLS.clientSecretName + - equal: + path: spec.controller.enabled + value: true + + - it: "keeps LinstorCluster on the LINSTOR controller while adopting" + set: + backend: blockstor + blockstorMigration: adopt + templates: + - templates/cluster.yaml + asserts: + - notExists: + path: spec.externalController + - equal: + path: spec.controller.enabled + value: true + + # Dropping the plunger configuration is a change to a piraeus object too, and + # its ConfigMap is mounted by the LINSTOR controller and satellite pods the + # operator keeps in place. + - it: "keeps the plunger and the LINSTOR API CA during the migration" + set: + backend: blockstor + blockstorMigration: hold + templates: + - templates/satellites-plunger.yaml + - templates/plunger/configmap-scripts.yaml + - templates/linstor-api-tls.yaml + asserts: + - hasDocuments: + count: 1 + template: templates/satellites-plunger.yaml + - hasDocuments: + count: 1 + template: templates/plunger/configmap-scripts.yaml + - hasDocuments: + count: 2 + template: templates/linstor-api-tls.yaml + + - it: "keeps scraping the LINSTOR controller during the migration" + set: + backend: blockstor + blockstorMigration: adopt + capabilities: + apiVersions: + - operator.victoriametrics.com/v1beta1 + templates: + - templates/podscrape.yaml + asserts: + - hasDocuments: + count: 2 + + - it: "moves LinstorCluster to external mode once the migration is cleared" + set: + backend: blockstor + blockstorMigration: "" + templates: + - templates/cluster.yaml + asserts: + - equal: + path: spec.externalController.url + value: https://blockstor-apiserver.cozy-linstor.svc:3371 + - notExists: + path: spec.controller + + - it: "deploys blockstor while holding" + set: + backend: blockstor + blockstorMigration: hold + templates: + - templates/blockstor-apiserver.yaml + - templates/blockstor-satellite.yaml + - templates/crds/crds.yaml + asserts: + - isKind: + of: Deployment + template: templates/blockstor-apiserver.yaml + documentSelector: + path: kind + value: Deployment + - isKind: + of: DaemonSet + template: templates/blockstor-satellite.yaml + documentSelector: + path: kind + value: DaemonSet + - hasDocuments: + count: 8 + template: templates/crds/crds.yaml + + # Started before the converted manifests are in, the controller places + # replicas of its own under definitions whose Resources have not arrived. + # Rendered at 0 rather than scaled by hand, so a retried or repeated upgrade + # does not start it. + - it: "stops the blockstor controller while holding" + set: + backend: blockstor + blockstorMigration: hold + template: templates/blockstor-controller.yaml + documentSelector: + path: kind + value: Deployment + asserts: + - equal: + path: spec.replicas + value: 0 + + - it: "runs the blockstor controller while adopting" + set: + backend: blockstor + blockstorMigration: adopt + template: templates/blockstor-controller.yaml + documentSelector: + path: kind + value: Deployment + asserts: + - equal: + path: spec.replicas + value: 1 + + # The hook names the passphrase Secret; the controller started in adopt has + # to find it in place. + - it: "points ControllerConfig at the passphrase while holding" + set: + backend: blockstor + blockstorMigration: hold + template: templates/blockstor-controllerconfig.yaml + documentSelector: + path: kind + value: Job + asserts: + - isKind: + of: Job + + # blockstor-satellite runs on the nodes from hold on, so the hook would down + # its DRBD resources there as it would after the switch. + - it: "refuses the Talos shutdown hook during the migration" + set: + backend: blockstor + blockstorMigration: adopt + talos.gracefulShutdown.enabled: true + templates: + - templates/satellites-talos.yaml + asserts: + - failedTemplate: + errorMessage: "linstor: talos.gracefulShutdown.enabled is not supported with backend blockstor: its hook would tear down blockstor's DRBD resources" + + - it: "refuses a migration step it does not know" + set: + backend: blockstor + blockstorMigration: adopted + templates: + - templates/backend-guard.yaml + asserts: + - failedTemplate: + errorMessage: 'linstor: blockstorMigration must be "", "hold" or "adopt", got "adopted"' + + - it: "refuses a migration step under the LINSTOR backend" + set: + backend: linstor + blockstorMigration: hold + templates: + - templates/backend-guard.yaml + asserts: + - failedTemplate: + errorMessage: 'linstor: blockstorMigration "hold" is only valid with backend blockstor; clear it together with switching the backend back' diff --git a/packages/system/linstor/tests/test_migration_render.py b/packages/system/linstor/tests/test_migration_render.py new file mode 100644 index 000000000..dd2a20122 --- /dev/null +++ b/packages/system/linstor/tests/test_migration_render.py @@ -0,0 +1,51 @@ +from pathlib import Path +import shutil +import subprocess +import unittest + +CHART = Path(__file__).resolve().parents[1] + +# Every template that renders a piraeus.io object, or something one of them +# references. While blockstorMigration is set the release must give piraeus +# nothing new: the operator keeps running through the migration, and has to +# find LINSTOR's shape and keep linstor-csi on LINSTOR until the value is +# cleared. helm-unittest asserts on one render at a time, so the comparison +# across renders lives here. +PIRAEUS_TEMPLATES = [ + "templates/cluster.yaml", + "templates/satellites-cozy.yaml", + "templates/satellites-plunger.yaml", + "templates/satellites-reloader.yaml", + "templates/satellites-talos.yaml", + "templates/plunger/configmap-scripts.yaml", + "templates/linstor-api-tls.yaml", + "templates/linstor-internal-tls.yaml", + "templates/podscrape.yaml", +] + + +def render(*values): + args = ["helm", "template", "linstor", str(CHART), "--namespace", "cozy-linstor", + "--api-versions", "operator.victoriametrics.com/v1beta1"] + for value in values: + args += ["--set", value] + out = subprocess.run(args, check=True, capture_output=True, text=True).stdout + sources = [f"# Source: cozy-linstor/{template}" for template in PIRAEUS_TEMPLATES] + docs = [doc.removeprefix("---\n") for doc in out.split("\n---\n")] + return [doc for doc in docs if doc.split("\n", 1)[0] in sources] + + +@unittest.skipUnless(shutil.which("helm"), "helm is required") +class MigrationLeavesPiraeusAlone(unittest.TestCase): + def test_hold_and_adopt_render_piraeus_as_linstor(self): + linstor = render("backend=linstor") + for step in ("hold", "adopt"): + with self.subTest(step=step): + self.assertEqual(render("backend=blockstor", f"blockstorMigration={step}"), linstor) + + def test_completed_migration_changes_piraeus(self): + self.assertNotEqual(render("backend=blockstor"), render("backend=linstor")) + + +if __name__ == "__main__": + unittest.main() diff --git a/packages/system/linstor/values.yaml b/packages/system/linstor/values.yaml index c893de51d..bf84c094c 100644 --- a/packages/system/linstor/values.yaml +++ b/packages/system/linstor/values.yaml @@ -10,12 +10,34 @@ # linstor-csi build, so the StorageClasses (provisioner # linstor.csi.linbit.com) are the same under either. # -# blockstor is for new clusters. This chart does not move a cluster that -# already holds LINSTOR volumes: blockstor adopts them with its own -# linstor-migrate runbook, which the chart does not run: -# https://blockstor.org/docs/migration/ +# A new cluster selects blockstor here. A cluster that already holds +# LINSTOR volumes moves only through blockstorMigration below, in the +# order README.md gives; switching backend alone does not adopt them. backend: linstor +# Set only while a cluster that holds LINSTOR volumes moves to blockstor, +# with backend blockstor. The runbook freezes CSI and stops LINSTOR before +# blockstor adopts the volumes, and repoints CSI only once adoption has +# converged; one backend flip cannot express that order. Each step is a +# value: +# +# hold — blockstor is deployed with its controller stopped. Every +# piraeus object renders as under backend linstor, so the +# operator keeps linstor-csi on LINSTOR. Apply the converted +# manifests in this step. +# adopt — the blockstor controller runs and adopts the volumes. piraeus +# is still untouched. +# "" — migration done: LinstorCluster goes external and the operator +# repoints linstor-csi at blockstor-apiserver. +# +# piraeus stays untouched because piraeus-operator keeps running through +# the migration, as blockstor's runbook has it: given LINSTOR's shape it +# has nothing to change, leaves linstor-controller and linstor-csi-controller +# at the zero replicas the freeze set, and keeps linstor-csi on LINSTOR. +# Clearing this value is the one step that changes piraeus. README.md has +# the order. +blockstorMigration: "" + piraeusServer: image: repository: ghcr.io/cozystack/cozystack/piraeus-server