Adding storage modules
This commit is contained in:
3
postgresql/outputs.tf
Normal file
3
postgresql/outputs.tf
Normal file
@@ -0,0 +1,3 @@
|
||||
output "host" {
|
||||
value = "${var.app_slug}-redis.${var.namespace}.svc"
|
||||
}
|
||||
87
postgresql/postgresql.tf
Normal file
87
postgresql/postgresql.tf
Normal file
@@ -0,0 +1,87 @@
|
||||
locals {
|
||||
app_slug = (var.component == var.instance || var.component=="") ? var.instance : format("%s-%s", var.component, var.instance)
|
||||
pg-labels = merge(local.labels, {
|
||||
"app.kubernetes.io/component" = "pg"
|
||||
})
|
||||
pool-labels = merge(local.labels, {
|
||||
"app.kubernetes.io/component" = "pg-pool"
|
||||
})
|
||||
}
|
||||
|
||||
resource "kubectl_manifest" "prj_pg" {
|
||||
yaml_body = join("", concat([<<-EOF
|
||||
apiVersion: postgresql.cnpg.io/v1
|
||||
kind: Cluster
|
||||
metadata:
|
||||
name: "${local.app_slug}-pg"
|
||||
namespace: "${var.namespace}"
|
||||
labels: ${jsonencode(local.pg-labels)}
|
||||
annotations:
|
||||
"k8up.io/backupcommand": "pg_dump -U postgres -d ${var.component} --clean"
|
||||
"k8up.io/file-extension": ".sql"
|
||||
spec:
|
||||
instances: ${var.replicas}
|
||||
imageName: "${var.images.postgresql.registry}/${var.images.postgresql.repository}:${var.images.postgresql.tag}"
|
||||
storage:
|
||||
size: "${var.storage.size}"
|
||||
bootstrap:
|
||||
initdb:
|
||||
database: "${var.component}"
|
||||
owner: "${var.component}"
|
||||
monitoring:
|
||||
enablePodMonitor: true
|
||||
EOF
|
||||
], var.backups.enable&&var.backups.use_barman?[<<-EOF
|
||||
backup:
|
||||
barmanObjectStore:
|
||||
destinationPath: "s3://${var.app_slug}-${var.namespace}/"
|
||||
endpointURL: "${var.backups.endpoint}/barman"
|
||||
s3Credentials:
|
||||
accessKeyId:
|
||||
name: "${var.backups.secret_name}"
|
||||
key: "${var.backups.key_id_key}"
|
||||
secretAccessKey:
|
||||
name: "${var.backups.secret_name}"
|
||||
key: "${var.backups.secret_key}"
|
||||
EOF
|
||||
]:[""]))
|
||||
}
|
||||
|
||||
resource "kubectl_manifest" "prj_pg_backup" {
|
||||
count = var.backups.enable ? 1:0
|
||||
yaml_body = <<-EOF
|
||||
apiVersion: postgresql.cnpg.io/v1
|
||||
kind: ScheduledBackup
|
||||
metadata:
|
||||
name: "${local.app_slug}-pg"
|
||||
namespace: "${var.namespace}"
|
||||
labels: ${jsonencode(local.pg-labels)}
|
||||
spec:
|
||||
schedule: "${var.backups.schedule.db}"
|
||||
backupOwnerReference: self
|
||||
cluster:
|
||||
name: "${local.app_slug}-pg"
|
||||
EOF
|
||||
}
|
||||
|
||||
resource "kubectl_manifest" "prj_pg_pool" {
|
||||
depends_on = [kubectl_manifest.prj_pg]
|
||||
yaml_body = <<-EOF
|
||||
apiVersion: postgresql.cnpg.io/v1
|
||||
kind: Pooler
|
||||
metadata:
|
||||
name: "${local.app_slug}-pool"
|
||||
namespace: "${var.namespace}"
|
||||
labels: ${jsonencode(local.pool-labels)}
|
||||
spec:
|
||||
cluster:
|
||||
name: "${local.app_slug}-pg"
|
||||
instances: 1
|
||||
type: rw
|
||||
pgbouncer:
|
||||
poolMode: session
|
||||
parameters:
|
||||
max_client_conn: "1000"
|
||||
default_pool_size: "10"
|
||||
EOF
|
||||
}
|
||||
8
postgresql/providers.tf
Normal file
8
postgresql/providers.tf
Normal file
@@ -0,0 +1,8 @@
|
||||
terraform {
|
||||
required_providers {
|
||||
kubectl = {
|
||||
source = "gavinbunney/kubectl"
|
||||
version = "~> 1.14.0"
|
||||
}
|
||||
}
|
||||
}
|
||||
63
postgresql/variables.tf
Normal file
63
postgresql/variables.tf
Normal file
@@ -0,0 +1,63 @@
|
||||
variable "component" {
|
||||
type = string
|
||||
}
|
||||
variable "instance" {
|
||||
type = string
|
||||
}
|
||||
variable "namespace" {
|
||||
type = string
|
||||
}
|
||||
variable "labels" {
|
||||
type = map(string)
|
||||
}
|
||||
|
||||
variable "backups" {
|
||||
default = {
|
||||
"enable" = false
|
||||
"endpoint" = ""
|
||||
"key_id_key" = "s3-id"
|
||||
"restic_key" = "bck-password"
|
||||
"schedule" = {
|
||||
"db" = "30 3 * * *"
|
||||
}
|
||||
"secret_key" = "s3-secret"
|
||||
"secret_name" = "backup-settings"
|
||||
"use_barman" = false
|
||||
}
|
||||
type = object({
|
||||
enable = optional(bool),
|
||||
endpoint = optional(string),
|
||||
key_id_key = optional(string),
|
||||
restic_key = optional(string),
|
||||
schedule = optional(object({
|
||||
db = optional(string),
|
||||
})),
|
||||
secret_key = optional(string),
|
||||
secret_name = optional(string),
|
||||
use_barman = optional(bool)
|
||||
})
|
||||
}
|
||||
variable "images" {
|
||||
type = object({
|
||||
postgresql = optional(object({registry = optional(string), repository = optional(string), tag = optional(number)})),
|
||||
})
|
||||
default = {
|
||||
"postgresql" = {
|
||||
"registry" = "ghcr.io"
|
||||
"repository" = "cloudnative-pg/postgresql"
|
||||
"tag" = 15.3
|
||||
}
|
||||
}
|
||||
}
|
||||
variable "replicas" {
|
||||
type = number
|
||||
default = 1
|
||||
}
|
||||
variable "storage" {
|
||||
type = object({
|
||||
size = optional(string)
|
||||
})
|
||||
default = {
|
||||
"size" = "5Gi"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user