Skip to content

Commit 9a439af

Browse files
committed
Deduplicate
1 parent b236b1d commit 9a439af

File tree

2 files changed

+11
-15
lines changed

2 files changed

+11
-15
lines changed

api/v1/postgres_types.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,14 @@ func (p *Postgres) generateDatabaseName() string {
624624
return generatedDatabaseName
625625
}
626626

627+
// ToReadWriteHostsList Force Patroni to add target_session_attrs=read-write by providing a comma separated list (with the same IP...).
628+
// This hopefully prevents cascading replicating by preventing the standby leader pod of the standby cluster to
629+
// connect to a read-only pod in the primary cluster (which would break our sync replication to the standby cluster).
630+
// https://patroni.readthedocs.io/en/latest/standby_cluster.html
631+
func (p *Postgres) ToReadWriteHostsList() string {
632+
return strings.Join([]string{p.Spec.PostgresConnection.ConnectionIP, p.Spec.PostgresConnection.ConnectionIP}, ",")
633+
}
634+
627635
func (p *Postgres) ToPeripheralResourceNamespace() string {
628636
// We only want letters and numbers
629637
projectID := alphaNumericRegExp.ReplaceAllString(p.Spec.ProjectID, "")
@@ -789,13 +797,7 @@ func (p *Postgres) ToUnstructuredZalandoPostgresql(z *zalando.Postgresql, c *cor
789797
z.Spec.StandbyCluster = nil
790798
} else {
791799
// overwrite connection info
792-
793-
// Force Patroni to add target_session_attrs=read-write by providing a comma separated list (with the same IP...).
794-
// This hopefully prevents cascading replicating by preventing the standby leader pod of the standby cluster to
795-
// connect to a read-only pod in the primary cluster (which would break our sync replication to the standby cluster).
796-
// https://patroni.readthedocs.io/en/latest/standby_cluster.html
797-
standbyHosts := strings.Join([]string{p.Spec.PostgresConnection.ConnectionIP, p.Spec.PostgresConnection.ConnectionIP}, ",")
798-
800+
standbyHosts := p.ToReadWriteHostsList()
799801
z.Spec.StandbyCluster = &zalando.StandbyDescription{
800802
StandbyHost: standbyHosts,
801803
StandbyPort: strconv.FormatInt(int64(p.Spec.PostgresConnection.ConnectionPort), 10),

controllers/postgres_controller.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1138,13 +1138,7 @@ func (r *PostgresReconciler) checkAndUpdatePatroniReplicationConfig(log logr.Log
11381138
return requeueAfterReconcile, r.httpPatchPatroni(log, ctx, instance, leaderIP, nil)
11391139
}
11401140

1141-
// Force Patroni to add target_session_attrs=read-write by providing a comma separated list (with the same IP...).
1142-
// This hopefully prevents cascading replicating by preventing the standby leader pod of the standby cluster to
1143-
// connect to a read-only pod in the primary cluster (which would break our sync replication to the standby cluster).
1144-
// https://patroni.readthedocs.io/en/latest/standby_cluster.html
1145-
standbyHosts := strings.Join([]string{instance.Spec.PostgresConnection.ConnectionIP, instance.Spec.PostgresConnection.ConnectionIP}, ",")
1146-
1147-
if resp.StandbyCluster.Host != standbyHosts {
1141+
if resp.StandbyCluster.Host != instance.ToReadWriteHostsList() {
11481142
log.V(debugLogLevel).Info("host mismatch, updating and requeing", "updating", resp)
11491143
return requeueAfterReconcile, r.httpPatchPatroni(log, ctx, instance, leaderIP, nil)
11501144
}
@@ -1260,7 +1254,7 @@ func (r *PostgresReconciler) httpPatchPatroni(log logr.Logger, ctx context.Conte
12601254
// This hopefully prevents cascading replicating by preventing the standby leader pod of the standby cluster to
12611255
// connect to a read-only pod in the primary cluster (which would break our sync replication to the standby cluster).
12621256
// https://patroni.readthedocs.io/en/latest/standby_cluster.html
1263-
standbyHosts := strings.Join([]string{instance.Spec.PostgresConnection.ConnectionIP, instance.Spec.PostgresConnection.ConnectionIP}, ",")
1257+
standbyHosts := instance.ToReadWriteHostsList()
12641258

12651259
request = PatroniConfig{
12661260
StandbyCluster: &PatroniStandbyCluster{

0 commit comments

Comments
 (0)