Files
Terraform_kube/module.tf
T

231 lines
5.7 KiB
Terraform

terraform {
required_version = "> 0.8.0"
}
provider "kubernetes" {
config_path = "~/.kube/config"
}
variable "kubernetes" {
type = map(object({
name = optional(string)
image = 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" })
}))
}
locals {
pvc_details = flatten([
for key, value in var.kubernetes : [
for index, pvc in value.pvc : {
index = index
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)
}
labels = {
deployment_method = "Terraform"
}
name = coalesce(each.value.namespace, each.key)
}
}
resource "kubernetes_persistent_volume_claim" "automatic-pvc" {
for_each = { for key, value in local.pvc_details : "${value.index}" => merge(value, {mew_key = value.index}) }
metadata {
namespace = each.value.namespace
name = "${each.value.name}-pvc"
labels = {
deployment_method = "Terraform"
}
}
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)}-svc"
namespace = coalesce(each.value.namespace, each.key)
labels = {
deployment_method = "Terraform"
}
}
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
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 : {}
content {
mount_path = volume_mount.value.mount_path
name = "${volume_mount.value.name}-pvc"
}
}
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 ? each.value.pvc : {}
content {
name = "${volume.value.name}-pvc"
persistent_volume_claim {
claim_name = "${volume.value.name}-pvc"
}
}
}
}
}
}
}