2021-12-11 23:49:53 +07:00
|
|
|
variable "cloudflare_account_id" {
|
|
|
|
type = string
|
|
|
|
}
|
|
|
|
|
2021-12-09 01:12:59 +07:00
|
|
|
provider "cloudflare" {
|
|
|
|
# Environment variables
|
2021-12-09 01:51:42 +07:00
|
|
|
# CLOUDFLARE_API_TOKEN
|
2021-12-08 09:24:09 +07:00
|
|
|
}
|
|
|
|
|
2021-12-09 01:12:59 +07:00
|
|
|
data "cloudflare_zone" "khuedoan_com" {
|
|
|
|
name = "khuedoan.com"
|
2021-12-08 09:24:09 +07:00
|
|
|
}
|
|
|
|
|
2021-12-09 01:12:59 +07:00
|
|
|
resource "random_password" "tunnel_secret" {
|
2021-12-08 09:24:09 +07:00
|
|
|
length = 64
|
|
|
|
special = false
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "cloudflare_argo_tunnel" "homelab" {
|
|
|
|
# TODO (optimize) Use variable for account_id
|
2021-12-11 23:49:53 +07:00
|
|
|
account_id = var.cloudflare_account_id
|
2021-12-08 09:24:09 +07:00
|
|
|
name = "homelab"
|
2021-12-09 01:12:59 +07:00
|
|
|
secret = base64encode(random_password.tunnel_secret.result)
|
2021-12-08 09:24:09 +07:00
|
|
|
}
|
|
|
|
|
2021-12-09 01:12:59 +07:00
|
|
|
resource "cloudflare_record" "tunnels" {
|
|
|
|
for_each = toset([
|
|
|
|
"git"
|
|
|
|
])
|
|
|
|
|
|
|
|
zone_id = data.cloudflare_zone.khuedoan_com.id
|
2021-12-08 09:24:09 +07:00
|
|
|
type = "CNAME"
|
2021-12-09 01:12:59 +07:00
|
|
|
name = each.key
|
2021-12-12 09:39:44 +07:00
|
|
|
value = "${cloudflare_argo_tunnel.homelab.id}.cfargotunnel.com"
|
2021-12-08 09:24:09 +07:00
|
|
|
proxied = true
|
|
|
|
ttl = 1 # Auto
|
|
|
|
}
|
2021-12-09 01:12:59 +07:00
|
|
|
|
|
|
|
# TODO
|
|
|
|
# api token
|
|
|
|
# add it to certmanager, external-dns, cloudflaredknamespace
|
2021-12-09 02:03:16 +07:00
|
|
|
|
|
|
|
resource "kubernetes_namespace" "namespaces" {
|
|
|
|
for_each = toset([
|
|
|
|
"cert-manager",
|
|
|
|
"cloudflared",
|
|
|
|
"external-dns",
|
|
|
|
"velero"
|
|
|
|
])
|
|
|
|
|
|
|
|
metadata {
|
|
|
|
name = each.key
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "kubernetes_secret" "cloudflared_credentials" {
|
|
|
|
metadata {
|
|
|
|
name = "cloudflared-credentials"
|
|
|
|
namespace = "cloudflared"
|
|
|
|
}
|
|
|
|
|
|
|
|
data = {
|
2021-12-12 00:22:05 +07:00
|
|
|
"credentials.json" = jsonencode({
|
|
|
|
AccountTag = var.cloudflare_account_id
|
2021-12-09 02:03:16 +07:00
|
|
|
TunnelName = cloudflare_argo_tunnel.homelab.name
|
|
|
|
TunnelID = cloudflare_argo_tunnel.homelab.id
|
|
|
|
TunnelSecret = base64encode(random_password.tunnel_secret.result)
|
2021-12-12 00:22:05 +07:00
|
|
|
})
|
2021-12-09 02:03:16 +07:00
|
|
|
}
|
|
|
|
}
|