Refacto and add lb
This commit is contained in:
@@ -1,15 +1,36 @@
|
||||
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 idx, port in var.lb_ports : {
|
||||
"port" = port
|
||||
"targetPort" = var.ports[idx]
|
||||
}] : []
|
||||
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"
|
||||
@@ -19,11 +40,18 @@ locals {
|
||||
"ExternalName" = {
|
||||
type = "ExternalName"
|
||||
externalName = var.target_host
|
||||
ports = local.ext_ports
|
||||
},
|
||||
"NodePort" = {
|
||||
type = "NodePort"
|
||||
selector = var.labels
|
||||
ports = local.node_ports
|
||||
},
|
||||
"LoadBalancer" = {
|
||||
type = "LoadBalancer"
|
||||
selector = var.labels
|
||||
ports = local.lb_ports
|
||||
externalTrafficPolicy = var.lb_policy
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -31,10 +59,7 @@ resource "kubectl_manifest" "service" {
|
||||
yaml_body = <<-EOF
|
||||
apiVersion: v1
|
||||
kind: Service
|
||||
metadata:
|
||||
name: "${var.instance}-${var.component}"
|
||||
namespace: "${var.namespace}"
|
||||
labels: ${jsonencode(var.labels)}
|
||||
metadata: ${jsonencode(local.metadata)}
|
||||
spec: ${jsonencode(local.spec[var.svc_type])}
|
||||
EOF
|
||||
}
|
||||
|
||||
@@ -10,12 +10,16 @@ variable "namespace" {
|
||||
variable "labels" {
|
||||
type = map(string)
|
||||
}
|
||||
variable "annotations" {
|
||||
type = map(string)
|
||||
default = {}
|
||||
}
|
||||
variable "svc_type" {
|
||||
type = string
|
||||
default = "ClusterIP"
|
||||
validation {
|
||||
condition = contains(["ClusterIP", "ExternalName", "NodePort"], var.svc_type)
|
||||
error_message = "Only ClusterIP or ExternalName is allowed"
|
||||
condition = contains(["ClusterIP", "ExternalName", "NodePort", "LoadBalancer"], var.svc_type)
|
||||
error_message = "Only ClusterIP, ExternalName, NodePort or LoadBalancer is allowed"
|
||||
}
|
||||
}
|
||||
variable "ports" {
|
||||
@@ -46,3 +50,15 @@ variable "node_ports" {
|
||||
error_message = "The range of valid ports is 30000-32767"
|
||||
}
|
||||
}
|
||||
variable "lb_ports" {
|
||||
type = list(number)
|
||||
default = [8080]
|
||||
}
|
||||
variable "lb_policy" {
|
||||
type = string
|
||||
default = "Cluster"
|
||||
validation {
|
||||
condition = contains(["Cluster", "Local"], var.lb_policy)
|
||||
error_message = "Only Cluster or Local is allowed"
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user