first commit

This commit is contained in:
2024-11-14 11:25:06 +00:00
commit 1594d8f76a
6 changed files with 649 additions and 0 deletions
+264
View File
@@ -0,0 +1,264 @@
terraform {
required_version = "> 0.8.0"
}
provider "kubernetes" {
config_path = "~/.kube/config"
}
variable "networks" {
type = map(object({
cidr_block = string
subnets = map(object({ cidr_block = string }))
}))
default = {
"private" = {
cidr_block = "10.1.0.0/16"
subnets = {
"db1" = {
cidr_block = "10.1.0.1/16"
}
"db2" = {
cidr_block = "10.1.0.2/16"
}
}
},
"public" = {
cidr_block = "10.1.1.0/16"
subnets = {
"webserver" = {
cidr_block = "10.1.1.1/16"
}
"email_server" = {
cidr_block = "10.1.1.2/16"
}
}
}
"dmz" = {
cidr_block = "10.1.2.0/16"
subnets = {
"firewall" = {
cidr_block = "10.1.2.1/16"
}
}
}
}
}
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)
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
# })))
}))
}
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 }
metadata {
namespace = coalesce(each.value.namespace, each.key)
name = "${each.key}-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_size != null ? [""] : []
content {
name = "${each.key}-volume"
persistent_volume_claim {
claim_name = "${each.key}-pvc"
}
}
}
}
}
}
}