以下一种方式创建名称空间,因此根据“ app_env”代码应用“角色绑定”
**variables.tf**
variable app_name {}
variable app_env {}
locals {
custom_role_dev = "Enterprise Development Project"
custom_role_prd = "Enterprise Production Project"
}
**main.tf**
resource "kubernetes_namespace" "kube_ns" {
metadata {
name = var.app_name
}
}
resource "kubernetes_role" "custom_role_dev" {
count var.app_env == "d" ? 1 : 0
metadata {
name = local.custom_role_dev
namespace = var.app_name
}
rule {
api_groups = [""]
resources = ["<options>"]
verbs = ["*"]
}
depends_on = [kubernetes_namespace.kube_ns]
}
resource "kubernetes_role" "custom_role_prd" {
count var.app_env == "p" ? 1 : 0
metadata {
name = local.custom_role_prd
namespace = var.app_name
}
rule {
api_groups = [""]
resources = ["<options>"]
verbs = ["*"]
}
depends_on = [kubernetes_namespace.kube_ns]
}
为了创建多个命名空间并应用它们各自的角色,我想使用“列表”替换“ app_name”变量,但是我不知道如何迭代“ kubernetes_role”块。
我认为这2个链接与我想做的非常接近
Convert list to map with index in Terraform
Terraform - conditionally creating a resource within a loop
可以使用“ for_each”或“ count”来完成此操作吗?