我有以下相对复杂的YAML:
apiVersion: v1
kind: ConfigMap
metadata:
name: aws-auth
namespace: kube-system
data:
mapRoles: |
- rolearn: ${aws_iam_role.tf-eks-node.arn}
username: system:node:{{EC2PrivateDNSName}}
groups:
- system:bootstrappers
- system:nodes
我现在想将其转换为Terraform-Kubernetes兼容的资源,该资源仅由字符串,列表和映射组成。我该怎么做呢?
我找到了一个使用连字符表示列表的LimitRange如何显示here的示例:
resource "kubernetes_limit_range" "example" {
metadata {
name = "terraform-example"
}
spec {
limit {
type = "Pod"
max {
cpu = "200m"
memory = "1024M"
}
}
limit {
type = "PersistentVolumeClaim"
min {
storage = "24M"
}
}
limit {
type = "Container"
default {
cpu = "50m"
memory = "24M"
}
}
}
}
我目前对mapRoles块的尝试如下:
mapRole { rolearn = "${aws_iam_role.tf-eks-node.arn}"
username = "system:node:{{EC2PrivateDNSName}}"
groups = ["system:bootstrappers","system:nodes"]}
这是不正确的,因为mapRoles是一个列表(因为连字符所表示的单数元素是一个列表元素);但是如果它是一个列表,并且连字符后面的所有内容都是list元素,那么列表标题是什么?
使用答案中提到的想法,我尝试将元数据块预先放在数据块中:
data "template_file" "map_roles" {
template = <<EOF
- rolearn: ${var.arn}
username: system:node:{{EC2PrivateDNSName}}
groups:
- system:bootstrappers
- system:nodes
EOF
vars {
arn = "${var.kube-arn}"
}
}
resource "kubernetes_config_map" "aws_auth" {
metadata {
name = "aws-auth"
namespace = "kube-system"
}
data {
mapRoles = "${data.template_file.map_roles.template}"
}
}
最佳答案
因此,当您在YAML中拥有此功能时:
data:
mapRoles: |
这意味着
|
之后的所有内容都是一个字符串,它是mapRoles
的值。我不太确定那是你想要的。如果需要列表列表,则需要这样的内容:data:
mapRoles:
- rolearn: ${aws_iam_role.tf-eks-node.arn}
- username: system:node:{{EC2PrivateDNSName}}
- groups:
- system:bootstrappers
- system:nodes
这将转化为HCL:
"data" = {
"mapRoles" = {
"rolearn" = "${aws_iam_role.tf-eks-node.arn}"
}
"mapRoles" = {
"username" = "system:node:{{EC2PrivateDNSName}}"
}
"mapRoles" = {
"groups" = ["system:bootstrappers", "system:nodes"]
}
}