terraform-library/main.tf

123 lines
3.0 KiB
HCL

resource "random_uuid" "identifier" {
}
locals {
label_name = format("%s-%s", var.application_name, random_uuid.identifier.result)
}
resource "kubernetes_persistent_volume_claim_v1" "storage" {
for_each = var.volumes
metadata {
name = format("%s-volumes-%s", var.application_name, each.key)
namespace = var.namespace
}
spec {
access_modes = ["ReadWriteMany"]
resources {
requests = {
storage = "2Gi"
}
}
}
}
resource "kubernetes_config_map_v1" "configuration_file" {
count = var.config_content == null ? 0 : 1
metadata {
name = "configuration-files"
namespace = var.namespace
}
data = {
"config" = var.config_content
}
}
resource "kubernetes_deployment_v1" "deployment" {
metadata {
name = var.application_name
namespace = var.namespace
labels = {
"app.kubernetes.io/part-of" = var.application_name
"app.kubernetes.io/managed-by" = "terraform"
}
}
spec {
replicas = var.replicas
selector {
match_labels = {
"app.kubernetes.io/name" = local.label_name
}
}
template {
metadata {
labels = {
"app.kubernetes.io/name" = local.label_name
}
}
spec {
container {
name = var.application_name
image = format("%s:%s", var.image.name, var.image.tag)
image_pull_policy = var.image.pull-policy
args = var.args
dynamic port {
for_each = var.ports
content {
name = format("port-%s", port.key)
container_port = port.value.container_port
}
}
dynamic env {
for_each = var.env
content {
name = env.value.name
value = env.value.value
}
}
dynamic volume_mount {
for_each = var.config_content != null ? [
kubernetes_config_map_v1.configuration_file.0.metadata.0.name
] : []
content {
name = "config-volume"
mount_path = "/application/config.json"
sub_path = "config"
}
}
dynamic volume_mount {
for_each = var.volumes
content {
name = volume_mount.key
mount_path = volume_mount.value.path
}
}
}
dynamic volume {
for_each = var.volumes
content {
name = volume.key
persistent_volume_claim {
claim_name = format("%s-volumes-%s", var.application_name, volume.key)
}
}
}
dynamic volume {
for_each = var.config_content != null ? [kubernetes_config_map_v1.configuration_file.0.metadata.0.name] : []
content {
name = "config-volume"
config_map {
name = volume.value
}
}
}
}
}
}
}