Skip to content

Commit 414c765

Browse files
committed
fix(iac): K3s - fix kubeconfig extraction when run remotely
1 parent f330d35 commit 414c765

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
resource "null_resource" "copy_kubeconfig" {
2+
depends_on = [openstack_compute_instance_v2.kubernetes_server]
3+
4+
provisioner "local-exec" {
5+
# Copy the kubeconfig file from the host to a local file using SCP.
6+
# Use ssh-keyscan to prevent interactive prompt on unknown host
7+
# Use sed to replace the localhost address in the KUBECONFIG file with the actual IP adddress of the created VM.
8+
command = <<EOT
9+
mkdir -p ${path.root}/.build/ && \
10+
ssh-keyscan -H ${openstack_compute_instance_v2.kubernetes_server.access_ip_v4} >> ${path.root}/.build/.known_hosts_cogstack && \
11+
scp -o UserKnownHostsFile=${path.root}/.build/.known_hosts_cogstack -o StrictHostKeyChecking=yes \
12+
-i ${local.ssh_keys.private_key_file} \
13+
ubuntu@${openstack_compute_instance_v2.kubernetes_server.access_ip_v4}:/etc/rancher/k3s/k3s.yaml \
14+
${local.kubeconfig_file} && \
15+
sed -i "s/127.0.0.1/${openstack_compute_instance_v2.kubernetes_server.access_ip_v4}/" ${local.kubeconfig_file}
16+
EOT
17+
}
18+
}
19+
20+
data "local_file" "kube_config_raw_file" {
21+
filename = local.kubeconfig_file
22+
depends_on = [ null_resource.copy_kubeconfig ]
23+
}
24+
25+
output "kube_config_raw" {
26+
value = data.local_file.kube_config_raw_file
27+
description = "Kubeconfig for this cluster"
28+
}

0 commit comments

Comments
 (0)