41 lines
1.0 KiB
HCL
41 lines
1.0 KiB
HCL
locals {
|
|
cluster_ports = var.svc_type == "ClusterIP" ? [for idx, target in var.targets : {
|
|
"name" = target
|
|
"port" = var.ports[idx]
|
|
"protocol" = var.protocols[idx]
|
|
"targetPort" = target
|
|
}] : []
|
|
node_ports = var.svc_type == "NodePort" ? [for idx, port in var.ports : {
|
|
"port" = port
|
|
"targetPort" = port
|
|
"nodePort" = var.node_ports[idx]
|
|
}] : []
|
|
spec = {
|
|
"ClusterIP" = {
|
|
type = "ClusterIP"
|
|
ports = local.cluster_ports
|
|
selector = var.labels
|
|
},
|
|
"ExternalName" = {
|
|
type = "ExternalName"
|
|
externalName = var.target_host
|
|
},
|
|
"NodePort" = {
|
|
type = "NodePort"
|
|
selector = var.labels
|
|
ports = local.node_ports
|
|
}
|
|
}
|
|
}
|
|
resource "kubectl_manifest" "service" {
|
|
yaml_body = <<-EOF
|
|
apiVersion: v1
|
|
kind: Service
|
|
metadata:
|
|
name: "${var.instance}-${var.component}"
|
|
namespace: "${var.namespace}"
|
|
labels: ${jsonencode(var.labels)}
|
|
spec: ${jsonencode(local.spec[var.svc_type])}
|
|
EOF
|
|
}
|