Improve modules
This commit is contained in:
@@ -1,17 +1,36 @@
|
|||||||
|
|
||||||
locals {
|
locals {
|
||||||
|
name = "${var.instance}${var.component == "" ? "" : "-"}${var.component}"
|
||||||
rules = [for v in var.dns_names : {
|
rules = [for v in var.dns_names : {
|
||||||
"host" = "${v}"
|
"host" = "${v}"
|
||||||
"http" = {
|
"http" = {
|
||||||
"paths" = [for idx, svc in var.services : {
|
"paths" = [for idx, svc in var.services : {
|
||||||
|
"path" = "/${var.sub_paths[idx]}"
|
||||||
|
"pathType" = "Prefix"
|
||||||
"backend" = {
|
"backend" = {
|
||||||
"service" = svc
|
"service" = svc
|
||||||
}
|
}
|
||||||
"path" = "/${var.sub_paths[idx]}"
|
|
||||||
"pathType" = "Prefix"
|
|
||||||
}]
|
}]
|
||||||
}
|
}
|
||||||
}]
|
}]
|
||||||
|
tls = var.enforce_tls ? [
|
||||||
|
{
|
||||||
|
secretName = var.cert_name != "" ? var.cert_name : "${local.name}-cert"
|
||||||
|
hosts = var.dns_names
|
||||||
|
}
|
||||||
|
] : []
|
||||||
|
middlewares = concat(
|
||||||
|
var.create_redirect ? ["${local.name}-https"] : [],
|
||||||
|
var.middlewares
|
||||||
|
)
|
||||||
|
annotations = merge(
|
||||||
|
var.entrypoint != "" ? {
|
||||||
|
"traefik.ingress.kubernetes.io/router.entrypoints" = var.entrypoint
|
||||||
|
} : {},
|
||||||
|
length(local.middlewares) > 0 ? {
|
||||||
|
"traefik.ingress.kubernetes.io/router.middlewares" : "${join(",", [for m in local.middlewares : format("%s-%s@kubernetescrd", var.namespace, m)])}"
|
||||||
|
} : {},
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
resource "kubectl_manifest" "prj_certificate" {
|
resource "kubectl_manifest" "prj_certificate" {
|
||||||
@@ -20,15 +39,15 @@ resource "kubectl_manifest" "prj_certificate" {
|
|||||||
apiVersion: "cert-manager.io/v1"
|
apiVersion: "cert-manager.io/v1"
|
||||||
kind: "Certificate"
|
kind: "Certificate"
|
||||||
metadata:
|
metadata:
|
||||||
name: "${var.instance}${var.component == "" ? "" : "-"}${var.component}"
|
name: "${local.name}"
|
||||||
namespace: "${var.namespace}"
|
namespace: "${var.namespace}"
|
||||||
labels: ${jsonencode(var.labels)}
|
labels: ${jsonencode(var.labels)}
|
||||||
spec:
|
spec:
|
||||||
secretName: "${var.instance}${var.component == "" ? "" : "-"}${var.component}-cert"
|
secretName: "${local.name}-cert"
|
||||||
dnsNames: ${jsonencode(var.dns_names)}
|
dnsNames: ${jsonencode(var.dns_names)}
|
||||||
issuerRef:
|
issuerRef:
|
||||||
name: "${var.issuer}"
|
|
||||||
kind: "ClusterIssuer"
|
kind: "ClusterIssuer"
|
||||||
|
name: "${var.issuer}"
|
||||||
group: "cert-manager.io"
|
group: "cert-manager.io"
|
||||||
EOF
|
EOF
|
||||||
}
|
}
|
||||||
@@ -39,7 +58,7 @@ resource "kubectl_manifest" "prj_https_redirect" {
|
|||||||
apiVersion: "traefik.containo.us/v1alpha1"
|
apiVersion: "traefik.containo.us/v1alpha1"
|
||||||
kind: "Middleware"
|
kind: "Middleware"
|
||||||
metadata:
|
metadata:
|
||||||
name: "${var.instance}-https"
|
name: "${local.name}-https"
|
||||||
namespace: "${var.namespace}"
|
namespace: "${var.namespace}"
|
||||||
labels: ${jsonencode(var.labels)}
|
labels: ${jsonencode(var.labels)}
|
||||||
spec:
|
spec:
|
||||||
@@ -55,16 +74,13 @@ resource "kubectl_manifest" "prj_ingress" {
|
|||||||
apiVersion: "networking.k8s.io/v1"
|
apiVersion: "networking.k8s.io/v1"
|
||||||
kind: "Ingress"
|
kind: "Ingress"
|
||||||
metadata:
|
metadata:
|
||||||
name: "${var.instance}${var.component == "" ? "" : "-${var.component}"}"
|
name: "${local.name}"
|
||||||
namespace: "${var.namespace}"
|
namespace: "${var.namespace}"
|
||||||
labels: ${jsonencode(var.labels)}
|
labels: ${jsonencode(var.labels)}
|
||||||
annotations:
|
annotations: ${jsonencode(local.annotations)}
|
||||||
"traefik.ingress.kubernetes.io/router.middlewares": "${join(",", [for m in concat(["${var.instance}-https"], var.middlewares) : format("%s-%s@kubernetescrd", var.namespace, m)])}"
|
|
||||||
spec:
|
spec:
|
||||||
ingressClassName: "${var.ingress_class}"
|
ingressClassName: "${var.ingress_class}"
|
||||||
rules: ${jsonencode(local.rules)}
|
rules: ${jsonencode(local.rules)}
|
||||||
tls:
|
tls: ${jsonencode(local.tls)}
|
||||||
- hosts: ${jsonencode(var.dns_names)}
|
|
||||||
secretName: "${var.instance}${var.secret_component != "" ? "-${var.secret_component}" : var.component == "" ? "" : "-${var.component}"}-cert"
|
|
||||||
EOF
|
EOF
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,17 +30,29 @@ variable "services" {
|
|||||||
|
|
||||||
variable "create_redirect" {
|
variable "create_redirect" {
|
||||||
type = bool
|
type = bool
|
||||||
default = false
|
default = true
|
||||||
}
|
}
|
||||||
variable "create_cert" {
|
variable "create_cert" {
|
||||||
type = bool
|
type = bool
|
||||||
default = true
|
default = true
|
||||||
}
|
}
|
||||||
|
variable "enforce_tls" {
|
||||||
|
type = bool
|
||||||
|
default = true
|
||||||
|
}
|
||||||
variable "sub_paths" {
|
variable "sub_paths" {
|
||||||
type = list(string)
|
type = list(string)
|
||||||
default = [""]
|
default = [""]
|
||||||
}
|
}
|
||||||
variable "secret_component" {
|
variable "cert_name" {
|
||||||
type = string
|
type = string
|
||||||
default = ""
|
default = ""
|
||||||
}
|
}
|
||||||
|
variable "entrypoint" {
|
||||||
|
type = string
|
||||||
|
default = ""
|
||||||
|
validation {
|
||||||
|
condition = contains(["", "web", "websecure"], var.entrypoint)
|
||||||
|
error_message = "Only empty \"\", web or websecure is allowed"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,22 +1,40 @@
|
|||||||
locals {
|
locals {
|
||||||
ports = [ for idx, target in var.targets : {
|
cluster_ports = var.svc_type == "ClusterIP" ? [for idx, target in var.targets : {
|
||||||
"name" = target
|
"name" = target
|
||||||
"port"= var.ports[idx]
|
"port" = var.ports[idx]
|
||||||
"protocol"= var.protocols[idx]
|
"protocol" = var.protocols[idx]
|
||||||
"targetPort" = target
|
"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" {
|
resource "kubectl_manifest" "service" {
|
||||||
yaml_body = <<-EOF
|
yaml_body = <<-EOF
|
||||||
apiVersion: v1
|
apiVersion: v1
|
||||||
kind: Service
|
kind: Service
|
||||||
metadata:
|
metadata:
|
||||||
name: "${var.instance}-${var.component}"
|
name: "${var.instance}-${var.component}"
|
||||||
namespace: "${var.namespace}"
|
namespace: "${var.namespace}"
|
||||||
labels: ${jsonencode(var.labels)}
|
labels: ${jsonencode(var.labels)}
|
||||||
spec:
|
spec: ${jsonencode(local.spec[var.svc_type])}
|
||||||
type: ClusterIP
|
|
||||||
ports: ${jsonencode(local.ports)}
|
|
||||||
selector: ${jsonencode(var.labels)}
|
|
||||||
EOF
|
EOF
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,24 +1,48 @@
|
|||||||
variable "component" {
|
variable "component" {
|
||||||
type = string
|
type = string
|
||||||
}
|
}
|
||||||
variable "instance" {
|
variable "instance" {
|
||||||
type = string
|
type = string
|
||||||
}
|
}
|
||||||
variable "namespace" {
|
variable "namespace" {
|
||||||
type = string
|
type = string
|
||||||
}
|
}
|
||||||
variable "labels" {
|
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" {
|
variable "ports" {
|
||||||
type = list
|
type = list(number)
|
||||||
default = [80]
|
default = [80]
|
||||||
}
|
}
|
||||||
variable "targets" {
|
variable "targets" {
|
||||||
type = list
|
type = list(string)
|
||||||
default = ["http"]
|
default = ["http"]
|
||||||
}
|
}
|
||||||
variable "protocols" {
|
variable "protocols" {
|
||||||
type = list
|
type = list(any)
|
||||||
default = ["TCP"]
|
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