From b3ef17671c1c4a7bc2da119dbe3e5eaf91ef4f02 Mon Sep 17 00:00:00 2001 From: Stamatis Katsaounis Date: Wed, 12 Jun 2024 13:02:35 +0300 Subject: [PATCH] feat: introduce pgbouncer as a PostgreSQL proxy --- anvil-python/anvil/versions.py | 11 +++++-- cloud/etc/deploy-maas-agent/main.tf | 2 +- cloud/etc/deploy-maas-region/main.tf | 36 +++++++++++++++++++++-- cloud/etc/deploy-maas-region/variables.tf | 18 ++++++++++++ 4 files changed, 62 insertions(+), 5 deletions(-) diff --git a/anvil-python/anvil/versions.py b/anvil-python/anvil/versions.py index 1351e4d..79adc06 100644 --- a/anvil-python/anvil/versions.py +++ b/anvil-python/anvil/versions.py @@ -15,7 +15,8 @@ MAAS_REGION_CHANNEL = "latest/edge" MAAS_AGENT_CHANNEL = "latest/edge" -POSTGRESQL_CHANNEL = "14/stable" +POSTGRESQL_CHANNEL = "14/candidate" +PGBOUNCER_CHANNEL = "1/candidate" HAPROXY_CHANNEL = "latest/stable" MACHINE_CHARMS = { @@ -23,6 +24,7 @@ "maas-agent": MAAS_AGENT_CHANNEL, "haproxy": HAPROXY_CHANNEL, "postgresql": POSTGRESQL_CHANNEL, + "pgbouncer": PGBOUNCER_CHANNEL, } K8S_CHARMS: dict[str, str] = {} @@ -43,7 +45,12 @@ "channel": "charm_maas_region_channel", "revision": "charm_maas_region_revision", "config": "charm_maas_region_config", - } + }, + "pgbouncer": { + "channel": "charm_pgbouncer_channel", + "revision": "charm_pgbouncer_revision", + "config": "charm_pgbouncer_config", + }, } } diff --git a/cloud/etc/deploy-maas-agent/main.tf b/cloud/etc/deploy-maas-agent/main.tf index f239eb0..8024fb5 100644 --- a/cloud/etc/deploy-maas-agent/main.tf +++ b/cloud/etc/deploy-maas-agent/main.tf @@ -45,7 +45,7 @@ resource "juju_application" "maas-agent" { config = var.charm_maas_agent_config } -resource "juju_integration" "maas-region-postgresql" { +resource "juju_integration" "maas-agent-region" { model = data.juju_model.machine_model.name application { diff --git a/cloud/etc/deploy-maas-region/main.tf b/cloud/etc/deploy-maas-region/main.tf index c7e7097..703124b 100644 --- a/cloud/etc/deploy-maas-region/main.tf +++ b/cloud/etc/deploy-maas-region/main.tf @@ -45,7 +45,39 @@ resource "juju_application" "maas-region" { config = var.charm_maas_region_config } -resource "juju_integration" "maas-region-postgresql" { +resource "juju_application" "pgbouncer" { + name = "pgbouncer" + model = data.juju_model.machine_model.name + units = 0 # it is a subordinate charm + + charm { + name = "pgbouncer" + channel = var.charm_pgbouncer_channel + revision = var.charm_pgbouncer_revision + base = "ubuntu@22.04" + } + + config = merge({ + pool_mode = "transaction" + max_db_connections = floor(90 / length(var.machine_ids)) + }, var.charm_pgbouncer_config) +} + +resource "juju_integration" "postgresql-pgbouncer" { + model = data.juju_model.machine_model.name + + application { + name = "postgresql" + endpoint = "database" + } + + application { + name = juju_application.pgbouncer.name + endpoint = "backend-database" + } +} + +resource "juju_integration" "maas-region-pgbouncer" { model = data.juju_model.machine_model.name application { @@ -54,7 +86,7 @@ resource "juju_integration" "maas-region-postgresql" { } application { - name = "postgresql" + name = juju_application.pgbouncer.name endpoint = "database" } } diff --git a/cloud/etc/deploy-maas-region/variables.tf b/cloud/etc/deploy-maas-region/variables.tf index 98198a7..9822e2f 100644 --- a/cloud/etc/deploy-maas-region/variables.tf +++ b/cloud/etc/deploy-maas-region/variables.tf @@ -47,3 +47,21 @@ variable "enable_haproxy" { type = bool default = false } + +variable "charm_pgbouncer_channel" { + description = "Operator channel for PgBouncer deployment" + type = string + default = "1/candidate" +} + +variable "charm_pgbouncer_revision" { + description = "Operator channel revision for PgBouncer deployment" + type = number + default = null +} + +variable "charm_pgbouncer_config" { + description = "Operator config for PgBouncer deployment" + type = map(string) + default = {} +}