Skip to content

Commit 0487ab7

Browse files
committed
Make Patroni Status Non-Pointer
Initially the patroni status only had one purpose meaning it could be overwritten when its one field (SystemIdentifier) was invalid. Now that another Patroni status is being added we need to be more careful when making changes to it. By making this change we can update the status in both places (i.e. the SystemIdentifier field and the new Switchover field) without having to worry about instantiating the pointer value. We can do this because we create the Patroni status on every cluster that gets created by the operator. There are also some checks throughout the code that get updated to check for a value in `Status.Patroni.SystemIdentifier` instead of the existence of `Status.Patroni`.
1 parent bd4a2de commit 0487ab7

File tree

7 files changed

+20
-25
lines changed

7 files changed

+20
-25
lines changed

internal/controller/postgrescluster/patroni.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -312,9 +312,7 @@ func (r *Reconciler) reconcilePatroniStatus(
312312
if err == nil {
313313
if dcs.Annotations["initialize"] != "" {
314314
// After bootstrap, Patroni writes the cluster system identifier to DCS.
315-
cluster.Status.Patroni = &v1beta1.PatroniStatus{
316-
SystemIdentifier: dcs.Annotations["initialize"],
317-
}
315+
cluster.Status.Patroni.SystemIdentifier = dcs.Annotations["initialize"]
318316
} else if readyInstance {
319317
// While we typically expect a value for the initialize key to be present in the
320318
// Endpoints above by the time the StatefulSet for any instance indicates "ready"

internal/controller/postgrescluster/pgbackrest.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -954,7 +954,7 @@ func (r *Reconciler) prepareForRestore(ctx context.Context,
954954
Message: "Restoring cluster in-place",
955955
})
956956
// the cluster is no longer bootstrapped
957-
cluster.Status.Patroni = nil
957+
cluster.Status.Patroni.SystemIdentifier = ""
958958
// the restore will change the contents of the database, so the pgbouncer and exporter hashes
959959
// are no longer valid
960960
cluster.Status.Proxy.PGBouncer.PostgreSQLRevision = ""

internal/controller/postgrescluster/pgbackrest_test.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ func TestReconcilePGBackRest(t *testing.T) {
223223

224224
// set status
225225
postgresCluster.Status = v1beta1.PostgresClusterStatus{
226-
Patroni: &v1beta1.PatroniStatus{SystemIdentifier: "12345abcde"},
226+
Patroni: v1beta1.PatroniStatus{SystemIdentifier: "12345abcde"},
227227
PGBackRest: &v1beta1.PGBackRestStatus{
228228
RepoHost: &v1beta1.RepoHostStatus{Ready: true},
229229
Repos: []v1beta1.RepoStatus{{Name: "repo1", StanzaCreated: true}}},
@@ -539,7 +539,7 @@ func TestReconcilePGBackRest(t *testing.T) {
539539

540540
// set status
541541
postgresCluster.Status = v1beta1.PostgresClusterStatus{
542-
Patroni: &v1beta1.PatroniStatus{SystemIdentifier: "12345abcde"},
542+
Patroni: v1beta1.PatroniStatus{SystemIdentifier: "12345abcde"},
543543
PGBackRest: &v1beta1.PGBackRestStatus{
544544
Repos: []v1beta1.RepoStatus{{Name: "repo1", StanzaCreated: true}}},
545545
}
@@ -828,7 +828,7 @@ func TestReconcileStanzaCreate(t *testing.T) {
828828
Reason: "RepoHostReady",
829829
Message: "pgBackRest dedicated repository host is ready",
830830
})
831-
postgresCluster.Status.Patroni = &v1beta1.PatroniStatus{
831+
postgresCluster.Status.Patroni = v1beta1.PatroniStatus{
832832
SystemIdentifier: "6952526174828511264",
833833
}
834834

@@ -977,7 +977,7 @@ func TestReconcileReplicaCreateBackup(t *testing.T) {
977977
Reason: "StanzaCreated",
978978
Message: "pgBackRest replica create repo is ready for backups",
979979
})
980-
postgresCluster.Status.Patroni = &v1beta1.PatroniStatus{
980+
postgresCluster.Status.Patroni = v1beta1.PatroniStatus{
981981
SystemIdentifier: "6952526174828511264",
982982
}
983983

@@ -2162,7 +2162,7 @@ func TestReconcilePostgresClusterDataSource(t *testing.T) {
21622162
cluster.Spec.DataSource = tc.dataSource
21632163
assert.NilError(t, tClient.Create(ctx, cluster))
21642164
if tc.clusterBootstrapped {
2165-
cluster.Status.Patroni = &v1beta1.PatroniStatus{
2165+
cluster.Status.Patroni = v1beta1.PatroniStatus{
21662166
SystemIdentifier: "123456789",
21672167
}
21682168
}
@@ -2952,7 +2952,7 @@ func TestPrepareForRestore(t *testing.T) {
29522952
}
29532953
clusterUID := clusterName
29542954
cluster := fakePostgresCluster(clusterName, namespace, clusterUID, dedicated)
2955-
cluster.Status.Patroni = &v1beta1.PatroniStatus{SystemIdentifier: "abcde12345"}
2955+
cluster.Status.Patroni = v1beta1.PatroniStatus{SystemIdentifier: "abcde12345"}
29562956
cluster.Status.Proxy.PGBouncer.PostgreSQLRevision = "abcde12345"
29572957
cluster.Status.Monitoring.ExporterConfiguration = "abcde12345"
29582958
meta.SetStatusCondition(&cluster.Status.Conditions, metav1.Condition{
@@ -3026,7 +3026,7 @@ func TestPrepareForRestore(t *testing.T) {
30263026
assert.Equal(t, tc.result.expectedClusterCondition.Message, condition.Message)
30273027
}
30283028
if tc.result.expectedClusterCondition.Reason == ReasonReadyForRestore {
3029-
assert.Assert(t, cluster.Status.Patroni == nil)
3029+
assert.Assert(t, cluster.Status.Patroni.SystemIdentifier == "")
30303030
assert.Assert(t, cluster.Status.Proxy.PGBouncer.PostgreSQLRevision == "")
30313031
assert.Assert(t, cluster.Status.Monitoring.ExporterConfiguration == "")
30323032
assert.Assert(t, meta.FindStatusCondition(cluster.Status.Conditions,
@@ -3089,7 +3089,7 @@ func TestReconcileScheduledBackups(t *testing.T) {
30893089
ConditionReplicaCreate: metav1.ConditionTrue,
30903090
},
30913091
status: &v1beta1.PostgresClusterStatus{
3092-
Patroni: &v1beta1.PatroniStatus{SystemIdentifier: "12345abcde"},
3092+
Patroni: v1beta1.PatroniStatus{SystemIdentifier: "12345abcde"},
30933093
PGBackRest: &v1beta1.PGBackRestStatus{
30943094
Repos: []v1beta1.RepoStatus{{Name: "repo1", StanzaCreated: true}}},
30953095
},
@@ -3107,7 +3107,7 @@ func TestReconcileScheduledBackups(t *testing.T) {
31073107
testDesc: "no repo host ready condition, should not reconcile",
31083108
dedicatedOnly: true,
31093109
status: &v1beta1.PostgresClusterStatus{
3110-
Patroni: &v1beta1.PatroniStatus{SystemIdentifier: "12345abcde"},
3110+
Patroni: v1beta1.PatroniStatus{SystemIdentifier: "12345abcde"},
31113111
PGBackRest: &v1beta1.PGBackRestStatus{
31123112
Repos: []v1beta1.RepoStatus{{Name: "repo1", StanzaCreated: true}}},
31133113
},
@@ -3116,7 +3116,7 @@ func TestReconcileScheduledBackups(t *testing.T) {
31163116
}, {
31173117
testDesc: "no replica create condition, should not reconcile",
31183118
status: &v1beta1.PostgresClusterStatus{
3119-
Patroni: &v1beta1.PatroniStatus{SystemIdentifier: "12345abcde"},
3119+
Patroni: v1beta1.PatroniStatus{SystemIdentifier: "12345abcde"},
31203120
PGBackRest: &v1beta1.PGBackRestStatus{
31213121
Repos: []v1beta1.RepoStatus{{Name: "repo1", StanzaCreated: true}}},
31223122
},
@@ -3126,7 +3126,7 @@ func TestReconcileScheduledBackups(t *testing.T) {
31263126
testDesc: "false repo host ready condition, should not reconcile",
31273127
dedicatedOnly: true,
31283128
status: &v1beta1.PostgresClusterStatus{
3129-
Patroni: &v1beta1.PatroniStatus{SystemIdentifier: "12345abcde"},
3129+
Patroni: v1beta1.PatroniStatus{SystemIdentifier: "12345abcde"},
31303130
PGBackRest: &v1beta1.PGBackRestStatus{
31313131
Repos: []v1beta1.RepoStatus{{Name: "repo1", StanzaCreated: true}}},
31323132
},
@@ -3135,7 +3135,7 @@ func TestReconcileScheduledBackups(t *testing.T) {
31353135
}, {
31363136
testDesc: "false replica create condition, should not reconcile",
31373137
status: &v1beta1.PostgresClusterStatus{
3138-
Patroni: &v1beta1.PatroniStatus{SystemIdentifier: "12345abcde"},
3138+
Patroni: v1beta1.PatroniStatus{SystemIdentifier: "12345abcde"},
31393139
PGBackRest: &v1beta1.PGBackRestStatus{
31403140
Repos: []v1beta1.RepoStatus{{Name: "repo1", StanzaCreated: true}}},
31413141
},
@@ -3148,7 +3148,7 @@ func TestReconcileScheduledBackups(t *testing.T) {
31483148
ConditionReplicaCreate: metav1.ConditionTrue,
31493149
},
31503150
status: &v1beta1.PostgresClusterStatus{
3151-
Patroni: &v1beta1.PatroniStatus{SystemIdentifier: "12345abcde"},
3151+
Patroni: v1beta1.PatroniStatus{SystemIdentifier: "12345abcde"},
31523152
PGBackRest: &v1beta1.PGBackRestStatus{
31533153
Repos: []v1beta1.RepoStatus{}},
31543154
},

internal/patroni/reconcile.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ import (
3434
// ClusterBootstrapped returns a bool indicating whether or not Patroni has successfully
3535
// bootstrapped the PostgresCluster
3636
func ClusterBootstrapped(postgresCluster *v1beta1.PostgresCluster) bool {
37-
return (postgresCluster.Status.Patroni != nil &&
38-
postgresCluster.Status.Patroni.SystemIdentifier != "")
37+
return postgresCluster.Status.Patroni.SystemIdentifier != ""
3938
}
4039

4140
// ClusterConfigMap populates the shared ConfigMap with fields needed to run Patroni.

pkg/apis/postgres-operator.crunchydata.com/v1beta1/postgrescluster_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ spec:
6363
postgresVersion: 0
6464
status:
6565
monitoring: {}
66+
patroni: {}
6667
proxy:
6768
pgBouncer: {}
6869
`)+"\n")
@@ -99,6 +100,7 @@ spec:
99100
postgresVersion: 0
100101
status:
101102
monitoring: {}
103+
patroni: {}
102104
proxy:
103105
pgBouncer: {}
104106
`)+"\n")

pkg/apis/postgres-operator.crunchydata.com/v1beta1/postgrescluster_types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ type PostgresClusterStatus struct {
315315
InstanceSets []PostgresInstanceSetStatus `json:"instances,omitempty"`
316316

317317
// +optional
318-
Patroni *PatroniStatus `json:"patroni,omitempty"`
318+
Patroni PatroniStatus `json:"patroni,omitempty"`
319319

320320
// Status information for pgBackRest
321321
// +optional

pkg/apis/postgres-operator.crunchydata.com/v1beta1/zz_generated.deepcopy.go

Lines changed: 1 addition & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)