diff --git a/core/services.tf b/core/services.tf index d26ab5f..9ed3e46 100644 --- a/core/services.tf +++ b/core/services.tf @@ -4,90 +4,11 @@ module "ingress" { } -resource "kubernetes_service_v1" "automatic_service" { - for_each = { for key, value in var.kubernetes : key => value if value.service_port != null && value.enabled == true && value.docker != true } - metadata { - name = "${each.key}-svc" - namespace = coalesce(each.value.namespace, each.value.connected_namespace, each.key) - labels = { - deployment_method = "Terraform" - } - annotations = each.value.service_type == "LoadBalancer" ? { - "metallb.universe.tf/ip-allocated-from-pool" = "first-pool" - } : {} - - - - } - spec { - selector = { - app = each.key - } - dynamic "port" { - for_each = each.value.service_port != null ? each.value.service_port : [] - content { - port = port.value - target_port = port.value - name = replace("${port.value}${each.key}", "/(.{0,15})(.*)/", "$1") - node_port = each.value.service_type == "NodePort" ? port.value : 0 - } - } - - type = each.value.service_type - - - } - - +module "services" { + source = "../modules/services" + kubernetes_input = var.kubernetes } -resource "kubernetes_service_v1" "automatic_service_database" { - for_each = { for key, value in var.kubernetes : key => value if value.database != null && value.enabled == true && !(try(value.database.shunt, false)) && value.docker != true } - metadata { - name = "${each.key}-db-svc" - namespace = coalesce(each.value.namespace, each.value.connected_namespace, each.key) - labels = { - deployment_method = "Terraform" - #username = "${resource.bitwarden-secrets_secret.automatic_bitwarden_secret_password[each.key].key}" - } - } - spec { - selector = { - app = "${each.key}-db" - } - port { - port = try(each.value.database.port, 5432) - target_port = try(each.value.database.port, 5432) - } - - } - - -} - - - -resource "kubernetes_service_v1" "automatic_service_redis" { - for_each = { for key, value in var.kubernetes : key => value if value.redis != null && value.enabled == true && value.docker != true } - metadata { - name = "${each.key}-redis-svc" - namespace = coalesce(each.value.namespace, each.value.connected_namespace, each.key) - labels = { - deployment_method = "Terraform" - } - } - spec { - selector = { - app = "${each.key}-redis" - } - port { - port = 6379 - target_port = 6379 - } - } -} - - resource "docker_network" "terraform_network" { #avoiding this to create if the shunt_docker is on diff --git a/infrastructure/deployment.tf b/infrastructure/deployment.tf index b8f6174..f559f65 100644 --- a/infrastructure/deployment.tf +++ b/infrastructure/deployment.tf @@ -1,50 +1,13 @@ -resource "kubernetes_service_v1" "automatic_service" { - for_each = { for key, value in var.kubernetes : key => value if value.service_port != null && value.enabled == true } - metadata { - name = "${each.key}-svc" - namespace = coalesce(each.value.namespace, each.value.connected_namespace, each.key) - labels = { - deployment_method = "Terraform" - } - annotations = each.value.service_type == "LoadBalancer" ? { - "metallb.universe.tf/ip-allocated-from-pool" = "first-pool" - } : {} - - - - } - spec { - selector = { - app = each.key - } - dynamic "port" { - for_each = each.value.service_port != null ? each.value.service_port : [] - content { - port = port.value - target_port = port.value - name = replace("${port.value}${each.key}", "/(.{0,15})(.*)/", "$1") - - } - } - - # port { - # port = each.value.service_port - # target_port = each.value.service_port - # } - type = each.value.service_type - - - } - depends_on = [resource.kubernetes_namespace_v1.automatic] - - -} module "ingress" { source = "../modules/ingress" kubernetes_input = var.kubernetes } +module "services" { + source = "../modules/services" + kubernetes_input = var.kubernetes +} resource "kubernetes_deployment_v1" "automatic" { for_each = { for key, value in var.kubernetes : key => value if value.enabled == true } diff --git a/modules/services/main.tf b/modules/services/main.tf new file mode 100644 index 0000000..451a0c1 --- /dev/null +++ b/modules/services/main.tf @@ -0,0 +1,141 @@ +variable "kubernetes_input" { + type = map(object({ + name = optional(string) + docker = optional(bool) + image = string + pvc = optional(map(object({ + name = optional(string) + config_map = optional(bool, false) + secret = optional(bool, false) + local_path = optional(bool, false) + host_path = optional(string) + storage_class_name = optional(string, "nfs-storage-intelligent") + annotations_fileType = optional(string) + mount_path = string + sub_path = optional(string) + }))) + environment = optional(map(object({ + name = optional(string) + value = optional(string) + value_from = optional(object({ + secret_key_ref = object({ + name = string + key = optional(string, null) + }) + })) + }))) + replicas = optional(number, 1) + namespace = optional(string) + connected_namespace = optional(string, "") + command = optional(list(string)) + service_port = optional(list(number)) + external_port = optional(bool, false) + service_type = optional(string, "ClusterIP") + service_name = optional(string) + domain_name = optional(string) + legacy_domain = optional(bool, false) + exposed = optional(bool, true) + liveness_tcp_socket = optional(number) + liveness_http_get = optional(string) + liveness_http_get_path = optional(string) + database = optional(map(string)) + database_command = optional(list(string)) + database_args = optional(list(string)) + redis = optional(map(string)) + enabled = optional(bool, true) + secret_ref = optional(list(string)) + config_map_ref = optional(list(string)) + config_map_import = optional(list(string)) + secret_import = optional(list(string)) + extra_manifest = optional(list(string)) + requirements = optional(map(string)) + dns_nameservers = optional(string) + service_account_name = optional(string) + environment_variables = optional(map(string), { name = "value" }) + security_context = optional(bool, false) + })) + +} + +resource "kubernetes_service_v1" "automatic_service" { + for_each = { for key, value in var.kubernetes_input : key => value if value.service_port != null && value.enabled == true && value.docker != true } + metadata { + name = "${each.key}-svc" + namespace = coalesce(each.value.namespace, each.value.connected_namespace, each.key) + labels = { + deployment_method = "Terraform" + } + annotations = each.value.service_type == "LoadBalancer" ? { + "metallb.universe.tf/ip-allocated-from-pool" = "first-pool" + } : {} + + + + } + spec { + selector = { + app = each.key + } + dynamic "port" { + for_each = each.value.service_port != null ? each.value.service_port : [] + content { + port = port.value + target_port = port.value + name = replace("${port.value}${each.key}", "/(.{0,15})(.*)/", "$1") + node_port = each.value.service_type == "NodePort" ? port.value : 0 + } + } + + type = each.value.service_type + + + } + + +} + +resource "kubernetes_service_v1" "automatic_service_database" { + for_each = { for key, value in var.kubernetes_input : key => value if value.database != null && value.enabled == true && !(try(value.database.shunt, false)) && value.docker != true } + metadata { + name = "${each.key}-db-svc" + namespace = coalesce(each.value.namespace, each.value.connected_namespace, each.key) + labels = { + deployment_method = "Terraform" + #username = "${resource.bitwarden-secrets_secret.automatic_bitwarden_secret_password[each.key].key}" + } + } + spec { + selector = { + app = "${each.key}-db" + } + port { + port = try(each.value.database.port, 5432) + target_port = try(each.value.database.port, 5432) + } + + } + + +} + + + +resource "kubernetes_service_v1" "automatic_service_redis" { + for_each = { for key, value in var.kubernetes_input : key => value if value.redis != null && value.enabled == true && value.docker != true } + metadata { + name = "${each.key}-redis-svc" + namespace = coalesce(each.value.namespace, each.value.connected_namespace, each.key) + labels = { + deployment_method = "Terraform" + } + } + spec { + selector = { + app = "${each.key}-redis" + } + port { + port = 6379 + target_port = 6379 + } + } +}