我以以下方式生成一个文件
resource "null_resource" "clone_manifest_argocd" {
provisioner "local-exec" {
command = <<EOT
echo "Clone ArgoCD main branch"
git clone https://${var.gitlab_user}:${var.gitlab_token}@gitlab.com/org/k8s-argocd.git
echo "Generate kustomize files for ${var.argocd_overlay} env"
kubectl kustomize k8s-argocd/overlays/${var.argocd_overlay}/ -o /tmp/${var.argocd_overlay}.yaml
cat /tmp/${var.argocd_overlay}.yaml
EOT
}
triggers = {
always = timestamp()
}
}
文件总是正确创建和填充
...
null_resource.clone_manifest_argocd (local-exec): app.kubernetes.io/part-of: argocd-applicationset
null_resource.clone_manifest_argocd (local-exec): name: argocd-applicationset-controller
null_resource.clone_manifest_argocd (local-exec): ---
null_resource.clone_manifest_argocd (local-exec): apiVersion: v1
null_resource.clone_manifest_argocd (local-exec): kind: ServiceAccount
null_resource.clone_manifest_argocd (local-exec): metadata:
null_resource.clone_manifest_argocd (local-exec): labels:
null_resource.clone_manifest_argocd (local-exec): app.kubernetes.io/component: dex-server
null_resource.clone_manifest_argocd (local-exec): app.kubernetes.io/name: argocd-dex-server
null_resource.clone_manifest_argocd (local-exec): app.kubernetes.io/part-of: argocd
null_resource.clone_manifest_argocd (local-exec): name: argocd-dex-server
null_resource.clone_manifest_argocd (local-exec): ---
...
现在为了部署它,我想使用kubectl提供程序。我设置了
provider "kubectl" {
load_config_file = false
host = module.eks.cluster_endpoint
cluster_ca_certificate = base64decode(module.eks.cluster_certificate_authority_data)
token = data.aws_eks_cluster_auth.eks_auth.token
}
我尝试了以下操作来获取YAML文件并部署它
data "kubectl_file_documents" "files" {
content = file("/tmp/${var.argocd_overlay}")
}
resource "kubectl_manifest" "test" {
for_each = data.kubectl_file_documents.files.manifests
yaml_body = each.value
}
我也试过类似的东西
data "template_file" "files" {
template = file("/tmp/${var.argocd_overlay}.yaml")
}
resource "kubectl_manifest" "public_alb" {
yaml_body = data.template_file.files.rendered
}
但无论哪种情况,我都会收到
│ Error: Invalid function argument
│
│ on main.tf line 161, in data "template_file" "public_alb":
│ 161: template = file("/tmp/${var.argocd_overlay}.yaml")
│ ├────────────────
│ │ var.argocd_overlay is "dev"
│
│ Invalid value for "path" parameter: no file exists at "/tmp/dev.yaml"; this
│ function works only with files that are distributed as part of the
│ configuration source code, so if this file will be created by a resource in
│ this configuration you must instead obtain this result from an attribute of
│ that resource.
我这里少了什么东西吗?
根据Terraform文件函数文档:
此函数只能用于Terraform运行开始时磁盘上已经存在的文件。函数不参与依赖关系图,因此此函数不能与Terraform操作期间动态生成的文件一起使用。
您可能想要尝试Kustoize提供程序,而不是动态创建文件。