Verified Commit 39b432b8 authored by Hugo's avatar Hugo
Browse files

Initial commit

parents
.terraform
\ No newline at end of file
# This file is maintained automatically by "terraform init".
# Manual edits may be lost in future updates.
provider "registry.terraform.io/hashicorp/template" {
version = "2.2.0"
hashes = [
"h1:94qn780bi1qjrbC3uQtjJh3Wkfwd5+tTtJHOb7KTg9w=",
"zh:01702196f0a0492ec07917db7aaa595843d8f171dc195f4c988d2ffca2a06386",
"zh:09aae3da826ba3d7df69efeb25d146a1de0d03e951d35019a0f80e4f58c89b53",
"zh:09ba83c0625b6fe0a954da6fbd0c355ac0b7f07f86c91a2a97849140fea49603",
"zh:0e3a6c8e16f17f19010accd0844187d524580d9fdb0731f675ffcf4afba03d16",
"zh:45f2c594b6f2f34ea663704cc72048b212fe7d16fb4cfd959365fa997228a776",
"zh:77ea3e5a0446784d77114b5e851c970a3dde1e08fa6de38210b8385d7605d451",
"zh:8a154388f3708e3df5a69122a23bdfaf760a523788a5081976b3d5616f7d30ae",
"zh:992843002f2db5a11e626b3fc23dc0c87ad3729b3b3cff08e32ffb3df97edbde",
"zh:ad906f4cebd3ec5e43d5cd6dc8f4c5c9cc3b33d2243c89c5fc18f97f7277b51d",
"zh:c979425ddb256511137ecd093e23283234da0154b7fa8b21c2687182d9aea8b2",
]
}
provider "registry.terraform.io/hetznercloud/hcloud" {
version = "1.26.0"
hashes = [
"h1:2LLe4UKLS7R+t+tQL1oOFLA8c8/rs3iCfT26LyiQcsk=",
"zh:03d7eb722a4ee25774949baace0125392060d0369d4cb9257d7d298ab6ece3ff",
"zh:0fed2e63ac4cb6fe6b2a5b6891abf973cb7c1716e487fbabc09216e0ec05e866",
"zh:1a84c8c1c8e2d6607de5aa09aa3f9254183cde75a5acc666cca5f4b02a1d290e",
"zh:23ac426aa3a0001fb20045dc35569978864f139732f45ab671c64e80123c91a1",
"zh:23b78348b24ae3e4679bd90989c999346efd71ee228d17368d5f556f63e5fd06",
"zh:2503fe28ac87661af96e7755a7404307000822104ac1abc571271eee46c95ab5",
"zh:3fe859b2611d20ed5cd65cc2ec812acf73c7dfb39f2fee45ef99a3896c2662a8",
"zh:51ef869ed35d0d8aada35f587c4a64802f1140dc93c40a4e7c9800560143bb1a",
"zh:69b93cf4adca465b89da08e4e3b4aaf831821f1fbae68e526c0a292b3cfa463d",
"zh:6a4e23c6aa86e3d30240e6e4c97daef3af9ad217be2c6f35300fe1839fdbf8b2",
"zh:97a513459692a981a62b4a566c1d736c4a67622d2fbbee3771ec3ea8d576d484",
"zh:fec6c07731e23d1dd45015b44747b89c4fee58b5b2560f96d24c7da5a8ecb2ad",
]
}
locals {
apiPorts = ["6443", "9443", "8132", "8133"]
}
resource "hcloud_load_balancer" "api_lb" {
name = "${var.name}-api"
load_balancer_type = var.api.lbType
location = var.location
labels = {
cluster = var.name
}
}
resource "hcloud_load_balancer_network" "api_lb_net" {
load_balancer_id = hcloud_load_balancer.api_lb.id
network_id = var.network.id
ip = var.api.privateIp
}
resource "hcloud_load_balancer_target" "api_lb_target" {
load_balancer_id = hcloud_load_balancer.api_lb.id
type = "label_selector"
label_selector = "cluster=${var.name},role=controller"
use_private_ip = true
}
resource "hcloud_load_balancer_service" "api_lb_service" {
for_each = toset(local.apiPorts)
load_balancer_id = hcloud_load_balancer.api_lb.id
protocol = "tcp"
listen_port = each.value
destination_port = each.value
}
resource "hcloud_load_balancer" "ingress_lb" {
name = "${var.name}-ingress"
load_balancer_type = var.ingress.lbType
location = var.location
labels = {
cluster = var.name
}
}
resource "hcloud_load_balancer_network" "ingress_lb_net" {
load_balancer_id = hcloud_load_balancer.ingress_lb.id
network_id = var.network.id
ip = var.ingress.privateIp
}
resource "hcloud_load_balancer_target" "ingress_lb_target" {
load_balancer_id = hcloud_load_balancer.ingress_lb.id
type = "label_selector"
label_selector = "cluster=${var.name},ingress=true"
use_private_ip = true
}
resource "hcloud_load_balancer_service" "ingress_lb_service" {
for_each = toset(var.ingress.ports)
load_balancer_id = hcloud_load_balancer.ingress_lb.id
protocol = "tcp"
listen_port = each.value
destination_port = each.value
}
resource "hcloud_network_subnet" "cloudnet" {
type = "cloud"
network_id = var.network.id
network_zone = var.network.zone
ip_range = var.network.cloudCidr
}
locals {
vms = [for name, node in var.nodes : name if node.type == "vm"]
volumes = merge([
for nodeName, node in var.nodes : {
for volumeName, volume in node.volumes : "${nodeName}:${volumeName}" => {
volume = volumeName
node = nodeName
}
}
if node.type == "vm"
]...)
}
resource "hcloud_server" "node" {
for_each = toset(local.vms)
name = "${var.name}-${each.value}"
image = "debian-10"
server_type = var.nodes[each.value].serverType
ssh_keys = var.sshKeys
location = var.location
labels = {
cluster = var.name
node = each.value
role = var.nodes[each.value].role
ingress = var.nodes[each.value].ingress
}
network {
network_id = var.network.id
ip = var.nodes[each.value].privateIp
}
depends_on = [
hcloud_network_subnet.cloudnet
]
}
resource "hcloud_volume" "volume" {
for_each = local.volumes
name = "${var.name}-${each.value.node}-${each.value.volume}"
size = var.nodes[each.value.node].volumes[each.value.volume].size
server_id = hcloud_server.node[each.value.node].id
labels = {
cluster = var.name
node = each.value.node
volume = each.value.volume
}
}
\ No newline at end of file
output "name" {
value = var.name
}
output "sshKeys" {
value = var.sshKeys
}
output "location" {
value = var.location
}
output "network" {
value = var.network
}
output "api" {
value = merge(var.api, {
publicIp = hcloud_load_balancer.api_lb.ipv4
})
}
output "ingress" {
value = merge(var.ingress, {
publicIp = hcloud_load_balancer.ingress_lb.ipv4
})
}
output "nodes" {
value = merge(var.nodes, {
for nodeName, node in hcloud_server.node : nodeName => merge(var.nodes[nodeName], {
"publicIp" = node.ipv4_address
})
})
}
provider "hcloud" {
token = var.hcloud_token
}
variable "hcloud_token" {
description = "Hetzner cloud auth token"
type = string
sensitive = true
}
variable "name" {
description = "Cluster's name"
type = string
}
variable "sshKeys" {
description = "List of Hetzner ssh key ids"
type = list(string)
}
variable "location" {
description = "Cluster's datacenter"
type = string
default = "fsn1"
}
variable "network" {
description = "Network config"
type = object({
id = string
zone = string
cloudCidr = string
})
}
variable "api" {
description = "Cluster's api configs"
type = object({
lbType = string
privateIp = string
})
}
variable "ingress" {
description = "Cluster's ingress configs"
type = object({
lbType = string
privateIp = string
ports = list(string)
})
}
variable "nodes" {
description = "Cluster's nodes configs"
type = map(object({
type = string
role = string
serverType = string
privateIp = string
ingress = bool
volumes = map(object({
size = number
}))
}))
}
terraform {
required_providers {
hcloud = {
source = "hetznercloud/hcloud"
}
template = {
source = "hashicorp/template"
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment