Skip to content

Commit

Permalink
chore: Add reloader (#51)
Browse files Browse the repository at this point in the history
* chore: Add PgBouncer reloader on DbSync

* Fix data
  • Loading branch information
gonzalezzfelipe authored Sep 11, 2024
1 parent 2cd2f00 commit e97b8b0
Show file tree
Hide file tree
Showing 9 changed files with 148 additions and 49 deletions.
2 changes: 1 addition & 1 deletion bootstrap/cell/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ module "dbsync_pgbouncer" {
namespace = var.namespace
pg_bouncer_replicas = var.pgbouncer_replicas
certs_configmap_name = var.certs_configmap_name
pg_bouncer_user_settings = var.pgbouncer_user_settings
pg_bouncer_auth_user_password = var.pgbouncer_auth_user_password
instance_role = "pgbouncer"
postgres_secret_name = var.postgres_secret_name
instance_name = "postgres-dbsync-v3-${var.salt}"
postgres_instance_name = local.postgres_host
pgbouncer_reloader_image_tag = var.pgbouncer_reloader_image_tag
}

module "dbsync_instances" {
Expand Down
11 changes: 3 additions & 8 deletions bootstrap/cell/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,11 @@ variable "pgbouncer_replicas" {
default = 1
}

variable "pgbouncer_user_settings" {
default = []
type = list(object({
name = string
password = string
max_connections = number
}))
variable "pgbouncer_auth_user_password" {
type = string
}

variable "pgbouncer_auth_user_password" {
variable "pgbouncer_reloader_image_tag" {
type = string
}

Expand Down
2 changes: 1 addition & 1 deletion bootstrap/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ module "dbsync_cells" {
// PGBouncer
pgbouncer_image_tag = var.pgbouncer_image_tag
pgbouncer_replicas = each.value.pgbouncer.replicas
pgbouncer_user_settings = var.pgbouncer_user_settings
pgbouncer_auth_user_password = var.pgbouncer_auth_user_password
pgbouncer_reloader_image_tag = var.pgbouncer_reloader_image_tag

// Instances
instances = each.value.instances
Expand Down
9 changes: 2 additions & 7 deletions bootstrap/pgbouncer/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,8 @@ variable "certs_configmap_name" {
default = "pgbouncer-certs"
}

variable "pg_bouncer_user_settings" {
default = []
type = list(object({
name = string
password = string
max_connections = number
}))
variable "pgbouncer_reloader_image_tag" {
type = string
}

variable "pg_bouncer_auth_user_password" {
Expand Down
135 changes: 122 additions & 13 deletions bootstrap/pgbouncer/pg-bouncer.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
locals {
users_volume = "/etc/pgbouncer"
tiers_configmap_name = "${var.instance_name}-tiers"
}

resource "kubernetes_deployment_v1" "pgbouncer" {
wait_for_rollout = false
metadata {
Expand Down Expand Up @@ -85,14 +90,9 @@ resource "kubernetes_deployment_v1" "pgbouncer" {
value = "5432"
}

env {
name = "PGBOUNCER_USERLIST_FILE"
value = "/etc/pgbouncer/users.txt"
}

volume_mount {
name = "pgbouncer-users"
mount_path = "/etc/pgbouncer"
mount_path = local.users_volume
}

volume_mount {
Expand All @@ -107,6 +107,91 @@ resource "kubernetes_deployment_v1" "pgbouncer" {

}

container {
name = "pgbouncer-reloader"
image = "ghcr.io/demeter-run/pgbouncer-reloader:${var.pgbouncer_reloader_image_tag}"

resources {
limits = {
memory = "250Mi"
}
requests = {
cpu = "100m"
memory = "250Mi"
}
}

env {
name = "TIERS_PATH"
value = "/etc/tiers/tiers.toml"
}

env {
name = "API_RESOURCE_GROUP"
value = "demeter.run"
}

env {
name = "API_RESOURCE_VERSION"
value = "v1alpha1"
}

env {
name = "API_RESOURCE_API_VERSION"
value = "demeter.run/v1alpha1"
}

env {
name = "API_RESOURCE_KIND"
value = "DbSyncPort"
}

env {
name = "API_RESOURCE_PLURAL"
value = "dbsyncports"
}

env {
name = "POSTGRES_PASSWORD"
value_from {
secret_key_ref {
name = var.postgres_secret_name
key = "password"
}
}
}

env {
name = "CONNECTION_OPTIONS"
value = "host=localhost user=pgbouncer password=${var.pg_bouncer_auth_user_password} dbname=pgbouncer port=6432"
}

env {
name = "PGBOUNCER_PASSWORD"
value = var.pg_bouncer_auth_user_password
}

env {
name = "USERS_INI_FILEPATH"
value = "${local.users_volume}/users.ini"
}

env {
name = "USERLIST_FILEPATH"
value = "${local.users_volume}/userlist.txt"
}

volume_mount {
name = "pgbouncer-users"
mount_path = local.users_volume
}

volume_mount {
name = "tiers"
mount_path = "/etc/tiers"
}
}

container {
name = "readiness"
image = "ghcr.io/demeter-run/cardano-dbsync-probe:${var.dbsync_probe_image_tag}"
Expand Down Expand Up @@ -156,11 +241,23 @@ resource "kubernetes_deployment_v1" "pgbouncer" {

}

init_container {
name = "init-user-files"
image = "busybox:1.28"
command = [
"sh", "-c",
"touch ${local.users_volume}/users.ini ${local.users_volume}/userlist.txt; echo '\"pgbouncer\" \"${var.pg_bouncer_auth_user_password}\"' > ${local.users_volume}/userlist.txt"
]

volume_mount {
name = "pgbouncer-users"
mount_path = local.users_volume
}
}

volume {
name = "pgbouncer-users"
config_map {
name = "${var.instance_name}-pgbouncer-users"
}
empty_dir {}
}

volume {
Expand All @@ -177,6 +274,13 @@ resource "kubernetes_deployment_v1" "pgbouncer" {
}
}

volume {
name = "tiers"
config_map {
name = local.tiers_configmap_name
}
}

toleration {
effect = "NoSchedule"
key = "demeter.run/compute-profile"
Expand All @@ -202,14 +306,14 @@ resource "kubernetes_deployment_v1" "pgbouncer" {
}


resource "kubernetes_config_map" "dbsync_pgbouncer_users" {
resource "kubernetes_config_map" "dbsync_pgbouncer_tiers" {
metadata {
namespace = var.namespace
name = "${var.instance_name}-pgbouncer-users"
name = local.tiers_configmap_name
}

data = {
"users.txt" = "${templatefile("${path.module}/users.txt.tftpl", { auth_user_password = "${var.pg_bouncer_auth_user_password}", users = var.pg_bouncer_user_settings })}"
"tiers.toml" = file("${path.module}/tiers.toml")
}
}

Expand All @@ -221,6 +325,11 @@ resource "kubernetes_config_map" "dbsync_pgbouncer_ini_config" {
}

data = {
"pgbouncer.ini" = "${templatefile("${path.module}/pgbouncer.ini.tftpl", { db_host = "${var.postgres_instance_name}", users = var.pg_bouncer_user_settings })}"
"pgbouncer.ini" = "${templatefile("${path.module}/pgbouncer.ini.tftpl", {
db_host = "${var.postgres_instance_name}",
users_volume = local.users_volume
})}"
# Empty file to bypass bitnami userlist bootstrapping, which we do ourselves.
"userlist.txt" = ""
}
}
7 changes: 2 additions & 5 deletions bootstrap/pgbouncer/pgbouncer.ini.tftpl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ listen_port=6432
listen_addr=0.0.0.0
unix_socket_dir=/tmp/
unix_socket_mode=0777
auth_file=/opt/bitnami/pgbouncer/conf/userlist.txt
auth_file=${users_volume}/userlist.txt
auth_type=scram-sha-256
auth_query=SELECT usename, passwd FROM user_search($1)
pidfile=/opt/bitnami/pgbouncer/tmp/pgbouncer.pid
Expand All @@ -29,7 +29,4 @@ tcp_keepintvl=75
admin_users=pgbouncer
max_db_connections=80
[users]
%{ for user in users ~}
${user.name} = max_user_connections=${user.max_connections}
%{ endfor ~}

%include ${users_volume}/users.ini
12 changes: 12 additions & 0 deletions bootstrap/pgbouncer/tiers.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[[tiers]]
name = "0"
max_connections = 1
[[tiers]]
name = "1"
max_connections = 3
[[tiers]]
name = "2"
max_connections = 10
[[tiers]]
name = "3"
max_connections = 40
4 changes: 0 additions & 4 deletions bootstrap/pgbouncer/users.txt.tftpl

This file was deleted.

15 changes: 5 additions & 10 deletions bootstrap/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ variable "pgbouncer_server_key" {
type = string
}

variable "pgbouncer_reloader_image_tag" {
type = string
}

variable "postgres_hosts" {
type = list(string)
default = null
Expand Down Expand Up @@ -73,15 +77,6 @@ variable "pgbouncer_image_tag" {
default = "1.21.0"
}

variable "pgbouncer_user_settings" {
default = []
type = list(object({
name = string
password = string
max_connections = number
}))
}

variable "pgbouncer_auth_user_password" {
type = string
}
Expand Down Expand Up @@ -125,7 +120,7 @@ variable "cells" {
empty_args = optional(bool, false)
custom_config = optional(bool, true)
network_env_var = optional(string, false)
topology_zone = optional(string)
topology_zone = optional(string)
dbsync_resources = optional(object({
requests = map(string)
limits = map(string)
Expand Down

0 comments on commit e97b8b0

Please sign in to comment.