@@ -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.
1111func 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.
5167func 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}
0 commit comments