251 lines
7.5 KiB
Terraform
251 lines
7.5 KiB
Terraform
|
|
resource "kubernetes_deployment_v1" "automatic" {
|
|
for_each = { for key, value in var.kubernetes : key => value if value.enabled == true && value.docker != true }
|
|
metadata {
|
|
namespace = coalesce(each.value.namespace, each.value.connected_namespace, each.key)
|
|
name = coalesce(each.value.name, each.key)
|
|
|
|
labels = {
|
|
app = each.key
|
|
deployment_method = "Terraform"
|
|
}
|
|
}
|
|
|
|
spec {
|
|
replicas = each.value.replicas
|
|
|
|
selector {
|
|
match_labels = {
|
|
app = each.key
|
|
}
|
|
}
|
|
|
|
template {
|
|
metadata {
|
|
labels = {
|
|
app = each.key
|
|
deployment_method = "Terraform"
|
|
}
|
|
}
|
|
|
|
spec {
|
|
container {
|
|
image = each.value.image
|
|
name = coalesce(each.value.name, each.key)
|
|
dynamic "port" {
|
|
for_each = each.value.service_port != null ? each.value.service_port : []
|
|
content {
|
|
container_port = port.value
|
|
# name = "${each.key}-${port.value}"
|
|
name = replace("${port.value}${each.key}", "/(.{0,15})(.*)/", "$1")
|
|
}
|
|
}
|
|
|
|
|
|
dynamic "volume_mount" {
|
|
for_each = each.value.pvc != null ? each.value.pvc : {}
|
|
content {
|
|
mount_path = volume_mount.value.mount_path
|
|
name = "${volume_mount.key}-volume"
|
|
sub_path = volume_mount.value.sub_path != null ? volume_mount.value.sub_path : ""
|
|
}
|
|
}
|
|
|
|
dynamic "volume_mount" {
|
|
for_each = try(each.value.database.engine == "sqlserver", false) && try(each.value.database.shunt, false) ? [1] : []
|
|
content {
|
|
mount_path = "/backup"
|
|
name = "${each.key}-db-backup"
|
|
}
|
|
}
|
|
|
|
dynamic "env_from" {
|
|
for_each = each.value.secret_ref != null ? each.value.secret_ref : []
|
|
content {
|
|
dynamic "secret_ref" {
|
|
for_each = [1]
|
|
content {
|
|
name = env_from.value
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
dynamic "env_from" {
|
|
for_each = each.value.config_map_ref != null ? each.value.config_map_ref : []
|
|
content {
|
|
dynamic "config_map_ref" {
|
|
for_each = [1]
|
|
content {
|
|
name = env_from.value
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
command = each.value.command != null ? each.value.command : []
|
|
|
|
|
|
dynamic "env" {
|
|
for_each = each.value.environment != null ? each.value.environment : {}
|
|
content {
|
|
name = env.key
|
|
value = env.value.value != null ? env.value.value : null
|
|
dynamic "value_from" {
|
|
for_each = env.value.value_from != null ? env.value.value_from : {}
|
|
content {
|
|
dynamic "secret_key_ref" {
|
|
for_each = [1]
|
|
content {
|
|
name = env.value.value_from.secret_key_ref.name
|
|
key = env.value.value_from.secret_key_ref.key
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
dynamic "resources" {
|
|
for_each = each.value.requirements != null ? [1] : []
|
|
content {
|
|
limits = {
|
|
cpu = each.value.requirements.limits-cpu
|
|
memory = each.value.requirements.limits-memory
|
|
}
|
|
requests = {
|
|
cpu = each.value.requirements.requests-cpu
|
|
memory = each.value.requirements.requests-memory
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
|
|
dynamic "security_context" {
|
|
for_each = each.value.security_context == true ? [1] : []
|
|
content {
|
|
privileged = true
|
|
#capabilities = {
|
|
# add : ["NET_ADMIN", "NET_RAW"]
|
|
#}
|
|
}
|
|
}
|
|
dynamic "liveness_probe" {
|
|
for_each = each.value.liveness_tcp_socket != null || each.value.liveness_http_get != null ? [""] : []
|
|
content {
|
|
dynamic "tcp_socket" {
|
|
for_each = each.value.liveness_tcp_socket != null ? [""] : []
|
|
content {
|
|
port = each.value.liveness_tcp_socket
|
|
}
|
|
}
|
|
|
|
dynamic "http_get" {
|
|
for_each = each.value.liveness_http_get != null ? [""] : []
|
|
content {
|
|
port = each.value.liveness_http_get
|
|
path = coalesce(each.value.liveness_http_get_path, "/")
|
|
}
|
|
}
|
|
|
|
initial_delay_seconds = 3
|
|
period_seconds = 3
|
|
}
|
|
}
|
|
}
|
|
dynamic "dns_config" {
|
|
for_each = each.value.dns_nameservers != null ? [1] : []
|
|
content {
|
|
nameservers = [each.value.dns_nameservers]
|
|
}
|
|
|
|
}
|
|
service_account_name = each.value.service_account_name != null ? each.value.service_account_name : null
|
|
|
|
dynamic "volume" {
|
|
for_each = each.value.pvc != null ? each.value.pvc : {}
|
|
content {
|
|
name = "${volume.key}-volume"
|
|
|
|
dynamic "config_map" {
|
|
for_each = volume.value.config_map ? [1] : []
|
|
content {
|
|
name = volume.key
|
|
}
|
|
}
|
|
|
|
dynamic "host_path" {
|
|
for_each = volume.value.host_path != null || (!(volume.value.config_map) && !(volume.value.secret)) ? [1] : []
|
|
content {
|
|
path = coalesce(volume.value.host_path, join("/", [var.storage_path.main_storage, coalesce(each.value.namespace, each.value.connected_namespace, each.key), volume.key]))
|
|
|
|
}
|
|
}
|
|
|
|
dynamic "secret" {
|
|
for_each = volume.value.secret ? [1] : []
|
|
content {
|
|
secret_name = volume.key
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
dynamic "volume" {
|
|
for_each = try(each.value.database.engine == "sqlserver", false) && try(each.value.database.shunt, false) ? [1] : []
|
|
content {
|
|
name = "${each.key}-db-backup"
|
|
|
|
persistent_volume_claim {
|
|
claim_name = "${each.key}-db-backup"
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
resource "docker_container" "deployment-automatic" {
|
|
for_each = { for key, value in var.kubernetes : key => value if value.enabled == true && value.docker == true }
|
|
image = each.value.image
|
|
name = coalesce(each.value.name, each.key)
|
|
hostname = coalesce(each.value.name, each.key)
|
|
|
|
depends_on = [docker_container.database-automatic]
|
|
|
|
networks_advanced {
|
|
name = "deployment_network"
|
|
}
|
|
|
|
dynamic "ports" {
|
|
for_each = each.value.service_port != null ? each.value.service_port : []
|
|
content {
|
|
internal = ports.value
|
|
external = each.value.external_port ? ports.value : null
|
|
}
|
|
}
|
|
|
|
dynamic "volumes" {
|
|
for_each = each.value.pvc != null ? each.value.pvc : {}
|
|
content {
|
|
container_path = volumes.value.mount_path
|
|
host_path = volumes.value.host_path
|
|
}
|
|
}
|
|
|
|
env = [
|
|
for key, value in(each.value.environment != null ? each.value.environment : {}) :
|
|
"${key}=${value.value != null ? value.value : ""}"
|
|
]
|
|
|
|
|
|
# null keeps the image's default command; [] would differ from it in state and force replacement
|
|
command = each.value.command
|
|
|
|
}
|