Guard the pgbouncer disable code in standby cluster
This code contains logic that requires SQL DDL execution, which
will not work on a standby cluster. However, the rest of the pgbouncer
deletion code will work, and as such we can pass through and execute
that.
@@ -244,28 +244,12 @@ func DeletePgbouncer(clientset *kubernetes.Clientset, restclient *rest.RESTClien
log.Debugf("delete pgbouncer from cluster [%s] in namespace [%s]", clusterName, namespace)
- // next, disable the pgbouncer user in the PostgreSQL cluster.
- // first, get the primary pod. If we cannot do this, let's consider it an
- // error and abort
- pod, err := util.GetPrimaryPod(clientset, cluster)
-
- if err != nil {
+ // if this is a standby cluster, we cannot execute any of the SQL on the
+ // PostgreSQL server, but we can still remove the Deployment and Service.
+ if !cluster.Spec.Standby {
+ if err := disablePgBouncer(clientset, restconfig, cluster); err != nil {
return err
}
-
- // This is safe from SQL injection as we are using constants and a well defined
- // string
- sql := strings.NewReader(fmt.Sprintf(sqlDisableLogin, crv1.PGUserPgBouncer))
- cmd := []string{"psql"}
-
- // exec into the pod to run the query
- _, stderr, err := kubeapi.ExecToPodThroughAPI(restconfig, clientset,
- cmd, "database", pod.Name, pod.ObjectMeta.Namespace, sql)
-
- // if there is an error, log the error from the stderr and return the error
- if err != nil {
- log.Error(stderr)
- return err
}
// next, delete the various Kubernetes objects associated with the pgbouncer
@@ -600,6 +584,38 @@ func createPgBouncerService(clientset *kubernetes.Clientset, cluster *crv1.Pgclu
return nil
}
+// disablePgBouncer executes codes on the primary PostgreSQL pod in order to
+// disable the "pgbouncer" role from being able to log in. It keeps the
+// artificats that were created during normal pgBouncer operation
+func disablePgBouncer(clientset *kubernetes.Clientset, restconfig *rest.Config, cluster *crv1.Pgcluster) error {
+ log.Debugf("disable pgbouncer user on cluster [%s]", cluster.Name)
+ // disable the pgbouncer user in the PostgreSQL cluster.
+ // first, get the primary pod. If we cannot do this, let's consider it an
+ // error and abort
+ pod, err := util.GetPrimaryPod(clientset, cluster)
+
+ if err != nil {
+ return err
+ }
+
+ // This is safe from SQL injection as we are using constants and a well defined
+ // string
+ sql := strings.NewReader(fmt.Sprintf(sqlDisableLogin, crv1.PGUserPgBouncer))
+ cmd := []string{"psql"}
+
+ // exec into the pod to run the query
+ _, stderr, err := kubeapi.ExecToPodThroughAPI(restconfig, clientset,
+ cmd, "database", pod.Name, pod.ObjectMeta.Namespace, sql)
+
+ // if there is an error, log the error from the stderr and return the error
+ if err != nil {
+ log.Error(stderr)
+ return err
+ }
+
+ return nil
+}
+
// execPgBouncerScript runs a script pertaining to the management of pgBouncer
// on the PostgreSQL pod
func execPgBouncerScript(clientset *kubernetes.Clientset, restconfig *rest.Config, pod *v1.Pod, databaseName, script string) {
