2024-05-28 22:25:56 +00:00
|
|
|
locals {
|
2024-09-25 20:06:35 +00:00
|
|
|
service_hostname = format("%s.localdomain", var.application_name)
|
2024-05-28 22:25:56 +00:00
|
|
|
at_least_one_port = length(var.ports) > 0 ? 1 : 0
|
|
|
|
ports_map = {
|
|
|
|
for index, port in var.ports : format("port-%s", index) => port
|
|
|
|
}
|
|
|
|
exposed_ports_map = {
|
|
|
|
for index, port in var.ports :
|
|
|
|
format("port-%s", index) => port if port.expose == true
|
|
|
|
}
|
|
|
|
certificate_secret_name = format("%s-certificate", var.application_name)
|
2024-09-25 20:06:35 +00:00
|
|
|
at_least_one_port_exposed = length(local.exposed_ports_map) > 0
|
2024-05-28 22:25:56 +00:00
|
|
|
}
|
|
|
|
|
2024-09-25 20:06:35 +00:00
|
|
|
|
2024-05-28 22:25:56 +00:00
|
|
|
resource "kubernetes_service_v1" "service" {
|
|
|
|
count = local.at_least_one_port
|
|
|
|
metadata {
|
|
|
|
name = var.application_name
|
|
|
|
namespace = var.namespace
|
2024-09-25 20:06:35 +00:00
|
|
|
labels = {
|
2024-05-28 22:25:56 +00:00
|
|
|
"app.kubernetes.io/part-of" = var.application_name
|
|
|
|
"app.kubernetes.io/managed-by" = "terraform"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
spec {
|
|
|
|
type = "ClusterIP"
|
|
|
|
|
|
|
|
dynamic port {
|
|
|
|
for_each = local.ports_map
|
|
|
|
content {
|
2024-09-25 20:06:35 +00:00
|
|
|
name = format("service-%s", port.key)
|
2024-05-28 22:25:56 +00:00
|
|
|
port = port.value.container_port
|
|
|
|
target_port = port.key
|
|
|
|
}
|
|
|
|
}
|
|
|
|
selector = {
|
|
|
|
"app.kubernetes.io/name" = local.label_name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "kubernetes_manifest" "certificate" {
|
2024-09-25 20:06:35 +00:00
|
|
|
# at_least_one_port_exposed is_test result
|
|
|
|
# 0 0 0
|
|
|
|
# 0 1 0
|
|
|
|
# 1 0 1
|
|
|
|
# 1 1 0
|
|
|
|
count = local.at_least_one_port_exposed && !var.is_test ? 1 : 0
|
2024-05-28 22:25:56 +00:00
|
|
|
|
|
|
|
manifest = {
|
|
|
|
apiVersion = "cert-manager.io/v1"
|
|
|
|
kind = "Certificate"
|
2024-09-25 20:06:35 +00:00
|
|
|
metadata = {
|
2024-05-28 22:25:56 +00:00
|
|
|
name = var.application_name
|
|
|
|
namespace = var.namespace
|
2024-09-25 20:06:35 +00:00
|
|
|
labels = {
|
2024-05-28 22:25:56 +00:00
|
|
|
"app.kubernetes.io/part-of" = var.application_name
|
|
|
|
"app.kubernetes.io/managed-by" = "terraform"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
spec = {
|
|
|
|
secretName = local.certificate_secret_name
|
2024-09-27 21:10:03 +00:00
|
|
|
dnsNames = concat([
|
2024-05-28 22:25:56 +00:00
|
|
|
local.service_hostname,
|
|
|
|
format("*.%s", local.service_hostname)
|
2024-09-27 21:10:03 +00:00
|
|
|
], var.additional_ingress_host)
|
2024-05-28 22:25:56 +00:00
|
|
|
issuerRef = {
|
|
|
|
kind = "ClusterIssuer"
|
|
|
|
name = "localdomain-issuer"
|
|
|
|
group = "cfssl-issuer.wikimedia.org"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "kubernetes_ingress_v1" "ingress" {
|
|
|
|
for_each = local.exposed_ports_map
|
|
|
|
|
|
|
|
metadata {
|
|
|
|
name = var.application_name
|
|
|
|
namespace = var.namespace
|
2024-09-25 20:06:35 +00:00
|
|
|
labels = {
|
2024-05-28 22:25:56 +00:00
|
|
|
"app.kubernetes.io/part-of" = var.application_name
|
|
|
|
"app.kubernetes.io/managed-by" = "terraform"
|
|
|
|
}
|
|
|
|
annotations = {
|
|
|
|
"traefik.ingress.kubernetes.io/router.middlewares" = "kube-ingress-gzip-compress@kubernetescrd"
|
|
|
|
"traefik.ingress.kubernetes.io/router.entrypoints" = "websecure"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
spec {
|
2024-09-25 20:06:35 +00:00
|
|
|
dynamic rule {
|
|
|
|
for_each = concat(
|
|
|
|
var.additional_ingress_host, [local.service_hostname]
|
|
|
|
)
|
|
|
|
content {
|
|
|
|
host = rule.value
|
|
|
|
http {
|
|
|
|
path {
|
|
|
|
path = "/"
|
|
|
|
backend {
|
|
|
|
service {
|
|
|
|
name = kubernetes_service_v1.service[0].metadata.0.name
|
|
|
|
port {
|
|
|
|
name = format("service-%s", each.key)
|
|
|
|
}
|
2024-05-28 22:25:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tls {
|
2024-09-25 20:06:35 +00:00
|
|
|
hosts = concat(
|
|
|
|
var.additional_ingress_host, [local.service_hostname]
|
|
|
|
)
|
2024-05-28 22:25:56 +00:00
|
|
|
secret_name = local.certificate_secret_name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# {{ application_name }}.localdomain IN CNAME internal-lb
|
|
|
|
resource "kubernetes_manifest" "record" {
|
2024-09-25 20:06:35 +00:00
|
|
|
# at_least_one_port_exposed is_test result
|
|
|
|
# 0 0 0
|
|
|
|
# 0 1 0
|
|
|
|
# 1 0 1
|
|
|
|
# 1 1 0
|
|
|
|
count = local.at_least_one_port_exposed && !var.is_test ? 1 : 0
|
2024-05-28 22:25:56 +00:00
|
|
|
|
|
|
|
manifest = {
|
|
|
|
apiVersion = "externaldns.k8s.io/v1alpha1"
|
|
|
|
kind = "DNSEndpoint"
|
2024-09-25 20:06:35 +00:00
|
|
|
metadata = {
|
2024-05-28 22:25:56 +00:00
|
|
|
name = var.application_name
|
|
|
|
namespace = var.namespace
|
2024-09-25 20:06:35 +00:00
|
|
|
labels = {
|
2024-05-28 22:25:56 +00:00
|
|
|
"app.kubernetes.io/part-of" = var.application_name
|
|
|
|
"app.kubernetes.io/managed-by" = "terraform"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
spec = {
|
|
|
|
endpoints = [
|
|
|
|
{
|
|
|
|
dnsName = local.service_hostname
|
|
|
|
recordTTL = "180"
|
|
|
|
recordType = "CNAME"
|
2024-09-25 20:06:35 +00:00
|
|
|
targets = [
|
2024-05-28 22:25:56 +00:00
|
|
|
"internal-lb.localdomain"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|