2024-07-08 20:27:08 +00:00
|
|
|
locals {
|
2024-07-16 19:13:04 +00:00
|
|
|
cluster_api_domain = "${var.cluster.api.subdomain}.${var.cluster.domain}"
|
|
|
|
cluster_endpoint = "https://${local.cluster_api_domain}:${var.cluster.api.port}"
|
2024-07-08 20:27:08 +00:00
|
|
|
|
2024-07-13 22:17:47 +00:00
|
|
|
podSubnets = [
|
|
|
|
"10.200.0.0/16",
|
2024-07-25 15:42:08 +00:00
|
|
|
"fd9b:5314:fc70::/56",
|
2024-07-13 22:17:47 +00:00
|
|
|
]
|
|
|
|
serviceSubnets = [
|
|
|
|
"10.201.0.0/16",
|
2024-07-25 15:42:08 +00:00
|
|
|
"fd9b:5314:fc71::/112",
|
2024-07-13 22:17:47 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
# Nodes will use IPs from this subnets
|
|
|
|
# for communication between each other.
|
|
|
|
tailscaleSubnets = [
|
|
|
|
"100.64.0.0/10",
|
|
|
|
"fd7a:115c:a1e0::/96"
|
|
|
|
]
|
2024-07-16 19:13:04 +00:00
|
|
|
|
2024-07-17 19:43:11 +00:00
|
|
|
talos_controlplane_config = {
|
2024-07-25 15:42:08 +00:00
|
|
|
machine = {
|
|
|
|
features = {
|
|
|
|
rbac = true
|
|
|
|
apidCheckExtKeyUsage = true
|
|
|
|
kubernetesTalosAPIAccess = {
|
|
|
|
enabled = true
|
|
|
|
allowedRoles = [
|
2024-07-20 11:09:30 +00:00
|
|
|
"os:reader"
|
|
|
|
]
|
2024-07-25 15:42:08 +00:00
|
|
|
allowedKubernetesNamespaces = [
|
2024-07-20 11:09:30 +00:00
|
|
|
"kube-system"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-07-25 15:42:08 +00:00
|
|
|
cluster = {
|
|
|
|
etcd = {
|
|
|
|
advertisedSubnets = local.tailscaleSubnets
|
|
|
|
listenSubnets = local.tailscaleSubnets
|
2024-07-17 19:43:11 +00:00
|
|
|
}
|
2024-07-25 15:42:08 +00:00
|
|
|
allowSchedulingOnControlPlanes = var.allow_scheduling_on_control_planes,
|
|
|
|
apiServer = {
|
|
|
|
extraArgs = {
|
|
|
|
"oidc-issuer-url" = "https://id.tjo.space/application/o/k8stjocloud/",
|
|
|
|
"oidc-client-id" = "HAI6rW0EWtgmSPGKAJ3XXzubQTUut2GMeTRS2spg",
|
|
|
|
"oidc-username-claim" = "sub",
|
|
|
|
"oidc-username-prefix" = "oidc:",
|
|
|
|
"oidc-groups-claim" = "groups",
|
|
|
|
"oidc-groups-prefix" = "oidc:groups:",
|
2024-07-17 19:43:11 +00:00
|
|
|
}
|
|
|
|
}
|
2024-07-25 15:42:08 +00:00
|
|
|
inlineManifests = [
|
2024-07-17 19:43:11 +00:00
|
|
|
{
|
2024-07-25 15:42:08 +00:00
|
|
|
name = "proxmox-cloud-controller-manager"
|
|
|
|
contents = data.helm_template.proxmox-ccm.manifest
|
2024-07-20 11:09:30 +00:00
|
|
|
},
|
|
|
|
{
|
2024-07-25 15:42:08 +00:00
|
|
|
name = "talos-cloud-controller-manager"
|
|
|
|
contents = data.helm_template.talos-ccm.manifest
|
2024-07-20 11:09:30 +00:00
|
|
|
},
|
|
|
|
{
|
2024-07-25 15:42:08 +00:00
|
|
|
name = "promxmox-csi-plugin"
|
|
|
|
contents = data.helm_template.proxmox-csi.manifest
|
2024-07-20 11:09:30 +00:00
|
|
|
},
|
|
|
|
{
|
2024-07-25 15:42:08 +00:00
|
|
|
name = "gateway-api-crds"
|
|
|
|
contents = file("${path.module}/manifests/gateway-api.crds.yaml")
|
2024-07-20 11:09:30 +00:00
|
|
|
},
|
|
|
|
{
|
2024-07-25 15:42:08 +00:00
|
|
|
name = "cilium"
|
|
|
|
contents = data.helm_template.cilium.manifest
|
2024-07-20 11:09:30 +00:00
|
|
|
},
|
|
|
|
{
|
2024-07-25 15:42:08 +00:00
|
|
|
name = "oidc-admins"
|
|
|
|
contents = <<-EOF
|
2024-07-17 19:43:11 +00:00
|
|
|
apiVersion: rbac.authorization.k8s.io/v1
|
|
|
|
kind: ClusterRoleBinding
|
|
|
|
metadata:
|
|
|
|
name: id-tjo-space:admins
|
|
|
|
subjects:
|
|
|
|
- kind: Group
|
|
|
|
name: oidc:groups:k8s.tjo.cloud admin
|
|
|
|
apiGroup: rbac.authorization.k8s.io
|
|
|
|
roleRef:
|
|
|
|
kind: ClusterRole
|
|
|
|
name: cluster-admin
|
|
|
|
apiGroup: rbac.authorization.k8s.io
|
|
|
|
EOF
|
|
|
|
},
|
|
|
|
]
|
|
|
|
}
|
2024-07-08 20:27:08 +00:00
|
|
|
}
|
|
|
|
|
2024-07-17 19:43:11 +00:00
|
|
|
talos_worker_config = {
|
2024-07-25 15:42:08 +00:00
|
|
|
cluster = {
|
|
|
|
externalCloudProvider = {
|
|
|
|
enabled = true
|
2024-07-20 11:09:30 +00:00
|
|
|
}
|
2024-07-25 15:42:08 +00:00
|
|
|
controlPlane = {
|
|
|
|
endpoint = local.cluster_endpoint
|
|
|
|
localAPIServerPort = var.cluster.api.port
|
2024-07-17 19:43:11 +00:00
|
|
|
}
|
2024-07-25 15:42:08 +00:00
|
|
|
network = {
|
|
|
|
cni = {
|
|
|
|
name = "none"
|
2024-07-08 20:27:08 +00:00
|
|
|
}
|
2024-07-25 15:42:08 +00:00
|
|
|
podSubnets = local.podSubnets
|
|
|
|
serviceSubnets = local.serviceSubnets
|
2024-07-17 19:43:11 +00:00
|
|
|
}
|
2024-07-25 15:42:08 +00:00
|
|
|
proxy = {
|
|
|
|
disabled = true
|
2024-07-08 20:27:08 +00:00
|
|
|
}
|
|
|
|
}
|
2024-07-17 19:43:11 +00:00
|
|
|
machine = {
|
|
|
|
kubelet = {
|
2024-07-25 15:42:08 +00:00
|
|
|
nodeIP = {
|
|
|
|
validSubnets = local.tailscaleSubnets
|
2024-07-17 19:43:11 +00:00
|
|
|
}
|
2024-07-25 15:42:08 +00:00
|
|
|
extraArgs = {
|
|
|
|
rotate-server-certificates = true
|
|
|
|
cloud-provider = "external"
|
2024-07-08 20:27:08 +00:00
|
|
|
}
|
|
|
|
}
|
2024-07-17 19:43:11 +00:00
|
|
|
install = {
|
|
|
|
image = "factory.talos.dev/installer/${var.talos.schematic_id}:${var.talos.version}"
|
|
|
|
disk = "/dev/vda"
|
|
|
|
}
|
2024-07-08 20:27:08 +00:00
|
|
|
}
|
|
|
|
}
|
2024-07-19 20:48:07 +00:00
|
|
|
|
|
|
|
talos_node_config = {
|
|
|
|
for k, node in local.nodes_with_address : k => [
|
|
|
|
yamlencode({
|
|
|
|
machine = {
|
|
|
|
network = {
|
|
|
|
hostname = node.name
|
2024-07-25 15:42:08 +00:00
|
|
|
kubespan = {
|
|
|
|
enabled = false
|
|
|
|
}
|
2024-07-19 20:48:07 +00:00
|
|
|
}
|
|
|
|
nodeLabels = {
|
2024-07-22 20:31:48 +00:00
|
|
|
"k8s.tjo.cloud/public" = node.public ? "true" : "false"
|
2024-07-19 20:48:07 +00:00
|
|
|
"k8s.tjo.cloud/host" = node.host
|
|
|
|
"k8s.tjo.cloud/proxmox" = var.proxmox.name
|
2024-07-21 10:27:40 +00:00
|
|
|
}
|
2024-07-23 18:42:22 +00:00
|
|
|
sysctls = {
|
|
|
|
"net.ipv4.ip_forward" = "1"
|
|
|
|
"net.ipv6.conf.all.forwarding" = "1"
|
|
|
|
}
|
2024-07-19 20:48:07 +00:00
|
|
|
}
|
|
|
|
}),
|
|
|
|
yamlencode(
|
|
|
|
{
|
2024-07-25 15:42:08 +00:00
|
|
|
apiVersion = "v1alpha1"
|
|
|
|
kind = "ExtensionServiceConfig"
|
|
|
|
name = "tailscale"
|
|
|
|
environment = [
|
2024-07-19 20:48:07 +00:00
|
|
|
"TS_AUTHKEY=${var.tailscale_authkey}",
|
|
|
|
"TS_HOSTNAME=${node.name}",
|
2024-07-23 18:42:22 +00:00
|
|
|
"TS_ROUTES=${join(",", local.podSubnets)},${join(",", local.serviceSubnets)}",
|
2024-07-25 15:42:08 +00:00
|
|
|
#"TS_EXTRA_ARGS=--accept-routes --snat-subnet-routes",
|
2024-07-19 20:48:07 +00:00
|
|
|
]
|
|
|
|
})
|
|
|
|
]
|
|
|
|
}
|
2024-07-08 20:27:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "digitalocean_record" "controlplane-A" {
|
2024-07-17 19:43:11 +00:00
|
|
|
for_each = { for k, node in local.nodes_with_address : k => node if node.type == "controlplane" }
|
2024-07-08 20:27:08 +00:00
|
|
|
|
2024-07-14 10:19:37 +00:00
|
|
|
domain = var.cluster.domain
|
2024-07-08 20:27:08 +00:00
|
|
|
type = "A"
|
2024-07-16 19:13:04 +00:00
|
|
|
name = var.cluster.api.subdomain
|
2024-07-17 19:43:11 +00:00
|
|
|
value = each.value.ipv4
|
2024-07-08 20:27:08 +00:00
|
|
|
ttl = 30
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "digitalocean_record" "controlplane-AAAA" {
|
2024-07-17 19:43:11 +00:00
|
|
|
for_each = { for k, node in local.nodes_with_address : k => node if node.type == "controlplane" }
|
2024-07-08 20:27:08 +00:00
|
|
|
|
2024-07-14 10:19:37 +00:00
|
|
|
domain = var.cluster.domain
|
2024-07-08 20:27:08 +00:00
|
|
|
type = "AAAA"
|
2024-07-16 19:13:04 +00:00
|
|
|
name = var.cluster.api.subdomain
|
2024-07-17 19:43:11 +00:00
|
|
|
value = each.value.ipv6
|
2024-07-08 20:27:08 +00:00
|
|
|
ttl = 30
|
|
|
|
}
|
|
|
|
|
2024-07-19 20:48:07 +00:00
|
|
|
resource "talos_machine_secrets" "this" {
|
|
|
|
talos_version = var.talos.version
|
|
|
|
}
|
2024-07-08 20:27:08 +00:00
|
|
|
|
|
|
|
data "talos_machine_configuration" "controlplane" {
|
2024-07-14 10:19:37 +00:00
|
|
|
cluster_name = var.cluster.name
|
2024-07-08 20:27:08 +00:00
|
|
|
machine_type = "controlplane"
|
|
|
|
cluster_endpoint = local.cluster_endpoint
|
|
|
|
machine_secrets = talos_machine_secrets.this.machine_secrets
|
|
|
|
|
2024-07-17 19:43:11 +00:00
|
|
|
talos_version = var.talos.version
|
|
|
|
kubernetes_version = var.talos.kubernetes
|
2024-07-13 22:17:47 +00:00
|
|
|
|
2024-07-08 20:27:08 +00:00
|
|
|
depends_on = [
|
|
|
|
digitalocean_record.controlplane-A,
|
|
|
|
digitalocean_record.controlplane-AAAA,
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
data "talos_machine_configuration" "worker" {
|
2024-07-14 10:19:37 +00:00
|
|
|
cluster_name = var.cluster.name
|
2024-07-08 20:27:08 +00:00
|
|
|
machine_type = "worker"
|
|
|
|
cluster_endpoint = local.cluster_endpoint
|
|
|
|
machine_secrets = talos_machine_secrets.this.machine_secrets
|
|
|
|
|
2024-07-17 19:43:11 +00:00
|
|
|
talos_version = var.talos.version
|
|
|
|
kubernetes_version = var.talos.kubernetes
|
2024-07-13 22:17:47 +00:00
|
|
|
|
2024-07-08 20:27:08 +00:00
|
|
|
depends_on = [
|
|
|
|
digitalocean_record.controlplane-A,
|
|
|
|
digitalocean_record.controlplane-AAAA
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
2024-07-10 21:13:36 +00:00
|
|
|
resource "talos_machine_configuration_apply" "controlplane" {
|
|
|
|
for_each = { for k, v in local.nodes_with_address : k => v if v.type == "controlplane" }
|
2024-07-08 20:27:08 +00:00
|
|
|
|
|
|
|
client_configuration = talos_machine_secrets.this.client_configuration
|
2024-07-10 21:13:36 +00:00
|
|
|
machine_configuration_input = data.talos_machine_configuration.controlplane.machine_configuration
|
2024-07-08 20:27:08 +00:00
|
|
|
|
|
|
|
node = each.value.name
|
2024-07-16 19:13:04 +00:00
|
|
|
endpoint = each.value.ipv4
|
2024-07-08 20:27:08 +00:00
|
|
|
|
2024-07-14 10:19:37 +00:00
|
|
|
apply_mode = "reboot"
|
2024-07-10 21:13:36 +00:00
|
|
|
|
2024-07-19 20:48:07 +00:00
|
|
|
config_patches = concat(
|
|
|
|
[
|
|
|
|
yamlencode(local.talos_worker_config),
|
|
|
|
yamlencode(local.talos_controlplane_config)
|
|
|
|
],
|
|
|
|
local.talos_node_config[each.key]
|
|
|
|
)
|
2024-07-10 21:13:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "talos_machine_configuration_apply" "worker" {
|
|
|
|
for_each = { for k, v in local.nodes_with_address : k => v if v.type == "worker" }
|
|
|
|
|
|
|
|
client_configuration = talos_machine_secrets.this.client_configuration
|
|
|
|
machine_configuration_input = data.talos_machine_configuration.worker.machine_configuration
|
|
|
|
|
|
|
|
node = each.value.name
|
2024-07-16 19:13:04 +00:00
|
|
|
endpoint = each.value.ipv4
|
2024-07-10 21:13:36 +00:00
|
|
|
|
2024-07-14 10:19:37 +00:00
|
|
|
apply_mode = "reboot"
|
2024-07-10 21:13:36 +00:00
|
|
|
|
2024-07-19 20:48:07 +00:00
|
|
|
config_patches = concat(
|
|
|
|
[
|
|
|
|
yamlencode(local.talos_worker_config)
|
|
|
|
],
|
|
|
|
local.talos_node_config[each.key]
|
|
|
|
)
|
2024-07-08 20:27:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
resource "talos_machine_bootstrap" "this" {
|
|
|
|
depends_on = [
|
2024-07-10 21:13:36 +00:00
|
|
|
talos_machine_configuration_apply.controlplane,
|
|
|
|
talos_machine_configuration_apply.worker
|
2024-07-08 20:27:08 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
node = local.first_controlplane_node.name
|
2024-07-16 19:13:04 +00:00
|
|
|
endpoint = local.first_controlplane_node.ipv4
|
2024-07-08 20:27:08 +00:00
|
|
|
client_configuration = talos_machine_secrets.this.client_configuration
|
|
|
|
}
|
|
|
|
|
|
|
|
data "talos_cluster_kubeconfig" "this" {
|
|
|
|
depends_on = [
|
|
|
|
talos_machine_bootstrap.this
|
|
|
|
]
|
|
|
|
|
|
|
|
client_configuration = talos_machine_secrets.this.client_configuration
|
2024-07-16 19:13:04 +00:00
|
|
|
node = local.first_controlplane_node.ipv4
|
2024-07-08 20:27:08 +00:00
|
|
|
}
|
2024-07-19 20:48:07 +00:00
|
|
|
|
|
|
|
resource "local_file" "kubeconfig" {
|
|
|
|
content = data.talos_cluster_kubeconfig.this.kubeconfig_raw
|
|
|
|
filename = "${path.root}/admin.kubeconfig"
|
2024-07-27 09:31:15 +00:00
|
|
|
|
|
|
|
lifecycle {
|
|
|
|
ignore_changes = [content]
|
|
|
|
}
|
2024-07-19 20:48:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
data "talos_client_configuration" "this" {
|
|
|
|
count = length(values({ for k, v in local.nodes_with_address : k => v if v.type == "controlplane" })) > 0 ? 1 : 0
|
|
|
|
|
|
|
|
cluster_name = var.cluster.name
|
|
|
|
client_configuration = talos_machine_secrets.this.client_configuration
|
|
|
|
endpoints = values({ for k, v in local.nodes_with_address : k => v if v.type == "controlplane" })[*].ipv4
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "local_file" "talosconfig" {
|
|
|
|
count = length(values({ for k, v in local.nodes : k => v if v.type == "controlplane" })) > 0 ? 1 : 0
|
|
|
|
|
|
|
|
content = nonsensitive(data.talos_client_configuration.this[0].talos_config)
|
|
|
|
filename = "${path.root}/admin.talosconfig"
|
|
|
|
}
|