Optimize service port definition

This commit is contained in:
2024-02-18 10:07:41 +01:00
parent bcdf666cc0
commit 82a179dad3
14 changed files with 301 additions and 129 deletions

View File

@@ -1,11 +1,15 @@
output "name" {
value = local.app_slug
}
output "default_definition" {
value = {
"name" = "${local.app_slug}"
"port" = {
"number" = var.ports[0]
output "ingress_backend_exposure" {
value = [for port_map in var.port_mapper :
{
"service" = {
"name" = "${local.app_slug}"
"port" = {
"name" = port_map.name
}
}
}
}
]
}

View File

@@ -7,21 +7,10 @@ locals {
"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]
node_ports = var.svc_type == "NodePort" ? [for port_map in var.port_mapper : {
"port" = port_map.port
"targetPort" = port_map.target
"nodePort" = port_map.port
}] : []
metadata = merge(
{
@@ -40,9 +29,9 @@ locals {
selector = local.selector
},
"ExternalName" = {
type = "ExternalName"
externalName = var.target_host
ports = local.ext_ports
type = "ExternalName"
externalName = var.target_host
ports = local.default_ports
},
"NodePort" = {
type = "NodePort"
@@ -78,6 +67,6 @@ resource "kubectl_manifest" "endpoint" {
subsets:
- addresses:
- ip: ${var.target_host}
ports: ${jsonencode([for port in var.ports : { "port" = port }])}
ports: ${jsonencode([for port_map in var.port_mapper : { "port" = port_map.port }])}
EOF
}

View File

@@ -36,43 +36,38 @@ variable "ip_family" {
}
}
variable "ports" {
type = list(number)
default = [80]
}
variable "targets" {
type = list(string)
default = ["http"]
}
variable "protocols" {
type = list(any)
default = ["TCP"]
variable "port_mapper" {
type = list(object({
name = optional(string)
port = number
protocol = string
target = string
}))
default = [{
"name" = "80-TCP",
"port" = 80,
"protocol" = "TCP"
"target" = "80-TCP"
}]
validation {
condition = alltrue([for proto in var.protocols : contains(["TCP", "UDP"], proto)])
error_message = "Only TCP or UDP is allowed"
condition = alltrue(
[for port_map in var.port_mapper : contains(["TCP", "UDP"], port_map.protocol)]
)
error_message = "Only numeric on containerPort and TCP or UDP on protocol is allowed"
}
# validation {
# condition = (var.svc_type == "NodePort") == alltrue(
# [for port_map in var.port_mapper : port_map.port >= 30000 && port_map.port <= 32767]
# )
# error_message = "The range of valid ports is 30000-32767 for a NodePort type"
# }
}
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"
}
}
variable "lb_ports" {
type = list(object({
name = string
port = object({
number = number
})
}))
default = []
}
variable "lb_policy" {
type = string
default = "Cluster"