From 761c183ce20ab338931ecc035a3bca99d4a37255 Mon Sep 17 00:00:00 2001 From: Alexandre Pires Date: Sun, 27 Apr 2025 19:09:32 +0200 Subject: [PATCH] feat: Add Kubernetes website module with configuration, resources, and NGINX setup --- terraform/k8sapps/websites.tf | 36 +++++++++++++++++++ terraform/modules/websites/config.tf | 6 ++++ terraform/modules/websites/provider.tf | 13 +++++++ .../resources/nginx/conf.d/default.conf | 35 ++++++++++++++++++ .../websites/resources/nginx/nginx.conf | 34 ++++++++++++++++++ terraform/modules/websites/variables.tf | 17 +++++++++ .../{k8sapps => modules/websites}/website.tf | 0 7 files changed, 141 insertions(+) create mode 100644 terraform/k8sapps/websites.tf create mode 100644 terraform/modules/websites/config.tf create mode 100644 terraform/modules/websites/provider.tf create mode 100644 terraform/modules/websites/resources/nginx/conf.d/default.conf create mode 100644 terraform/modules/websites/resources/nginx/nginx.conf create mode 100644 terraform/modules/websites/variables.tf rename terraform/{k8sapps => modules/websites}/website.tf (100%) diff --git a/terraform/k8sapps/websites.tf b/terraform/k8sapps/websites.tf new file mode 100644 index 0000000..c09c59c --- /dev/null +++ b/terraform/k8sapps/websites.tf @@ -0,0 +1,36 @@ +module "websites" { + source = "../modules/websites" + persistent_folder = local.persistent_folder + primary_domain = local.primary_domain + websites = local.websites +} + +moved { + from = kubernetes_namespace.website + to = module.websites.kubernetes_namespace.website +} + +moved { + from = kubernetes_service_account.website_service_account + to = module.websites.kubernetes_service_account.website_service_account +} + +moved { + from = kubernetes_service.website + to = module.websites.kubernetes_service.website +} + +moved { + from = kubernetes_config_map.nginx_config + to = module.websites.kubernetes_config_map.nginx_config +} + +moved { + from = kubernetes_ingress_v1.website_ingress + to = module.websites.kubernetes_ingress_v1.website_ingress +} + +moved { + from = kubernetes_deployment.website + to = module.websites.kubernetes_deployment.website +} diff --git a/terraform/modules/websites/config.tf b/terraform/modules/websites/config.tf new file mode 100644 index 0000000..ffce05d --- /dev/null +++ b/terraform/modules/websites/config.tf @@ -0,0 +1,6 @@ +locals { + persistent_folder = var.persistent_folder + primary_domain = var.primary_domain + websites = var.websites +} + diff --git a/terraform/modules/websites/provider.tf b/terraform/modules/websites/provider.tf new file mode 100644 index 0000000..8b9fe2f --- /dev/null +++ b/terraform/modules/websites/provider.tf @@ -0,0 +1,13 @@ +terraform { + required_version = "~>1.8" + required_providers { + kubernetes = { + source = "hashicorp/kubernetes" + version = "~>2.18" + } + scaleway = { + source = "scaleway/scaleway" + version = "~>2.13" + } + } +} diff --git a/terraform/modules/websites/resources/nginx/conf.d/default.conf b/terraform/modules/websites/resources/nginx/conf.d/default.conf new file mode 100644 index 0000000..d352c7b --- /dev/null +++ b/terraform/modules/websites/resources/nginx/conf.d/default.conf @@ -0,0 +1,35 @@ +server { + listen 8080; + server_name localhost; + + gzip on; + gzip_comp_level 9; + gzip_types + text/html + text/plain + text/css + text/js + text/xml + text/javascript + application/javascript + application/json + application/xml + application/rss+xml + image/svg+xml; + + root /tmp/site; + + location / { + index index.html index.htm; + } + + location ~* ^/([^/]+) { + index index.html index.htm; + error_page 404 = @error; + } + + error_page 404 /404.html; + location @error { + try_files /$1/404.html /404.html =404; + } +} diff --git a/terraform/modules/websites/resources/nginx/nginx.conf b/terraform/modules/websites/resources/nginx/nginx.conf new file mode 100644 index 0000000..12c02fa --- /dev/null +++ b/terraform/modules/websites/resources/nginx/nginx.conf @@ -0,0 +1,34 @@ +worker_processes auto; + +error_log /var/log/nginx/error.log notice; +pid /tmp/nginx.pid; + + +events { + worker_connections 1024; +} + + +http { + proxy_temp_path /tmp/proxy_temp; + client_body_temp_path /tmp/client_temp; + fastcgi_temp_path /tmp/fastcgi_temp; + uwsgi_temp_path /tmp/uwsgi_temp; + scgi_temp_path /tmp/scgi_temp; + + include /etc/nginx/mime.types; + default_type application/octet-stream; + + log_format main '$remote_addr - $remote_user [$time_local] "$request" ' + '$status $body_bytes_sent "$http_referer" ' + '"$http_user_agent" "$http_x_forwarded_for"'; + + access_log /var/log/nginx/access.log main; + + sendfile on; + + keepalive_timeout 65; + + + include /etc/nginx/conf.d/*.conf; +} \ No newline at end of file diff --git a/terraform/modules/websites/variables.tf b/terraform/modules/websites/variables.tf new file mode 100644 index 0000000..55afc4b --- /dev/null +++ b/terraform/modules/websites/variables.tf @@ -0,0 +1,17 @@ +variable "persistent_folder" { + description = "The path to the persistent folder" + type = string +} + +variable "primary_domain" { + description = "The primary domain for the websites" + type = string +} + +variable "websites" { + description = "A list of websites to be deployed" + type = map(object({ + domain_name = string + fqdn = list(string) + })) +} diff --git a/terraform/k8sapps/website.tf b/terraform/modules/websites/website.tf similarity index 100% rename from terraform/k8sapps/website.tf rename to terraform/modules/websites/website.tf