Skip to content

Commit a33ed5d

Browse files
authored
feat(storage): replace one Umami standby on Longhorn V2 (#6716)
* fix(storage): preserve Umami standby claims during replacement Fence detachment on the current replica and its retained PVC, PV, and Longhorn volume. Keep the original claim and verify the exact data binding after removing controller ownership. Part of #5842 * fix(storage): verify the retained Umami volume is detached Require the original PVC to remain bound and protected after removing the standby. Refuse a faulted backend or any remaining Pod consumer or CSI attachment. Part of #5842 * feat(storage): replace one Umami standby on Longhorn V2 Prove the retained archive and healthy V2 spare, journal each write before retaining the original claim or evicting its Pod, and verify detached recovery storage and the replacement member. Keep the primary and unaffected database members bound across the operation. Part of #5842
1 parent 16c0564 commit a33ed5d

13 files changed

Lines changed: 1037 additions & 39 deletions

‎.github/workflows/prepare-longhorn-v2-node.yaml‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ name: Prepare Longhorn V2 Migration
4949
resume-umami-archive-isolation,
5050
resume-umami-archive-data,
5151
observe-umami-v2-standby,
52+
replace-umami-v2-standby,
5253
register-temporary-v2-storage,
5354
repair-v1-reservation,
5455
reserve-v1-scratch,
@@ -195,6 +196,10 @@ jobs:
195196
[[ -z "${LONGHORN_V2_NODE}" && "${LONGHORN_V2_DATABASE}" == umami-db &&
196197
"${LONGHORN_V2_CONFIRM}" =~ ^observe-umami-v2-standby:umami-db-[1-9][0-9]*:umami-db-[1-9][0-9]*$ ]]
197198
;;
199+
replace-umami-v2-standby)
200+
[[ -z "${LONGHORN_V2_NODE}" && "${LONGHORN_V2_DATABASE}" == umami-db &&
201+
"${LONGHORN_V2_CONFIRM}" =~ ^replace-umami-v2-standby:umami-db-[1-9][0-9]*:umami-db-[1-9][0-9]*$ ]]
202+
;;
198203
prepare-worker)
199204
[[ "${LONGHORN_V2_NODE}" =~ ^prod-worker-[123]$ && "${LONGHORN_V2_CONFIRM}" == "prepare-longhorn-v2-${LONGHORN_V2_NODE}" ]]
200205
;;
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package main
2+
3+
import (
4+
"errors"
5+
"maps"
6+
"reflect"
7+
)
8+
9+
// umamiStandbyClaimRetentionChanges prepares the metadata change used by
10+
// CloudNativePG's keep-PVC operation. The caller must apply it through the
11+
// existing UID and resource-version patch fence, then verify the retained
12+
// claim before asking the eviction API to stop the original consumer.
13+
func umamiStandbyClaimRetentionChanges(witness cnpgStandbyCutover, cluster, pod, claim, pv, volume object) ([]object, error) {
14+
const (
15+
statusAnnotation = "cnpg.io/pvcStatus"
16+
statusPath = "/metadata/annotations/cnpg.io~1pvcStatus"
17+
instanceLabel = "cnpg.io/instanceName"
18+
)
19+
name := witness.Consumer.Name
20+
owners, _ := value(claim, "metadata", "ownerReferences").([]any)
21+
if benchmarkIdentity(cluster) != witness.Database || !umamiV2Transition(cluster) ||
22+
text(cluster, "status", "currentPrimary") == name || text(pod, "metadata", "labels", "cnpg.io/instanceRole") != "replica" ||
23+
benchmarkIdentity(pod) != witness.Consumer || text(pod, "metadata", "resourceVersion") != witness.ConsumerVersion ||
24+
benchmarkIdentity(claim) != witness.Claim || benchmarkIdentity(pv) != witness.PV ||
25+
benchmarkIdentity(volume) != witness.Volume || !umamiV1StorageRetained(cluster, pod, claim, pv, volume) ||
26+
len(owners) != 1 || text(claim, "metadata", "annotations", statusAnnotation) != "ready" ||
27+
text(claim, "metadata", "labels", instanceLabel) != name {
28+
return nil, errors.New("the original standby claim changed before retention")
29+
}
30+
return []object{
31+
{"op": "test", "path": "/metadata/ownerReferences", "value": owners},
32+
{"op": "test", "path": statusPath, "value": "ready"},
33+
{"op": "replace", "path": statusPath, "value": "detached"},
34+
{"op": "remove", "path": "/metadata/ownerReferences/0"},
35+
}, nil
36+
}
37+
38+
func verifyUmamiStandbyClaimRetention(original, retained object) error {
39+
wantAnnotations := maps.Clone(valueObject(original, "metadata", "annotations"))
40+
if wantAnnotations["cnpg.io/pvcStatus"] != "ready" {
41+
return errors.New("original standby claim readiness is unverified")
42+
}
43+
wantAnnotations["cnpg.io/pvcStatus"] = "detached"
44+
owners := value(retained, "metadata", "ownerReferences")
45+
if owners != nil && !reflect.DeepEqual(owners, []any{}) {
46+
return errors.New("retained standby claim still has an owner")
47+
}
48+
if !recoveredNativeObject(retained, "v1", "PersistentVolumeClaim", "umami") ||
49+
benchmarkIdentity(retained) != benchmarkIdentity(original) ||
50+
text(retained, "metadata", "resourceVersion") == text(original, "metadata", "resourceVersion") ||
51+
!reflect.DeepEqual(wantAnnotations, value(retained, "metadata", "annotations")) ||
52+
!reflect.DeepEqual(value(retained, "metadata", "labels"), value(original, "metadata", "labels")) ||
53+
!reflect.DeepEqual(value(retained, "metadata", "finalizers"), value(original, "metadata", "finalizers")) ||
54+
!reflect.DeepEqual(value(retained, "spec"), value(original, "spec")) ||
55+
text(retained, "status", "phase") != "Bound" {
56+
return errors.New("the retained standby claim could not be verified")
57+
}
58+
return nil
59+
}
60+
61+
// verifyUmamiStandbyDetached accepts the old backend only when its original
62+
// claim still protects it and neither a Pod nor a CSI attachment can use it.
63+
func verifyUmamiStandbyDetached(witness cnpgStandbyCutover, claim, pv, volume object, pods, attachments []object) error {
64+
refused := errors.New("the retained standby storage is not demonstrably detached")
65+
name := witness.Consumer.Name
66+
owners := value(claim, "metadata", "ownerReferences")
67+
robustness := text(volume, "status", "robustness")
68+
if pods == nil || attachments == nil ||
69+
benchmarkIdentity(claim) != witness.Claim || benchmarkIdentity(pv) != witness.PV ||
70+
benchmarkIdentity(volume) != witness.Volume || !verifyUmamiOriginalClaimBinding(claim, pv, volume) ||
71+
text(claim, "metadata", "labels", "cnpg.io/instanceName") != name ||
72+
text(claim, "metadata", "annotations", "cnpg.io/pvcStatus") != "detached" ||
73+
(owners != nil && !reflect.DeepEqual(owners, []any{})) ||
74+
text(volume, "status", "state") != "detached" || text(volume, "status", "currentNodeID") != "" ||
75+
text(volume, "spec", "nodeID") != "" || (robustness != "unknown" && robustness != "healthy") {
76+
return refused
77+
}
78+
for _, pod := range pods {
79+
if text(pod, "metadata", "namespace") != witness.Consumer.Namespace {
80+
return refused
81+
}
82+
if text(pod, "metadata", "name") == name {
83+
return refused
84+
}
85+
mounts, _ := value(pod, "spec", "volumes").([]any)
86+
for _, raw := range mounts {
87+
mount, _ := raw.(object)
88+
if text(mount, "persistentVolumeClaim", "claimName") == witness.Claim.Name {
89+
return refused
90+
}
91+
}
92+
}
93+
for _, attachment := range attachments {
94+
if text(attachment, "spec", "source", "persistentVolumeName") == witness.PV.Name {
95+
return refused
96+
}
97+
}
98+
return nil
99+
}
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"reflect"
6+
"testing"
7+
8+
jsonpatch "gopkg.in/evanphx/json-patch.v4"
9+
)
10+
11+
func TestUmamiStandbyRetentionChangesOnlyControllerOwnership(t *testing.T) {
12+
for _, fault := range []string{"", "replacement UID", "became primary", "extra owner", "wrong instance label", "unready claim", "lost Retain"} {
13+
t.Run(fault, func(t *testing.T) {
14+
observation, pod, claim, pv, volume, pdb := umamiStandbyCutoverFixture(t)
15+
cluster := observation.Cluster
16+
target := text(pod, "metadata", "name")
17+
valueObject(claim, "metadata", "labels")["cnpg.io/instanceName"] = target
18+
valueObject(claim, "metadata")["annotations"] = object{"cnpg.io/pvcStatus": "ready", "preserve": "existing"}
19+
witness, err := bindUmamiStandbyCutover(observation, observation.Pods, target, claim, pv, volume, pdb)
20+
if err != nil {
21+
t.Fatal(err)
22+
}
23+
switch fault {
24+
case "replacement UID":
25+
valueObject(claim, "metadata")["uid"] = "ac005841-6666-4666-8666-666666666666"
26+
case "became primary":
27+
valueObject(cluster, "status")["currentPrimary"] = target
28+
case "extra owner":
29+
refs := value(claim, "metadata", "ownerReferences").([]any)
30+
valueObject(claim, "metadata")["ownerReferences"] = append(refs, object{"kind": "Other"})
31+
case "wrong instance label":
32+
valueObject(claim, "metadata", "labels")["cnpg.io/instanceName"] = "umami-db-3"
33+
case "unready claim":
34+
valueObject(claim, "metadata", "annotations")["cnpg.io/pvcStatus"] = "initializing"
35+
case "lost Retain":
36+
valueObject(pv, "spec")["persistentVolumeReclaimPolicy"] = "Delete"
37+
}
38+
changes, err := umamiStandbyClaimRetentionChanges(witness, cluster, pod, claim, pv, volume)
39+
if fault != "" {
40+
if err == nil || len(changes) != 0 {
41+
t.Fatal("changed original storage accepted")
42+
}
43+
return
44+
}
45+
if err != nil {
46+
t.Fatal(err)
47+
}
48+
data, err := json.Marshal(append(identityTests(claim), changes...))
49+
if err != nil {
50+
t.Fatal(err)
51+
}
52+
patch, err := jsonpatch.DecodePatch(data)
53+
if err != nil {
54+
t.Fatal(err)
55+
}
56+
original, err := json.Marshal(claim)
57+
if err != nil {
58+
t.Fatal(err)
59+
}
60+
patched, err := patch.Apply(original)
61+
var retained object
62+
if err != nil || json.Unmarshal(patched, &retained) != nil {
63+
t.Fatal("retention patch did not apply")
64+
}
65+
valueObject(retained, "metadata")["resourceVersion"] = "12"
66+
if benchmarkIdentity(retained) != witness.Claim || !reflect.DeepEqual(value(retained, "spec"), value(claim, "spec")) ||
67+
!reflect.DeepEqual(value(retained, "metadata", "labels"), value(claim, "metadata", "labels")) ||
68+
text(retained, "metadata", "annotations", "cnpg.io/pvcStatus") != "detached" ||
69+
text(retained, "metadata", "annotations", "preserve") != "existing" ||
70+
!reflect.DeepEqual(value(retained, "metadata", "ownerReferences"), []any{}) {
71+
t.Fatal("retention patch changed storage or left CNPG ownership")
72+
}
73+
if err := verifyUmamiStandbyClaimRetention(claim, retained); err != nil {
74+
t.Fatal(err)
75+
}
76+
valueObject(retained, "spec")["volumeName"] = "foreign"
77+
if verifyUmamiStandbyClaimRetention(claim, retained) == nil {
78+
t.Fatal("retention readback accepted a different backing volume")
79+
}
80+
})
81+
}
82+
}
83+
84+
func TestUmamiStandbyDetachedKeepsOriginalVolumeBoundAndUnused(t *testing.T) {
85+
for _, fault := range []string{"", "unretained", "claim deletion", "faulted", "unknown pods", "same pod name", "foreign consumer", "CSI attachment"} {
86+
t.Run(fault, func(t *testing.T) {
87+
observation, pod, claim, pv, volume, pdb := umamiStandbyCutoverFixture(t)
88+
target := text(pod, "metadata", "name")
89+
witness, err := bindUmamiStandbyCutover(observation, observation.Pods, target, claim, pv, volume, pdb)
90+
if err != nil {
91+
t.Fatal(err)
92+
}
93+
delete(valueObject(claim, "metadata"), "ownerReferences")
94+
valueObject(claim, "metadata")["annotations"] = object{"cnpg.io/pvcStatus": "detached"}
95+
valueObject(claim, "metadata", "labels")["cnpg.io/instanceName"] = target
96+
status := valueObject(volume, "status")
97+
status["state"], status["robustness"], status["currentNodeID"] = "detached", "unknown", ""
98+
pods, attachments := []object{}, []object{}
99+
switch fault {
100+
case "unretained":
101+
valueObject(pv, "spec")["persistentVolumeReclaimPolicy"] = "Delete"
102+
case "claim deletion":
103+
valueObject(claim, "metadata")["deletionTimestamp"] = "2026-11-27T05:00:00Z"
104+
case "faulted":
105+
status["robustness"] = "faulted"
106+
case "unknown pods":
107+
pods = nil
108+
case "same pod name":
109+
valueObject(pod, "metadata")["uid"] = "new"
110+
pods = []object{pod}
111+
case "foreign consumer":
112+
valueObject(pod, "metadata")["name"] = "foreign"
113+
pods = []object{pod}
114+
case "CSI attachment":
115+
attachments = []object{{"spec": object{"source": object{"persistentVolumeName": witness.PV.Name}}}}
116+
}
117+
if err := verifyUmamiStandbyDetached(witness, claim, pv, volume, pods, attachments); (err == nil) != (fault == "") {
118+
t.Fatalf("retained original attachment result: %v", err)
119+
}
120+
})
121+
}
122+
}

‎scripts/migrate-longhorn-v2/cnpg_standby_cutover.go‎

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,25 @@ func umamiStandbyDisruptionAllowed(budget, cluster object) bool {
7474
}
7575

7676
func umamiV1StorageRetained(cluster, pod, claim, pv, volume object) bool {
77+
return cnpgOwned(claim, cluster) && text(claim, "metadata", "name") == text(pod, "metadata", "name") &&
78+
verifyUmamiOriginalClaimBinding(claim, pv, volume) && umamiOriginalVolumeAttached(pod, volume)
79+
}
80+
81+
func umamiOriginalVolumeAttached(pod, volume object) bool {
82+
node := text(pod, "spec", "nodeName")
83+
return node != "" && text(volume, "status", "currentNodeID") == node &&
84+
text(volume, "status", "state") == "attached" && text(volume, "status", "robustness") == "healthy"
85+
}
86+
87+
func verifyUmamiOriginalClaimBinding(claim, pv, volume object) bool {
7788
const namespace, class, size = "umami", "longhorn-wffc", "5Gi"
78-
name := text(pod, "metadata", "name")
89+
name := text(claim, "metadata", "name")
7990
uid := text(claim, "metadata", "uid")
8091
backend := "pvc-" + uid
81-
node := text(pod, "spec", "nodeName")
8292
finalizers, _ := value(claim, "metadata", "finalizers").([]any)
8393
return recoveredNativeObject(claim, "v1", "PersistentVolumeClaim", namespace) &&
8494
slices.Contains(finalizers, "kubernetes.io/pvc-protection") &&
85-
cnpgOwned(claim, cluster) && text(claim, "metadata", "name") == name && text(claim, "status", "phase") == "Bound" &&
95+
text(claim, "status", "phase") == "Bound" &&
8696
text(claim, "spec", "storageClassName") == class && text(claim, "spec", "volumeName") == backend &&
8797
text(claim, "spec", "resources", "requests", "storage") == size && text(claim, "status", "capacity", "storage") == size &&
8898
providerRWOFilesystem(claim) &&
@@ -95,7 +105,6 @@ func umamiV1StorageRetained(cluster, pod, claim, pv, volume object) bool {
95105
recoveredNativeObject(volume, "longhorn.io/v1beta2", "Volume", "longhorn-system") &&
96106
text(volume, "metadata", "name") == backend && text(volume, "spec", "dataEngine") == "v1" &&
97107
text(volume, "spec", "size") == "5368709120" && value(volume, "spec", "numberOfReplicas") == float64(3) &&
98-
node != "" && text(volume, "status", "currentNodeID") == node && text(volume, "status", "state") == "attached" &&
99-
text(volume, "status", "robustness") == "healthy" && text(volume, "status", "kubernetesStatus", "pvName") == backend &&
108+
text(volume, "status", "kubernetesStatus", "pvName") == backend &&
100109
text(volume, "status", "kubernetesStatus", "pvcName") == name && text(volume, "status", "kubernetesStatus", "namespace") == namespace
101110
}

‎scripts/migrate-longhorn-v2/cnpg_standby_cutover_observe.go‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,18 @@ import (
1111
const umamiStandbyObservationOperation = "observe-umami-v2-standby"
1212

1313
func authorizeUmamiStandbyObservation(getenv func(string) string) (string, string, error) {
14+
return authorizeUmamiStandbyTarget(getenv, umamiStandbyObservationOperation)
15+
}
16+
17+
func authorizeUmamiStandbyTarget(getenv func(string) string, operation string) (string, string, error) {
1418
const database = "umami-db"
15-
if getenv("LONGHORN_V2_OPERATION") != umamiStandbyObservationOperation || getenv("LONGHORN_V2_DATABASE") != database ||
19+
if getenv("LONGHORN_V2_OPERATION") != operation || getenv("LONGHORN_V2_DATABASE") != database ||
1620
getenv("LONGHORN_V2_NODE") != "" {
1721
return "", "", errors.New("the exact Umami standby observation is required")
1822
}
1923
confirmation := getenv("LONGHORN_V2_CONFIRM")
2024
items := strings.Split(confirmation, ":")
21-
if len(items) != 3 || items[0] != umamiStandbyObservationOperation || items[1] == items[2] {
25+
if len(items) != 3 || items[0] != operation || items[1] == items[2] {
2226
return "", "", errors.New("the original and spare standby must be specified and distinct")
2327
}
2428
for _, name := range items[1:] {

0 commit comments

Comments
 (0)