Merge pull request #97 from kube-hetzner/auto-destroy

Auto destroy LB on terraform destroy
This commit is contained in:
Karim Naufal 2022-02-25 01:54:57 +01:00 committed by GitHub
commit 9a88433059
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 1 deletions

27
main.tf
View File

@ -158,3 +158,30 @@ data "hcloud_load_balancer" "traefik" {
depends_on = [null_resource.kustomization]
}
resource "null_resource" "destroy_traefik_loadbalancer" {
# this only gets triggered before total destruction of the cluster, but when the necessary elements to run the commands are still available
triggers = {
kustomization_id = null_resource.kustomization.id
}
# Important when issuing terraform destroy, otherwise the LB will not let the network get deleted
provisioner "local-exec" {
when = destroy
command = <<-EOT
kubectl -n kube-system delete service traefik --kubeconfig ${path.module}/kubeconfig.yaml
EOT
on_failure = continue
}
depends_on = [
local_file.kubeconfig,
null_resource.control_planes[0],
hcloud_network_subnet.k3s,
hcloud_network.k3s,
hcloud_firewall.k3s,
hcloud_placement_group.k3s,
hcloud_ssh_key.k3s
]
}

View File

@ -65,7 +65,7 @@ locals {
rpm --import https://rpm.rancher.io/public.key
zypper refresh
zypper --gpg-auto-import-keys install -y https://rpm.rancher.io/k3s/stable/common/microos/noarch/k3s-selinux-0.4-1.sle.noarch.rpm
udevadm settle
udevadm settle || true
EOF
}