From 6739b4a493af4372e8cfca082f40f8d00246cf25 Mon Sep 17 00:00:00 2001 From: Karim Naufal Date: Thu, 24 Feb 2022 01:44:56 +0100 Subject: [PATCH 1/3] added null ressouce to destroy lb --- main.tf | 17 +++++++++++++++++ modules/host/locals.tf | 1 + 2 files changed, 18 insertions(+) diff --git a/main.tf b/main.tf index 3e71a22..61bcc8b 100644 --- a/main.tf +++ b/main.tf @@ -158,3 +158,20 @@ data "hcloud_load_balancer" "traefik" { depends_on = [null_resource.kustomization] } + +resource "null_resource" "destroy_lb" { + triggers = { + token = random_password.k3s_token.result + } + + # Important when issuing terraform destroy, otherwise the LB will not let the network get deleted + provisioner "local-exec" { + when = destroy + command = <<-EOT + hcloud load-balancer delete traefik + hcloud network delete k3s + EOT + + on_failure = continue + } +} diff --git a/modules/host/locals.tf b/modules/host/locals.tf index 5a61b2a..4e32a8c 100644 --- a/modules/host/locals.tf +++ b/modules/host/locals.tf @@ -66,6 +66,7 @@ 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 +exit 0 EOF } From 115163b8be675ef6533243db6b796c1c55a166e1 Mon Sep 17 00:00:00 2001 From: Karim Naufal Date: Fri, 25 Feb 2022 00:21:28 +0100 Subject: [PATCH 2/3] traefik deletes successfully --- main.tf | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/main.tf b/main.tf index 61bcc8b..cbab0cf 100644 --- a/main.tf +++ b/main.tf @@ -159,19 +159,29 @@ data "hcloud_load_balancer" "traefik" { depends_on = [null_resource.kustomization] } -resource "null_resource" "destroy_lb" { + +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 = { - token = random_password.k3s_token.result + 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 - hcloud load-balancer delete traefik - hcloud network delete k3s + 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 + ] } From f0fb1adc6d532c79d496c69a440268b7f6c7d1c5 Mon Sep 17 00:00:00 2001 From: Karim Naufal Date: Fri, 25 Feb 2022 01:51:01 +0100 Subject: [PATCH 3/3] small fix --- modules/host/locals.tf | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/modules/host/locals.tf b/modules/host/locals.tf index 4e32a8c..0547e70 100644 --- a/modules/host/locals.tf +++ b/modules/host/locals.tf @@ -65,8 +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 -exit 0 +udevadm settle || true EOF }