Terraform 如何在没有空数据的情况下为Kubernetes创建动态配置映射?
当所有数据都在一个变量内时,我尝试为每个循环使用Terraform 如何在没有空数据的情况下为Kubernetes创建动态配置映射?,terraform,Terraform,当所有数据都在一个变量内时,我尝试为每个循环使用创建kubernetes\u config\u映射: 变量。tf: variable "extra_configmaps" { type = list(object({ name = string namespace = string data = object({}) })) default = [] } resource "kubernetes_config_map" "extra_co
创建kubernetes\u config\u映射
:
变量。tf
:
variable "extra_configmaps" {
type = list(object({
name = string
namespace = string
data = object({})
}))
default = []
}
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)
}
terraform.tfvars
:
extra_configmaps = [
{
name = "common"
namespace = "production"
data = {
"somekey" = "somevalue"
}
}
]
main.tf
:
variable "extra_configmaps" {
type = list(object({
name = string
namespace = string
data = object({})
}))
default = []
}
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)
}
但是,创建configmap时没有数据
# 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.
我知道,数据
需要一张地图,但即使我提供了地图,计划也没有显示出来
如何在没有空数据的情况下为Kubernetes创建动态配置映射?如何在没有空数据的情况下为Kubernetes创建动态配置映射?您已指定data
属性的类型约束为object({})
,这是一种完全没有属性的对象类型。对象类型约束要求至少存在给定的属性,但由于未指定任何属性,因此此约束将匹配任何对象值,但在类型转换期间也将放弃其所有属性,因为在类型约束中未指定任何属性
对于一个完全空的对象类型,这种行为当然有点奇怪。规则允许声明对象类型约束,该约束是由提供者的资源类型定义的对象类型的子集,这是一个自然但可能令人尴尬的结果,为了使调用者能够方便地传入整个资源,而不必写出模块实际上不需要或不关心的所有属性:
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
})
}
kubernetes\u config\u map
的data
属性是用类型约束map(string)
定义的,因此我认为获得您在这里寻找的结果的最佳方法是在变量中回显相同的类型约束,如下所示:
variable "extra_configmaps" {
type = list(object({
name = string
namespace = string
data = map(string)
}))
default = []
}
中有关于Terraform类型转换规则的更多信息,这是上的文档的一部分。您已指定数据
属性的类型约束为对象({})
,这是一种完全没有属性的对象类型。对象类型约束要求至少存在给定的属性,但由于未指定任何属性,因此此约束将匹配任何对象值,但在类型转换期间也将放弃其所有属性,因为在类型约束中未指定任何属性
对于一个完全空的对象类型,这种行为当然有点奇怪。规则允许声明对象类型约束,该约束是由提供者的资源类型定义的对象类型的子集,这是一个自然但可能令人尴尬的结果,为了使调用者能够方便地传入整个资源,而不必写出模块实际上不需要或不关心的所有属性:
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
})
}
kubernetes\u config\u map
的data
属性是用类型约束map(string)
定义的,因此我认为获得您在这里寻找的结果的最佳方法是在变量中回显相同的类型约束,如下所示:
variable "extra_configmaps" {
type = list(object({
name = string
namespace = string
data = map(string)
}))
default = []
}
中有关于Terraform类型转换规则的更多信息,这是上的文档的一部分。生成的计划正是我从提供的配置中所期望的。您对该计划的期望是什么?@MattSchuchard希望在tfvars
文件中包含数据的configmap:somekey:somevalue
您指的是哪个data
?您有一个数据源、一个数据键、一个数据资源参数等等?生成的计划正是我从提供的配置中所期望的。您对该计划的期望是什么?@MattSchuchard希望在tfvars
文件中包含数据的configmap:somekey:somevalue
您指的是哪个data
?您有数据源、数据键、数据资源参数等。?