【问题标题】:Error when try apply configmap to auth with EKS cluster尝试使用 EKS 集群将 configmap 应用于身份验证时出错
【发布时间】:2021-12-02 09:20:50
【问题描述】:

我有以下问题。我尝试使用带有 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

【问题讨论】:

    标签: kubernetes terraform gitlab-ci amazon-eks


    【解决方案1】:

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

    如文档所示 (terrafom doc link)

    不要使用“heredoc”字符串来生成 JSON 或 YAML。相反,使用 jsonencode 函数或 yamlencode 函数,以便 Terraform 可以 负责保证有效的 JSON 或 YAML 语法。

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

    如果您想继续使用现在的内容,请尝试使用 output -json 或 -raw 提供这些选项

    • terraform 输出 -json authconfig > authconfig.yaml 或
    • terraform 输出 -raw authconfig > authconfig.yaml

    【讨论】:

    • 感谢帮助,一切正常!
    【解决方案2】:

    错误信息告诉你 authconfig.yaml 文件无法从 YAML 转换为 JSON,提示它不是一个有效的 yaml

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

    之前删除这些

    您的评论表明您已经知道这一点 - 那么您为什么不询问 terraform,而不是向我们展示 kubectl create 失败?从您的帖子来看,听起来您实际上是在复制/粘贴您的工作输出,甚至没有阅读它。

    因此,很明显,下一步是 terraform 输出 -raw 或 -json,在他们的文档或知识库中有多次提及,谷歌搜索会指向您:

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

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

    【讨论】:

    • 好的。这是我已经知道的,为了让 GitLab 能够应用 configMap,但是当我运行 CI / CD 时,它会与这个 EOT 一起出现
    • 查看 terraform 输出文档,有几个选项可以尝试:terraform.io/docs/cli/commands/output.html。以 -raw 开头。如果它不起作用,-json 可能会。
    • @MiltonJesus 您可以使用yamlencode() 来避免这些类型的问题,而不是在字符串模板中构建您的 yaml。
    • 谢谢大家,这个解决方案有效
    猜你喜欢
    • 2021-10-22
    • 1970-01-01
    • 2021-05-24
    • 2019-11-06
    • 2015-12-25
    • 2022-06-20
    • 2019-10-28
    • 2021-03-25
    • 2021-02-21
    相关资源
    最近更新 更多