Skip to content

Commit 9d216d2

Browse files
jsilvelaarmrumnencia
authored
feat(rollout): honor primaryUpdateMethod when adding PVCs (#9720)
Currently, when upgrading a cluster to add a new tablespace or set a dedicated WAL volume, the configured choice of `primaryUpdateMethod` is ignored, and primary restart is forced. This PR enables cluster upgrade for these cases to proceed with primary switchover if so configured. Closes #5564 Closes #5484 Signed-off-by: Jaime Silvela <[email protected]> Signed-off-by: Armando Ruocco <[email protected]> Signed-off-by: Marco Nenciarini <[email protected]> Co-authored-by: Armando Ruocco <[email protected]> Co-authored-by: Marco Nenciarini <[email protected]>
1 parent 3168832 commit 9d216d2

14 files changed

Lines changed: 400 additions & 156 deletions

‎api/v1/cluster_types.go‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1298,8 +1298,9 @@ type NodeMaintenanceWindow struct {
12981298
// the primary server of the cluster as part of rolling updates
12991299
type PrimaryUpdateStrategy string
13001300

1301-
// PrimaryUpdateMethod contains the method to use when upgrading
1302-
// the primary server of the cluster as part of rolling updates
1301+
// PrimaryUpdateMethod defines the method to use when upgrading
1302+
// the primary instance of the cluster as part of rolling updates.
1303+
// The default method is "restart"
13031304
type PrimaryUpdateMethod string
13041305

13051306
const (

‎docs/src/cloudnative-pg.v1.md‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1957,8 +1957,9 @@ _Appears in:_
19571957

19581958
_Underlying type:_ _string_
19591959

1960-
PrimaryUpdateMethod contains the method to use when upgrading
1961-
the primary server of the cluster as part of rolling updates
1960+
PrimaryUpdateMethod defines the method to use when upgrading
1961+
the primary instance of the cluster as part of rolling updates.
1962+
The default method is "restart"
19621963

19631964

19641965

‎docs/src/rolling_update.md‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ updated.
4747

4848
When `primaryUpdateStrategy` is set to `unsupervised`, the rolling update
4949
process is managed by Kubernetes and is entirely automated. Once the replicas
50-
have been upgraded, the selected `primaryUpdateMethod` operation will initiate
51-
on the primary. This is the default behavior.
50+
have been upgraded, the selected `primaryUpdateMethod` operation will be executed
51+
on the primary. Unsupervised update is the default behavior.
5252

5353
The `primaryUpdateMethod` option accepts one of the following values:
5454

@@ -78,7 +78,7 @@ requirements in terms of [RPO](before_you_start.md#postgresql-terminology) and
7878
[RTO](before_you_start.md#postgresql-terminology), whether your PostgreSQL architecture is shared
7979
or shared nothing, and so on.
8080

81-
Indeed, being PostgreSQL a primary/standby architecture database management
81+
Indeed, since PostgreSQL is a primary/standby architecture database management
8282
system, the update process inevitably generates a downtime for your
8383
applications. One important aspect to consider for your context is the time it
8484
takes for your pod to download the new PostgreSQL container image, as that

‎internal/controller/cluster_upgrade.go‎

Lines changed: 38 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -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
4646
var 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
5050
var 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

161179
func (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
272288
type 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

526541
func 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

Comments
 (0)