Skip to content

Commit 1e967d6

Browse files
tjmoore4jkatz
authored andcommitted
Check nested struct initialization during CM upgrade
During the upgrade of an existing pgcluster, the existing <clustername>-pgha-config configmap is used to populate the struct object so that key values are upgraded, as needed. In certain cases where the configmap exists but was is not initialized as expected, the exist function can hit a runtime panic when referencing uninitialized structs or maps. This update checks that these structs and maps are not nil before attempting to set any values.
1 parent df83a50 commit 1e967d6

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

internal/operator/cluster/upgrade.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -879,16 +879,24 @@ func updateClusterConfig(clientset kubeapi.Interface, pgcluster *crv1.Pgcluster,
879879
return err
880880
}
881881

882-
// set the updated path values for both DCS and LocalDB configs
882+
// set the updated path values for both DCS and LocalDB configs, if the fields and maps exist
883883
// as of version 4.6, the /crunchyadm directory no longer exists (previously set as a unix socket directory)
884884
// and the /opt/cpm... directories are now set under /opt/crunchy
885-
dcsConf.PostgreSQL.Parameters["unix_socket_directories"] = "/tmp"
886-
dcsConf.PostgreSQL.Parameters["archive_command"] = `source /opt/crunchy/bin/postgres-ha/pgbackrest/pgbackrest-set-env.sh && pgbackrest archive-push "%p"`
887-
dcsConf.PostgreSQL.RecoveryConf["restore_command"] = `source /opt/crunchy/bin/postgres-ha/pgbackrest/pgbackrest-set-env.sh && pgbackrest archive-get %f "%p"`
885+
if dcsConf.PostgreSQL != nil && dcsConf.PostgreSQL.Parameters != nil {
886+
dcsConf.PostgreSQL.Parameters["unix_socket_directories"] = "/tmp"
887+
dcsConf.PostgreSQL.Parameters["archive_command"] = `source /opt/crunchy/bin/postgres-ha/pgbackrest/pgbackrest-set-env.sh && pgbackrest archive-push "%p"`
888+
dcsConf.PostgreSQL.RecoveryConf["restore_command"] = `source /opt/crunchy/bin/postgres-ha/pgbackrest/pgbackrest-set-env.sh && pgbackrest archive-get %f "%p"`
889+
}
888890

889-
localDBConf.PostgreSQL.Callbacks.OnRoleChange = "/opt/crunchy/bin/postgres-ha/callbacks/pgha-on-role-change.sh"
890-
localDBConf.PostgreSQL.PGBackRest.Command = "/opt/crunchy/bin/postgres-ha/pgbackrest/pgbackrest-create-replica.sh replica"
891-
localDBConf.PostgreSQL.PGBackRestStandby.Command = "/opt/crunchy/bin/postgres-ha/pgbackrest/pgbackrest-create-replica.sh standby"
891+
if localDBConf.PostgreSQL.Callbacks != nil {
892+
localDBConf.PostgreSQL.Callbacks.OnRoleChange = "/opt/crunchy/bin/postgres-ha/callbacks/pgha-on-role-change.sh"
893+
}
894+
if localDBConf.PostgreSQL.PGBackRest != nil {
895+
localDBConf.PostgreSQL.PGBackRest.Command = "/opt/crunchy/bin/postgres-ha/pgbackrest/pgbackrest-create-replica.sh replica"
896+
}
897+
if localDBConf.PostgreSQL.PGBackRestStandby != nil {
898+
localDBConf.PostgreSQL.PGBackRestStandby.Command = "/opt/crunchy/bin/postgres-ha/pgbackrest/pgbackrest-create-replica.sh standby"
899+
}
892900

893901
// set up content and patch DCS config
894902
dcsContent, err := yaml.Marshal(dcsConf)

0 commit comments

Comments
 (0)