|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "reflect" |
| 6 | + "slices" |
| 7 | +) |
| 8 | + |
| 9 | +type cnpgStandbyCutover struct { |
| 10 | + Database, Consumer, Claim, PV, Volume, Budget benchmarkResource |
| 11 | + ConsumerVersion, BudgetVersion string |
| 12 | + Spare cnpgRecoveredStorage |
| 13 | +} |
| 14 | + |
| 15 | +// Bind the current database and its sole volume consumer before recording a |
| 16 | +// replacement. The original PV and its V1 backend remain reserved to this |
| 17 | +// claim while CloudNativePG provisions a replacement from its V2 template. |
| 18 | +func bindUmamiStandbyCutover(observation cnpgObservation, pods []object, target string, claim, pv, volume, budget object) (cnpgStandbyCutover, error) { |
| 19 | + cluster := observation.Cluster |
| 20 | + ns := text(cluster, "metadata", "namespace") |
| 21 | + primary := text(cluster, "status", "currentPrimary") |
| 22 | + unqualified := errors.New("the selected database standby or retained storage binding is unverified") |
| 23 | + if !umamiV2Transition(cluster) || target == primary || len(observation.Pods) != 4 || len(observation.States) != 4 || |
| 24 | + verifyPostgresStandby(observation.States[primary], observation.States[target]) != nil { |
| 25 | + return cnpgStandbyCutover{}, unqualified |
| 26 | + } |
| 27 | + byName, err := indexed(observation.Pods) |
| 28 | + if err != nil { |
| 29 | + return cnpgStandbyCutover{}, err |
| 30 | + } |
| 31 | + pod := byName[target] |
| 32 | + if verifyPostgresPod(pod, pod, cluster) != nil || text(pod, "metadata", "labels", "cnpg.io/instanceRole") != "replica" || |
| 33 | + !recoveredPostgresMount(pod, claim) || !onlyCNPGStandbyUsesClaim(pods, pod, target, ns) || |
| 34 | + !umamiStandbyDisruptionAllowed(budget, cluster) || !umamiV1StorageRetained(cluster, pod, claim, pv, volume) { |
| 35 | + return cnpgStandbyCutover{}, unqualified |
| 36 | + } |
| 37 | + return cnpgStandbyCutover{Database: benchmarkIdentity(cluster), Consumer: benchmarkIdentity(pod), |
| 38 | + ConsumerVersion: text(pod, "metadata", "resourceVersion"), Claim: benchmarkIdentity(claim), |
| 39 | + PV: benchmarkIdentity(pv), Volume: benchmarkIdentity(volume), Budget: benchmarkIdentity(budget), |
| 40 | + BudgetVersion: text(budget, "metadata", "resourceVersion")}, nil |
| 41 | +} |
| 42 | + |
| 43 | +func onlyCNPGStandbyUsesClaim(pods []object, pod object, claim, namespace string) bool { |
| 44 | + seen := 0 |
| 45 | + for _, other := range pods { |
| 46 | + if text(other, "metadata", "namespace") != namespace { |
| 47 | + continue |
| 48 | + } |
| 49 | + specVolumes, _ := value(other, "spec", "volumes").([]any) |
| 50 | + for _, raw := range specVolumes { |
| 51 | + volume, _ := raw.(object) |
| 52 | + if text(volume, "persistentVolumeClaim", "claimName") == claim { |
| 53 | + if benchmarkIdentity(other) != benchmarkIdentity(pod) || value(other, "metadata", "deletionTimestamp") != nil { |
| 54 | + return false |
| 55 | + } |
| 56 | + seen++ |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + return seen == 1 |
| 61 | +} |
| 62 | + |
| 63 | +func umamiStandbyDisruptionAllowed(budget, cluster object) bool { |
| 64 | + labels := object{"cnpg.io/cluster": "umami-db", "cnpg.io/instanceRole": "replica"} |
| 65 | + generation, ok := nativeObservationUint(value(budget, "metadata", "generation")) |
| 66 | + return recoveredNativeObject(budget, "policy/v1", "PodDisruptionBudget", "umami") && |
| 67 | + text(budget, "metadata", "name") == "umami-db" && cnpgOwnerReference(budget, cluster) && ok && generation > 0 && |
| 68 | + reflect.DeepEqual(valueObject(budget, "spec", "selector"), object{"matchLabels": labels}) && |
| 69 | + value(budget, "spec", "minAvailable") == float64(2) && value(budget, "spec", "maxUnavailable") == nil && |
| 70 | + value(budget, "metadata", "generation") == value(budget, "status", "observedGeneration") && |
| 71 | + value(budget, "status", "currentHealthy") == float64(3) && value(budget, "status", "expectedPods") == float64(3) && |
| 72 | + value(budget, "status", "desiredHealthy") == float64(2) && value(budget, "status", "disruptionsAllowed") == float64(1) && |
| 73 | + len(valueObject(budget, "status", "disruptedPods")) == 0 |
| 74 | +} |
| 75 | + |
| 76 | +func umamiV1StorageRetained(cluster, pod, claim, pv, volume object) bool { |
| 77 | + const namespace, class, size = "umami", "longhorn-wffc", "5Gi" |
| 78 | + name := text(pod, "metadata", "name") |
| 79 | + uid := text(claim, "metadata", "uid") |
| 80 | + backend := "pvc-" + uid |
| 81 | + node := text(pod, "spec", "nodeName") |
| 82 | + finalizers, _ := value(claim, "metadata", "finalizers").([]any) |
| 83 | + return recoveredNativeObject(claim, "v1", "PersistentVolumeClaim", namespace) && |
| 84 | + slices.Contains(finalizers, "kubernetes.io/pvc-protection") && |
| 85 | + cnpgOwned(claim, cluster) && text(claim, "metadata", "name") == name && text(claim, "status", "phase") == "Bound" && |
| 86 | + text(claim, "spec", "storageClassName") == class && text(claim, "spec", "volumeName") == backend && |
| 87 | + text(claim, "spec", "resources", "requests", "storage") == size && text(claim, "status", "capacity", "storage") == size && |
| 88 | + providerRWOFilesystem(claim) && |
| 89 | + recoveredNativeObject(pv, "v1", "PersistentVolume", "") && providerRWOFilesystem(pv) && |
| 90 | + text(pv, "metadata", "name") == backend && text(pv, "status", "phase") == "Bound" && |
| 91 | + text(pv, "spec", "persistentVolumeReclaimPolicy") == "Retain" && text(pv, "spec", "storageClassName") == class && |
| 92 | + text(pv, "spec", "capacity", "storage") == size && text(pv, "spec", "claimRef", "uid") == uid && |
| 93 | + text(pv, "spec", "claimRef", "name") == name && text(pv, "spec", "claimRef", "namespace") == namespace && |
| 94 | + text(pv, "spec", "csi", "driver") == "driver.longhorn.io" && text(pv, "spec", "csi", "volumeHandle") == backend && |
| 95 | + recoveredNativeObject(volume, "longhorn.io/v1beta2", "Volume", "longhorn-system") && |
| 96 | + text(volume, "metadata", "name") == backend && text(volume, "spec", "dataEngine") == "v1" && |
| 97 | + 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 && |
| 100 | + text(volume, "status", "kubernetesStatus", "pvcName") == name && text(volume, "status", "kubernetesStatus", "namespace") == namespace |
| 101 | +} |
0 commit comments