2025-04-27 14:03:56 +02:00
|
|
|
resource "kubernetes_namespace" "website" {
|
|
|
|
|
metadata {
|
|
|
|
|
annotations = {
|
|
|
|
|
name = "website"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
labels = {
|
|
|
|
|
name = "website"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
name = "website"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
resource "kubernetes_service_account" "website_service_account" {
|
|
|
|
|
|
|
|
|
|
metadata {
|
|
|
|
|
name = "website-app-sa"
|
|
|
|
|
namespace = kubernetes_namespace.website.metadata[0].name
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
automount_service_account_token = false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
resource "kubernetes_service" "website" {
|
|
|
|
|
for_each = local.websites
|
|
|
|
|
metadata {
|
|
|
|
|
name = format("website-%s", replace(each.key, ".", "-"))
|
|
|
|
|
namespace = kubernetes_namespace.website.metadata[0].name
|
|
|
|
|
}
|
|
|
|
|
spec {
|
|
|
|
|
port {
|
|
|
|
|
port = 80
|
|
|
|
|
target_port = "http"
|
|
|
|
|
}
|
|
|
|
|
selector = {
|
|
|
|
|
app = format("website-%s", replace(each.key, ".", "-"))
|
|
|
|
|
}
|
|
|
|
|
cluster_ip = "None"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
resource "kubernetes_config_map" "nginx_config" {
|
|
|
|
|
metadata {
|
|
|
|
|
name = "nginx-config"
|
|
|
|
|
namespace = kubernetes_namespace.website.metadata[0].name
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
data = {
|
2025-05-05 00:47:36 +02:00
|
|
|
"nginx.conf" = local.nginx_config
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
resource "kubernetes_config_map" "nginx_default_config" {
|
|
|
|
|
for_each = local.websites
|
|
|
|
|
metadata {
|
|
|
|
|
name = format("website-%s-default", replace(each.key, ".", "-"))
|
|
|
|
|
namespace = kubernetes_namespace.website.metadata[0].name
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
data = {
|
|
|
|
|
"default.conf" = each.value.config
|
2025-04-27 14:03:56 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
resource "kubernetes_ingress_v1" "website_ingress" {
|
|
|
|
|
for_each = local.websites
|
|
|
|
|
metadata {
|
|
|
|
|
name = "website-ingress"
|
|
|
|
|
namespace = kubernetes_namespace.website.metadata[0].name
|
|
|
|
|
|
|
|
|
|
annotations = {
|
|
|
|
|
"kubernetes.io/ingress.class" = "public"
|
|
|
|
|
"cert-manager.io/cluster-issuer" = "letsencrypt-prod"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
spec {
|
|
|
|
|
tls {
|
|
|
|
|
hosts = each.value.fqdn
|
|
|
|
|
secret_name = format("website-%s-tls", replace(each.key, ".", "-"))
|
|
|
|
|
}
|
|
|
|
|
dynamic "rule" {
|
|
|
|
|
for_each = each.value.fqdn
|
|
|
|
|
content {
|
|
|
|
|
host = rule.value
|
|
|
|
|
http {
|
|
|
|
|
path {
|
|
|
|
|
backend {
|
|
|
|
|
service {
|
|
|
|
|
name = format("website-%s", replace(each.key, ".", "-"))
|
|
|
|
|
port {
|
|
|
|
|
number = 80
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
resource "kubernetes_deployment" "website" {
|
|
|
|
|
for_each = local.websites
|
|
|
|
|
|
|
|
|
|
metadata {
|
|
|
|
|
name = "website-app"
|
|
|
|
|
namespace = kubernetes_namespace.website.metadata[0].name
|
|
|
|
|
}
|
|
|
|
|
spec {
|
|
|
|
|
selector {
|
|
|
|
|
match_labels = {
|
|
|
|
|
app = format("website-%s", replace(each.key, ".", "-"))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
strategy {
|
|
|
|
|
type = "RollingUpdate"
|
|
|
|
|
}
|
|
|
|
|
template {
|
|
|
|
|
metadata {
|
|
|
|
|
labels = {
|
|
|
|
|
app = format("website-%s", replace(each.key, ".", "-"))
|
|
|
|
|
}
|
2025-05-05 00:47:36 +02:00
|
|
|
annotations = {
|
|
|
|
|
"checksum/nginx" = local.nginx_config_checksum
|
|
|
|
|
"checksum/default" = sha256(each.value.config)
|
|
|
|
|
}
|
2025-04-27 14:03:56 +02:00
|
|
|
}
|
|
|
|
|
spec {
|
|
|
|
|
service_account_name = kubernetes_service_account.website_service_account.metadata[0].name
|
|
|
|
|
|
|
|
|
|
security_context {
|
|
|
|
|
fs_group = 1000
|
|
|
|
|
run_as_user = 1000
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
container {
|
|
|
|
|
name = "website"
|
2025-04-27 19:40:16 +02:00
|
|
|
image = "nginx:${var.tag}"
|
2025-04-27 14:03:56 +02:00
|
|
|
image_pull_policy = "Always"
|
|
|
|
|
|
|
|
|
|
resources {
|
|
|
|
|
requests = {
|
|
|
|
|
memory = "100Mi"
|
|
|
|
|
}
|
|
|
|
|
limits = {
|
|
|
|
|
memory = "500Mi"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
readiness_probe {
|
|
|
|
|
http_get {
|
|
|
|
|
path = "/"
|
|
|
|
|
port = 8080
|
|
|
|
|
}
|
|
|
|
|
initial_delay_seconds = 5
|
|
|
|
|
period_seconds = 10
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
liveness_probe {
|
|
|
|
|
http_get {
|
|
|
|
|
path = "/"
|
|
|
|
|
port = 8080
|
|
|
|
|
}
|
|
|
|
|
initial_delay_seconds = 5
|
|
|
|
|
period_seconds = 10
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
security_context {
|
|
|
|
|
allow_privilege_escalation = false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
port {
|
|
|
|
|
container_port = 8080
|
|
|
|
|
name = "http"
|
|
|
|
|
}
|
|
|
|
|
volume_mount {
|
2025-05-05 00:47:36 +02:00
|
|
|
name = "nginx"
|
2025-04-27 14:03:56 +02:00
|
|
|
mount_path = "/etc/nginx/nginx.conf"
|
|
|
|
|
sub_path = "nginx.conf"
|
|
|
|
|
read_only = true
|
|
|
|
|
}
|
|
|
|
|
volume_mount {
|
2025-05-05 00:47:36 +02:00
|
|
|
name = "default"
|
2025-04-27 14:03:56 +02:00
|
|
|
mount_path = "/etc/nginx/conf.d/default.conf"
|
|
|
|
|
sub_path = "default.conf"
|
|
|
|
|
read_only = true
|
|
|
|
|
}
|
|
|
|
|
volume_mount {
|
|
|
|
|
name = "website"
|
|
|
|
|
mount_path = "/tmp/site"
|
|
|
|
|
read_only = true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
volume {
|
2025-05-05 00:47:36 +02:00
|
|
|
name = "nginx"
|
2025-04-27 14:03:56 +02:00
|
|
|
config_map {
|
|
|
|
|
name = kubernetes_config_map.nginx_config.metadata[0].name
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-05-05 00:47:36 +02:00
|
|
|
volume {
|
|
|
|
|
name = "default"
|
|
|
|
|
config_map {
|
|
|
|
|
name = kubernetes_config_map.nginx_default_config[each.key].metadata[0].name
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-04-27 14:03:56 +02:00
|
|
|
volume {
|
|
|
|
|
name = "website"
|
|
|
|
|
host_path {
|
|
|
|
|
path = format("%s/%s", local.websites_path, each.key)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-05-05 00:47:36 +02:00
|
|
|
lifecycle {
|
|
|
|
|
ignore_changes = [spec[0].template[0].metadata[0].annotations["kubectl.kubernetes.io/restartedAt"]]
|
|
|
|
|
}
|
2025-04-27 14:03:56 +02:00
|
|
|
}
|
|
|
|
|
|