Refacto and add modules

This commit is contained in:
2024-02-06 11:03:20 +01:00
parent 159b576b24
commit bcdf666cc0
47 changed files with 661 additions and 338 deletions

View File

@@ -1,24 +1,25 @@
locals {
app_slug = "${var.instance}${var.component==""?"":"-"}${var.component}"
app_slug = "${var.instance}${var.component == "" ? "" : "-"}${var.component}"
pres_labels = merge(var.labels, {
"app.kubernetes.io/component" = "presentation"
})
rules = [for v in var.dns_names : {
"host" = "${v}"
"http" = {
"paths" = [for idx, svc in var.services : {
"path" = "/${var.sub_paths[idx]}"
"paths" = [for mapper in var.services_mapping : {
"path" = "/${mapper.path}"
"pathType" = "Prefix"
"backend" = {
"service" = svc
"service" = mapper.service
}
}]
}
}]
tls = var.enforce_tls ? [
secret_name = var.cert_name != "" ? var.cert_name : "${local.app_slug}-cert"
tls = var.entrypoint == "" || length(regexall(".*websecure.*", var.entrypoint)) > 0 ? [
{
secretName = var.cert_name != "" ? var.cert_name : "${local.app_slug}-cert"
secretName = local.secret_name
hosts = var.dns_names
}
] : []
@@ -36,8 +37,8 @@ locals {
)
}
resource "kubectl_manifest" "prj_certificate" {
count = var.create_cert ? 1 : 0
resource "kubectl_manifest" "certificate" {
count = var.cert_name == "" ? 1 : 0
yaml_body = <<-EOF
apiVersion: "cert-manager.io/v1"
kind: "Certificate"
@@ -46,7 +47,7 @@ resource "kubectl_manifest" "prj_certificate" {
namespace: "${var.namespace}"
labels: ${jsonencode(local.pres_labels)}
spec:
secretName: "${local.app_slug}-cert"
secretName: "${local.secret_name}"
dnsNames: ${jsonencode(var.dns_names)}
issuerRef:
kind: "ClusterIssuer"
@@ -55,8 +56,8 @@ resource "kubectl_manifest" "prj_certificate" {
EOF
}
resource "kubectl_manifest" "prj_https_redirect" {
count = var.create_redirect || var.component == "" ? 1 : 0
resource "kubectl_manifest" "https_redirect" {
count = var.create_redirect ? 1 : 0
yaml_body = <<-EOF
apiVersion: "traefik.io/v1alpha1"
kind: "Middleware"
@@ -71,7 +72,7 @@ resource "kubectl_manifest" "prj_https_redirect" {
EOF
}
resource "kubectl_manifest" "prj_ingress" {
resource "kubectl_manifest" "ingress" {
force_conflicts = true
yaml_body = <<-EOF
apiVersion: "networking.k8s.io/v1"

View File

@@ -1,3 +1,3 @@
output "secret_name" {
value = var.create_cert ? "${local.app_slug}-cert":""
value = var.cert_name == "" ? "${local.app_slug}-cert" : var.cert_name
}

View File

@@ -2,8 +2,8 @@
terraform {
required_providers {
kubectl = {
source = "gavinbunney/kubectl"
version = "~> 1.14.0"
source = "gavinbunney/kubectl"
version = "~> 1.14.0"
}
}
}

View File

@@ -24,35 +24,37 @@ variable "middlewares" {
type = list(string)
default = []
}
variable "services" {
type = list(any)
variable "services_mapping" {
type = list(object({
path = optional(string)
service = object({
name= string
port = object({
number = number
})
})
}))
}
variable "create_redirect" {
type = bool
default = true
}
variable "create_cert" {
type = bool
default = true
}
variable "enforce_tls" {
type = bool
default = true
}
variable "sub_paths" {
type = list(string)
default = [""]
}
variable "cert_name" {
type = string
default = ""
}
variable "entrypoint" {
type = string
default = ""
type = string
default = ""
description = "Define ingres support, if empty or define to websecure, tls will be activate"
validation {
condition = contains(["", "web", "websecure"], var.entrypoint)
error_message = "Only empty \"\", web or websecure is allowed"
}
}
variable "cert_name" {
type = string
default = ""
description = "Give a secret name for tls, if empty and entrypointis websecure or empty, one will be created"
}
variable "create_redirect" {
type = bool
default = true
description = "Enfore https redirection"
}