main.tf 2.7 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
  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

  readiness_probe = {
    http_get = {
      path   = var.probe_http_path
      port   = var.probe_http_port
      scheme = "HTTP"
    }
    success_threshold     = 1
    failure_threshold     = 3
    initial_delay_seconds = 30
    period_seconds        = 30
    timeout_seconds       = 3
  }

  # start time 4 minute failure_threshold * period_seconds  = 4m
  startup_probe = {
    http_get = {
      path   = var.probe_http_path
      port   = var.probe_http_port
      scheme = "HTTP"
    }
    success_threshold     = 1
    failure_threshold     = 3
    initial_delay_seconds = 10
    period_seconds        = 10
    timeout_seconds       = 5
  }

}

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  = [
    {
      name          = "port-${var.port}"
      internal_port = var.port
      external_port = var.port
      protocol      = "TCP"
    }
  ]

}
module "ingress" {
  source = "./modules/ingress/kubernetes"

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

  rule = [
    {
      domain        = var.domain_name
      external_port = var.port // Service port
      path          = var.path
      path_type     = var.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)
}