85 lines
2.4 KiB
HCL
85 lines
2.4 KiB
HCL
locals {
|
|
app_slug = "${var.instance}${var.component == "" ? "" : "-"}${var.component}"
|
|
cluster_ports = var.svc_type == "ClusterIP" ? [for idx, target in var.targets : {
|
|
"name" = target
|
|
"port" = var.ports[idx]
|
|
"protocol" = var.protocols[idx]
|
|
"targetPort" = target
|
|
}] : []
|
|
ext_ports = var.svc_type == "ExternalName" ? [for idx, target in var.targets : {
|
|
"name" = target
|
|
"port" = var.ports[idx]
|
|
"protocol" = var.protocols[idx]
|
|
"targetPort" = var.ports[idx]
|
|
}] : []
|
|
lb_ports = var.svc_type == "LoadBalancer" ? [for port in var.lb_ports : {
|
|
"port" = port.port.number
|
|
"name" = port.name
|
|
"targetPort" = port.port.number
|
|
}] : []
|
|
node_ports = var.svc_type == "NodePort" ? [for idx, port in var.ports : {
|
|
"port" = port
|
|
"targetPort" = port
|
|
"nodePort" = var.node_ports[idx]
|
|
}] : []
|
|
metadata = merge(
|
|
{
|
|
"name" = local.app_slug
|
|
"namespace" = var.namespace
|
|
"labels" = var.labels
|
|
},
|
|
length(var.annotations) > 0 ? {
|
|
"annotations" = var.annotations
|
|
} : {}
|
|
)
|
|
spec = {
|
|
"ClusterIP" = {
|
|
type = "ClusterIP"
|
|
ports = local.cluster_ports
|
|
selector = var.labels
|
|
ipFamilyPolicy = var.ip_family
|
|
},
|
|
"ExternalName" = {
|
|
type = "ExternalName"
|
|
externalName = var.target_host
|
|
ports = local.ext_ports
|
|
},
|
|
"NodePort" = {
|
|
type = "NodePort"
|
|
selector = var.labels
|
|
ports = local.node_ports
|
|
ipFamilyPolicy = var.ip_family
|
|
},
|
|
"LoadBalancer" = {
|
|
type = "LoadBalancer"
|
|
selector = var.labels
|
|
ports = local.lb_ports
|
|
externalTrafficPolicy = var.lb_policy
|
|
ipFamilyPolicy = var.ip_family
|
|
}
|
|
}
|
|
}
|
|
resource "kubectl_manifest" "service" {
|
|
yaml_body = <<-EOF
|
|
apiVersion: v1
|
|
kind: Service
|
|
metadata: ${jsonencode(local.metadata)}
|
|
spec: ${jsonencode(local.spec[var.svc_type])}
|
|
EOF
|
|
}
|
|
resource "kubectl_manifest" "endpoint" {
|
|
count = var.svc_type == "ExternalName" ? 1 : 0
|
|
yaml_body = <<-EOF
|
|
apiVersion: v1
|
|
kind: Endpoints
|
|
metadata:
|
|
name: "${local.app_slug}"
|
|
namespace: "${var.namespace}"
|
|
labels: ${jsonencode(var.labels)}
|
|
subsets:
|
|
- addresses:
|
|
- ip: ${var.target_host}
|
|
ports: ${jsonencode([for port in var.ports : { "port" = port }])}
|
|
EOF
|
|
}
|