当所有数据都在变量内时,我尝试使用kubernetes_config_map
循环创建for_each
:
variables.tf
:
variable "extra_configmaps" {
type = list(object({
name = string
namespace = string
data = object({})
}))
default = []
}
terraform.tfvars
:
extra_configmaps = [
{
name = "common"
namespace = "production"
data = {
"somekey" = "somevalue"
}
}
]
main.tf
:
resource "kubernetes_config_map" "extra_configmaps" {
for_each = { for record in var.extra_configmaps : record.name => record }
metadata {
name = each.key
namespace = each.value.namespace
}
data = tomap(each.value.data)
}
但是,配置映射是在没有数据的情况下创建的!
# kubernetes_config_map.extra_configmaps["common"] will be created
+ resource "kubernetes_config_map" "extra_configmaps" {
+ id = (known after apply)
+ metadata {
+ generation = (known after apply)
+ name = "common"
+ namespace = "production"
+ resource_version = (known after apply)
+ self_link = (known after apply)
+ uid = (known after apply)
}
}
Plan: 1 to add, 0 to change, 0 to destroy.
我知道data
需要一张地图,但是即使我提供了地图,该计划也没有显示。
如何为没有空数据的Kubernetes创建动态配置图?如何在无空数据的情况下为Kubernetes创建动态配置图?
答案 0 :(得分:2)
您已指定data
属性的类型约束为object({})
,这是一个完全没有属性的对象类型。对象类型约束要求至少存在 个给定的属性,但是由于您未指定任何属性,因此该约束将匹配 any 对象值,但也会丢弃所有其值类型转换期间添加属性,因为类型约束中未指定任何属性。
对于完全空的对象类型,这种行为当然有点奇怪。这是规则的自然但可能有些尴尬的结果,它允许声明一个对象类型约束,该约束是提供者的资源类型定义的对象类型的子集,以允许调用者方便地传递整个资源,而不必写出所有该模块实际上不需要或不在乎的属性:
variable "aws_vpc" {
type = object({
id = string
cidr_block = string
# aws_vpc's object type has many other attributes,
# but this module only needs the two above so it
# will accept and silently discard all of the
# others. Caller can therefore do this:
# aws_vpc = aws_vpc.example
})
}
data
的{{1}}属性是用类型约束kubernetes_config_map
定义的,所以我认为获得您在此处寻找结果的最佳方法是回显相同的类型约束在您的变量中,如下所示:
map(string)
Conversion of Complex Types中有关于Terraform的类型转换规则的更多信息,这是Type Constraints文档的一部分。