Refacto and add modules
This commit is contained in:
@@ -1,11 +1,11 @@
|
||||
output "name" {
|
||||
value = "${local.app_slug}"
|
||||
value = local.app_slug
|
||||
}
|
||||
output "default_definition" {
|
||||
value = {
|
||||
"name" = "${local.app_slug}"
|
||||
"name" = "${local.app_slug}"
|
||||
"port" = {
|
||||
"number" = var.ports[0]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
terraform {
|
||||
required_providers {
|
||||
kubectl = {
|
||||
source = "gavinbunney/kubectl"
|
||||
version = "~> 1.14.0"
|
||||
source = "gavinbunney/kubectl"
|
||||
version = "~> 1.14.0"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
locals {
|
||||
app_slug = "${var.instance}${var.component==""?"":"-"}${var.component}"
|
||||
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]
|
||||
@@ -12,9 +12,10 @@ locals {
|
||||
"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]
|
||||
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
|
||||
@@ -23,35 +24,38 @@ locals {
|
||||
}] : []
|
||||
metadata = merge(
|
||||
{
|
||||
"name"= "${local.app_slug}"
|
||||
"namespace"= var.namespace
|
||||
"labels"= var.labels
|
||||
"name" = local.app_slug
|
||||
"namespace" = var.namespace
|
||||
"labels" = var.labels
|
||||
},
|
||||
length(var.annotations) > 0 ? {
|
||||
"annotations"= var.annotations
|
||||
"annotations" = var.annotations
|
||||
} : {}
|
||||
)
|
||||
spec = {
|
||||
"ClusterIP" = {
|
||||
type = "ClusterIP"
|
||||
ports = local.cluster_ports
|
||||
selector = var.labels
|
||||
type = "ClusterIP"
|
||||
ports = local.cluster_ports
|
||||
selector = var.labels
|
||||
ipFamilyPolicy = var.ip_family
|
||||
},
|
||||
"ExternalName" = {
|
||||
type = "ExternalName"
|
||||
externalName = var.target_host
|
||||
ports = local.ext_ports
|
||||
type = "ExternalName"
|
||||
externalName = var.target_host
|
||||
ports = local.ext_ports
|
||||
},
|
||||
"NodePort" = {
|
||||
type = "NodePort"
|
||||
selector = var.labels
|
||||
ports = local.node_ports
|
||||
type = "NodePort"
|
||||
selector = var.labels
|
||||
ports = local.node_ports
|
||||
ipFamilyPolicy = var.ip_family
|
||||
},
|
||||
"LoadBalancer" = {
|
||||
type = "LoadBalancer"
|
||||
selector = var.labels
|
||||
ports = local.lb_ports
|
||||
type = "LoadBalancer"
|
||||
selector = var.labels
|
||||
ports = local.lb_ports
|
||||
externalTrafficPolicy = var.lb_policy
|
||||
ipFamilyPolicy = var.ip_family
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -63,3 +67,18 @@ resource "kubectl_manifest" "service" {
|
||||
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
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ variable "labels" {
|
||||
type = map(string)
|
||||
}
|
||||
variable "annotations" {
|
||||
type = map(string)
|
||||
type = map(string)
|
||||
default = {}
|
||||
}
|
||||
variable "svc_type" {
|
||||
@@ -22,6 +22,16 @@ variable "svc_type" {
|
||||
error_message = "Only ClusterIP, ExternalName, NodePort or LoadBalancer is allowed"
|
||||
}
|
||||
}
|
||||
|
||||
variable "ip_family" {
|
||||
type = string
|
||||
default = "PreferDualStack"
|
||||
validation {
|
||||
condition = contains(["SingleStack", "PreferDualStack"], var.ip_family)
|
||||
error_message = "Only SingleStack or PreferDualStack is allowed"
|
||||
}
|
||||
}
|
||||
|
||||
variable "ports" {
|
||||
type = list(number)
|
||||
default = [80]
|
||||
@@ -51,8 +61,13 @@ variable "node_ports" {
|
||||
}
|
||||
}
|
||||
variable "lb_ports" {
|
||||
type = list(number)
|
||||
default = [8080]
|
||||
type = list(object({
|
||||
name = string
|
||||
port = object({
|
||||
number = number
|
||||
})
|
||||
}))
|
||||
default = []
|
||||
}
|
||||
variable "lb_policy" {
|
||||
type = string
|
||||
|
||||
Reference in New Issue
Block a user