Skip to content

Commit dc4a4ca

Browse files
chore: Bootstrap using cells (#42)
* chore: Bootstrap using cells * PVC name as optional
1 parent d2f662d commit dc4a4ca

File tree

6 files changed

+412
-4
lines changed

6 files changed

+412
-4
lines changed

bootstrap/cell/main.tf

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// Each cell of the dbsync extension containes 1 PVC, 1 Postgres instance, 1
2+
// PGBouncer that acts proxy and an amount of instances (commonly 3, one per
3+
// network).
4+
locals {
5+
postgres_host = "postgres-dbsync-v3-${var.salt}"
6+
db_volume_claim = coalesce(var.db_volume_claim, "pvc-${var.salt}")
7+
postgres_config_name = coalesce(var.postgres_config_name, "postgres-config-${var.salt}")
8+
}
9+
module "dbsync_pvc" {
10+
source = "../pvc"
11+
namespace = var.namespace
12+
volume_name = var.volume_name
13+
storage_size = var.storage_size
14+
name = local.db_volume_claim
15+
}
16+
17+
module "dbsync_postgres" {
18+
source = "../postgres"
19+
20+
namespace = var.namespace
21+
db_volume_claim = local.db_volume_claim
22+
instance_name = local.postgres_host
23+
postgres_config_name = local.postgres_config_name
24+
topology_zone = var.topology_zone
25+
postgres_image_tag = var.postgres_image_tag
26+
postgres_secret_name = var.postgres_secret_name
27+
postgres_resources = var.postgres_resources
28+
}
29+
30+
module "dbsync_pgbouncer" {
31+
source = "../pgbouncer"
32+
33+
namespace = var.namespace
34+
pg_bouncer_replicas = var.pgbouncer_replicas
35+
certs_configmap_name = var.certs_configmap_name
36+
pg_bouncer_user_settings = var.pgbouncer_user_settings
37+
pg_bouncer_auth_user_password = var.pgbouncer_auth_user_password
38+
instance_role = "pgbouncer"
39+
postgres_secret_name = var.postgres_secret_name
40+
instance_name = "postgres-dbsync-v3-${var.salt}"
41+
postgres_instance_name = local.postgres_host
42+
}
43+
44+
module "dbsync_instances" {
45+
source = "../instance"
46+
for_each = var.instances
47+
48+
namespace = var.namespace
49+
network = each.value.network
50+
salt = coalesce(each.value.salt, var.salt)
51+
dbsync_image_tag = each.value.dbsync_image_tag
52+
node_n2n_tcp_endpoint = each.value.node_n2n_tcp_endpoint
53+
release = each.value.release
54+
topology_zone = coalesce(each.value.topology_zone, var.topology_zone)
55+
sync_status = each.value.sync_status
56+
57+
enable_postgrest = each.value.enable_postgrest
58+
postgres_database = "dbsync-${each.value.network}"
59+
postgres_instance_name = local.postgres_host
60+
postgres_secret_name = var.postgres_secret_name
61+
62+
dbsync_resources = coalesce(each.value.dbsync_resources, {
63+
"limits" = {
64+
"memory" = "4Gi"
65+
}
66+
"requests" = {
67+
"memory" = "4Gi"
68+
"cpu" = "100m"
69+
}
70+
})
71+
dbsync_volume = coalesce(each.value.dbsync_volume, {
72+
manual = false
73+
storage_class = "fast"
74+
size = "10Gi"
75+
})
76+
}

bootstrap/cell/variables.tf

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
variable "namespace" {
2+
type = string
3+
}
4+
5+
variable "salt" {
6+
type = string
7+
description = "Salt used to identify all components as part of the cell. Should be unique between cells."
8+
}
9+
10+
variable "certs_configmap_name" {
11+
type = string
12+
default = "pgbouncer-certs"
13+
}
14+
15+
// PVC
16+
variable "volume_name" {
17+
type = string
18+
}
19+
20+
variable "storage_size" {
21+
type = string
22+
}
23+
24+
variable "db_volume_claim" {
25+
type = string
26+
default = null
27+
}
28+
29+
// Postgres
30+
variable "topology_zone" {
31+
type = string
32+
}
33+
34+
variable "postgres_image_tag" {
35+
type = string
36+
}
37+
38+
variable "postgres_resources" {
39+
type = object({
40+
requests = map(string)
41+
limits = map(string)
42+
})
43+
44+
default = {
45+
"limits" = {
46+
memory = "2Gi"
47+
cpu = "4000m"
48+
}
49+
"requests" = {
50+
memory = "2Gi"
51+
cpu = "100m"
52+
}
53+
}
54+
}
55+
56+
variable "postgres_secret_name" {
57+
type = string
58+
}
59+
60+
variable "postgres_config_name" {
61+
type = string
62+
default = null
63+
}
64+
65+
// PGBouncer
66+
variable "pgbouncer_image_tag" {
67+
default = "1.21.0"
68+
}
69+
70+
variable "pgbouncer_replicas" {
71+
default = 1
72+
}
73+
74+
variable "pgbouncer_user_settings" {
75+
default = []
76+
type = list(object({
77+
name = string
78+
password = string
79+
max_connections = number
80+
}))
81+
}
82+
83+
variable "pgbouncer_auth_user_password" {
84+
type = string
85+
}
86+
87+
// Instance
88+
variable "instances" {
89+
type = map(object({
90+
salt = optional(string)
91+
network = string
92+
dbsync_image_tag = string
93+
node_n2n_tcp_endpoint = string
94+
release = string
95+
sync_status = string
96+
enable_postgrest = bool
97+
topology_zone = optional(string)
98+
dbsync_resources = optional(object({
99+
requests = map(string)
100+
limits = map(string)
101+
}))
102+
dbsync_volume = optional(object({
103+
storage_class = string
104+
size = string
105+
}))
106+
}))
107+
}
108+

bootstrap/feature/main.tf

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@ variable "namespace" {
22
type = string
33
}
44

5-
variable "instance_name" {
6-
type = string
7-
}
8-
95
variable "operator_image_tag" {
106
type = string
117
}

bootstrap/main.tf

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
resource "kubernetes_namespace" "namespace" {
2+
metadata {
3+
name = var.namespace
4+
}
5+
}
6+
7+
// Feature
8+
module "dbsync_feature" {
9+
depends_on = [kubernetes_namespace.namespace]
10+
source = "./feature"
11+
12+
namespace = var.namespace
13+
operator_image_tag = var.operator_image_tag
14+
metrics_delay = var.metrics_delay
15+
dcu_per_second = var.dcu_per_second
16+
postgres_password = var.postgres_password
17+
postgres_secret_name = var.postgres_secret_name
18+
pgbouncer_server_crt = var.pgbouncer_server_crt
19+
pgbouncer_server_key = var.pgbouncer_server_key
20+
21+
postgres_hosts = coalesce(var.postgres_hosts, [for key in keys(var.cells) : "postgres-dbsync-v3-${key}"])
22+
}
23+
24+
// Service
25+
module "dbsync_service" {
26+
depends_on = [kubernetes_namespace.namespace]
27+
source = "./service"
28+
29+
namespace = var.namespace
30+
}
31+
32+
// Cells
33+
module "dbsync_cells" {
34+
depends_on = [module.dbsync_feature]
35+
for_each = var.cells
36+
source = "./cell"
37+
38+
namespace = var.namespace
39+
salt = each.key
40+
41+
// PVC
42+
volume_name = each.value.pvc.volume_name
43+
storage_size = each.value.pvc.storage_size
44+
db_volume_claim = each.value.pvc.name
45+
46+
// PG
47+
topology_zone = each.value.postgres.topology_zone
48+
postgres_image_tag = each.value.postgres.image_tag
49+
postgres_secret_name = var.postgres_secret_name
50+
postgres_resources = each.value.postgres.resources
51+
postgres_config_name = each.value.postgres.config_name
52+
53+
// PGBouncer
54+
pgbouncer_image_tag = var.pgbouncer_image_tag
55+
pgbouncer_replicas = each.value.pgbouncer.replicas
56+
pgbouncer_user_settings = var.pgbouncer_user_settings
57+
pgbouncer_auth_user_password = var.pgbouncer_auth_user_password
58+
59+
// Instances
60+
instances = each.value.instances
61+
}

bootstrap/service/main.tf

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
variable "namespace" {
2+
type = string
3+
}
4+
5+
variable "service_name" {
6+
default = "dbsync-v3-pgbouncer"
7+
}
8+
9+
resource "kubernetes_service_v1" "dbsync_v3_service" {
10+
metadata {
11+
namespace = var.namespace
12+
name = var.service_name
13+
annotations = {
14+
"beta.kubernetes.io/aws-load-balancer-nlb-target-type" = "instance"
15+
"service.beta.kubernetes.io/aws-load-balancer-scheme" = "internet-facing"
16+
"service.beta.kubernetes.io/aws-load-balancer-type" = "external"
17+
}
18+
}
19+
20+
spec {
21+
type = "LoadBalancer"
22+
load_balancer_class = "service.k8s.aws/nlb"
23+
24+
port {
25+
protocol = "TCP"
26+
port = 5432
27+
target_port = 6432
28+
}
29+
30+
selector = {
31+
"role" = "pgbouncer"
32+
}
33+
}
34+
}

0 commit comments

Comments
 (0)