Skip to content

Commit

Permalink
Replaced Bird by native MetalLB
Browse files Browse the repository at this point in the history
  • Loading branch information
e-minguez committed Sep 24, 2024
1 parent 3e9a768 commit 47b8fee
Show file tree
Hide file tree
Showing 10 changed files with 262 additions and 159 deletions.
14 changes: 7 additions & 7 deletions examples/demo_cluster/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,20 @@ variable "deploy_demo" {
}

variable "clusters" {
description = "K3s cluster definition"
description = "Cluster definition"
type = list(object({
name = optional(string, "K3s demo cluster")
name = optional(string, "Demo cluster")
metro = optional(string, "FR")
plan_control_plane = optional(string, "c3.small.x86")
plan_node = optional(string, "c3.small.x86")
node_count = optional(number, 0)
k3s_ha = optional(bool, false)
ha = optional(bool, false)
os = optional(string, "debian_11")
control_plane_hostnames = optional(string, "k3s-cp")
node_hostnames = optional(string, "k3s-node")
custom_k3s_token = optional(string, "")
control_plane_hostnames = optional(string, "cp")
node_hostnames = optional(string, "node")
custom_token = optional(string, "")
ip_pool_count = optional(number, 0)
k3s_version = optional(string, "")
kube_version = optional(string, "")
metallb_version = optional(string, "")
}))
default = [{}]
Expand Down
12 changes: 6 additions & 6 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ locals {
}

################################################################################
# K3S Cluster In-line Module
# K8s Cluster In-line Module
################################################################################

module "k3s_cluster" {
source = "./modules/k3s_cluster"
module "kube_cluster" {
source = "./modules/kube_cluster"

for_each = { for cluster in var.clusters : cluster.name => cluster }

Expand All @@ -18,12 +18,12 @@ module "k3s_cluster" {
plan_control_plane = each.value.plan_control_plane
plan_node = each.value.plan_node
node_count = each.value.node_count
k3s_ha = each.value.k3s_ha
ha = each.value.ha
os = each.value.os
control_plane_hostnames = each.value.control_plane_hostnames
node_hostnames = each.value.node_hostnames
custom_k3s_token = each.value.custom_k3s_token
k3s_version = each.value.k3s_version
custom_token = each.value.custom_token
kube_version = each.value.kube_version
metallb_version = each.value.metallb_version
ip_pool_count = each.value.ip_pool_count
metal_project_id = var.metal_project_id
Expand Down
File renamed without changes.
40 changes: 20 additions & 20 deletions modules/k3s_cluster/main.tf → modules/kube_cluster/main.tf
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
locals {
k3s_token = coalesce(var.custom_k3s_token, random_string.random_k3s_token.result)
api_vip = var.k3s_ha ? equinix_metal_reserved_ip_block.api_vip_addr[0].address : equinix_metal_device.all_in_one[0].network[0].address
token = coalesce(var.custom_token, random_string.random_token.result)
api_vip = var.ha ? equinix_metal_reserved_ip_block.api_vip_addr[0].address : equinix_metal_device.all_in_one[0].network[0].address
ip_pool_cidr = var.ip_pool_count > 0 ? equinix_metal_reserved_ip_block.ip_pool[0].cidr_notation : ""
}

resource "random_string" "random_k3s_token" {
resource "random_string" "random_token" {
length = 16
special = false
}
Expand All @@ -20,14 +20,14 @@ resource "equinix_metal_device" "control_plane_master" {
operating_system = var.os
billing_cycle = "hourly"
project_id = var.metal_project_id
count = var.k3s_ha ? 1 : 0
count = var.ha ? 1 : 0
description = var.cluster_name
user_data = templatefile("${path.module}/templates/user-data.tftpl", {
k3s_token = local.k3s_token,
token = local.token,
API_IP = local.api_vip,
global_ip_cidr = var.global_ip_cidr,
ip_pool = local.ip_pool_cidr,
k3s_version = var.k3s_version,
kube_version = var.kube_version,
metallb_version = var.metallb_version,
deploy_demo = var.deploy_demo,
node_type = "control-plane-master" })
Expand All @@ -36,16 +36,16 @@ resource "equinix_metal_device" "control_plane_master" {
resource "equinix_metal_bgp_session" "control_plane_master" {
device_id = equinix_metal_device.control_plane_master[0].id
address_family = "ipv4"
count = var.k3s_ha ? 1 : 0
count = var.ha ? 1 : 0
}

resource "equinix_metal_reserved_ip_block" "api_vip_addr" {
count = var.k3s_ha ? 1 : 0
count = var.ha ? 1 : 0
project_id = var.metal_project_id
metro = var.metal_metro
type = "public_ipv4"
quantity = 1
description = "K3s API IP"
description = "Kubernetes API IP"
}

resource "equinix_metal_device" "control_plane_others" {
Expand All @@ -55,15 +55,15 @@ resource "equinix_metal_device" "control_plane_others" {
operating_system = var.os
billing_cycle = "hourly"
project_id = var.metal_project_id
count = var.k3s_ha ? 2 : 0
count = var.ha ? 2 : 0
description = var.cluster_name
depends_on = [equinix_metal_device.control_plane_master]
user_data = templatefile("${path.module}/templates/user-data.tftpl", {
k3s_token = local.k3s_token,
token = local.token,
API_IP = local.api_vip,
global_ip_cidr = "",
ip_pool = "",
k3s_version = var.k3s_version,
kube_version = var.kube_version,
metallb_version = var.metallb_version,
deploy_demo = false,
node_type = "control-plane" })
Expand All @@ -72,13 +72,13 @@ resource "equinix_metal_device" "control_plane_others" {
resource "equinix_metal_bgp_session" "control_plane_second" {
device_id = equinix_metal_device.control_plane_others[0].id
address_family = "ipv4"
count = var.k3s_ha ? 1 : 0
count = var.ha ? 1 : 0
}

resource "equinix_metal_bgp_session" "control_plane_third" {
device_id = equinix_metal_device.control_plane_others[1].id
address_family = "ipv4"
count = var.k3s_ha ? 1 : 0
count = var.ha ? 1 : 0
}

################################################################################
Expand Down Expand Up @@ -109,11 +109,11 @@ resource "equinix_metal_device" "nodes" {
description = var.cluster_name
depends_on = [equinix_metal_device.control_plane_master]
user_data = templatefile("${path.module}/templates/user-data.tftpl", {
k3s_token = local.k3s_token,
token = local.token,
API_IP = local.api_vip,
global_ip_cidr = "",
ip_pool = "",
k3s_version = var.k3s_version,
kube_version = var.kube_version,
metallb_version = var.metallb_version,
deploy_demo = false,
node_type = "node" })
Expand All @@ -130,14 +130,14 @@ resource "equinix_metal_device" "all_in_one" {
operating_system = var.os
billing_cycle = "hourly"
project_id = var.metal_project_id
count = var.k3s_ha ? 0 : 1
count = var.ha ? 0 : 1
description = var.cluster_name
user_data = templatefile("${path.module}/templates/user-data.tftpl", {
k3s_token = local.k3s_token,
token = local.token,
global_ip_cidr = var.global_ip_cidr,
ip_pool = local.ip_pool_cidr,
API_IP = "",
k3s_version = var.k3s_version,
kube_version = var.kube_version,
metallb_version = var.metallb_version,
deploy_demo = var.deploy_demo,
node_type = "all-in-one" })
Expand All @@ -147,5 +147,5 @@ resource "equinix_metal_device" "all_in_one" {
resource "equinix_metal_bgp_session" "all_in_one" {
device_id = equinix_metal_device.all_in_one[0].id
address_family = "ipv4"
count = var.k3s_ha ? 0 : 1
count = var.ha ? 0 : 1
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
output "k3s_api_ip" {
output "kube_api_ip" {
value = try(equinix_metal_reserved_ip_block.api_vip_addr[0].address, equinix_metal_device.all_in_one[0].network[0].address)
description = "K3s API IPs"
description = "K8s API IPs"
}
Loading

0 comments on commit 47b8fee

Please sign in to comment.