Skip to content

Commit b68873e

Browse files
jkatzJonathan S. Katz
authored andcommitted
Update logic for pgbouncer password modification on creation
If the AddPgBouncer call is invoked as the Operator attempts to add a new pgBouncer and it is determined that a pgBouncer Secret already exists, do not attempt to update the password within PostgreSQL. This also ensures that an existing Secret storing the pgbouncer user credential is synchronized to be the password represented for the pgbouncer user in the database itself. Issue: [ch9457]
1 parent d98a354 commit b68873e

File tree

1 file changed

+24
-14
lines changed

1 file changed

+24
-14
lines changed

internal/operator/cluster/pgbouncer.go

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -156,15 +156,30 @@ func AddPgbouncer(clientset kubernetes.Interface, restconfig *rest.Config, clust
156156
}
157157
}
158158

159-
// set the password that will be used for the "pgbouncer" PostgreSQL account
160-
pgBouncerPassword, err := generatePassword()
161-
162-
if err != nil {
163-
return err
164-
}
165-
166159
// only attempt to set the password if the cluster is not in standby mode
160+
// and the secret does not already exist. If GetPasswordFromSecret returns
161+
// no errors, then we can assume that the Secret does not exist
167162
if !cluster.Spec.Standby {
163+
secretName := util.GeneratePgBouncerSecretName(cluster.Name)
164+
pgBouncerPassword, err := util.GetPasswordFromSecret(clientset, cluster.Namespace, secretName)
165+
166+
if err != nil {
167+
// set the password that will be used for the "pgbouncer" PostgreSQL account
168+
newPassword, err := generatePassword()
169+
170+
if err != nil {
171+
return err
172+
}
173+
174+
pgBouncerPassword = newPassword
175+
176+
// create the secret that pgbouncer will include the pgBouncer
177+
// credentials
178+
if err := createPgbouncerSecret(clientset, cluster, pgBouncerPassword); err != nil {
179+
return err
180+
}
181+
}
182+
168183
// attempt to update the password in PostgreSQL, as this is how pgBouncer
169184
// will properly interface with PostgreSQL
170185
if err := setPostgreSQLPassword(clientset, restconfig, pod, cluster.Spec.Port, pgBouncerPassword); err != nil {
@@ -178,12 +193,6 @@ func AddPgbouncer(clientset kubernetes.Interface, restconfig *rest.Config, clust
178193
return err
179194
}
180195

181-
// next, create the secret that pgbouncer will include the pgBouncer
182-
// credentials
183-
if err := createPgbouncerSecret(clientset, cluster, pgBouncerPassword); err != nil {
184-
return err
185-
}
186-
187196
// next, create the pgBouncer deployment
188197
if err := createPgBouncerDeployment(clientset, cluster); err != nil {
189198
return err
@@ -227,7 +236,8 @@ func DeletePgbouncer(clientset kubernetes.Interface, restconfig *rest.Config, cl
227236
}
228237

229238
// next, delete the various Kubernetes objects associated with the pgbouncer
230-
// these include the Service, Deployment, and the pgBouncer secret
239+
// these include the Service, Deployment, Secret and ConfigMap associated with
240+
// pgbouncer
231241
// If these fail, we'll just pass through
232242
//
233243
// First, delete the Service and Deployment, which share the same naem

0 commit comments

Comments
 (0)