尝试将 configmap 应用于 EKS 集群的身份验证时出错

Error when try apply configmap to auth with EKS cluster

我有以下问题。我尝试使用带有 Gitlab CI/CD 的 Terraform 连接到 eks 集群,我收到了错误消息,但是当在我的计算中尝试它时,这个错误没有出现,有人有同样的错误吗?


$ terraform output authconfig > authconfig.yaml
$ cat authconfig.yaml
<<EOT
apiVersion: v1
kind: ConfigMap
metadata:
    name: aws-auth
    namespace: kube-system
data:
    mapRoles: |
      - rolearn: "arn:aws:iam::503655390180:role/clusters-production-workers"
            username: system:node:{{EC2PrivateDNSName}}
            groups:
                - system:bootstrappers
                - system:nodes
EOT
$ kubectl create -f authconfig.yaml -n kube-system
error: error parsing authconfig.yaml: error converting YAML to JSON: yaml: line 2: mapping values are not allowed in this context

错误消息告诉您 authconfig.yaml 文件无法从 YAML 转换为 JSON,表明它不是有效的 yaml

您向我们展示的猫 authconfig.yaml 包含一些 <<EOTEOT 标签。我建议在 运行 kubectl create -f

之前删除那些

您的评论表明您已经知道这一点 - 那么您为什么不询问 terraform,而不是向我们展示 kubectl create 失败?从你的 post 来看,这听起来真的像是你 copy/pasted 你的工作成果,甚至没有阅读它。

所以,显然,下一步是 terraform 输出 -raw,或 -json,在他们的文档或知识库中有多次提及,google 搜索将指向:

最后:我们可以问为什么?当您可以让 terraform 写入文件时,您为什么要 terraform output > 东西?

虽然作为一般规则,每当将 terraform stdout/stderr 写入文件时,我强烈建议使用无色。

输出包含 EOT(EndOfText) 标记,因为它最初是作为多行字符串生成的。

如文档所示 (terrafom doc link)

Don't use "heredoc" strings to generate JSON or YAML. Instead, use the jsonencode function or the yamlencode function so that Terraform can be responsible for guaranteeing valid JSON or YAML syntax.

在构建输出之前使用 json 编码或 yaml 编码。

如果你想继续这样使用你现在拥有的,那么尝试为这些选项提供输出 -json 或 -raw

  • terraform 输出 -json authconfig > authconfig.yaml 或者
  • terraform 输出-原始 authconfig > authconfig.yaml