-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
303 lines (276 loc) Β· 10.6 KB
/
main.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
resource "github_repository" "homelab-repo" {
auto_init = false
description = "π§βπ» My personal infrastructure using Kubernetes, Terraform, Cloudflare and more"
name = var.gh-repo-name
visibility = "public"
vulnerability_alerts = true
}
resource "github_repository_ruleset" "not-to-main" {
name = "Not to main"
repository = github_repository.homelab-repo.name
target = "branch"
enforcement = "active"
conditions {
ref_name {
include = ["~DEFAULT_BRANCH"]
exclude = []
}
}
bypass_actors {
actor_id = 5
actor_type = "RepositoryRole"
bypass_mode = "always"
}
rules {
deletion = true
non_fast_forward = true
pull_request {
dismiss_stale_reviews_on_push = false
require_code_owner_review = true
require_last_push_approval = true
required_approving_review_count = 1
required_review_thread_resolution = false
}
}
}
# resource "github_repository_environment" "production" {
# environment = "production"
# repository = github_repository.homelab-repo.name
# prevent_self_review = false
# reviewers {
# users = [data.github_user.current.id]
# }
# deployment_branch_policy {
# protected_branches = true
# custom_branch_policies = false
# }
# }
# resource "github_actions_environment_secret" "gh-ts-oauth-client-id" {
# repository = github_repository.homelab-repo.name
# environment = github_repository_environment.production.environment
# secret_name = "TS_OAUTH_CLIENT_ID"
# plaintext_value = var.tailscale-client-id
# }
# resource "github_actions_environment_secret" "gh-ts-oauth-secret" {
# repository = github_repository.homelab-repo.name
# environment = github_repository_environment.production.environment
# secret_name = "TS_OAUTH_SECRET"
# plaintext_value = var.tailscale-client-secret
# }
resource "tailscale_tailnet_key" "tailscale-grx01-key" {
reusable = true
ephemeral = false
preauthorized = true
expiry = 3600
description = "Tailscale grx01"
tags = ["tag:ci"]
}
resource "ssh_resource" "grx01" {
host = var.node-address
user = var.node-username
private_key = file(var.private-ssh-key-path)
triggers = {
always_run = timestamp()
}
commands = flatten([
# Install basic stuff
"sudo dnf install --assumeyes curl git iproute bind-utils telnet epel-release",
# Seteamos SELinux a permisivo (equivalente a desactivarlo)
"sudo sed -Ei 's/SELINUX=enforcing/SELINUX=permissive/' /etc/selinux/config",
"sudo setenforce 0",
# Enable Alma linux web console on port 9090
"sudo systemctl enable --now cockpit.socket",
# Se recomienda deshabilitar el firewall para el uso de k3s
"sudo systemctl disable firewalld --now",
# Configuramos los discos a montar
[for disk in var.disk-mounts : "sudo mkdir -p ${disk.path} && sudo grep -q '${disk.uuid}' /etc/fstab && echo 'El disco ya existe, omitiendo' || echo 'UUID=${disk.uuid} ${disk.path} ${disk.type} ${disk.options}' | sudo tee -a /etc/fstab"],
"sudo systemctl daemon-reload",
"sudo mount -a",
# Instalamos K3s
"curl -sfL https://get.k3s.io | sudo sh -",
# Copiamos el fichero de config para kubectl para despues descargarlo
"mkdir -p /home/${var.node-username}/.kube && sudo cp /etc/rancher/k3s/k3s.yaml /home/${var.node-username}/.kube/config && chown ${var.node-username}:${var.node-username} /home/${var.node-username}/.kube/config",
# Copiamos el archivo de configuracion del server de k3s
"cat <<EOF | sudo tee /etc/rancher/k3s/config.yaml\n${templatefile("./k3s-config.tftpl", { "k8s-default-storage-path" = var.k8s-default-storage-path })}\nEOF",
# Reiniciamos k3s para aplicar los cambios
"sudo systemctl restart k3s",
# Configuramos el IP Forward
"echo 'net.ipv4.ip_forward = 1' | sudo tee -a /etc/sysctl.d/99-tailscale.conf",
"echo 'net.ipv6.conf.all.forwarding = 1' | sudo tee -a /etc/sysctl.d/99-tailscale.conf",
"sudo sysctl -p /etc/sysctl.d/99-tailscale.conf",
# Instalamos tailscale
"curl -fsSL https://tailscale.com/install.sh | sh",
"sudo tailscale up --ssh --advertise-tags=tag:ci --advertise-exit-node --accept-routes --advertise-routes=192.168.1.0/24 --auth-key=${tailscale_tailnet_key.tailscale-grx01-key.key}"
])
# Descargamos el fichero de configuracion de kubectl y cambiamos la IP del loopback a la real
provisioner "local-exec" {
command = "scp -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i ${var.private-ssh-key-path} ${self.user}@${self.host}:~/.kube/config ./kubeconfig && sed -Ei 's/127.0.0.1/${var.node-address}/' ./kubeconfig"
}
depends_on = [tailscale_tailnet_key.tailscale-grx01-key]
}
# resource "github_actions_environment_secret" "gh-ts-host-ip" {
# repository = github_repository.homelab-repo.name
# environment = github_repository_environment.production.environment
# secret_name = "TS_HOST_IP"
# plaintext_value = "grx01.taild52040.ts.net"
# }
# -------------------------------------------------------------------------------------------
# Releases de HELM y otros manifiestos de k8s
# -------------------------------------------------------------------------------------------
# Las releases de helm y manifiestos necesarios para el basico funcionamiento de la infra
# las gestioneremos con terraform, mientras que las releases de "aplicaciones"
# que despleguemos, es decir, las que proveen funcionalidad no relacionada con la infra,
# seran gestionadas por Argo CD
# -------------------------------------------------------------------------------------------
# Instalamos cert manager para la gestion de certificados, principalmente del SSL de los ingress
resource "helm_release" "cert-manager" {
name = "cert-manager"
version = "v1.16.1"
repository = "https://charts.jetstack.io"
chart = "cert-manager"
create_namespace = true
namespace = "cert-manager"
dependency_update = true
force_update = true
verify = false
wait = true
wait_for_jobs = true
timeout = 600
depends_on = [ssh_resource.grx01]
set {
name = "crds.enabled"
value = true
}
}
# Aplicamos el secreto que contiene el token de cloudflare
# para generar los certificados.
# Nota: Los proveedores de kubernetes y kubectl no son aptos para este
# caso. En el caso de kubernetes_manifest filtra los secretos y kubectl
# produce errores varios. Ver: https://medium.com/@danieljimgarcia/dont-use-the-terraform-kubernetes-manifest-resource-6c7ff4fe629a
resource "helm_release" "k8s-cloudflare-secret" {
name = "cloudflare-secret"
repository = "https://bedag.github.io/helm-charts/"
chart = "raw"
version = "2.0.0"
values = sensitive([
<<-EOF
resources:
- apiVersion: v1
kind: Secret
metadata:
name: cloudflare-api-token-secret
namespace: cert-manager
type: Opaque
stringData:
api-token: ${sensitive(var.cloudflare-token)}
EOF
])
}
# Aplicamos el ClusterIssuer encargado de generar los certificados
# con un challenge DNS desde cloudflare
resource "kubernetes_manifest" "cloudflare-issuer" {
manifest = yamldecode(file("./kubernetes/cloudflare-certs.yaml"))
}
# Instalamos el stack de observabilidad de prometheus+grafana
resource "helm_release" "kube-prometheus-stack" {
name = "kube-prometheus-stack"
version = "65.2.0"
repository = "https://prometheus-community.github.io/helm-charts"
chart = "kube-prometheus-stack"
create_namespace = true
namespace = "observability"
dependency_update = true
force_update = true
verify = false
wait = true
wait_for_jobs = true
depends_on = [ssh_resource.grx01]
values = sensitive([
file("kubernetes/prom.values.yaml")
])
set_sensitive {
name = "grafana.adminPassword"
value = sensitive(var.grafana-admin-password)
}
}
# Instalamos trivy operator para monitorizar la seguridad del cluster
resource "helm_release" "trivy-operator" {
name = "trivy-operator"
version = "0.24.1"
repository = "https://aquasecurity.github.io/helm-charts/"
chart = "trivy-operator"
create_namespace = true
namespace = "trivy-operator"
dependency_update = true
force_update = true
verify = false
wait = true
wait_for_jobs = true
timeout = 600
depends_on = [ssh_resource.grx01]
# Habilitamos la integracion con prometheus
set {
name = "serviceMonitor.enabled"
value = true
}
}
# Instalamos pihole para gestion de DNS, bloqueo de trackers y adblocking
resource "helm_release" "k8s-pihole-secret" {
name = "k8s-pihole-secret"
repository = "https://bedag.github.io/helm-charts/"
chart = "raw"
version = "2.0.0"
values = sensitive([
<<-EOF
resources:
- apiVersion: v1
kind: Namespace
metadata:
name: pihole
- apiVersion: v1
kind: Secret
metadata:
name: pihole-secrets
namespace: pihole
type: Opaque
data:
WEBPASSWORD: ${base64encode(var.pihole-admin-password)}
EOF
])
}
resource "kubernetes_manifest" "pihole-storage" {
manifest = yamldecode(file("./kubernetes/pihole/storage-pihole.yaml"))
}
resource "kubernetes_manifest" "pihole-storage-dnsmasq" {
manifest = yamldecode(file("./kubernetes/pihole/storage-dnsmasq.yaml"))
}
resource "kubernetes_manifest" "pihole" {
manifest = yamldecode(file("./kubernetes/pihole/pihole.yaml"))
depends_on = [kubernetes_config_map.pihole-dominios-internos]
}
resource "helm_release" "argocd" {
name = "argocd"
repository = "https://argoproj.github.io/argo-helm"
chart = "argo-cd"
version = "7.6.12"
create_namespace = true
namespace = "argocd"
dependency_update = true
force_update = true
verify = false
wait = true
wait_for_jobs = true
timeout = 600
depends_on = [ssh_resource.grx01]
values = sensitive([
file("kubernetes/argo.values.yaml")
])
set_sensitive {
name = "configs.secret.argocdServerAdminPassword"
value = sensitive(bcrypt(var.argo-admin-password))
}
}
resource "kubernetes_manifest" "argocd-apps" {
manifest = yamldecode(file("./kubernetes/argocd/argocd.yaml"))
depends_on = [helm_release.argocd]
}