diff --git a/module.tf b/module.tf index be5dc16..079712d 100644 --- a/module.tf +++ b/module.tf @@ -2,14 +2,11 @@ terraform { required_version = "> 0.8.0" } - provider "kubernetes" { config_path = "~/.kube/config" } - - variable "kubernetes" { type = map(object({ name = optional(string) @@ -18,6 +15,7 @@ variable "kubernetes" { name = string pvc_size = string mount_path = string +# database = optional(bool,false) }))) environment = optional(map(object({ name = optional(string) @@ -25,7 +23,7 @@ variable "kubernetes" { value_from = optional(object({ secret_key_ref = object({ name = string - key = optional(string,null) + key = optional(string, null) }) })) # secret_name = optional(string) @@ -37,7 +35,10 @@ variable "kubernetes" { liveness_tcp_socket = optional(number) liveness_http_get = optional(string) liveness_http_get_path = optional(string) - enabled = optional(bool, true) + database = optional(map(object({ + image = optional(string) + }))) + enabled = optional(bool, true) requirements = optional(map(string), { limits-cpu = "800m" limits-memory = "500Mi" @@ -57,6 +58,8 @@ locals { name = pvc.name pvc_size = pvc.pvc_size mount_path = pvc.mount_path +# database = value.database +# database = pvc.database namespace = coalesce(value.namespace, key) }] if value.enabled == true && value.pvc != null ]) @@ -76,13 +79,16 @@ locals { # value = { for key, value in local.pvc_details : key => value } #} -output "env_test" { - value = { for key, value in local.env_details : key => value } +#output "env_test" { +# value = { for key, value in local.env_details : key => value } -} +#} + +data "kubernetes_all_namespaces" "allns" {} resource "kubernetes_namespace" "automatic" { for_each = { for key, value in var.kubernetes : key => value if value.enabled == true } + # for_each = { for key, value in var.kubernetes : key => value if value.enabled == true && !contains(data.kubernetes_all_namespaces.allns.namespaces, key) } metadata { annotations = { name = coalesce(each.value.namespace, each.key) @@ -92,11 +98,12 @@ resource "kubernetes_namespace" "automatic" { } name = coalesce(each.value.namespace, each.key) } + depends_on = [data.kubernetes_all_namespaces.allns] } resource "kubernetes_persistent_volume_claim" "automatic-pvc" { - for_each = { for key, value in local.pvc_details : "${value.index}" => merge(value, { mew_key = value.index }) } + for_each = { for key, value in local.pvc_details : "${value.index}" => merge(value, { mew_key = value.index }) if value.index != "database" } metadata { namespace = each.value.namespace name = "${each.value.name}-pvc" @@ -114,12 +121,31 @@ resource "kubernetes_persistent_volume_claim" "automatic-pvc" { } } } +resource "kubernetes_persistent_volume_claim" "automatic-pvc-database" { + for_each = { for key, value in var.kubernetes : key => value if value.database != null && value.enabled == true } + metadata { + namespace = each.value.namespace + name = "${each.key}-db-pvc" + labels = { + deployment_method = "Terraform" + } + } + spec { + access_modes = ["ReadWriteMany"] + storage_class_name = "nfs-storage-intelligent" + resources { + requests = { + storage = "1Gi" + } + } + } +} resource "kubernetes_service" "automatic_service" { for_each = { for key, value in var.kubernetes : key => value if value.service_port != null && value.enabled == true } metadata { - name = "${coalesce(each.value.namespace, each.key)}-svc" + name = "${each.key}-svc" namespace = coalesce(each.value.namespace, each.key) labels = { deployment_method = "Terraform" @@ -140,9 +166,175 @@ resource "kubernetes_service" "automatic_service" { } +resource "kubernetes_service" "automatic_service_database" { + for_each = { for key, value in var.kubernetes : key => value if value.database != null && value.enabled == true } + metadata { + name = "${each.key}-db-svc" + namespace = coalesce(each.value.namespace, each.key) + labels = { + deployment_method = "Terraform" + } + } + spec { + selector = { + app = "${each.key}-db" + } + port { + port = 5432 + target_port = 5432 + } + } + + +} + resource "kubernetes_deployment_v1" "automatic" { + for_each = { for key, value in var.kubernetes : key => value if value.enabled == true } + metadata { + namespace = coalesce(each.value.namespace, each.key) + 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 ? [""] : [] + content { + container_port = each.value.service_port + name = each.key + } + } + + + dynamic "volume_mount" { + for_each = each.value.pvc != null ? each.value.pvc : {} +# for_each = each.value.pvc != null ? (each.value.pvc.database != null ? each.value.pvc : {}) : {} + content { + mount_path = volume_mount.value.mount_path + name = "${volume_mount.value.name}-pvc" + } + } + + + + + 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 + optional = true + } + } + # dynamic "secret_ref" { + # for_each = [1] + # content { + # name = env.value.value_from.secret_key_ref.name + # optional = true + # } + # } + } + } + + } + } + + + resources { + 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 "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 "volume" { + for_each = each.value.pvc != null ? each.value.pvc : {} + content { + name = "${volume.value.name}-pvc" + persistent_volume_claim { + claim_name = "${volume.value.name}-pvc" + } + } + } + + } + } + } +} + } + + + } + + +} + + + +resource "kubernetes_deployment_v1" "database_automatic" { for_each = { for key, value in var.kubernetes : key => value if value.enabled == true } metadata { namespace = coalesce(each.value.namespace, each.key) diff --git a/variables.auto.tfvars b/variables.auto.tfvars index 611f3b7..0324dc3 100644 --- a/variables.auto.tfvars +++ b/variables.auto.tfvars @@ -23,15 +23,18 @@ kubernetes = { onitama = { enabled = true + namespace = "onitama" image = "ghcr.io/jackadamson/onitama:latest" service_port = 8080 + database = { + + } requirements = { limits-cpu = "50m" limits-memory = "50Mi" requests-cpu = "25m" requests-memory = "25Mi" } - # environment_variables = { PORT = 80, test = 22 } environment = { MY_VAR = { value_from = { @@ -56,6 +59,12 @@ kubernetes = { pvc_size = "1Mi" mount_path = "/code" } +# database = { +# name = "databaseonitama" +# pvc_size = "1Mi" +# mount_path = "/sql" +# database = true +# } } } @@ -186,6 +195,86 @@ kubernetes = { service_port = 80 } + nextcloud-web = { + namespace = "nextcloud-terraform" + enabled = false + image = "nextcloud:29.0.3-apache" + service_port = 80 + requirements = { + limits-cpu = "1000m" + limits-memory = "500Mi" + requests-cpu = "500m" + requests-memory = "25Mi" + } + environment = { + POSTGRES_HOST = { + value = "db" + } + } + pvc = { + nextcloud-webserver = { + name = "nextcloud-webserver" + pvc_size = "2Mi" + mount_path = "/var/www/html" + } + } + } + + nextcloud-db = { + namespace = "nextcloud-terraform" + enabled = false + image = "docker.io/library/postgres:14" + service_port = 5432 + requirements = { + limits-cpu = "1000m" + limits-memory = "500Mi" + requests-cpu = "500m" + requests-memory = "25Mi" + } + environment = { + POSTGRES_HOST = { + value = "db" + } + } + + environment = { + POSTGRES_DB = { + value_from = { + secret_key_ref = { + name = "nextcloud-credentials" + key = "POSTGRES_DB" + } + } + } + POSTGRES_USER = { + value_from = { + secret_key_ref = { + name = "nextcloud-credentials" + key = "POSTGRES_USER" + } + } + } + POSTGRES_PASSWORD = { + value_from = { + secret_key_ref = { + name = "nextcloud-credentials" + key = "POSTGRES_PASSWORD" + } + } + } + PORT = { + value = "80" + } + } + + pvc = { + nextcloud-database = { + name = "nextcloud-database" + pvc_size = "2Mi" + mount_path = "/var/lib/postgresql/data" + } + } + } }