Refacto and add modules
This commit is contained in:
@@ -1,3 +1,23 @@
|
||||
output "host" {
|
||||
value = "${var.app_slug}-redis.${var.namespace}.svc"
|
||||
output "conn_string" {
|
||||
value = "postgres://${urlencode(data.kubernetes_secret_v1.credentials.data["username"])}:${urlencode(data.kubernetes_secret_v1.credentials.data["password"])}@${local.app_slug}-pg-rw.${var.namespace}.svc:5432/${var.component}"
|
||||
}
|
||||
|
||||
output "service" {
|
||||
value = "${local.app_slug}-pg-rw.${var.namespace}.svc"
|
||||
}
|
||||
|
||||
output "db_host" {
|
||||
value = "${local.app_slug}-pg-rw"
|
||||
}
|
||||
|
||||
output "db_name" {
|
||||
value = var.component
|
||||
}
|
||||
|
||||
output "db_port" {
|
||||
value = "5432"
|
||||
}
|
||||
|
||||
output "secret_name" {
|
||||
value = "${local.app_slug}-pg-app"
|
||||
}
|
||||
@@ -1,21 +1,21 @@
|
||||
locals {
|
||||
app_slug = (var.component == var.instance || var.component=="") ? var.instance : format("%s-%s", var.component, var.instance)
|
||||
pg-labels = merge(local.labels, {
|
||||
app_slug = "${var.instance}${var.component == "" ? "" : "-"}${var.component}"
|
||||
pg_labels = merge(var.labels, {
|
||||
"app.kubernetes.io/component" = "pg"
|
||||
})
|
||||
pool-labels = merge(local.labels, {
|
||||
pool_labels = merge(var.labels, {
|
||||
"app.kubernetes.io/component" = "pg-pool"
|
||||
})
|
||||
}
|
||||
|
||||
resource "kubectl_manifest" "prj_pg" {
|
||||
resource "kubectl_manifest" "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)}
|
||||
labels: ${jsonencode(local.pg_labels)}
|
||||
annotations:
|
||||
"k8up.io/backupcommand": "pg_dump -U postgres -d ${var.component} --clean"
|
||||
"k8up.io/file-extension": ".sql"
|
||||
@@ -34,7 +34,7 @@ resource "kubectl_manifest" "prj_pg" {
|
||||
], var.backups.enable&&var.backups.use_barman?[<<-EOF
|
||||
backup:
|
||||
barmanObjectStore:
|
||||
destinationPath: "s3://${var.app_slug}-${var.namespace}/"
|
||||
destinationPath: "s3://${local.app_slug}-${var.namespace}/"
|
||||
endpointURL: "${var.backups.endpoint}/barman"
|
||||
s3Credentials:
|
||||
accessKeyId:
|
||||
@@ -47,7 +47,7 @@ resource "kubectl_manifest" "prj_pg" {
|
||||
]:[""]))
|
||||
}
|
||||
|
||||
resource "kubectl_manifest" "prj_pg_backup" {
|
||||
resource "kubectl_manifest" "pg_backup" {
|
||||
count = var.backups.enable ? 1:0
|
||||
yaml_body = <<-EOF
|
||||
apiVersion: postgresql.cnpg.io/v1
|
||||
@@ -55,7 +55,7 @@ resource "kubectl_manifest" "prj_pg_backup" {
|
||||
metadata:
|
||||
name: "${local.app_slug}-pg"
|
||||
namespace: "${var.namespace}"
|
||||
labels: ${jsonencode(local.pg-labels)}
|
||||
labels: ${jsonencode(local.pg_labels)}
|
||||
spec:
|
||||
schedule: "${var.backups.schedule.db}"
|
||||
backupOwnerReference: self
|
||||
@@ -64,15 +64,15 @@ resource "kubectl_manifest" "prj_pg_backup" {
|
||||
EOF
|
||||
}
|
||||
|
||||
resource "kubectl_manifest" "prj_pg_pool" {
|
||||
depends_on = [kubectl_manifest.prj_pg]
|
||||
resource "kubectl_manifest" "pg_pool" {
|
||||
depends_on = [kubectl_manifest.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)}
|
||||
labels: ${jsonencode(local.pool_labels)}
|
||||
spec:
|
||||
cluster:
|
||||
name: "${local.app_slug}-pg"
|
||||
@@ -85,3 +85,11 @@ resource "kubectl_manifest" "prj_pg_pool" {
|
||||
default_pool_size: "10"
|
||||
EOF
|
||||
}
|
||||
|
||||
data "kubernetes_secret_v1" "credentials" {
|
||||
depends_on = [ kubectl_manifest.pg ]
|
||||
metadata {
|
||||
name = "${local.app_slug}-pg-app"
|
||||
namespace = var.namespace
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
terraform {
|
||||
required_providers {
|
||||
kubectl = {
|
||||
source = "gavinbunney/kubectl"
|
||||
version = "~> 1.14.0"
|
||||
source = "gavinbunney/kubectl"
|
||||
version = "~> 1.14.0"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,52 +12,52 @@ variable "labels" {
|
||||
}
|
||||
|
||||
variable "backups" {
|
||||
default = {
|
||||
"enable" = false
|
||||
"endpoint" = ""
|
||||
default = {
|
||||
"enable" = false
|
||||
"endpoint" = ""
|
||||
"key_id_key" = "s3-id"
|
||||
"restic_key" = "bck-password"
|
||||
"schedule" = {
|
||||
"db" = "30 3 * * *"
|
||||
}
|
||||
"secret_key" = "s3-secret"
|
||||
"secret_key" = "s3-secret"
|
||||
"secret_name" = "backup-settings"
|
||||
"use_barman" = false
|
||||
"use_barman" = false
|
||||
}
|
||||
type = object({
|
||||
enable = optional(bool),
|
||||
endpoint = optional(string),
|
||||
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_key = optional(string),
|
||||
secret_name = optional(string),
|
||||
use_barman = optional(bool)
|
||||
use_barman = optional(bool)
|
||||
})
|
||||
}
|
||||
variable "images" {
|
||||
type = object({
|
||||
postgresql = optional(object({registry = optional(string), repository = optional(string), tag = optional(number)})),
|
||||
type = object({
|
||||
postgresql = optional(object({ registry = optional(string), repository = optional(string), tag = optional(number) })),
|
||||
})
|
||||
default = {
|
||||
default = {
|
||||
"postgresql" = {
|
||||
"registry" = "ghcr.io"
|
||||
"registry" = "ghcr.io"
|
||||
"repository" = "cloudnative-pg/postgresql"
|
||||
"tag" = 15.3
|
||||
"tag" = 15.3
|
||||
}
|
||||
}
|
||||
}
|
||||
variable "replicas" {
|
||||
type = number
|
||||
default = 1
|
||||
type = number
|
||||
default = 1
|
||||
}
|
||||
variable "storage" {
|
||||
type = object({
|
||||
type = object({
|
||||
size = optional(string)
|
||||
})
|
||||
default = {
|
||||
default = {
|
||||
"size" = "5Gi"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user