main.tf 2.57 KB
terraform {
  required_providers {
    kubectl = {
      source  = "gavinbunney/kubectl"
      version = "1.14.0"
    }
  }
}

module "deployment" {
  # disable wait for all pods be ready.
  #
  wait_for_rollout = false

  # always pull latest version avoid fake updating.
  #
  image_pull_policy = "Always"

  # Use local paths to avoid accessing external networks
  # This module comes from terraform registry "terraform-iaac/deployment/kubernetes 1.4.2"
  source = "./modules/deployment/kubernetes"

  name           = local.name
  namespace      = local.namespace
  image          = var.image
  replicas       = var.replicas
  restart_policy = var.restart_policy
  args           = [var.args]
  resources      = {
    request_cpu    = var.request_cpu == "" ? null : var.request_cpu
    limit_cpu      = var.limit_cpu == "" ? null : var.limit_cpu
    request_memory = var.request_memory == "" ? null : var.request_memory
    limit_memory   = var.limit_memory == "" ? null : var.limit_memory
  }
  env = var.env



  volume_host_path = [
    {
      volume_name  = var.app_log_path_volume_name
      path_on_node = var.app_log_path
    }
  ]

  volume_mount = [
    {
      mount_path  = var.app_log_path
      volume_name = var.app_log_path_volume_name
    }
  ]

  #domian map to /etc/hosts
  hosts = [
  for host_list in var.hosts:
  {
    hostname = host_list.hostname,
    ip       = host_list.ip
  }
  ]
}

module "service" {

  # Use local paths to avoid accessing external networks
  # This module comes from terraform registry "terraform-iaac/service/kubernetes 1.0.4"
  source = "./modules/service/kubernetes"

  app_name      = local.name
  app_namespace = local.namespace
  type          = "NodePort"
  port_mapping  = [
  for p in var.ports :
  {
    name          = "port-${p}"
    internal_port = p
    external_port = p
    protocol      = "TCP"
  }
  ]

}
module "ingress" {
  count  = var.ingress_switch.switch == "on" ? 1 : 0
  source = "./modules/ingress/kubernetes"

  ingress_name      = local.name
  service_namespace = local.namespace
  service_name      = local.name

  rule = [
    {
      domain        = var.ingress_switch.domain_name
      external_port = var.ports[0] // Service port
      path      = var.ingress_switch.path
      path_type = var.ingress_switch.path_type
    }
  ]

}


data "kubernetes_service" "service" {
  depends_on = [module.service]

  metadata {
    name      = local.name
    namespace = local.namespace
  }
}

locals {
  name      = coalesce(var.name, "${var.walrus_metadata_service_name}")
  namespace = coalesce(var.namespace, var.walrus_metadata_namespace_name)
}