@@ -886,7 +886,7 @@ func (r *PostgresReconciler) ensurePostgresSecrets(log logr.Logger, ctx context.
886
886
}
887
887
888
888
func (r * PostgresReconciler ) ensureStandbySecrets (log logr.Logger , ctx context.Context , instance * pg.Postgres ) error {
889
- if instance .IsReplicationPrimary () {
889
+ if instance .IsReplicationPrimaryOrStandalone () {
890
890
// nothing is configured, or we are the leader. nothing to do.
891
891
return nil
892
892
}
@@ -1024,11 +1024,6 @@ func (r *PostgresReconciler) checkAndUpdatePatroniReplicationConfig(log logr.Log
1024
1024
const requeueAfterReconcile = true
1025
1025
const allDone = false
1026
1026
1027
- // If there is no connected postgres, no need to tinker with patroni directly
1028
- if instance .Spec .PostgresConnection == nil {
1029
- return allDone , nil
1030
- }
1031
-
1032
1027
log .V (debugLogLevel ).Info ("Checking replication config from Patroni API" )
1033
1028
1034
1029
// Get the leader pod
@@ -1045,6 +1040,12 @@ func (r *PostgresReconciler) checkAndUpdatePatroniReplicationConfig(log logr.Log
1045
1040
}
1046
1041
leaderIP := leaderPods .Items [0 ].Status .PodIP
1047
1042
1043
+ // If there is no connected postgres, we still need to possibly clean up a former synchronous primary
1044
+ if instance .Spec .PostgresConnection == nil {
1045
+ log .V (debugLogLevel ).Info ("single instance, updating with empty config and requeing" )
1046
+ return allDone , r .httpPatchPatroni (log , ctx , instance , leaderIP , nil )
1047
+ }
1048
+
1048
1049
var resp * PatroniConfig
1049
1050
resp , err = r .httpGetPatroniConfig (log , ctx , leaderIP )
1050
1051
if err != nil {
@@ -1056,7 +1057,7 @@ func (r *PostgresReconciler) checkAndUpdatePatroniReplicationConfig(log logr.Log
1056
1057
return requeueAfterReconcile , nil
1057
1058
}
1058
1059
1059
- if instance .IsReplicationPrimary () {
1060
+ if instance .IsReplicationPrimaryOrStandalone () {
1060
1061
if resp .StandbyCluster != nil {
1061
1062
log .V (debugLogLevel ).Info ("standby_cluster mismatch, requeing" , "response" , resp )
1062
1063
return requeueAfterReconcile , nil
@@ -1076,7 +1077,11 @@ func (r *PostgresReconciler) checkAndUpdatePatroniReplicationConfig(log logr.Log
1076
1077
} else {
1077
1078
synchronousStandbyApplicationName = pointer .String (s .ToPeripheralResourceName ())
1078
1079
}
1079
- if resp .SynchronousNodesAdditional == nil || * resp .SynchronousNodesAdditional != * synchronousStandbyApplicationName {
1080
+ // compare the actual value with the expected value
1081
+ if synchronousStandbyApplicationName == nil {
1082
+ log .V (debugLogLevel ).Info ("could not fetch synchronous_nodes_additional, disabling sync replication and requeing" , "response" , resp )
1083
+ return requeueAfterReconcile , r .httpPatchPatroni (log , ctx , instance , leaderIP , nil )
1084
+ } else if resp .SynchronousNodesAdditional == nil || * resp .SynchronousNodesAdditional != * synchronousStandbyApplicationName {
1080
1085
log .V (debugLogLevel ).Info ("synchronous_nodes_additional mismatch, updating and requeing" , "response" , resp )
1081
1086
return requeueAfterReconcile , r .httpPatchPatroni (log , ctx , instance , leaderIP , synchronousStandbyApplicationName )
1082
1087
}
@@ -1179,7 +1184,9 @@ func (r *PostgresReconciler) httpPatchPatroni(log logr.Logger, ctx context.Conte
1179
1184
1180
1185
log .V (debugLogLevel ).Info ("Preparing request" )
1181
1186
var request PatroniConfig
1182
- if instance .IsReplicationPrimary () {
1187
+ if instance .Spec .PostgresConnection == nil {
1188
+ // use empty config
1189
+ } else if instance .IsReplicationPrimaryOrStandalone () {
1183
1190
request = PatroniConfig {
1184
1191
StandbyCluster : nil ,
1185
1192
}
@@ -1206,7 +1213,6 @@ func (r *PostgresReconciler) httpPatchPatroni(log logr.Logger, ctx context.Conte
1206
1213
request .SynchronousNodesAdditional = nil
1207
1214
}
1208
1215
} else {
1209
- // TODO check values first
1210
1216
request = PatroniConfig {
1211
1217
StandbyCluster : & PatroniStandbyCluster {
1212
1218
CreateReplicaMethods : []string {"basebackup_fast_xlog" },
@@ -1241,8 +1247,21 @@ func (r *PostgresReconciler) httpPatchPatroni(log logr.Logger, ctx context.Conte
1241
1247
}
1242
1248
defer resp .Body .Close ()
1243
1249
1250
+ if resp .StatusCode / 100 != 2 {
1251
+ err = fmt .Errorf ("received unexpected return code %d" , resp .StatusCode )
1252
+ log .Error (err , "could not perform PATCH request" )
1253
+ return err
1254
+ }
1255
+
1256
+ log .V (debugLogLevel ).Info ("Performed request" )
1257
+
1258
+ // fake error when standbyApplicationName is required but not provided
1259
+ if instance .Spec .PostgresConnection != nil && instance .IsReplicationPrimaryOrStandalone () && instance .Spec .PostgresConnection .SynchronousReplication && synchronousStandbyApplicationName == nil {
1260
+ return fmt .Errorf ("missing application_name of synchronous standby, disable synchronous replication" )
1261
+ }
1262
+
1244
1263
// fake error when standbyApplicationName is required but not provided
1245
- if instance .IsReplicationPrimary () && instance .Spec .PostgresConnection .SynchronousReplication && synchronousStandbyApplicationName == nil {
1264
+ if instance .Spec . PostgresConnection != nil && instance .Spec .PostgresConnection .SynchronousReplication && synchronousStandbyApplicationName == nil {
1246
1265
return fmt .Errorf ("missing application_name of synchronous standby, disable synchronous replication" )
1247
1266
}
1248
1267
@@ -1487,7 +1506,7 @@ func (r *PostgresReconciler) createOrUpdateExporterSidecarServices(log logr.Logg
1487
1506
pes .Spec .Ports = []corev1.ServicePort {
1488
1507
{
1489
1508
Name : postgresExporterServicePortName ,
1490
- Port : int32 (exporterServicePort ),
1509
+ Port : int32 (exporterServicePort ), //nolint
1491
1510
Protocol : corev1 .ProtocolTCP ,
1492
1511
TargetPort : intstr .FromInt (int (exporterServiceTargetPort )),
1493
1512
},
@@ -1752,7 +1771,7 @@ func (r *PostgresReconciler) ensureInitDBJob(log logr.Logger, ctx context.Contex
1752
1771
cm .Data = map [string ]string {}
1753
1772
1754
1773
// only execute SQL when encountering a **new** database, not for standbies or clones
1755
- if instance .IsReplicationPrimary () && instance .Spec .PostgresRestore == nil {
1774
+ if instance .IsReplicationPrimaryOrStandalone () && instance .Spec .PostgresRestore == nil {
1756
1775
// try to fetch the global initjob configmap
1757
1776
cns := types.NamespacedName {
1758
1777
Namespace : r .PostgresletNamespace ,
0 commit comments