diff --git a/terraform/.gitignore b/terraform/.gitignore index 6bc0c55..d7822b3 100644 --- a/terraform/.gitignore +++ b/terraform/.gitignore @@ -5,6 +5,5 @@ *.tfvars !terraform.tfvars.example .terraform/ -.terraform.lock.hcl kubeconfig crash.log diff --git a/terraform/.terraform.lock.hcl b/terraform/.terraform.lock.hcl new file mode 100644 index 0000000..910e934 --- /dev/null +++ b/terraform/.terraform.lock.hcl @@ -0,0 +1,40 @@ +# This file is maintained automatically by "terraform init". +# Manual edits may be lost in future updates. + +provider "registry.terraform.io/cloudflare/cloudflare" { + version = "5.28.1" + constraints = "5.28.1" + hashes = [ + "h1:9YL6g+iEfg3OzX6qSZOYsictVO0fNP09D6jRwIZKwQ0=", + "zh:219d464dd727c0e5c90a97b44aa5d7988acc4eec47f3684f7ecd006b17c46d9c", + "zh:2511f2380209c81121e6eb943d7e68d03602a4fcf4f0311dfc67e198179b9303", + "zh:2ae200482bf592cd703981c4d4fe87a52c0dfae81a70261295f59b1bb0d01a6c", + "zh:7df6fe6573379c2faf2e801fdbca58ac909266dbee604feaa993439b8616c9b3", + "zh:9fb5a295588351ce565515dc2a770047e8bab786a4a9665511ab9a9ca2db5f1b", + "zh:a182f4aec86c74e9db4affa22323c5a21f5d92c1fcf91b934570753d04f4daf8", + "zh:a39bb8624fb6c714e38bc0e632aae117a9fede5a18a248ab15dd1b60013c0324", + "zh:bef2ffbdb68569549fe432b550592e07456759a7288f84f7c37c9d933a493f54", + "zh:f809ab383cca0a5f83072981c64208cbd7fa67e986a86ee02dd2c82333221e32", + ] +} + +provider "registry.terraform.io/hetznercloud/hcloud" { + version = "1.70.2" + constraints = "1.70.2" + hashes = [ + "h1:BZWdJOileBlR+WMz0D0wrJXFhNLJpHcQJIgDqmPNlpw=", + "zh:05a474ecd2b019be03f857877c9bf1eca8f96c0897f6e76b91780adf3a8dd388", + "zh:1d4e4c0525c02618c015c079dc3e8d8b693ca796d698f95b59c4412114cbf166", + "zh:1f3bc703423f08a295e3e8f6d39c3f155e672da5e20b9a143ef7e7a465be37e3", + "zh:20ced3eec332b587a998ad3d87da322736aef49cb7e561a545a57f9a08000050", + "zh:619152b472b9ac266046db9d0c3f85946559ce0791bcb6fc78d278bb555d4572", + "zh:658a84427cc6b5385d68e1506678498dd7d3d04c0cf3410e3f2278c05b247ade", + "zh:6d9be3d79479345d21cef66e83002086153189535c82ab299d6ca5021aec7d0e", + "zh:72b0d26aadcc422ccb17e7a89abf92dfbe52dca6fc80025a934d7c4e058888cd", + "zh:865c0dc4912a5009f7b8c7ff374d47e496f9cfe96cbcebadc48f3434b7c12b46", + "zh:99f3f0778398d7348e5ab7dab42de0c918339a5250d140b6959272e50be4e372", + "zh:b3df6415bbe0f7021f94bff9e640c42e6d45b4a459ded3d267f10e2104a77760", + "zh:e7645b34e1aa45cb17dc473b33d7fa4d60e81d7e14332c7b04bc97535c867074", + "zh:f60b3e67be5cafe10001bb556ae7693e4a7696fb6946294643182946b36117f9", + ] +} diff --git a/terraform/README.md b/terraform/README.md index 8453055..bb23a27 100644 --- a/terraform/README.md +++ b/terraform/README.md @@ -1,6 +1,6 @@ # Terraform -Provisions everything outside the cluster on `server0`. Rebuild is +Provisions everything outside the cluster on `cv-control-plane`. Rebuild is `terraform apply` plus `kubectl apply -k deploy/`. Layout follows `OKF_CV_Website/architecture/terraform.md`: diff --git a/terraform/dns.tf b/terraform/dns.tf index bd2df4c..9e9f23a 100644 --- a/terraform/dns.tf +++ b/terraform/dns.tf @@ -1,3 +1,18 @@ # DNS layer: A record for the apex domain pointing at the server's public IPv4. # The zone is at Cloudflare; Caddy does its own ACME challenge, so no # additional records are needed. +# +# proxied = false on purpose: an orange-cloud record terminates TLS at +# Cloudflare and Caddy can never complete its ACME challenge. + +provider "cloudflare" {} + +resource "cloudflare_dns_record" "apex" { + zone_id = var.cloudflare_zone_id + name = var.domain + type = "A" + content = hcloud_server.control_plane.ipv4_address + ttl = 1 + proxied = false + comment = "Remote Server for CV Website" +} diff --git a/terraform/main.tf b/terraform/main.tf index 4497790..e9bab6e 100644 --- a/terraform/main.tf +++ b/terraform/main.tf @@ -1,6 +1,20 @@ -# Server layer: packages, swap, containerd, kubeadm control plane, Calico, -# metrics-server, local-path provisioner, and Caddy. -# -# Terraform does not manage anything inside the cluster beyond the bootstrap -# (namespace cv, quota, addons). Workloads belong to kustomize. -# See OKF_CV_Website/architecture/terraform.md. +provider "hcloud" {} + +resource "hcloud_ssh_key" "cv_infra" { + name = "cv-infra" + public_key = file(pathexpand(var.ssh_public_key_path)) +} + +resource "hcloud_server" "control_plane" { + name = "cv-control-plane" + server_type = var.server_type + image = var.server_image + location = var.server_location + ssh_keys = [hcloud_ssh_key.cv_infra.id] + labels = { role = "cv-control-plane" } + + public_net { + ipv4_enabled = true + ipv6_enabled = false + } +} diff --git a/terraform/outputs.tf b/terraform/outputs.tf index dc6b7d4..04058ba 100644 --- a/terraform/outputs.tf +++ b/terraform/outputs.tf @@ -1,8 +1,13 @@ # Values other tooling and the operator need after apply. output "server_ip" { - description = "Public IPv4 of server0." - value = var.server_host + description = "Public IPv4 of the control plane." + value = hcloud_server.control_plane.ipv4_address +} + +output "ssh_command" { + description = "Ready-to-paste SSH command for the control plane." + value = "ssh -i ${var.ssh_private_key_path} ${var.ssh_user}@${hcloud_server.control_plane.ipv4_address}" } output "proxy_url" { @@ -10,5 +15,3 @@ output "proxy_url" { value = "https://${var.domain}" } -# Kubeconfig stays on this machine and never enters Git. Add its path here once -# main.tf writes it, e.g. "${path.module}/kubeconfig". diff --git a/terraform/plan.md b/terraform/plan.md new file mode 100644 index 0000000..1aa4d86 --- /dev/null +++ b/terraform/plan.md @@ -0,0 +1,139 @@ +# Build plan + +Working order for `terraform/`. Each step ends in something verifiable; do not +start the next one until the check passes. + +Goal: `terraform apply` + `kubectl apply -k deploy/` rebuilds the whole system +on an empty machine. See `OKF_CV_Website/architecture/terraform.md`. + +## Open decisions + +- [X] **Where does the server come from?** Proxmox VM on the home server, or a + cloud VM. Sets the provider, the key mechanism, and whether `server_host` + is known before `apply` or produced by it. -> Server comes from Hetzner +- [X] **Does Terraform create the VM, or only bootstrap it?** Creating it makes + `server_host` an output instead of an input. -> creates a server at hetzner not a vm (yes i know they will use a virtualizer but you understand) +- [X] **Provisioning mechanism:** `remote-exec` over SSH, or provider + `user_data`/cloud-init. cloud-init is idempotent at boot and needs no SSH; + provisioners are what `variables.tf` is currently shaped for. Pick one + before writing `main.tf`. -> depends i don't know the tradeoffs + +## Steps + +### 1. Server source — done + +`main.tf` creates `cv-control-plane` as a Hetzner `hcloud_server` (`cpx32`, +`debian-13`) +and registers `~/.ssh/cv_infra.pub` as an `hcloud_ssh_key`, so the key is in +`authorized_keys` before the machine boots. `terraform.md` and `README.md` still +describe `server_host` as an input; they need updating. + +Check: `terraform output server_ip` returns the address. + +### 2. SSH key — done + +`~/.ssh/cv_infra` and `~/.ssh/cv_infra.pub` exist. The public half goes onto the +server during creation (step 6), not after. + +Check: `ssh-keygen -y -f ~/.ssh/cv_infra` prints the public key. + +### 3. Provider and versions — done + +`versions.tf` pins `cloudflare` 5.28.1 and `hcloud` 1.70.2; init and validate +pass. `required_version` and a `null` provider still need adding before step 7. + +Check: `terraform init` succeeds, `.terraform.lock.hcl` exists. + +### 4. Inputs + +Copy `terraform.tfvars.example` to `terraform.tfvars`, fill `server_host`, +`domain`, `cloudflare_zone_id`, and set +`ssh_private_key_path = "~/.ssh/cv_infra"`. Export `CLOUDFLARE_API_TOKEN`. + +Check: `terraform validate` passes. + +### 5. DNS — written + +`dns.tf` creates `cloudflare_dns_record.apex` from +`hcloud_server.control_plane.ipv4_address`, so the record can only be created after +the server — one `apply` does both, in order. `proxied = false` is required for +Caddy's ACME challenge. + +Check: `terraform apply` creates the record, `dig +short ` returns the +server IP. + +### 6. Server creation — written + +`main.tf` creates the server with the SSH public key attached at creation. + +Check: `terraform apply` creates it, `terraform output ssh_command` works +without a password. + +### 7. Connection skeleton + +One `null_resource` with a `connection` block that runs `uname -a`. Sort out +host key handling here (`host_key` pinned vs. `insecure_ignore_host_key`) and +the wait-for-port-22 problem. + +Check: `terraform apply` returns the remote `uname -a` output. + +### 8. Base system + +Packages, 2 GB swapfile, containerd, then kubeadm/kubelet/kubectl. Every command +must be safe to re-run — provisioners have no state tracking. + +Check: `swapfile` active, `containerd` and `kubelet` enabled, `kubeadm version` +matches the design. + +### 9. Control plane + +`kubeadm init` with `--pod-network-cidr` for Calico, then install the kubeconfig +on the local machine (never in Git). + +Check: `kubectl get nodes` shows the control plane `Ready` once Calico is in. + +### 10. Addons + +Calico, metrics-server, local-path provisioner — bootstrap only, never +reconciled after this. + +Check: `kubectl top nodes` returns data, a test PVC binds. + +### 11. Caddy + +Install Caddy, template the Caddyfile with `reverse_proxy 127.0.0.1:8080`. DNS +record must be DNS-only at Cloudflare (grey cloud) or ACME fails. + +Check: `curl -I https://` returns a valid certificate; 502 is fine at +this point because nothing is listening yet. + +### 12. Outputs + +Add the kubeconfig path next to the placeholder in `outputs.tf:13-14`. + +Check: `terraform output` shows `server_ip`, `proxy_url`, kubeconfig path. + +### 13. Workloads in `deploy/` + +Namespace `cv`, quota, gateway Service on NodePort 8080, then the site +workloads. Applied with `kubectl apply -k deploy/`, not Terraform. + +Check: site loads over HTTPS. + +### 14. Acceptance test + +```sh +terraform destroy && terraform apply +kubectl apply -k deploy/ +``` + +Check: site back up from nothing, no manual steps in between. This is the only +test that matters; everything above is scaffolding for it. + +## Notes + +- State is local and Git-ignored on purpose; a lost state file means rebuild. +- Terraform manages nothing inside the cluster beyond bootstrap. +- A partial `remote-exec` failure leaves a half-configured server and a state + file that thinks it succeeded. When step 8 misbehaves, `terraform taint` is + the escape hatch. diff --git a/terraform/terraform.tfvars.example b/terraform/terraform.tfvars.example index 002fcf1..1205372 100644 --- a/terraform/terraform.tfvars.example +++ b/terraform/terraform.tfvars.example @@ -1,6 +1,9 @@ # Copy to terraform.tfvars and fill in. terraform.tfvars is Git-ignored. -server_host = "" ssh_user = "root" -ssh_private_key_path = "~/.ssh/id_ed25519" -domain = "example.dev" -cloudflare_zone_id = "" +ssh_private_key_path = "~/.ssh/cv_infra" +ssh_public_key_path = "~/.ssh/cv_infra.pub" +server_type = "cpx32" +server_image = "debian-13" +server_location = "nbg1" +domain = "noelsabia.dev" +cloudflare_zone_id = "e8dcd36819c27ef0b77fe4babbe57e21" diff --git a/terraform/variables.tf b/terraform/variables.tf index a8c8652..dd89eb7 100644 --- a/terraform/variables.tf +++ b/terraform/variables.tf @@ -1,10 +1,8 @@ # Inputs: the server, its SSH access, and the domain. # Nothing secret belongs here; secrets come from the environment or tfvars. - -variable "server_host" { - description = "Public IP or hostname of server0." - type = string -} +# +# server_host is gone: Terraform now creates the server, so its IP is an output +# rather than an input. variable "ssh_user" { description = "SSH user Terraform logs in as." @@ -17,6 +15,30 @@ variable "ssh_private_key_path" { type = string } +variable "ssh_public_key_path" { + description = "Path to the matching public key, installed at server creation." + type = string + default = "~/.ssh/cv_infra.pub" +} + +variable "server_type" { + description = "Hetzner server type, e.g. cpx32 (4 vCPU, 8 GB)." + type = string + default = "cpx32" +} + +variable "server_image" { + description = "Hetzner image for the base system. Hetzner names Debian images by major version only." + type = string + default = "debian-13" +} + +variable "server_location" { + description = "Hetzner location, e.g. nbg1, fsn1, hel1." + type = string + default = "nbg1" +} + variable "domain" { description = "Apex domain served by the site, e.g. noelsabia.dev." type = string diff --git a/terraform/versions.tf b/terraform/versions.tf index 25f6c56..3ed732b 100644 --- a/terraform/versions.tf +++ b/terraform/versions.tf @@ -1,17 +1,15 @@ -# Provider requirements. +terraform { + required_version = ">= 1.9" + + required_providers { + cloudflare = { + source = "cloudflare/cloudflare" + version = "5.28.1" + } + hcloud = { + source = "hetznercloud/hcloud" + version = "1.70.2" + } + } +} -# Set once the providers are chosen and versions verified with `terraform init`. -# DNS lives at Cloudflare, so that provider is expected here. -# terraform { -# required_version = ">= 1.9" -# required_providers { -# cloudflare = { -# source = "cloudflare/cloudflare" -# version = "~> x.y" -# } -# null = { -# source = "hashicorp/null" -# version = "~> 3.2" -# } -# } -# }