我正在尝试使用 Terraform 为 nginx 构建 Helm 图表。我创建了一个附加的values.yaml 文件,其中包含以下内容:
allowableIPs: "{{ .Values.allowableIPs }}"
填写信息的 Terraform 代码就像是的,应该想到 IP 地址的另一个名称:
variable "allowable_ips" {
type = list(string)
default = []
}
resource "helm_release" "nginx" {
name = "nginx"
chart = "${path.module}/nginx"
namespace = "ns-rebotics"
force_update = false
recreate_pods = true
create_namespace = true
set {
name = "allowableIPs"
value = jsonencode(var.allowable_ips)
}
}
以及要传入的变量,我只是键入而不是通过代码创建,以限制出错的可能性。它位于 locals.tf 文件中:
ip_addresses = ["52.24.34.195/32","34.216.251.107/32","183.14.29.54/32","212.112.111.26/32","172.112.148.169/32","52.34.216.69/32","104.2.87.241/32","107.2.155.54/32","100.15.233.56/32"]
当我执行 TF 时,出现以下错误:
错误:无法解析值为 [“52.24.34.195/32”、“34.216.251.107/32”、“183.14.29.54/32”、“212.112.111.26/32”、“172.112.148.169/32”的键“allowableIPs” ","52.34.216.69/32","104.2.87.241/32","107.2.155.54/32","100.15.233.56/32"],键“107/32””没有值(不能以 , )
使用 module.client_instance.module.kubecharts.helm_release.nginx, 在 REB3Modules/container/nginx/v1/nginx.tf 第 63 行,资源“helm_release”“nginx”中: 63:资源“helm_release”“nginx”{
要将复杂列表从 Terraform 传递到 Helm 图表,对我有用的一种有效方法是使用
values
资源中的 helm_release
块以及 YAML 文件。
为了解决您面临的问题,我建议您在 Terraform 模块目录中创建一个名为
custom-values.yaml.tpl
的文件。该模板将包含 Terraform 将替换的动态内容的占位符,在您的情况下是允许的 IP。所以像这样:
# custom-values.yaml.tpl
allowableIPs:
%{ for ip in allowable_ips ~%}
- "${ip}"
%{ endfor ~%}
修改您的 Terraform
helm_release
资源以使用 values
块来设置动态生成的值文件。使用 Terraform 的 templatefile
函数使用您的 local.ip_addresses
或 var.allowable_ips
中的实际 IP 填充模板。 (我看到您指定了两者,但您可以坚持使用本地文件中的 ip_addresses )。对于此更改,它将如下所示:
resource "helm_release" "nginx" {
name = "nginx"
chart = "${path.module}/nginx"
namespace = "ns-rebotics"
force_update = false
recreate_pods = true
create_namespace = true
values = [templatefile("${path.module}/custom-values.yaml.tpl", {
allowable_ips = local.ip_addresses
})]
}
这样
templatefile
将读取您的 custom-values.yaml.tpl
,用 local.ip_addresses
中的 IP 填充它,并将渲染的 YAML 作为值传递到 Helm 图表。
还要确保您的
locals
块在您定义 helm_release
资源的范围内可访问。
尝试一下,如果有帮助请告诉我。