Skip to content

Commit 4c2059e

Browse files
committed
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 cfad2c8 commit 4c2059e

11 files changed

Lines changed: 809 additions & 38 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
;;

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

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

7676
func umamiV1StorageRetained(cluster, pod, claim, pv, volume object) bool {
77-
node := text(pod, "spec", "nodeName")
7877
return cnpgOwned(claim, cluster) && text(claim, "metadata", "name") == text(pod, "metadata", "name") &&
79-
verifyUmamiOriginalClaimBinding(claim, pv, volume) &&
80-
node != "" && text(volume, "status", "currentNodeID") == node && text(volume, "status", "state") == "attached" &&
81-
text(volume, "status", "robustness") == "healthy"
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"
8285
}
8386

8487
func verifyUmamiOriginalClaimBinding(claim, pv, volume object) bool {

‎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:] {

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

Lines changed: 64 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -9,75 +9,107 @@ import (
99
// Ask the Kubernetes Eviction API to exercise admission and its PDB checks for
1010
// the exact observed standby with persistence disabled at both API boundaries.
1111
func verifyUmamiStandbyEvictionAdmission(ctx context.Context, c client, source string, witness cnpgStandbyCutover) error {
12+
if !validUmamiEvictionTarget(c, witness) {
13+
return errors.New("the Umami eviction target is unverified")
14+
}
15+
if err := recheckUmamiStandbyAdmission(ctx, c, witness); err != nil {
16+
return err
17+
}
18+
if err := sourceCurrent(ctx, c.run, source); err != nil {
19+
return err
20+
}
21+
if err := requestUmamiStandbyEviction(ctx, c, witness, true); err != nil {
22+
return err
23+
}
24+
if err := recheckUmamiStandbyAdmission(ctx, c, witness); err != nil {
25+
return err
26+
}
27+
return sourceCurrent(ctx, c.run, source)
28+
}
29+
30+
func requestUmamiStandbyEviction(ctx context.Context, c client, witness cnpgStandbyCutover, dryRun bool) error {
1231
const namespace = "umami"
1332
pod := witness.Consumer
14-
refused := errors.New("the exact standby did not pass the Kubernetes dry-run eviction")
15-
if _, err := cnpgInstanceID(pod.Name, "umami-db"); err != nil || pod.Namespace != namespace ||
16-
!nativeRestoreUID(pod.UID) || witness.ConsumerVersion == "" || witness.BudgetVersion == "" ||
17-
c.context != "admin@prod" || c.run == nil {
33+
refused := errors.New("the exact standby did not pass Kubernetes eviction admission")
34+
if !validUmamiEvictionTarget(c, witness) {
1835
return refused
1936
}
20-
input := object{"apiVersion": "policy/v1", "kind": "Eviction",
21-
"metadata": object{"name": pod.Name, "namespace": namespace},
22-
"deleteOptions": object{"dryRun": []string{"All"}, "preconditions": object{
23-
"uid": pod.UID, "resourceVersion": witness.ConsumerVersion,
24-
}},
37+
path := "/api/v1/namespaces/" + namespace + "/pods/" + pod.Name + "/eviction"
38+
options := object{"preconditions": object{"uid": pod.UID, "resourceVersion": witness.ConsumerVersion}}
39+
if dryRun {
40+
options["dryRun"] = []string{"All"}
41+
path += "?dryRun=All"
2542
}
43+
input := object{"apiVersion": "policy/v1", "kind": "Eviction",
44+
"metadata": object{"name": pod.Name, "namespace": namespace}, "deleteOptions": options}
2645
encoded, err := json.Marshal(input)
2746
if err != nil {
2847
return refused
2948
}
30-
if err := recheckUmamiStandbyAdmission(ctx, c, witness); err != nil {
31-
return err
32-
}
33-
if err := sourceCurrent(ctx, c.run, source); err != nil {
34-
return err
35-
}
36-
path := "/api/v1/namespaces/" + namespace + "/pods/" + pod.Name + "/eviction?dryRun=All"
3749
raw, err := c.kubectl(ctx, encoded, "create", "--raw", path, "-f", "-")
3850
var status object
3951
if err != nil || json.Unmarshal(raw, &status) != nil || text(status, "apiVersion") != "v1" ||
4052
text(status, "kind") != "Status" || text(status, "status") != "Success" {
4153
return refused
4254
}
43-
if err := recheckUmamiStandbyAdmission(ctx, c, witness); err != nil {
44-
return err
45-
}
46-
return sourceCurrent(ctx, c.run, source)
55+
return nil
56+
}
57+
58+
func validUmamiEvictionTarget(c client, witness cnpgStandbyCutover) bool {
59+
pod := witness.Consumer
60+
_, err := cnpgInstanceID(pod.Name, "umami-db")
61+
return err == nil && pod.Namespace == "umami" && nativeRestoreUID(pod.UID) &&
62+
witness.ConsumerVersion != "" && witness.BudgetVersion != "" && c.context == "admin@prod" && c.run != nil
4763
}
4864

4965
// A dry-run success can also mean that no budget matched. Check the same
5066
// budget revision, pod, retained volume and sole consumer on both sides.
5167
func recheckUmamiStandbyAdmission(ctx context.Context, c client, witness cnpgStandbyCutover) error {
68+
actual, err := readUmamiStandbyAdmission(ctx, c, witness)
69+
if err != nil {
70+
return err
71+
}
72+
if !umamiV1StorageRetained(actual.cluster, actual.pod, actual.claim, actual.pv, actual.volume) {
73+
return errors.New("the original standby storage changed during admission")
74+
}
75+
return nil
76+
}
77+
78+
type umamiStandbyAdmission struct {
79+
cluster, pod, claim, pv, volume, budget object
80+
}
81+
82+
func readUmamiStandbyAdmission(ctx context.Context, c client, witness cnpgStandbyCutover) (umamiStandbyAdmission, error) {
5283
refused := errors.New("the standby, disruption budget or retained storage changed during admission")
53-
var cluster, claim, pv, volume, budget object
84+
var actual umamiStandbyAdmission
5485
for _, resource := range []struct {
5586
kind string
5687
identity benchmarkResource
5788
dest *object
5889
}{
59-
{"clusters.postgresql.cnpg.io", witness.Database, &cluster},
60-
{"persistentvolumeclaim", witness.Claim, &claim},
61-
{"persistentvolume", witness.PV, &pv},
62-
{"volumes.longhorn.io", witness.Volume, &volume},
63-
{"poddisruptionbudget", witness.Budget, &budget},
90+
{"clusters.postgresql.cnpg.io", witness.Database, &actual.cluster},
91+
{"persistentvolumeclaim", witness.Claim, &actual.claim},
92+
{"persistentvolume", witness.PV, &actual.pv},
93+
{"volumes.longhorn.io", witness.Volume, &actual.volume},
94+
{"poddisruptionbudget", witness.Budget, &actual.budget},
6495
} {
6596
current, err := c.get(ctx, resource.kind, resource.identity.Namespace, resource.identity.Name, false)
6697
if err != nil || benchmarkIdentity(current) != resource.identity {
67-
return refused
98+
return umamiStandbyAdmission{}, refused
6899
}
69100
*resource.dest = current
70101
}
71102
pods, err := c.list(ctx, "pods", "umami")
72-
pod := cnpgRecoveryPodByName(pods, witness.Consumer.Name)
103+
actual.pod = cnpgRecoveryPodByName(pods, witness.Consumer.Name)
104+
pod, cluster, budget := actual.pod, actual.cluster, actual.budget
73105
if err != nil || benchmarkIdentity(pod) != witness.Consumer ||
74106
text(pod, "metadata", "resourceVersion") != witness.ConsumerVersion ||
75107
text(budget, "metadata", "resourceVersion") != witness.BudgetVersion ||
76108
!umamiV2Transition(cluster) || text(cluster, "status", "currentPrimary") == witness.Consumer.Name ||
77109
verifyPostgresPod(pod, pod, cluster) != nil || text(pod, "metadata", "labels", "cnpg.io/instanceRole") != "replica" ||
78-
!recoveredPostgresMount(pod, claim) || !onlyCNPGStandbyUsesClaim(pods, pod, witness.Claim.Name, witness.Consumer.Namespace) ||
79-
!umamiStandbyDisruptionAllowed(budget, cluster) || !umamiV1StorageRetained(cluster, pod, claim, pv, volume) {
80-
return refused
110+
!recoveredPostgresMount(pod, actual.claim) || !onlyCNPGStandbyUsesClaim(pods, pod, witness.Claim.Name, witness.Consumer.Namespace) ||
111+
!umamiStandbyDisruptionAllowed(budget, cluster) {
112+
return umamiStandbyAdmission{}, refused
81113
}
82-
return nil
114+
return actual, nil
83115
}

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,34 @@ func TestUmamiStandbyEvictionPreflightOnlyUsesNativeDryRun(t *testing.T) {
107107
}
108108
}
109109
}
110+
111+
func TestUmamiStandbyEvictionWritesOnlyTheExactPodSubresource(t *testing.T) {
112+
witness := cnpgStandbyCutover{
113+
Consumer: benchmarkResource{"umami-db-2", "umami", "ac005841-5555-4555-8555-555555555555"},
114+
ConsumerVersion: "23",
115+
BudgetVersion: "50",
116+
}
117+
calls := 0
118+
c := client{context: "admin@prod", run: func(_ context.Context, input []byte, tool string, args ...string) ([]byte, error) {
119+
calls++
120+
want := []string{"--context=admin@prod", "--request-timeout=20s", "create", "--raw", "/api/v1/namespaces/umami/pods/umami-db-2/eviction", "-f", "-"}
121+
if tool != "kubectl" || !slices.Equal(args, want) {
122+
t.Fatal("the real request did not use the exact Eviction subresource")
123+
}
124+
var body object
125+
if json.Unmarshal(input, &body) != nil || text(body, "apiVersion") != "policy/v1" || text(body, "kind") != "Eviction" ||
126+
text(body, "deleteOptions", "preconditions", "uid") != witness.Consumer.UID ||
127+
text(body, "deleteOptions", "preconditions", "resourceVersion") != witness.ConsumerVersion ||
128+
value(body, "deleteOptions", "dryRun") != nil || value(body, "deleteOptions", "gracePeriodSeconds") != nil {
129+
t.Fatal("the real eviction lost identity fencing or normal termination")
130+
}
131+
return json.Marshal(object{"apiVersion": "v1", "kind": "Status", "status": "Success"})
132+
}}
133+
if err := requestUmamiStandbyEviction(t.Context(), c, witness, false); err != nil {
134+
t.Fatal(err)
135+
}
136+
witness.ConsumerVersion = ""
137+
if err := requestUmamiStandbyEviction(t.Context(), c, witness, false); err == nil || calls != 1 {
138+
t.Fatal("an unfenced Pod reached the real eviction endpoint")
139+
}
140+
}

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,29 @@ func TestUmamiStandbyObservationEntryPointUsesTheProtectedMainDispatch(t *testin
6363
}
6464
}
6565

66+
func TestUmamiStandbyReplacementRequiresItsOwnConfirmation(t *testing.T) {
67+
workflow := migrationDispatchWorkflow(t)
68+
choices, _ := value(workflow, "on", "workflow_dispatch", "inputs", "operation", "options").([]any)
69+
if !slices.Contains(choices, umamiStandbyReplacementOperation) {
70+
t.Fatal("the production workflow cannot run the standby replacement")
71+
}
72+
env := umamiStandbyDispatch()
73+
env["LONGHORN_V2_OPERATION"] = umamiStandbyReplacementOperation
74+
lookup := func(key string) string { return env[key] }
75+
if _, _, err := authorizeUmamiStandbyTarget(lookup, umamiStandbyReplacementOperation); err == nil {
76+
t.Fatal("a read-only confirmation authorized replacement")
77+
}
78+
env["LONGHORN_V2_CONFIRM"] = "replace-umami-v2-standby:umami-db-1:umami-db-4"
79+
if old, spare, err := authorizeUmamiStandbyTarget(lookup, umamiStandbyReplacementOperation); err != nil ||
80+
old != "umami-db-1" || spare != "umami-db-4" {
81+
t.Fatalf("the confirmed old standby and spare were refused: %v", err)
82+
}
83+
env["GITHUB_REF"] = "refs/heads/other"
84+
if _, _, err := authorizeUmamiStandbyTarget(lookup, umamiStandbyReplacementOperation); err == nil {
85+
t.Fatal("a branch dispatch authorized replacement")
86+
}
87+
}
88+
6689
func umamiStandbyDispatch() map[string]string {
6790
env := dispatchFixture()
6891
env["LONGHORN_V2_OPERATION"], env["LONGHORN_V2_NODE"] = umamiStandbyObservationOperation, ""

0 commit comments

Comments
 (0)