diff --git a/terraform/infra/dev/terraform.tfvars b/terraform/infra/dev/terraform.tfvars index 35bdc9a..8892d54 100644 --- a/terraform/infra/dev/terraform.tfvars +++ b/terraform/infra/dev/terraform.tfvars @@ -1,27 +1,27 @@ # K3s Server Configuration server_ip = "reg.benadis.org" # Replace with your server IP -worker_ips = [ # Replace with your worker IPs - "worker1.benadis.org", +worker_ips = [ # Replace with your worker IPs + # "worker1.benadis.org", "worker2.benadis.org" ] -ssh_user = "root" # Replace with your SSH username +ssh_user = "root" # Replace with your SSH username ssh_private_key = "~/.ssh/id_rsa" # Path to your private SSH key replace_url = "reg.benadis.org" # Optional: URL to replace in kubeconfig, leave empty to use server_ip # Cluster Configuration -domain = "reg.benadis.org" # Your domain name -k3s_version = "latest" # K3s version to install -k3s_channel = "stable" # K3s release channel -kubeconfig_path = "./k3s.yaml" # Where to save kubeconfig +domain = "reg.benadis.org" # Your domain name +k3s_version = "v1.32.3+k3s1" # K3s version to install +k3s_channel = "stable" # K3s release channel +kubeconfig_path = "./k3s.yaml" # Where to save kubeconfig node_token_path = "./tmp/node-token" # Where to save node token # Optional Features -enable_traefik_dashboard = false # Enable Traefik dashboard -enable_ssl = false # Enable SSL -ssl_cert_path = "~/.tls/wildcard.benadis.org.crt" # Path to SSL certificate -ssl_key_path = "~/.tls/wildcard.benadis.org.key" # Path to SSL key -install_argocd = false # Install ArgoCD -enable_ssh_tunnel = false # Enable SSH tunneling +enable_traefik_dashboard = false # Enable Traefik dashboard +enable_ssl = false # Enable SSL +ssl_cert_path = "~/.tls/wildcard.benadis.org.crt" # Path to SSL certificate +ssl_key_path = "~/.tls/wildcard.benadis.org.key" # Path to SSL key +install_argocd = false # Install ArgoCD +enable_ssh_tunnel = false # Enable SSH tunneling # Advanced Configuration k3s_extra_server_args = "" # Extra args for K3s server diff --git a/terraform/infra/dev/variables.tf b/terraform/infra/dev/variables.tf index ee9d96a..7a9f36b 100644 --- a/terraform/infra/dev/variables.tf +++ b/terraform/infra/dev/variables.tf @@ -35,7 +35,7 @@ variable "domain" { variable "k3s_version" { description = "Version of K3s to install" type = string - default = "v1.27.3+k3s1" + default = "v1.32.3+k3s1" } variable "k3s_channel" { @@ -47,7 +47,7 @@ variable "k3s_channel" { variable "kubeconfig_path" { description = "Path where to save the kubeconfig file locally" type = string - default = "~/.kube/config" + default = "./k3s.yaml" } variable "node_token_path" { diff --git a/terraform/modules/k3s-install/main.tf b/terraform/modules/k3s-install/main.tf index 228873d..6526728 100644 --- a/terraform/modules/k3s-install/main.tf +++ b/terraform/modules/k3s-install/main.tf @@ -13,8 +13,8 @@ terraform { } locals { - ssh_config_path = "${path.module}/ssh_config" - temporary_dir = "/tmp/k3s-terraform" + temporary_dir = "/tmp" + ssh_config_path = "${local.temporary_dir}/ssh_config" server_kubeconfig = "${local.temporary_dir}/k3s.yaml" node_token_path = "${local.temporary_dir}/node-token" worker_count = length(var.worker_ips) @@ -23,16 +23,25 @@ locals { # Create a temporary SSH config file for secure connections resource "null_resource" "setup_ssh_config" { triggers = { - server_ip = var.server_ip - worker_ips = join(",", var.worker_ips) - ssh_user = var.ssh_user + server_ip = var.server_ip + worker_ips = join(",", var.worker_ips) + ssh_user = var.ssh_user ssh_private_key = var.ssh_private_key ssh_config_path = local.ssh_config_path } provisioner "local-exec" { command = <<-EOT - mkdir -p ${dirname(local.ssh_config_path)} + # Check if directory exists, create only if it doesn't + SSH_CONFIG_DIR=$(dirname "${local.ssh_config_path}") + if [ ! -d "$SSH_CONFIG_DIR" ]; then + echo "Creating directory $SSH_CONFIG_DIR" + mkdir -p "$SSH_CONFIG_DIR" + else + echo "Directory $SSH_CONFIG_DIR already exists" + fi + + # Create SSH config file cat > ${local.ssh_config_path} << 'EOF' Host ${var.server_ip} User ${var.ssh_user} @@ -41,7 +50,7 @@ Host ${var.server_ip} UserKnownHostsFile /dev/null ${join("\n", [ - for ip in var.worker_ips : <<-WORKER + for ip in var.worker_ips : <<-WORKER Host ${ip} User ${var.ssh_user} IdentityFile ${var.ssh_private_key} @@ -51,16 +60,16 @@ WORKER ])} EOF EOT - interpreter = ["bash", "-c"] - } +interpreter = ["bash", "-c"] +} - # Clean up SSH config on destroy - provisioner "local-exec" { - when = destroy - command = "rm -f ${self.triggers.ssh_config_path}" - interpreter = ["bash", "-c"] - on_failure = continue - } +# Clean up SSH config on destroy +provisioner "local-exec" { + when = destroy + command = "rm -f ${self.triggers.ssh_config_path}" + interpreter = ["bash", "-c"] + on_failure = continue +} } # Install K3s on the server node @@ -87,7 +96,7 @@ resource "null_resource" "install_k3s_server" { } inline = [ - "mkdir -p ${local.temporary_dir}", + "if [ ! -d \"${local.temporary_dir}\" ]; then mkdir -p ${local.temporary_dir}; else echo \"Directory ${local.temporary_dir} already exists\"; fi", "curl -sfL https://get.k3s.io | INSTALL_K3S_VERSION=${var.k3s_version} INSTALL_K3S_CHANNEL=${var.k3s_channel} sh -s - server ${var.k3s_extra_server_args}", "until systemctl is-active --quiet k3s; do echo 'Waiting for k3s to start...'; sleep 5; done", "echo 'K3s server installation complete'" @@ -133,7 +142,8 @@ resource "null_resource" "get_k3s_config" { } inline = [ - "mkdir -p ${local.temporary_dir}", + "if [ ! -d \"${local.temporary_dir}\" ]; then mkdir -p ${local.temporary_dir}; else echo \"Directory ${local.temporary_dir} already exists\"; fi", + "if [ ! -d \"$(dirname ${local.server_kubeconfig})\" ]; then mkdir -p $(dirname ${local.server_kubeconfig}); else echo \"Directory $(dirname ${local.server_kubeconfig}) already exists\"; fi", "sudo cp /etc/rancher/k3s/k3s.yaml ${local.server_kubeconfig}", "sudo chmod 644 ${local.server_kubeconfig}" ] @@ -141,18 +151,31 @@ resource "null_resource" "get_k3s_config" { # Download kubeconfig to local machine provisioner "local-exec" { - command = "mkdir -p ${dirname(var.kubeconfig_path)} && scp -F ${local.ssh_config_path} ${var.ssh_user}@${var.server_ip}:${local.server_kubeconfig} ${var.kubeconfig_path}" + command = <<-EOT + # Check if directory exists, create only if it doesn't + KUBECONFIG_DIR=$(dirname "${var.kubeconfig_path}") + if [ ! -d "$KUBECONFIG_DIR" ]; then + echo "Creating directory $KUBECONFIG_DIR" + mkdir -p "$KUBECONFIG_DIR" + else + echo "Directory $KUBECONFIG_DIR already exists" + fi + + # Copy the kubeconfig file + scp -F ${local.ssh_config_path} ${var.ssh_user}@${var.server_ip}:${local.server_kubeconfig} ${var.kubeconfig_path} + EOT + interpreter = ["bash", "-c"] } # Update server URL in kubeconfig if needed provisioner "local-exec" { - command = <<-EOT + command = <<-EOT if [ -n "${var.replace_url}" ]; then sed -i 's|https://127.0.0.1:6443|https://${var.replace_url}:6443|g' ${var.kubeconfig_path} fi EOT interpreter = ["bash", "-c"] - on_failure = continue + on_failure = continue } } @@ -177,7 +200,7 @@ resource "null_resource" "get_k3s_token" { } inline = [ - "mkdir -p ${local.temporary_dir}", + "if [ ! -d \"$(dirname ${local.node_token_path})\" ]; then mkdir -p $(dirname ${local.node_token_path}); else echo \"Directory $(dirname ${local.node_token_path}) already exists\"; fi", "sudo cat /var/lib/rancher/k3s/server/node-token > ${local.node_token_path}", "sudo chmod 644 ${local.node_token_path}" ] @@ -185,7 +208,20 @@ resource "null_resource" "get_k3s_token" { # Download node token to local machine provisioner "local-exec" { - command = "mkdir -p ${dirname(var.node_token_path)} && scp -F ${local.ssh_config_path} ${var.ssh_user}@${var.server_ip}:${local.node_token_path} ${var.node_token_path}" + command = <<-EOT + # Check if directory exists, create only if it doesn't + TOKEN_DIR=$(dirname "${var.node_token_path}") + if [ ! -d "$TOKEN_DIR" ]; then + echo "Creating directory $TOKEN_DIR" + mkdir -p "$TOKEN_DIR" + else + echo "Directory $TOKEN_DIR already exists" + fi + + # Copy the node token file + scp -F ${local.ssh_config_path} ${var.ssh_user}@${var.server_ip}:${local.node_token_path} ${var.node_token_path} + EOT + interpreter = ["bash", "-c"] } } @@ -212,7 +248,7 @@ resource "null_resource" "copy_token_to_workers" { } inline = [ - "mkdir -p ${local.temporary_dir}" + "if [ ! -d \"${local.temporary_dir}\" ]; then mkdir -p ${local.temporary_dir}; fi" ] }