Improve modules
This commit is contained in:
@@ -1,22 +1,40 @@
|
||||
locals {
|
||||
ports = [ for idx, target in var.targets : {
|
||||
"name" = target
|
||||
"port"= var.ports[idx]
|
||||
"protocol"= var.protocols[idx]
|
||||
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
|
||||
yaml_body = <<-EOF
|
||||
apiVersion: v1
|
||||
kind: Service
|
||||
metadata:
|
||||
name: "${var.instance}-${var.component}"
|
||||
namespace: "${var.namespace}"
|
||||
labels: ${jsonencode(var.labels)}
|
||||
spec:
|
||||
type: ClusterIP
|
||||
ports: ${jsonencode(local.ports)}
|
||||
selector: ${jsonencode(var.labels)}
|
||||
spec: ${jsonencode(local.spec[var.svc_type])}
|
||||
EOF
|
||||
}
|
||||
|
||||
@@ -1,24 +1,48 @@
|
||||
variable "component" {
|
||||
type = string
|
||||
type = string
|
||||
}
|
||||
variable "instance" {
|
||||
type = string
|
||||
type = string
|
||||
}
|
||||
variable "namespace" {
|
||||
type = string
|
||||
type = string
|
||||
}
|
||||
variable "labels" {
|
||||
type = map(string)
|
||||
type = map(string)
|
||||
}
|
||||
variable "svc_type" {
|
||||
type = string
|
||||
default = "ClusterIP"
|
||||
validation {
|
||||
condition = contains(["ClusterIP", "ExternalName", "NodePort"], var.svc_type)
|
||||
error_message = "Only ClusterIP or ExternalName is allowed"
|
||||
}
|
||||
}
|
||||
variable "ports" {
|
||||
type = list
|
||||
default = [80]
|
||||
type = list(number)
|
||||
default = [80]
|
||||
}
|
||||
variable "targets" {
|
||||
type = list
|
||||
default = ["http"]
|
||||
type = list(string)
|
||||
default = ["http"]
|
||||
}
|
||||
variable "protocols" {
|
||||
type = list
|
||||
default = ["TCP"]
|
||||
}
|
||||
type = list(any)
|
||||
default = ["TCP"]
|
||||
validation {
|
||||
condition = alltrue([for proto in var.protocols : contains(["TCP", "UDP"], proto)])
|
||||
error_message = "Only TCP or UDP is allowed"
|
||||
}
|
||||
}
|
||||
variable "target_host" {
|
||||
type = string
|
||||
default = ""
|
||||
}
|
||||
variable "node_ports" {
|
||||
type = list(number)
|
||||
default = [30080]
|
||||
validation {
|
||||
condition = alltrue([for port in var.node_ports : port >= 30000 && port <= 32767])
|
||||
error_message = "The range of valid ports is 30000-32767"
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user