Skip to content

Commit d222250

Browse files
committed
ensure Standby is removed while update and sync if manifest changed
1 parent bb8031d commit d222250

2 files changed

Lines changed: 71 additions & 0 deletions

File tree

pkg/cluster/cluster.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1123,6 +1123,13 @@ func (c *Cluster) Update(oldSpec, newSpec *cpov1.Postgresql) error {
11231123
}
11241124
}()
11251125

1126+
// add or remove standby_cluster section from Patroni config depending on changes in standby section
1127+
if !reflect.DeepEqual(oldSpec.Spec.StandbyCluster, newSpec.Spec.StandbyCluster) {
1128+
if err := c.syncStandbyClusterConfiguration(); err != nil {
1129+
return fmt.Errorf("could not set StandbyCluster configuration options: %v", err)
1130+
}
1131+
}
1132+
11261133
// pod disruption budget
11271134
if oldSpec.Spec.NumberOfInstances != newSpec.Spec.NumberOfInstances {
11281135
c.logger.Debug("syncing pod disruption budgets")

pkg/cluster/sync.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,13 @@ func (c *Cluster) Sync(newSpec *cpov1.Postgresql) error {
238238
}
239239
}
240240

241+
// add or remove standby_cluster section from Patroni config depending on changes in standby section
242+
if !reflect.DeepEqual(oldSpec.Spec.StandbyCluster, newSpec.Spec.StandbyCluster) {
243+
if err := c.syncStandbyClusterConfiguration(); err != nil {
244+
return fmt.Errorf("could not sync StandbyCluster configuration: %v", err)
245+
}
246+
}
247+
241248
c.logger.Debug("syncing pod disruption budgets")
242249
if err = c.syncPodDisruptionBudget(false); err != nil {
243250
err = fmt.Errorf("could not sync pod disruption budget: %v", err)
@@ -973,6 +980,63 @@ func (c *Cluster) checkAndSetGlobalPostgreSQLConfiguration(pod *v1.Pod, effectiv
973980
return configPatched, requiresMasterRestart, nil
974981
}
975982

983+
// syncStandbyClusterConfiguration checks whether standby cluster
984+
// parameters have changed and if necessary sets it via the Patroni API
985+
func (c *Cluster) syncStandbyClusterConfiguration() error {
986+
var (
987+
err error
988+
pods []v1.Pod
989+
)
990+
991+
standbyOptionsToSet := make(map[string]interface{})
992+
if c.Spec.StandbyCluster != nil {
993+
c.logger.Infof("turning %q into a standby cluster", c.Name)
994+
standbyOptionsToSet["create_replica_methods"] = []string{"bootstrap_standby_with_wale", "basebackup_fast_xlog"}
995+
standbyOptionsToSet["restore_command"] = "envdir \"/run/etc/wal-e.d/env-standby\" /scripts/restore_command.sh \"%f\" \"%p\""
996+
997+
if c.Spec.StandbyCluster.StandbyHost != "" {
998+
standbyOptionsToSet["host"] = c.Spec.StandbyCluster.StandbyHost
999+
} else {
1000+
standbyOptionsToSet["host"] = nil
1001+
}
1002+
1003+
if c.Spec.StandbyCluster.StandbyPort != "" {
1004+
standbyOptionsToSet["port"] = c.Spec.StandbyCluster.StandbyPort
1005+
} else {
1006+
standbyOptionsToSet["port"] = nil
1007+
}
1008+
1009+
if c.Spec.StandbyCluster.StandbyPrimarySlotName != "" {
1010+
standbyOptionsToSet["primary_slot_name"] = c.Spec.StandbyCluster.StandbyPrimarySlotName
1011+
} else {
1012+
standbyOptionsToSet["primary_slot_name"] = nil
1013+
}
1014+
} else {
1015+
c.logger.Infof("promoting standby cluster and detach from source")
1016+
standbyOptionsToSet = nil
1017+
}
1018+
1019+
if pods, err = c.listPods(); err != nil {
1020+
return err
1021+
}
1022+
if len(pods) == 0 {
1023+
return fmt.Errorf("could not call Patroni API: cluster has no pods")
1024+
}
1025+
// try all pods until the first one that is successful, as it doesn't matter which pod
1026+
// carries the request to change configuration through
1027+
for _, pod := range pods {
1028+
podName := util.NameFromMeta(pod.ObjectMeta)
1029+
c.logger.Infof("patching Postgres config via Patroni API on pod %s with following options: %s",
1030+
podName, standbyOptionsToSet)
1031+
if err = c.patroni.SetStandbyClusterParameters(&pod, standbyOptionsToSet); err == nil {
1032+
return nil
1033+
}
1034+
c.logger.Warningf("could not patch postgres parameters within pod %s: %v", podName, err)
1035+
}
1036+
return fmt.Errorf("could not reach Patroni API to set Postgres options: failed on every pod (%d total)",
1037+
len(pods))
1038+
}
1039+
9761040
func (c *Cluster) syncSecrets() error {
9771041
c.logger.Info("syncing secrets")
9781042
c.setProcessName("syncing secrets")

0 commit comments

Comments
 (0)