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
5 changes: 5 additions & 0 deletions .github/workflows/prepare-longhorn-v2-node.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ name: Prepare Longhorn V2 Migration
resume-umami-archive-isolation,
resume-umami-archive-data,
observe-umami-v2-standby,
replace-umami-v2-standby,
register-temporary-v2-storage,
repair-v1-reservation,
reserve-v1-scratch,
Expand Down Expand Up @@ -195,6 +196,10 @@ jobs:
[[ -z "${LONGHORN_V2_NODE}" && "${LONGHORN_V2_DATABASE}" == umami-db &&
"${LONGHORN_V2_CONFIRM}" =~ ^observe-umami-v2-standby:umami-db-[1-9][0-9]*:umami-db-[1-9][0-9]*$ ]]
;;
replace-umami-v2-standby)
[[ -z "${LONGHORN_V2_NODE}" && "${LONGHORN_V2_DATABASE}" == umami-db &&
"${LONGHORN_V2_CONFIRM}" =~ ^replace-umami-v2-standby:umami-db-[1-9][0-9]*:umami-db-[1-9][0-9]*$ ]]
;;
prepare-worker)
[[ "${LONGHORN_V2_NODE}" =~ ^prod-worker-[123]$ && "${LONGHORN_V2_CONFIRM}" == "prepare-longhorn-v2-${LONGHORN_V2_NODE}" ]]
;;
Expand Down
99 changes: 99 additions & 0 deletions scripts/migrate-longhorn-v2/cnpg_standby_claim_retention.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package main

import (
"errors"
"maps"
"reflect"
)

// umamiStandbyClaimRetentionChanges prepares the metadata change used by
// CloudNativePG's keep-PVC operation. The caller must apply it through the
// existing UID and resource-version patch fence, then verify the retained
// claim before asking the eviction API to stop the original consumer.
func umamiStandbyClaimRetentionChanges(witness cnpgStandbyCutover, cluster, pod, claim, pv, volume object) ([]object, error) {
const (
statusAnnotation = "cnpg.io/pvcStatus"
statusPath = "/metadata/annotations/cnpg.io~1pvcStatus"
instanceLabel = "cnpg.io/instanceName"
)
name := witness.Consumer.Name
owners, _ := value(claim, "metadata", "ownerReferences").([]any)
if benchmarkIdentity(cluster) != witness.Database || !umamiV2Transition(cluster) ||
text(cluster, "status", "currentPrimary") == name || text(pod, "metadata", "labels", "cnpg.io/instanceRole") != "replica" ||
benchmarkIdentity(pod) != witness.Consumer || text(pod, "metadata", "resourceVersion") != witness.ConsumerVersion ||
benchmarkIdentity(claim) != witness.Claim || benchmarkIdentity(pv) != witness.PV ||
benchmarkIdentity(volume) != witness.Volume || !umamiV1StorageRetained(cluster, pod, claim, pv, volume) ||
len(owners) != 1 || text(claim, "metadata", "annotations", statusAnnotation) != "ready" ||
text(claim, "metadata", "labels", instanceLabel) != name {
return nil, errors.New("the original standby claim changed before retention")
}
return []object{
{"op": "test", "path": "/metadata/ownerReferences", "value": owners},
{"op": "test", "path": statusPath, "value": "ready"},
{"op": "replace", "path": statusPath, "value": "detached"},
{"op": "remove", "path": "/metadata/ownerReferences/0"},
}, nil
}

func verifyUmamiStandbyClaimRetention(original, retained object) error {
wantAnnotations := maps.Clone(valueObject(original, "metadata", "annotations"))
if wantAnnotations["cnpg.io/pvcStatus"] != "ready" {
return errors.New("original standby claim readiness is unverified")
}
wantAnnotations["cnpg.io/pvcStatus"] = "detached"
owners := value(retained, "metadata", "ownerReferences")
if owners != nil && !reflect.DeepEqual(owners, []any{}) {
return errors.New("retained standby claim still has an owner")
}
if !recoveredNativeObject(retained, "v1", "PersistentVolumeClaim", "umami") ||
benchmarkIdentity(retained) != benchmarkIdentity(original) ||
text(retained, "metadata", "resourceVersion") == text(original, "metadata", "resourceVersion") ||
!reflect.DeepEqual(wantAnnotations, value(retained, "metadata", "annotations")) ||
!reflect.DeepEqual(value(retained, "metadata", "labels"), value(original, "metadata", "labels")) ||
!reflect.DeepEqual(value(retained, "metadata", "finalizers"), value(original, "metadata", "finalizers")) ||
!reflect.DeepEqual(value(retained, "spec"), value(original, "spec")) ||
text(retained, "status", "phase") != "Bound" {
return errors.New("the retained standby claim could not be verified")
}
return nil
}

// verifyUmamiStandbyDetached accepts the old backend only when its original
// claim still protects it and neither a Pod nor a CSI attachment can use it.
func verifyUmamiStandbyDetached(witness cnpgStandbyCutover, claim, pv, volume object, pods, attachments []object) error {
refused := errors.New("the retained standby storage is not demonstrably detached")
name := witness.Consumer.Name
owners := value(claim, "metadata", "ownerReferences")
robustness := text(volume, "status", "robustness")
if pods == nil || attachments == nil ||
benchmarkIdentity(claim) != witness.Claim || benchmarkIdentity(pv) != witness.PV ||
benchmarkIdentity(volume) != witness.Volume || !verifyUmamiOriginalClaimBinding(claim, pv, volume) ||
text(claim, "metadata", "labels", "cnpg.io/instanceName") != name ||
text(claim, "metadata", "annotations", "cnpg.io/pvcStatus") != "detached" ||
(owners != nil && !reflect.DeepEqual(owners, []any{})) ||
text(volume, "status", "state") != "detached" || text(volume, "status", "currentNodeID") != "" ||
text(volume, "spec", "nodeID") != "" || (robustness != "unknown" && robustness != "healthy") {
return refused
}
for _, pod := range pods {
if text(pod, "metadata", "namespace") != witness.Consumer.Namespace {
return refused
}
if text(pod, "metadata", "name") == name {
return refused
}
mounts, _ := value(pod, "spec", "volumes").([]any)
for _, raw := range mounts {
mount, _ := raw.(object)
if text(mount, "persistentVolumeClaim", "claimName") == witness.Claim.Name {
return refused
}
}
}
for _, attachment := range attachments {
if text(attachment, "spec", "source", "persistentVolumeName") == witness.PV.Name {
return refused
}
}
return nil
}
122 changes: 122 additions & 0 deletions scripts/migrate-longhorn-v2/cnpg_standby_claim_retention_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package main

import (
"encoding/json"
"reflect"
"testing"

jsonpatch "gopkg.in/evanphx/json-patch.v4"
)

func TestUmamiStandbyRetentionChangesOnlyControllerOwnership(t *testing.T) {
for _, fault := range []string{"", "replacement UID", "became primary", "extra owner", "wrong instance label", "unready claim", "lost Retain"} {
t.Run(fault, func(t *testing.T) {
observation, pod, claim, pv, volume, pdb := umamiStandbyCutoverFixture(t)
cluster := observation.Cluster
target := text(pod, "metadata", "name")
valueObject(claim, "metadata", "labels")["cnpg.io/instanceName"] = target
valueObject(claim, "metadata")["annotations"] = object{"cnpg.io/pvcStatus": "ready", "preserve": "existing"}
witness, err := bindUmamiStandbyCutover(observation, observation.Pods, target, claim, pv, volume, pdb)
if err != nil {
t.Fatal(err)
}
switch fault {
case "replacement UID":
valueObject(claim, "metadata")["uid"] = "ac005841-6666-4666-8666-666666666666"
case "became primary":
valueObject(cluster, "status")["currentPrimary"] = target
case "extra owner":
refs := value(claim, "metadata", "ownerReferences").([]any)
valueObject(claim, "metadata")["ownerReferences"] = append(refs, object{"kind": "Other"})
case "wrong instance label":
valueObject(claim, "metadata", "labels")["cnpg.io/instanceName"] = "umami-db-3"
case "unready claim":
valueObject(claim, "metadata", "annotations")["cnpg.io/pvcStatus"] = "initializing"
case "lost Retain":
valueObject(pv, "spec")["persistentVolumeReclaimPolicy"] = "Delete"
}
changes, err := umamiStandbyClaimRetentionChanges(witness, cluster, pod, claim, pv, volume)
if fault != "" {
if err == nil || len(changes) != 0 {
t.Fatal("changed original storage accepted")
}
return
}
if err != nil {
t.Fatal(err)
}
data, err := json.Marshal(append(identityTests(claim), changes...))
if err != nil {
t.Fatal(err)
}
patch, err := jsonpatch.DecodePatch(data)
if err != nil {
t.Fatal(err)
}
original, err := json.Marshal(claim)
if err != nil {
t.Fatal(err)
}
patched, err := patch.Apply(original)
var retained object
if err != nil || json.Unmarshal(patched, &retained) != nil {
t.Fatal("retention patch did not apply")
}
valueObject(retained, "metadata")["resourceVersion"] = "12"
if benchmarkIdentity(retained) != witness.Claim || !reflect.DeepEqual(value(retained, "spec"), value(claim, "spec")) ||
!reflect.DeepEqual(value(retained, "metadata", "labels"), value(claim, "metadata", "labels")) ||
text(retained, "metadata", "annotations", "cnpg.io/pvcStatus") != "detached" ||
text(retained, "metadata", "annotations", "preserve") != "existing" ||
!reflect.DeepEqual(value(retained, "metadata", "ownerReferences"), []any{}) {
t.Fatal("retention patch changed storage or left CNPG ownership")
}
if err := verifyUmamiStandbyClaimRetention(claim, retained); err != nil {
t.Fatal(err)
}
valueObject(retained, "spec")["volumeName"] = "foreign"
if verifyUmamiStandbyClaimRetention(claim, retained) == nil {
t.Fatal("retention readback accepted a different backing volume")
}
})
}
}

func TestUmamiStandbyDetachedKeepsOriginalVolumeBoundAndUnused(t *testing.T) {
for _, fault := range []string{"", "unretained", "claim deletion", "faulted", "unknown pods", "same pod name", "foreign consumer", "CSI attachment"} {
t.Run(fault, func(t *testing.T) {
observation, pod, claim, pv, volume, pdb := umamiStandbyCutoverFixture(t)
target := text(pod, "metadata", "name")
witness, err := bindUmamiStandbyCutover(observation, observation.Pods, target, claim, pv, volume, pdb)
if err != nil {
t.Fatal(err)
}
delete(valueObject(claim, "metadata"), "ownerReferences")
valueObject(claim, "metadata")["annotations"] = object{"cnpg.io/pvcStatus": "detached"}
valueObject(claim, "metadata", "labels")["cnpg.io/instanceName"] = target
status := valueObject(volume, "status")
status["state"], status["robustness"], status["currentNodeID"] = "detached", "unknown", ""
pods, attachments := []object{}, []object{}
switch fault {
case "unretained":
valueObject(pv, "spec")["persistentVolumeReclaimPolicy"] = "Delete"
case "claim deletion":
valueObject(claim, "metadata")["deletionTimestamp"] = "2026-11-27T05:00:00Z"
case "faulted":
status["robustness"] = "faulted"
case "unknown pods":
pods = nil
case "same pod name":
valueObject(pod, "metadata")["uid"] = "new"
pods = []object{pod}
case "foreign consumer":
valueObject(pod, "metadata")["name"] = "foreign"
pods = []object{pod}
case "CSI attachment":
attachments = []object{{"spec": object{"source": object{"persistentVolumeName": witness.PV.Name}}}}
}
if err := verifyUmamiStandbyDetached(witness, claim, pv, volume, pods, attachments); (err == nil) != (fault == "") {
t.Fatalf("retained original attachment result: %v", err)
}
})
}
}
19 changes: 14 additions & 5 deletions scripts/migrate-longhorn-v2/cnpg_standby_cutover.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,25 @@ func umamiStandbyDisruptionAllowed(budget, cluster object) bool {
}

func umamiV1StorageRetained(cluster, pod, claim, pv, volume object) bool {
return cnpgOwned(claim, cluster) && text(claim, "metadata", "name") == text(pod, "metadata", "name") &&
verifyUmamiOriginalClaimBinding(claim, pv, volume) && umamiOriginalVolumeAttached(pod, volume)
}

func umamiOriginalVolumeAttached(pod, volume object) bool {
node := text(pod, "spec", "nodeName")
return node != "" && text(volume, "status", "currentNodeID") == node &&
text(volume, "status", "state") == "attached" && text(volume, "status", "robustness") == "healthy"
}

func verifyUmamiOriginalClaimBinding(claim, pv, volume object) bool {
const namespace, class, size = "umami", "longhorn-wffc", "5Gi"
name := text(pod, "metadata", "name")
name := text(claim, "metadata", "name")
uid := text(claim, "metadata", "uid")
backend := "pvc-" + uid
node := text(pod, "spec", "nodeName")
finalizers, _ := value(claim, "metadata", "finalizers").([]any)
return recoveredNativeObject(claim, "v1", "PersistentVolumeClaim", namespace) &&
slices.Contains(finalizers, "kubernetes.io/pvc-protection") &&
cnpgOwned(claim, cluster) && text(claim, "metadata", "name") == name && text(claim, "status", "phase") == "Bound" &&
text(claim, "status", "phase") == "Bound" &&
text(claim, "spec", "storageClassName") == class && text(claim, "spec", "volumeName") == backend &&
text(claim, "spec", "resources", "requests", "storage") == size && text(claim, "status", "capacity", "storage") == size &&
providerRWOFilesystem(claim) &&
Expand All @@ -95,7 +105,6 @@ func umamiV1StorageRetained(cluster, pod, claim, pv, volume object) bool {
recoveredNativeObject(volume, "longhorn.io/v1beta2", "Volume", "longhorn-system") &&
text(volume, "metadata", "name") == backend && text(volume, "spec", "dataEngine") == "v1" &&
text(volume, "spec", "size") == "5368709120" && value(volume, "spec", "numberOfReplicas") == float64(3) &&
node != "" && text(volume, "status", "currentNodeID") == node && text(volume, "status", "state") == "attached" &&
text(volume, "status", "robustness") == "healthy" && text(volume, "status", "kubernetesStatus", "pvName") == backend &&
text(volume, "status", "kubernetesStatus", "pvName") == backend &&
text(volume, "status", "kubernetesStatus", "pvcName") == name && text(volume, "status", "kubernetesStatus", "namespace") == namespace
}
8 changes: 6 additions & 2 deletions scripts/migrate-longhorn-v2/cnpg_standby_cutover_observe.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,18 @@ import (
const umamiStandbyObservationOperation = "observe-umami-v2-standby"

func authorizeUmamiStandbyObservation(getenv func(string) string) (string, string, error) {
return authorizeUmamiStandbyTarget(getenv, umamiStandbyObservationOperation)
}

func authorizeUmamiStandbyTarget(getenv func(string) string, operation string) (string, string, error) {
const database = "umami-db"
if getenv("LONGHORN_V2_OPERATION") != umamiStandbyObservationOperation || getenv("LONGHORN_V2_DATABASE") != database ||
if getenv("LONGHORN_V2_OPERATION") != operation || getenv("LONGHORN_V2_DATABASE") != database ||
getenv("LONGHORN_V2_NODE") != "" {
return "", "", errors.New("the exact Umami standby observation is required")
}
confirmation := getenv("LONGHORN_V2_CONFIRM")
items := strings.Split(confirmation, ":")
if len(items) != 3 || items[0] != umamiStandbyObservationOperation || items[1] == items[2] {
if len(items) != 3 || items[0] != operation || items[1] == items[2] {
return "", "", errors.New("the original and spare standby must be specified and distinct")
}
for _, name := range items[1:] {
Expand Down
Loading
Loading