Files
Terraform_kube/module.tf
T
2024-11-18 13:26:49 +00:00

250 lines
6.5 KiB
Terraform

terraform {
required_version = "> 0.8.0"
}
provider "kubernetes" {
config_path = "~/.kube/config"
}
variable "kubernetes" {
type = map(object({
#service = string
# pvc_name = optional(string)
name = optional(string)
image = string
# mount_path = optional(string, "/mount")
# pvc_size = optional(string)
pvc = optional(map(object({
name = string
pvc_size = string
mount_path = string
})))
replicas = optional(number, 1)
namespace = optional(string)
service_port = optional(number)
service_name = optional(string)
liveness_tcp_socket = optional(number)
liveness_http_get = optional(string)
liveness_http_get_path = optional(string)
enabled = optional(bool, true)
requirements = optional(map(string), {
limits-cpu = "800m"
limits-memory = "500Mi"
requests-cpu = "200m"
requests-memory = "200Mi"
})
environment_variables = optional(map(string), { name = "value" })
# environment_variables = optional(list(object({
# name = string
# value = string
# })))
}))
}
locals {
pvc_details = flatten([
for key, value in var.kubernetes : [
for pvc in value.pvc : {
name = pvc.name
pvc_size = pvc.pvc_size
mount_path = pvc.mount_path
namespace = coalesce(value.namespace, key)
}] if value.enabled == true && value.pvc != null
])
}
output "pvc_test" {
value = { for key, value in local.pvc_details : key => value }
}
resource "kubernetes_namespace" "automatic" {
for_each = { for key, value in var.kubernetes : key => value if value.enabled == true }
metadata {
annotations = {
name = coalesce(each.value.namespace, each.key)
}
name = coalesce(each.value.namespace, each.key)
}
}
resource "kubernetes_persistent_volume_claim" "automatic-pvc" {
## for_each = { for key, value in var.kubernetes : key => value if value.pvc_size != null && value.enabled == true }
for_each = { for key, value in local.pvc_details : key => value }
metadata {
# namespace = coalesce(each.value.namespace, each.key)
namespace = each.value.namespace
name = "${each.value.name}-pvc"
}
spec {
access_modes = ["ReadWriteMany"]
storage_class_name = "nfs-storage-intelligent"
resources {
requests = {
storage = each.value.pvc_size
}
}
}
}
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)
# name = "${each.value.namespace}-svc"
name = "${coalesce(each.value.namespace, each.key)}-svc"
namespace = coalesce(each.value.namespace, each.key)
}
spec {
selector = {
app = each.key
}
port {
port = each.value.service_port
target_port = each.value.service_port
}
}
}
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 = coalesce(each.value.namespace, each.key)
labels = {
app = each.key
}
}
spec {
replicas = each.value.replicas
selector {
match_labels = {
app = each.key
}
}
template {
metadata {
labels = {
app = each.key
}
}
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_size != null ? [""] : []
# content {
# mount_path = each.value.mount_path
# name = "${each.key}-volume"
# }
# }
#########################################
############# working but not goign through every arguments
# dynamic "env" {
# for_each = each.value.environment_variables != null ? [""] : []
# content {
# name = keys(each.value.environment_variables)[0]
# value = values(each.value.environment_variables)[0]
# }
# }
#######################################
dynamic "env" {
for_each = each.value.environment_variables != null ? each.value.environment_variables : {}
content {
name = env.key
value = env.value
}
}
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 ? [""] : []
# for_each = each.value.pvc_size != null ? [""] : []
content {
name = "${each.key}-volume"
persistent_volume_claim {
claim_name = "${each.key}-pvc"
}
}
}
}
}
}
}