Skip to content

Commit

Permalink
examples/hetzner: refactor network creation
Browse files Browse the repository at this point in the history
Signed-off-by: Marko Mudrinić <mudrinic.mare@gmail.com>
  • Loading branch information
xmudrii committed Apr 19, 2024
1 parent d293998 commit 923d302
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 13 deletions.
34 changes: 24 additions & 10 deletions examples/terraform/hetzner/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,20 @@ locals {

cluster_autoscaler_min_replicas = var.cluster_autoscaler_min_replicas > 0 ? var.cluster_autoscaler_min_replicas : var.initial_machinedeployment_replicas
cluster_autoscaler_max_replicas = var.cluster_autoscaler_max_replicas > 0 ? var.cluster_autoscaler_max_replicas : var.initial_machinedeployment_replicas

base_network_mask = parseint(split("/", var.base_network_cidr)[1], 10)
subnet_newbits = var.subnet_mask - local.base_network_mask
subnet_netnum = pow(2, local.subnet_newbits) - 1
ip_range = cidrsubnet(
var.base_network_cidr,
local.subnet_newbits,
random_integer.random_subnet_netnum.result,
)
}

resource "random_integer" "random_subnet_netnum" {
min = 0
max = local.subnet_netnum
}

resource "hcloud_ssh_key" "kubeone" {
Expand All @@ -34,7 +48,14 @@ resource "hcloud_ssh_key" "kubeone" {

resource "hcloud_network" "net" {
name = var.cluster_name
ip_range = var.ip_range
ip_range = local.ip_range
}

resource "hcloud_network_subnet" "kubeone" {
network_id = hcloud_network.net.id
type = "server"
network_zone = var.network_zone
ip_range = local.ip_range
}

resource "hcloud_firewall" "cluster" {
Expand Down Expand Up @@ -63,7 +84,7 @@ resource "hcloud_firewall" "cluster" {
protocol = "tcp"
port = "any"
source_ips = [
var.ip_range,
hcloud_network.net.ip_range,
]
}

Expand All @@ -73,7 +94,7 @@ resource "hcloud_firewall" "cluster" {
protocol = "udp"
port = "any"
source_ips = [
var.ip_range,
hcloud_network.net.ip_range,
]
}

Expand All @@ -98,13 +119,6 @@ resource "hcloud_firewall" "cluster" {
}
}

resource "hcloud_network_subnet" "kubeone" {
network_id = hcloud_network.net.id
type = "server"
network_zone = var.network_zone
ip_range = var.ip_range
}

resource "hcloud_server_network" "control_plane" {
count = var.control_plane_vm_count
server_id = element(hcloud_server.control_plane.*.id, count.index)
Expand Down
12 changes: 9 additions & 3 deletions examples/terraform/hetzner/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -190,12 +190,18 @@ variable "image" {
type = string
}

variable "ip_range" {
default = "192.168.0.0/16"
description = "ip range to use for private network"
variable "base_network_cidr" {
default = "10.100.0.0/16"
description = "base cidr, resulting cidr is randomly generated depending on provided subnet_mask"
type = string
}

variable "subnet_mask" {
default = 24
description = "subnet mask to use for generating cidr for a private network"
type = number
}

variable "network_zone" {
default = "eu-central"
description = "network zone to use for private network"
Expand Down

0 comments on commit 923d302

Please sign in to comment.