# Generate Talos machine configurations

locals {
  pod_cidr           = var.cluster_config.pod_subnets[0]
  service_cidr       = var.cluster_config.service_subnets[0]
  controlplane_ip    = var.cluster_config.controlplane_ip
  cluster_dns_ip     = "10.96.0.10"
  kubelet_image      = "ghcr.io/siderolabs/kubelet:${var.kubernetes_version}"
  kube_apiserver_img = "registry.k8s.io/kube-apiserver:${var.kubernetes_version}"
  controller_mgr_img = "registry.k8s.io/kube-controller-manager:${var.kubernetes_version}"
  kube_proxy_img     = "registry.k8s.io/kube-proxy:${var.kubernetes_version}"
  scheduler_img      = "registry.k8s.io/kube-scheduler:${var.kubernetes_version}"

  factory_image = "factory.talos.dev/installer/613e1592b2da41ae5e265e8789429f22e121aab91cb4deb6bc3c0b6262961245:${var.talos_version}"
}

# Control plane machine configurations
resource "local_file" "controlplane_configs" {
  for_each = var.controlplane_configs

  filename = "${path.module}/../cluster-config/${each.key}.yaml"

  content = templatefile("${path.module}/templates/controlplane.tftpl", {
    version             = "v1alpha1"
    hostname            = each.value.hostname
    token               = var.machine_token
    ca_crt              = var.machine_ca_crt
    ca_key              = var.machine_ca_key
    lan_ip              = each.value.lan_ip
    lan_subnet          = each.value.lan_subnet
    lan_gateway         = each.value.lan_gateway
    wg0_ip              = each.value.wg0_ip
    wg0_subnet          = each.value.wg0_subnet
    wg0_port            = each.value.wg0_port
    wg0_private_key     = each.value.wg0_private_key
    wg0_peers           = each.value.wg0_peers
    wg1_ip              = each.value.wg1_ip
    wg1_subnet          = each.value.wg1_subnet
    wg1_port            = each.value.wg1_port
    wg1_private_key     = each.value.wg1_private_key
    wg1_peers           = each.value.wg1_peers
    kubelet_image       = local.kubelet_image
    cluster_dns_ip      = local.cluster_dns_ip
    install_disk        = each.value.install_disk
    factory_image       = local.factory_image
    talos_version       = var.talos_version
    longhorn_disks      = each.value.longhorn_disks
    dns_servers         = var.cluster_config.dns_servers
    forgejo_registry_ip = var.forgejo_registry_ip
    forgejo_hostname    = var.forgejo_hostname
    zone                = each.value.zone
    allow_scheduling    = each.value.allow_scheduling

    # Cluster config
    cluster_id      = var.cluster_id
    cluster_secret  = var.cluster_secret
    controlplane_ip = local.controlplane_ip
    cluster_name    = var.cluster_name
    pod_subnets     = var.cluster_config.pod_subnets
    service_subnets = var.cluster_config.service_subnets
    dns_domain      = var.cluster_config.dns_domain
    bootstrap_token = var.bootstrap_token

    # Kubernetes certs
    kubernetes_ca_crt           = var.kubernetes_ca_crt
    kubernetes_ca_key           = var.kubernetes_ca_key
    etcd_ca_crt                 = var.etcd_ca_crt
    etcd_ca_key                 = var.etcd_ca_key
    aggregator_ca_crt           = var.aggregator_ca_crt
    aggregator_ca_key           = var.aggregator_ca_key
    service_account_key         = var.service_account_key
    secretbox_encryption_secret = var.secretbox_encryption_secret

    # Component images
    kube_apiserver_img = local.kube_apiserver_img
    controller_mgr_img = local.controller_mgr_img
    kube_proxy_img     = local.kube_proxy_img
    scheduler_img      = local.scheduler_img
  })
}

# Output paths for reference
output "controlplane_config_paths" {
  value = {
    for k, v in local_file.controlplane_configs : k => v.filename
  }
  description = "Paths to generated controlplane configs"
}
