@@ -84,14 +84,33 @@ func (c *Controller) onUpdate(oldObj, newObj interface{}) {
8484 // Lookup the pgcluster CR for PG cluster associated with this Pod. Since a 'pg-cluster'
8585 // label was found on updated Pod, this lookup should always succeed.
8686 clusterName := newPodLabels [config .LABEL_PG_CLUSTER ]
87+ namespace := newPod .ObjectMeta .Namespace
8788 cluster := crv1.Pgcluster {}
88- _ , err := kubeapi .Getpgcluster (c .PodClient , & cluster , clusterName ,
89- newPod .ObjectMeta .Namespace )
89+ _ , err := kubeapi .Getpgcluster (c .PodClient , & cluster , clusterName , namespace )
9090 if err != nil {
9191 log .Error (err .Error ())
9292 return
9393 }
9494
95+ // For the following upgrade and cluster initialization scenarios we only care about updates
96+ // where the database container within the pod is becoming ready. We can therefore return
97+ // at this point if this condition is false.
98+ if cluster .Status .State != crv1 .PgclusterStateInitialized &&
99+ (isDBContainerBecomingReady (oldPod , newPod ) ||
100+ isBackRestRepoBecomingReady (oldPod , newPod )) {
101+ if err := c .handleClusterInit (newPod , & cluster ); err != nil {
102+ log .Error (err )
103+ return
104+ }
105+ return
106+ }
107+
108+ // the handlers called below are only applicable to PG pods when the cluster is
109+ // in an initialized status
110+ if cluster .Status .State != crv1 .PgclusterStateInitialized || ! isPostgresPod (newPod ) {
111+ return
112+ }
113+
95114 // Handle the "role" label change from "replica" to "master" following a failover. This
96115 // logic is only triggered when the cluster has already been initialized, which implies
97116 // a failover or switchover has occurred.
@@ -104,8 +123,7 @@ func (c *Controller) onUpdate(oldObj, newObj interface{}) {
104123 }
105124 }
106125
107- if cluster .Status .State == crv1 .PgclusterStateInitialized &&
108- isPromotedStandby (oldPod , newPod ) {
126+ if isPromotedStandby (oldPod , newPod ) {
109127 log .Debugf ("Pod Controller: standby pod %s in namespace %s promoted, calling standby pod " +
110128 "promotion handler" , newPod .Name , newPod .Namespace )
111129 if err := c .handleStandbyPromotion (newPod , cluster ); err != nil {
@@ -114,32 +132,17 @@ func (c *Controller) onUpdate(oldObj, newObj interface{}) {
114132 }
115133 }
116134
117- // For the following upgrade and cluster initialization scenarios we only care about updates
118- // where the database container within the pod is becoming ready. We can therefore return
119- // at this point if this condition is false.
120- if ! isDBContainerBecomingReady (oldPod , newPod ) {
121- return
122- }
123-
124135 // First handle pod update as needed if the update was part of an ongoing upgrade
125136 if cluster .Labels [config .LABEL_MINOR_UPGRADE ] == config .LABEL_UPGRADE_IN_PROGRESS {
126- log .Debugf ("Pod Controller: upgrade pod %s now ready, calling pod upgrade " +
127- "handler" , newPod .Name , newPod . Namespace )
137+ log .Debugf ("Pod Controller: upgrade pod %s (namespace %s) now ready, calling pod upgrade " +
138+ "handler" , newPod .Name , namespace )
128139 if err := c .handleUpgradePodUpdate (newPod , & cluster ); err != nil {
129140 log .Error (err )
130141 return
131142 }
132143 }
133144
134- // Handle postgresql pod updates as needed for cluster initialization
135- if cluster .Status .State != crv1 .PgclusterStateInitialized && isPostgresPrimaryPod (newPod ) {
136- log .Debugf ("Pod Controller: pg pod %s now ready in an unintialized cluster, calling " +
137- "cluster init handler" , newPod .Name , newPod .Namespace )
138- if err := c .handleClusterInit (newPod , & cluster ); err != nil {
139- log .Error (err )
140- return
141- }
142- }
145+ return
143146}
144147
145148// onDelete is called when a pgcluster is deleted
@@ -166,25 +169,43 @@ func (c *Controller) AddPodEventHandler() {
166169 log .Debugf ("Pod Controller: added event handler to informer" )
167170}
168171
169- // isDBContainerBecomingReady checks to see if the Pod update shows that the Pod has
170- // transitioned from an 'unready' status to a 'ready' status.
171- func isDBContainerBecomingReady (oldPod , newPod * apiv1.Pod ) bool {
172- if ! isPostgresPod (newPod ) {
172+ // isBackRestRepoBecomingReady checks to see if the Pod update shows that the BackRest
173+ // repo Pod has transitioned from an 'unready' status to a 'ready' status.
174+ func isBackRestRepoBecomingReady (oldPod , newPod * apiv1.Pod ) bool {
175+ if ! isBackRestRepoPod (newPod ) {
173176 return false
174177 }
175- var oldDatabaseStatus bool
176- // first see if the old version of the pod was not ready
178+ return isContainerBecomingReady ("database" , oldPod , newPod )
179+ }
180+
181+ // isBackRestRepoPod determines whether or not a pod is a pgBackRest repository Pod. This is
182+ // determined by checking to see if the 'pgo-backrest-repo' label is present on the Pod (also,
183+ // this controller will only process pod with the 'vendor=crunchydata' label, so that label is
184+ // assumed to be present), specifically because this label will only be included on pgBackRest
185+ // repository Pods.
186+ func isBackRestRepoPod (newpod * apiv1.Pod ) bool {
187+
188+ _ , backrestRepoLabelExists := newpod .ObjectMeta .Labels [config .LABEL_PGO_BACKREST_REPO ]
189+
190+ return backrestRepoLabelExists
191+ }
192+
193+ // isContainerBecomingReady determines whether or not that container specified is moving
194+ // from an unready status to a ready status.
195+ func isContainerBecomingReady (containerName string , oldPod , newPod * apiv1.Pod ) bool {
196+ var oldContainerStatus bool
197+ // first see if the old version of the container was not ready
177198 for _ , v := range oldPod .Status .ContainerStatuses {
178- if v .Name == "database" {
179- oldDatabaseStatus = v .Ready
199+ if v .Name == containerName {
200+ oldContainerStatus = v .Ready
180201 break
181202 }
182203 }
183- // if the old version of the pod was not ready, now check if the
204+ // if the old version of the container was not ready, now check if the
184205 // new version is ready
185- if ! oldDatabaseStatus {
206+ if ! oldContainerStatus {
186207 for _ , v := range newPod .Status .ContainerStatuses {
187- if v .Name == "database" {
208+ if v .Name == containerName {
188209 if v .Ready {
189210 return true
190211 }
@@ -194,19 +215,26 @@ func isDBContainerBecomingReady(oldPod, newPod *apiv1.Pod) bool {
194215 return false
195216}
196217
197- // isPostgresPrimaryPod determines whether or not the specific Pod provided is the primary database
198- // Pod within a PG cluster. This is done by checking to see if the "role" label for the Pod is set
199- // to either "master", as set by Patroni to identify the current primary, or "promoted", as set by
200- // Patroni when promoting a replica to be the new primary.
201- func isPostgresPrimaryPod (newPod * apiv1.Pod ) bool {
218+ // isDBContainerBecomingReady checks to see if the Pod update shows that the Pod has
219+ // transitioned from an 'unready' status to a 'ready' status.
220+ func isDBContainerBecomingReady (oldPod , newPod * apiv1.Pod ) bool {
202221 if ! isPostgresPod (newPod ) {
203222 return false
204223 }
205- if newPod .ObjectMeta .Labels [config .LABEL_PGHA_ROLE ] == "master" ||
206- newPod .ObjectMeta .Labels [config .LABEL_PGHA_ROLE ] == "promoted" {
207- return true
208- }
209- return false
224+ return isContainerBecomingReady ("database" , oldPod , newPod )
225+ }
226+
227+ // isPostgresPod determines whether or not a pod is a PostreSQL Pod, specifically either the
228+ // primary or a replica pod within a PG cluster. This is determined by checking to see if the
229+ // 'pgo-pg-database' label is present on the Pod (also, this controller will only process pod with
230+ // the 'vendor=crunchydata' label, so that label is assumed to be present), specifically because
231+ // this label will only be included on primary and replica PostgreSQL database pods (and will be
232+ // present as soon as the deployment and pod is created).
233+ func isPostgresPod (newpod * apiv1.Pod ) bool {
234+
235+ _ , pgDatabaseLabelExists := newpod .ObjectMeta .Labels [config .LABEL_PG_DATABASE ]
236+
237+ return pgDatabaseLabelExists
210238}
211239
212240// isPromotedPostgresPod determines if the Pod update is the result of the promotion of the pod
@@ -225,11 +253,11 @@ func isPromotedPostgresPod(oldPod, newPod *apiv1.Pod) bool {
225253 return false
226254}
227255
228- // isPromotedPostgresPod determines if the Pod update is the result of the promotion of the pod
256+ // isPromotedStandby determines if the Pod update is the result of the promotion of the standby pod
229257// from a replica to the primary within a PG cluster. This is determined by comparing the 'role'
230258// label from the old Pod to the 'role' label in the New pod, specifically to determine if the
231- // label has changed from "promoted " to "master" (this is the label change that will be performed
232- // by Patroni when promoting a pod).
259+ // label has changed from "standby_leader " to "master" (this is the label change that will be
260+ // performed by Patroni when promoting a pod).
233261func isPromotedStandby (oldPod , newPod * apiv1.Pod ) bool {
234262 if ! isPostgresPod (newPod ) {
235263 return false
@@ -243,16 +271,3 @@ func isPromotedStandby(oldPod, newPod *apiv1.Pod) bool {
243271 }
244272 return false
245273}
246-
247- // isPostgresPod determines whether or not a pod is a PostreSQL Pod, specifically either the
248- // primary or a replica pod within a PG cluster. This is determined by checking to see if the
249- // 'pgo-pg-database' label is present on the Pod (also, this controller will only process pod with
250- // the 'vendor=crunchydata' label, so that label is assumed to be present), specifically because
251- // this label will only be included on primary and replica PostgreSQL database pods (and will be
252- // present as soon as the deployment and pod is created).
253- func isPostgresPod (newpod * apiv1.Pod ) bool {
254-
255- _ , pgDatabaseLabelExists := newpod .ObjectMeta .Labels [config .LABEL_PG_DATABASE ]
256-
257- return pgDatabaseLabelExists
258- }
0 commit comments