Skip to content

Commit

Permalink
feat: separate ports for k3s and rke2
Browse files Browse the repository at this point in the history
  • Loading branch information
KoLiBer committed Dec 13, 2023
1 parent 0da338b commit 283c979
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 66 deletions.
142 changes: 92 additions & 50 deletions cluster.tf
Original file line number Diff line number Diff line change
@@ -1,89 +1,131 @@
locals {
leader = var.masters[keys(var.masters)[0]]
servers = merge(
{ for key, val in var.masters : "master_${key}" => merge(val, {
exec = "server"
channel = try(val.channel, var.channel)
version = try(val.version, var.version_)
registries = base64encode(yamlencode(merge(var.registries, try(val.registries, {}))))
configs = base64encode(yamlencode(merge(var.configs, try(val.configs, {}), {
"write-kubeconfig-mode" = "0644"
"cluster-init" = (key == keys(var.masters)[0] ? "true" : "false")
"server" = (key == keys(var.masters)[0] ? "" : "https://${var.server_ip}:6443")
"token" = random_password.server.result
"agent-token" = random_password.agent.result
})))
}) },
{ for key, val in var.workers : "worker_${key}" => merge(val, {
exec = "agent"
channel = try(val.channel, var.channel)
version = try(val.version, var.version_)
registries = base64encode(yamlencode(merge(var.registries, try(val.registries, {}))))
configs = base64encode(yamlencode(merge(var.configs, try(val.configs, {}), {
"server" = "https://${var.server_ip}:6443"
"token" = random_password.agent.result
})))
}) }
)
port = var.type == "k3s" ? "6443" : "9345"
leader_key = keys(var.servers)[0]
}

module "install" {
source = "cktf/script/module"
version = "1.1.0"
for_each = local.servers
source = "cktf/script/module"
version = "1.1.0"
for_each = merge(
{ for key, val in var.servers : "server_${key}" => merge(val, { exec = "server" }) },
{ for key, val in var.agents : "agent_${key}" => merge(val, { exec = "agent" }) }
)

connection = each.value.connection
create = join("\n", [
"export INSTALL_${upper(var.type)}_SKIP_START=true",
"export INSTALL_${upper(var.type)}_NAME=${each.value.exec}",
"export INSTALL_${upper(var.type)}_EXEC=${each.value.exec}",
"export INSTALL_${upper(var.type)}_CHANNEL=${each.value.channel}",
"export INSTALL_${upper(var.type)}_VERSION=${each.value.version}",
"export INSTALL_${upper(var.type)}_CHANNEL=${each.value.channel != null ? each.value.channel : var.channel}",
"export INSTALL_${upper(var.type)}_VERSION=${each.value.version != null ? each.value.version : var.version_}",
"curl -sfL https://get.${var.type}.io | sh -",
"systemctl enable ${var.type}-${each.value.exec}.service",
"mkdir -p /etc/rancher/${var.type} /var/lib/rancher/${var.type}/${each.value.exec}/manifests",
"curl -sfL https://get.${var.type}.io | sh -"
])
destroy = join("\n", [
"/usr/local/bin/${var.type}-${each.value.exec}-uninstall.sh"
"/usr/local/bin/${var.type}*uninstall.sh"
])
}

module "configs" {
module "leader" {
source = "cktf/script/module"
version = "1.1.0"
for_each = local.servers
depends_on = [module.install]
for_each = {
for key, val in var.servers : key => merge(val, {
registries = base64encode(yamlencode(merge(var.registries, val.registries)))
configs = base64encode(yamlencode(merge(var.configs, val.configs, {
"write-kubeconfig-mode" = "0644"
"agent-token" = random_password.agent.result
"token" = random_password.server.result
"cluster-init" = var.external_db == "" ? "true" : "false"
"datastore-endpoint" = var.external_db
})))
})
if key == local.leader_key
}

connection = each.value.connection
create = join("\n", [
"echo ${each.value.registries} | base64 -d > /etc/rancher/${var.type}/registries.yaml",
"echo ${each.value.configs} | base64 -d > /etc/rancher/${var.type}/config.yaml",
"systemctl restart ${var.type}-server.service"
])
}

module "servers" {
source = "cktf/script/module"
version = "1.1.0"
depends_on = [module.leader]
for_each = {
for key, val in var.servers : key => merge(val, {
registries = base64encode(yamlencode(merge(var.registries, val.registries)))
configs = base64encode(yamlencode(merge(var.configs, val.configs, {
"write-kubeconfig-mode" = "0644"
"agent-token" = random_password.agent.result
"token" = random_password.server.result
"server" = var.external_db == "" ? "https://${var.server_ip}:${local.port}" : ""
"datastore-endpoint" = var.external_db
})))
})
if key != local.leader_key
}

connection = each.value.connection
create = join("\n", [
"echo ${each.value.registries} | base64 -d > /etc/rancher/${var.type}/registries.yaml",
"echo ${each.value.configs} | base64 -d > /etc/rancher/${var.type}/config.yaml",
"systemctl restart ${var.type}-server.service"
])
}

module "agents" {
source = "cktf/script/module"
version = "1.1.0"
depends_on = [module.servers]
for_each = {
for key, val in var.agents : key => merge(val, {
registries = base64encode(yamlencode(merge(var.registries, val.registries)))
configs = base64encode(yamlencode(merge(var.configs, val.configs, {
"server" = "https://${var.server_ip}:${local.port}"
"token" = random_password.agent.result
})))
})
}

connection = each.value.connection
create = join("\n", [
"echo ${each.value.registries} | base64 -d > /etc/rancher/${var.type}/registries.yaml",
"echo ${each.value.configs} | base64 -d > /etc/rancher/${var.type}/config.yaml",
"systemctl restart ${var.type}-${each.value.exec}.service"
"systemctl restart ${var.type}-agent.service"
])
}

module "addons" {
source = "cktf/script/module"
version = "1.1.0"
for_each = var.addons
depends_on = [module.configs]
depends_on = [module.servers]
for_each = {
for key, val in var.addons : key => base64encode(val)
}

connection = local.leader
create = "echo ${base64encode(each.value)} | base64 -d > /var/lib/rancher/${var.type}/server/manifests/${each.key}.yaml"
connection = var.servers[local.leader_key].connection
create = "echo ${each.value} | base64 -d > /var/lib/rancher/${var.type}/server/manifests/${each.key}.yaml"
destroy = "echo > /var/lib/rancher/${var.type}/server/manifests/${each.key}.yaml"
}

resource "ssh_sensitive_resource" "kubeconfig" {
depends_on = [module.configs]
depends_on = [module.servers]

host = try(local.leader.connection.host, null)
port = try(local.leader.connection.port, null)
user = try(local.leader.connection.user, null)
password = try(local.leader.connection.password, null)
timeout = try(local.leader.connection.timeout, null)
private_key = try(local.leader.connection.private_key, null)
agent = try(local.leader.connection.agent, null)
bastion_host = try(local.leader.connection.bastion_host, null)
bastion_port = try(local.leader.connection.bastion_port, null)
host = try(var.servers[local.leader_key].connection.host, null)
port = try(var.servers[local.leader_key].connection.port, null)
user = try(var.servers[local.leader_key].connection.user, null)
password = try(var.servers[local.leader_key].connection.password, null)
timeout = try(var.servers[local.leader_key].connection.timeout, null)
private_key = try(var.servers[local.leader_key].connection.private_key, null)
agent = try(var.servers[local.leader_key].connection.agent, null)
bastion_host = try(var.servers[local.leader_key].connection.bastion_host, null)
bastion_port = try(var.servers[local.leader_key].connection.bastion_port, null)

commands = ["cat /etc/rancher/${var.type}/${var.type}.yaml"]
}
2 changes: 1 addition & 1 deletion outputs.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
output "host" {
value = "https://${var.server_ip}:6443"
value = "https://${var.server_ip}:${local.port}"
sensitive = false
description = "Cluster Host"
}
Expand Down
49 changes: 34 additions & 15 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ variable "type" {

validation {
condition = contains(["k3s", "rke2"], var.type)
error_message = "Valid values for `type` are (k3s, rke2)."
error_message = "Valid values for 'type' are (k3s, rke2)."
}
}

Expand All @@ -18,7 +18,7 @@ variable "channel" {

validation {
condition = contains(["stable", "latest", "testing"], var.channel)
error_message = "Valid values for `channel` are (stable, latest, testing)."
error_message = "Valid values for 'channel' are (stable, latest, testing)."
}
}

Expand All @@ -29,11 +29,11 @@ variable "version_" {
description = "Cluster Version"
}

variable "addons" {
type = map(string)
variable "registries" {
type = any
default = {}
sensitive = false
description = "Cluster AddOns"
description = "Cluster Registries"
}

variable "configs" {
Expand All @@ -43,30 +43,49 @@ variable "configs" {
description = "Cluster Configs"
}

variable "registries" {
type = any
variable "addons" {
type = map(string)
default = {}
sensitive = false
description = "Cluster Registries"
description = "Cluster AddOns"
}

variable "server_ip" {
type = string
default = null
default = ""
sensitive = false
description = "Cluster Server IP"
}

variable "masters" {
type = map(any)
variable "external_db" {
type = string
default = ""
sensitive = false
description = "Cluster External DB"
}

variable "servers" {
type = map(object({
connection = any
channel = optional(string)
version = optional(string)
registries = optional(any, {})
configs = optional(any, {})
}))
default = {}
sensitive = false
description = "Cluster Masters"
description = "Cluster Servers"
}

variable "workers" {
type = map(any)
variable "agents" {
type = map(object({
connection = any
channel = optional(string)
version = optional(string)
registries = optional(any, {})
configs = optional(any, {})
}))
default = {}
sensitive = false
description = "Cluster Workers"
description = "Cluster Agents"
}

0 comments on commit 283c979

Please sign in to comment.