@@ -40,12 +40,12 @@ import (
4040 "github.com/cloudnative-pg/cloudnative-pg/pkg/utils"
4141)
4242
43- // errLogShippingReplicaElected is raised when the pod update process need
43+ // errLogShippingReplicaElected is raised when the pod update process needs
4444// to select a new primary before upgrading the old primary, but the chosen
4545// instance is not connected via streaming replication
4646var errLogShippingReplicaElected = errors .New ("log shipping replica elected as a new post-switchover primary" )
4747
48- // errRolloutDelayed is raised the a pod rollout have been delayed because
48+ // errRolloutDelayed is raised when a pod rollout has been delayed because
4949// of the operator configuration
5050var errRolloutDelayed = errors .New ("pod rollout delayed" )
5151
@@ -155,7 +155,25 @@ func (r *ClusterReconciler) rolloutRequiredInstances(
155155 }
156156
157157 return r .updatePrimaryPod (ctx , cluster , podList , * primaryPostgresqlStatus .Pod ,
158- podRollout .canBeInPlace , podRollout .primaryForceRecreate , podRollout .reason )
158+ podRollout .canBeInPlace , podRollout .reason )
159+ }
160+
161+ func (r * ClusterReconciler ) switchPrimary (
162+ ctx context.Context ,
163+ cluster * apiv1.Cluster ,
164+ currentPrimaryName string ,
165+ targetPrimaryName string ,
166+ reason rolloutReason ,
167+ ) (bool , error ) {
168+ r .Recorder .Eventf (cluster , "Normal" , "Switchover" ,
169+ "Initiating switchover to %s to upgrade %s" , targetPrimaryName , currentPrimaryName )
170+ if err := r .RegisterPhase (ctx , cluster , apiv1 .PhaseUpgrade , reason ); err != nil {
171+ return false , err
172+ }
173+ if err := r .setPrimaryInstance (ctx , cluster , targetPrimaryName ); err != nil {
174+ return false , err
175+ }
176+ return true , nil
159177}
160178
161179func (r * ClusterReconciler ) updatePrimaryPod (
@@ -164,13 +182,12 @@ func (r *ClusterReconciler) updatePrimaryPod(
164182 podList * postgres.PostgresqlStatusList ,
165183 primaryPod corev1.Pod ,
166184 inPlacePossible bool ,
167- forceRecreate bool ,
168185 reason rolloutReason ,
169186) (bool , error ) {
170187 contextLogger := log .FromContext (ctx )
171188 contextLogger = contextLogger .WithValues ("primaryPod" , primaryPod .Name )
172189
173- if cluster .GetPrimaryUpdateMethod () == apiv1 .PrimaryUpdateMethodRestart || forceRecreate {
190+ if cluster .GetPrimaryUpdateMethod () == apiv1 .PrimaryUpdateMethodRestart {
174191 if inPlacePossible {
175192 // In-place restart is possible
176193 if err := r .updateRestartAnnotation (ctx , cluster , primaryPod ); err != nil {
@@ -214,8 +231,8 @@ func (r *ClusterReconciler) updatePrimaryPod(
214231 // is not active.
215232 if ! targetInstance .IsWalReceiverActive {
216233 contextLogger .Info (
217- "chosen new primary is still not connected via streaming replication, " +
218- "interrupting the primaryPodUpdate " ,
234+ "chosen new primary is still not connected via streaming replication. " +
235+ "Delaying the switchover " ,
219236 "updateReason" , reason ,
220237 "currentPrimary" , primaryPod .Name ,
221238 "targetPrimary" , targetInstance .Pod .Name ,
@@ -228,9 +245,8 @@ func (r *ClusterReconciler) updatePrimaryPod(
228245 "currentPrimary" , primaryPod .Name ,
229246 "targetPrimary" , targetInstance .Pod .Name )
230247 podList .LogStatus (ctx )
231- r .Recorder .Eventf (cluster , "Normal" , "Switchover" ,
232- "Initiating switchover to %s to upgrade %s" , targetInstance .Pod .Name , primaryPod .Name )
233- return true , r .setPrimaryInstance (ctx , cluster , targetInstance .Pod .Name )
248+
249+ return r .switchPrimary (ctx , cluster , primaryPod .Name , targetInstance .Pod .Name , reason )
234250 }
235251
236252 // if there is only one instance in the cluster, we should upgrade it even if it's a primary
@@ -270,12 +286,11 @@ func (r *ClusterReconciler) updateRestartAnnotation(
270286// rollout describes whether a rollout should happen, and if so whether it can
271287// be done in-place, and what the reason for the rollout is
272288type rollout struct {
273- required bool
274- canBeInPlace bool
275- primaryForceRecreate bool
289+ required bool
290+ canBeInPlace bool
276291
277- needsChangeOperatorImage bool
278- needsChangeOperandImage bool
292+ needsToChangeOperatorImage bool
293+ needsToChangeOperandImage bool
279294
280295 reason string
281296}
@@ -302,7 +317,7 @@ func isInstanceNeedingRollout(
302317 required : true ,
303318 reason : fmt .Sprintf ("pod '%s' is not reporting the executable hash" ,
304319 status .Pod .Name ),
305- needsChangeOperatorImage : true ,
320+ needsToChangeOperatorImage : true ,
306321 }
307322 }
308323
@@ -496,7 +511,7 @@ func checkPodImageIsOutdated(_ context.Context, pod *corev1.Pod, cluster *apiv1.
496511 required : true ,
497512 reason : fmt .Sprintf ("the instance is using a different image: %s -> %s" ,
498513 pgCurrentImageName , targetImageName ),
499- needsChangeOperandImage : true ,
514+ needsToChangeOperandImage : true ,
500515 }, nil
501516}
502517
@@ -519,16 +534,15 @@ func checkPodBootstrapImage(_ context.Context, pod *corev1.Pod, _ *apiv1.Cluster
519534 required : true ,
520535 reason : fmt .Sprintf ("the instance is using an old bootstrap container image: %s -> %s" ,
521536 opCurrentImageName , configuration .Current .OperatorImageName ),
522- needsChangeOperatorImage : true ,
537+ needsToChangeOperatorImage : true ,
523538 }, nil
524539}
525540
526541func checkHasMissingPVCs (_ context.Context , pod * corev1.Pod , cluster * apiv1.Cluster ) (rollout , error ) {
527542 if persistentvolumeclaim .InstanceHasMissingMounts (cluster , pod ) {
528543 return rollout {
529- required : true ,
530- primaryForceRecreate : true ,
531- reason : "attaching a new PVC to the instance Pod" ,
544+ required : true ,
545+ reason : "attaching a new PVC to the instance Pod" ,
532546 }, nil
533547 }
534548 return rollout {}, nil
@@ -544,9 +558,8 @@ func checkClusterHasDifferentRestartAnnotation(
544558 podRestart := pod .Annotations [utils .ClusterRestartAnnotationName ]
545559 if clusterRestart != podRestart {
546560 return rollout {
547- required : true ,
548- reason : "cluster has been explicitly restarted via annotation" ,
549- canBeInPlace : false ,
561+ required : true ,
562+ reason : "cluster has been explicitly restarted via annotation" ,
550563 }, nil
551564 }
552565 }
@@ -642,7 +655,7 @@ func checkPodSpecIsOutdated(
642655 required : true ,
643656 reason : fmt .Sprintf ("the instance is using an old bootstrap container image: %s -> %s" ,
644657 opCurrentImageName , configuration .Current .OperatorImageName ),
645- needsChangeOperatorImage : true ,
658+ needsToChangeOperatorImage : true ,
646659 }, nil
647660 }
648661
0 commit comments