You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

550 lines
16 KiB

  1. locals {
  2. # Create a list of all disks to create
  3. disks = flatten([
  4. for node_name, machine in var.machines : [
  5. for disk_name, disk in machine.additional_disks : {
  6. disk = disk
  7. disk_name = disk_name
  8. node_name = node_name
  9. }
  10. ]
  11. ])
  12. lb_backend_servers = flatten([
  13. for lb_name, loadbalancer in var.loadbalancers : [
  14. for backend_server in loadbalancer.backend_servers : {
  15. port = loadbalancer.port
  16. lb_name = lb_name
  17. server_name = backend_server
  18. }
  19. ]
  20. ])
  21. # If prefix is set, all resources will be prefixed with "${var.prefix}-"
  22. # Else don't prefix with anything
  23. resource-prefix = "%{ if var.prefix != ""}${var.prefix}-%{ endif }"
  24. }
  25. resource "upcloud_network" "private" {
  26. name = "${local.resource-prefix}k8s-network"
  27. zone = var.zone
  28. ip_network {
  29. address = var.private_network_cidr
  30. dhcp = true
  31. family = "IPv4"
  32. }
  33. }
  34. resource "upcloud_storage" "additional_disks" {
  35. for_each = {
  36. for disk in local.disks: "${disk.node_name}_${disk.disk_name}" => disk.disk
  37. }
  38. size = each.value.size
  39. tier = each.value.tier
  40. title = "${local.resource-prefix}${each.key}"
  41. zone = var.zone
  42. }
  43. resource "upcloud_server" "master" {
  44. for_each = {
  45. for name, machine in var.machines :
  46. name => machine
  47. if machine.node_type == "master"
  48. }
  49. hostname = "${local.resource-prefix}${each.key}"
  50. plan = each.value.plan
  51. cpu = each.value.plan == null ? each.value.cpu : null
  52. mem = each.value.plan == null ? each.value.mem : null
  53. zone = var.zone
  54. template {
  55. storage = var.template_name
  56. size = each.value.disk_size
  57. }
  58. # Public network interface
  59. network_interface {
  60. type = "public"
  61. }
  62. # Private network interface
  63. network_interface {
  64. type = "private"
  65. network = upcloud_network.private.id
  66. }
  67. # Ignore volumes created by csi-driver
  68. lifecycle {
  69. ignore_changes = [storage_devices]
  70. }
  71. firewall = var.firewall_enabled
  72. dynamic "storage_devices" {
  73. for_each = {
  74. for disk_key_name, disk in upcloud_storage.additional_disks :
  75. disk_key_name => disk
  76. # Only add the disk if it matches the node name in the start of its name
  77. if length(regexall("^${each.key}_.+", disk_key_name)) > 0
  78. }
  79. content {
  80. storage = storage_devices.value.id
  81. }
  82. }
  83. # Include at least one public SSH key
  84. login {
  85. user = var.username
  86. keys = var.ssh_public_keys
  87. create_password = false
  88. }
  89. }
  90. resource "upcloud_server" "worker" {
  91. for_each = {
  92. for name, machine in var.machines :
  93. name => machine
  94. if machine.node_type == "worker"
  95. }
  96. hostname = "${local.resource-prefix}${each.key}"
  97. plan = each.value.plan
  98. cpu = each.value.plan == null ? each.value.cpu : null
  99. mem = each.value.plan == null ? each.value.mem : null
  100. zone = var.zone
  101. template {
  102. storage = var.template_name
  103. size = each.value.disk_size
  104. }
  105. # Public network interface
  106. network_interface {
  107. type = "public"
  108. }
  109. # Private network interface
  110. network_interface {
  111. type = "private"
  112. network = upcloud_network.private.id
  113. }
  114. # Ignore volumes created by csi-driver
  115. lifecycle {
  116. ignore_changes = [storage_devices]
  117. }
  118. firewall = var.firewall_enabled
  119. dynamic "storage_devices" {
  120. for_each = {
  121. for disk_key_name, disk in upcloud_storage.additional_disks :
  122. disk_key_name => disk
  123. # Only add the disk if it matches the node name in the start of its name
  124. if length(regexall("^${each.key}_.+", disk_key_name)) > 0
  125. }
  126. content {
  127. storage = storage_devices.value.id
  128. }
  129. }
  130. # Include at least one public SSH key
  131. login {
  132. user = var.username
  133. keys = var.ssh_public_keys
  134. create_password = false
  135. }
  136. }
  137. resource "upcloud_firewall_rules" "master" {
  138. for_each = upcloud_server.master
  139. server_id = each.value.id
  140. dynamic firewall_rule {
  141. for_each = var.master_allowed_remote_ips
  142. content {
  143. action = "accept"
  144. comment = "Allow master API access from this network"
  145. destination_port_end = "6443"
  146. destination_port_start = "6443"
  147. direction = "in"
  148. family = "IPv4"
  149. protocol = "tcp"
  150. source_address_end = firewall_rule.value.end_address
  151. source_address_start = firewall_rule.value.start_address
  152. }
  153. }
  154. dynamic firewall_rule {
  155. for_each = length(var.master_allowed_remote_ips) > 0 ? [1] : []
  156. content {
  157. action = "drop"
  158. comment = "Deny master API access from other networks"
  159. destination_port_end = "6443"
  160. destination_port_start = "6443"
  161. direction = "in"
  162. family = "IPv4"
  163. protocol = "tcp"
  164. source_address_end = "255.255.255.255"
  165. source_address_start = "0.0.0.0"
  166. }
  167. }
  168. dynamic firewall_rule {
  169. for_each = var.k8s_allowed_remote_ips
  170. content {
  171. action = "accept"
  172. comment = "Allow SSH from this network"
  173. destination_port_end = "22"
  174. destination_port_start = "22"
  175. direction = "in"
  176. family = "IPv4"
  177. protocol = "tcp"
  178. source_address_end = firewall_rule.value.end_address
  179. source_address_start = firewall_rule.value.start_address
  180. }
  181. }
  182. dynamic firewall_rule {
  183. for_each = length(var.k8s_allowed_remote_ips) > 0 ? [1] : []
  184. content {
  185. action = "drop"
  186. comment = "Deny SSH from other networks"
  187. destination_port_end = "22"
  188. destination_port_start = "22"
  189. direction = "in"
  190. family = "IPv4"
  191. protocol = "tcp"
  192. source_address_end = "255.255.255.255"
  193. source_address_start = "0.0.0.0"
  194. }
  195. }
  196. dynamic firewall_rule {
  197. for_each = var.master_allowed_ports
  198. content {
  199. action = "accept"
  200. comment = "Allow access on this port"
  201. destination_port_end = firewall_rule.value.port_range_max
  202. destination_port_start = firewall_rule.value.port_range_min
  203. direction = "in"
  204. family = "IPv4"
  205. protocol = firewall_rule.value.protocol
  206. source_address_end = firewall_rule.value.end_address
  207. source_address_start = firewall_rule.value.start_address
  208. }
  209. }
  210. dynamic firewall_rule {
  211. for_each = var.firewall_default_deny_in ? ["tcp", "udp"] : []
  212. content {
  213. action = "accept"
  214. comment = "UpCloud DNS"
  215. source_port_end = "53"
  216. source_port_start = "53"
  217. direction = "in"
  218. family = "IPv4"
  219. protocol = firewall_rule.value
  220. source_address_end = "94.237.40.9"
  221. source_address_start = "94.237.40.9"
  222. }
  223. }
  224. dynamic firewall_rule {
  225. for_each = var.firewall_default_deny_in ? ["tcp", "udp"] : []
  226. content {
  227. action = "accept"
  228. comment = "UpCloud DNS"
  229. source_port_end = "53"
  230. source_port_start = "53"
  231. direction = "in"
  232. family = "IPv4"
  233. protocol = firewall_rule.value
  234. source_address_end = "94.237.127.9"
  235. source_address_start = "94.237.127.9"
  236. }
  237. }
  238. dynamic firewall_rule {
  239. for_each = var.firewall_default_deny_in ? ["tcp", "udp"] : []
  240. content {
  241. action = "accept"
  242. comment = "UpCloud DNS"
  243. source_port_end = "53"
  244. source_port_start = "53"
  245. direction = "in"
  246. family = "IPv6"
  247. protocol = firewall_rule.value
  248. source_address_end = "2a04:3540:53::1"
  249. source_address_start = "2a04:3540:53::1"
  250. }
  251. }
  252. dynamic firewall_rule {
  253. for_each = var.firewall_default_deny_in ? ["tcp", "udp"] : []
  254. content {
  255. action = "accept"
  256. comment = "UpCloud DNS"
  257. source_port_end = "53"
  258. source_port_start = "53"
  259. direction = "in"
  260. family = "IPv6"
  261. protocol = firewall_rule.value
  262. source_address_end = "2a04:3544:53::1"
  263. source_address_start = "2a04:3544:53::1"
  264. }
  265. }
  266. dynamic firewall_rule {
  267. for_each = var.firewall_default_deny_in ? ["udp"] : []
  268. content {
  269. action = "accept"
  270. comment = "NTP Port"
  271. source_port_end = "123"
  272. source_port_start = "123"
  273. direction = "in"
  274. family = "IPv4"
  275. protocol = firewall_rule.value
  276. source_address_end = "255.255.255.255"
  277. source_address_start = "0.0.0.0"
  278. }
  279. }
  280. dynamic firewall_rule {
  281. for_each = var.firewall_default_deny_in ? ["udp"] : []
  282. content {
  283. action = "accept"
  284. comment = "NTP Port"
  285. source_port_end = "123"
  286. source_port_start = "123"
  287. direction = "in"
  288. family = "IPv6"
  289. protocol = firewall_rule.value
  290. }
  291. }
  292. firewall_rule {
  293. action = var.firewall_default_deny_in ? "drop" : "accept"
  294. direction = "in"
  295. }
  296. firewall_rule {
  297. action = var.firewall_default_deny_out ? "drop" : "accept"
  298. direction = "out"
  299. }
  300. }
  301. resource "upcloud_firewall_rules" "k8s" {
  302. for_each = upcloud_server.worker
  303. server_id = each.value.id
  304. dynamic firewall_rule {
  305. for_each = var.k8s_allowed_remote_ips
  306. content {
  307. action = "accept"
  308. comment = "Allow SSH from this network"
  309. destination_port_end = "22"
  310. destination_port_start = "22"
  311. direction = "in"
  312. family = "IPv4"
  313. protocol = "tcp"
  314. source_address_end = firewall_rule.value.end_address
  315. source_address_start = firewall_rule.value.start_address
  316. }
  317. }
  318. dynamic firewall_rule {
  319. for_each = length(var.k8s_allowed_remote_ips) > 0 ? [1] : []
  320. content {
  321. action = "drop"
  322. comment = "Deny SSH from other networks"
  323. destination_port_end = "22"
  324. destination_port_start = "22"
  325. direction = "in"
  326. family = "IPv4"
  327. protocol = "tcp"
  328. source_address_end = "255.255.255.255"
  329. source_address_start = "0.0.0.0"
  330. }
  331. }
  332. dynamic firewall_rule {
  333. for_each = var.worker_allowed_ports
  334. content {
  335. action = "accept"
  336. comment = "Allow access on this port"
  337. destination_port_end = firewall_rule.value.port_range_max
  338. destination_port_start = firewall_rule.value.port_range_min
  339. direction = "in"
  340. family = "IPv4"
  341. protocol = firewall_rule.value.protocol
  342. source_address_end = firewall_rule.value.end_address
  343. source_address_start = firewall_rule.value.start_address
  344. }
  345. }
  346. dynamic firewall_rule {
  347. for_each = var.firewall_default_deny_in ? ["tcp", "udp"] : []
  348. content {
  349. action = "accept"
  350. comment = "UpCloud DNS"
  351. source_port_end = "53"
  352. source_port_start = "53"
  353. direction = "in"
  354. family = "IPv4"
  355. protocol = firewall_rule.value
  356. source_address_end = "94.237.40.9"
  357. source_address_start = "94.237.40.9"
  358. }
  359. }
  360. dynamic firewall_rule {
  361. for_each = var.firewall_default_deny_in ? ["tcp", "udp"] : []
  362. content {
  363. action = "accept"
  364. comment = "UpCloud DNS"
  365. source_port_end = "53"
  366. source_port_start = "53"
  367. direction = "in"
  368. family = "IPv4"
  369. protocol = firewall_rule.value
  370. source_address_end = "94.237.127.9"
  371. source_address_start = "94.237.127.9"
  372. }
  373. }
  374. dynamic firewall_rule {
  375. for_each = var.firewall_default_deny_in ? ["tcp", "udp"] : []
  376. content {
  377. action = "accept"
  378. comment = "UpCloud DNS"
  379. source_port_end = "53"
  380. source_port_start = "53"
  381. direction = "in"
  382. family = "IPv6"
  383. protocol = firewall_rule.value
  384. source_address_end = "2a04:3540:53::1"
  385. source_address_start = "2a04:3540:53::1"
  386. }
  387. }
  388. dynamic firewall_rule {
  389. for_each = var.firewall_default_deny_in ? ["tcp", "udp"] : []
  390. content {
  391. action = "accept"
  392. comment = "UpCloud DNS"
  393. source_port_end = "53"
  394. source_port_start = "53"
  395. direction = "in"
  396. family = "IPv6"
  397. protocol = firewall_rule.value
  398. source_address_end = "2a04:3544:53::1"
  399. source_address_start = "2a04:3544:53::1"
  400. }
  401. }
  402. dynamic firewall_rule {
  403. for_each = var.firewall_default_deny_in ? ["udp"] : []
  404. content {
  405. action = "accept"
  406. comment = "NTP Port"
  407. source_port_end = "123"
  408. source_port_start = "123"
  409. direction = "in"
  410. family = "IPv4"
  411. protocol = firewall_rule.value
  412. source_address_end = "255.255.255.255"
  413. source_address_start = "0.0.0.0"
  414. }
  415. }
  416. dynamic firewall_rule {
  417. for_each = var.firewall_default_deny_in ? ["udp"] : []
  418. content {
  419. action = "accept"
  420. comment = "NTP Port"
  421. source_port_end = "123"
  422. source_port_start = "123"
  423. direction = "in"
  424. family = "IPv6"
  425. protocol = firewall_rule.value
  426. }
  427. }
  428. firewall_rule {
  429. action = var.firewall_default_deny_in ? "drop" : "accept"
  430. direction = "in"
  431. }
  432. firewall_rule {
  433. action = var.firewall_default_deny_out ? "drop" : "accept"
  434. direction = "out"
  435. }
  436. }
  437. resource "upcloud_loadbalancer" "lb" {
  438. count = var.loadbalancer_enabled ? 1 : 0
  439. configured_status = "started"
  440. name = "${local.resource-prefix}lb"
  441. plan = var.loadbalancer_plan
  442. zone = var.zone
  443. network = upcloud_network.private.id
  444. }
  445. resource "upcloud_loadbalancer_backend" "lb_backend" {
  446. for_each = var.loadbalancer_enabled ? var.loadbalancers : {}
  447. loadbalancer = upcloud_loadbalancer.lb[0].id
  448. name = "lb-backend-${each.key}"
  449. }
  450. resource "upcloud_loadbalancer_frontend" "lb_frontend" {
  451. for_each = var.loadbalancer_enabled ? var.loadbalancers : {}
  452. loadbalancer = upcloud_loadbalancer.lb[0].id
  453. name = "lb-frontend-${each.key}"
  454. mode = "tcp"
  455. port = each.value.port
  456. default_backend_name = upcloud_loadbalancer_backend.lb_backend[each.key].name
  457. }
  458. resource "upcloud_loadbalancer_static_backend_member" "lb_backend_member" {
  459. for_each = {
  460. for be_server in local.lb_backend_servers:
  461. "${be_server.server_name}-lb-backend-${be_server.lb_name}" => be_server
  462. if var.loadbalancer_enabled
  463. }
  464. backend = upcloud_loadbalancer_backend.lb_backend[each.value.lb_name].id
  465. name = "${local.resource-prefix}${each.key}"
  466. ip = merge(upcloud_server.master, upcloud_server.worker)[each.value.server_name].network_interface[1].ip_address
  467. port = each.value.port
  468. weight = 100
  469. max_sessions = var.loadbalancer_plan == "production-small" ? 50000 : 1000
  470. enabled = true
  471. }