|
| 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 | +} |
0 commit comments