我正在嘗試使用帶有以下代碼的 terraform 部署 crd
resource "kubectl_manifest" "crd-deploy" {
for_each = [ for crd in var.crdslist : crd ]
yaml_body = (fileexists("${path.module}/crds/${crd}.yaml") ? file("${path.module}/crds/${crd}") : "")
}
但我仍然收到錯誤
Error: Invalid reference
on ../../00-modules/00-global/25-crd/10-crd.tf line 3, in resource "kubectl_manifest" "crd-deploy":
3: yaml_body = (fileexists("${path.module}/crds/${crd}.yaml") ? file("${path.module}/crds/${crd}") : "")
A reference to a resource type must be followed by at least one attribute access, specifying the resource name.
module_level 中的 input.tf
variable "crdslist" {
type = list(string)
default = []
}
execution_module_level 中的 input.tf
locals {
crdslist = ["crd-test"]
}
我從哪里運行 terraform 計劃以部署 K8s CRD
module "crds" {
source = "../../modules/global/25-crd"
crdslist = local.crdslist
}
uj5u.com熱心網友回復:
由于使用的變數是 type list(string),這意味著它不能for_each以其原始形式使用。for_each元引數要求變數的型別為或map[ set1]:
如果資源或模塊塊包含值是映射或一組字串的 for_each 引數,則 Terraform 將為該映射或集合的每個成員創建一個實體。
為了使當前配置按預期作業,使用 Terraform 內置函式toset[2] 就足夠了。唯一需要做的改變是在模塊內部:
resource "kubectl_manifest" "crd-deploy" {
for_each = toset(var.crdslist)
yaml_body = (fileexists("${path.module}/crds/${each.vlaue}.yaml") ? file("${path.module}/crds/${each.vlaue}") : "")
}
請注意,當串列轉換為集合時,鍵和值是相同的,即,each.key可以each.value互換使用[3]。
[1] https://www.terraform.io/language/meta-arguments/for_each
[2] https://www.terraform.io/language/functions/toset
[3] https://www.terraform.io/language/meta-arguments/for_each#the-each-object
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/446644.html
標籤:Kubernetes 地形 Kubernetes-自定义资源
