通过 Terraform 的本地执行器部署的 Kubernetes 服务在应用"terraform 销毁"时不会销毁



我想通过Terraform模板化我的Kubernetes清单,并从local-exec提供器执行kubectl apply

一切似乎都很顺利,直到我意识到资源(服务和部署(实际上并没有被terraform apply破坏。

清单以这种方式模板化和应用:

data "template_file" "service_template" {
count    = length(var.services)
template = file("${path.module}/templates/${lookup(var.services[count.index], "name")}.tpl")

vars = {
cluster_name = var.cluster_name
tag          = lookup(var.services[count.index], "tag")
}
}
resource "local_file" "template" {
count    = length(var.services)
content  = data.template_file.service_template[count.index].rendered
filename = "${path.module}/deployments/${lookup(var.services[count.index], "name")}.yaml"
}

resource "null_resource" "apply" {
count    = length(var.services)
provisioner "local-exec" {
command = "kubectl apply -f ${path.module}/deployments/${lookup(var.services[count.index], "name")}.yaml --kubeconfig config_file_path"
}
}

我想生成清单,以便在需要时能够轻松地手动管理我的服务/部署。

有人部署了这样的清单,并在销毁后保持了干净的状态吗?

或者唯一的解决方案是使用kubernetes_service&kubernetes_deployment资源?

编辑:

我试图使用一个本地exec provisioner,它将on_destroy值分配给when,但遇到了交叉引用错误:

Destroy-time provisioners and their connection configurations may only
reference attributes of the related resource, via 'self', 'count.index', or
'each.key'.
References to other resources during the destroy phase can cause dependency
cycles and interact poorly with create_before_destroy.

您需要添加一个销毁时间设置器

https://www.terraform.io/docs/language/resources/provisioners/syntax.html#destroy-时间提供者

最新更新