【问题标题】:Terraform "file name too long" when executing with "null_resource" "apply"使用“null_resource”“apply”执行时,Terraform“文件名太长”
【发布时间】:2022-02-06 04:25:23
【问题描述】:

我正在尝试执行以下命令: kubectl get cm aws-auth -n kube-system -o json | jq --arg add "`cat additional_roles_aws_auth.yaml`" '.data.mapRoles += $add' | kubectl apply -f - 作为本地 Terraform 执行的一部分,如下所示:

locals {
  kubeconfig = yamlencode({
    apiVersion      = "v1"
    kind            = "Config"
    current-context = "terraform"
    clusters = [{
      name = module.eks.cluster_id
      cluster = {
        certificate-authority-data = module.eks.cluster_certificate_authority_data
        server                     = module.eks.cluster_endpoint
      }
    }]
    contexts = [{
      name = "terraform"
      context = {
        cluster = module.eks.cluster_id
        user    = "terraform"
      }
    }]
    users = [{
      name = "terraform"
      user = {
        token = data.aws_eks_cluster_auth.this.token
      }
    }]
  })
}
resource "null_resource" "apply" {
  triggers = {
    kubeconfig = base64encode(local.kubeconfig)
    cmd_patch  = <<-EOT
      kubectl get cm aws-auth -n kube-system -o json | jq --arg add "`cat additional_roles_aws_auth.yaml`" '.data.mapRoles += $add' | kubectl apply -f -
    EOT
  }
    provisioner "local-exec" {
    interpreter = ["/bin/bash", "-c"]
    environment = {
      KUBECONFIG = self.triggers.kubeconfig
    }
    command = self.triggers.cmd_patch
  }
}

在 Terraform 之外执行相同的命令,显然在命令行上工作正常。 但是,作为 Terraform 脚本的一部分执行时,我总是收到以下错误:

│ ': exit status 1. Output:
│ iAic2FtcGxlLWNsdXN0ZXI...WaU5ERXdNekEiCg==":
│ open
│ ImFwaVZlcnNpb24iOiAidjEiy...RXdNekEiCg==:
│ file name too long

有人知道问题可能是什么吗?

【问题讨论】:

  • local.kubeconfig 中有什么内容?
  • KUBECONFIG 环境变量是配置文件的路径列表,而不是配置文件本身:kubernetes.io/docs/tasks/access-application-cluster/….. 从代码输出来看,您要么在文件上做了 base64encode路径或文件本身。如果在文件路径上使用,您可以使用base64decode(self.triggers.kubeconfig)。否则,您需要提供路径。
  • 我添加了 local.kubeconfig 代码。很抱歉我错过了。
  • @MarkoE 非常感谢。这个提示对我帮助很大。我不再收到错误消息。我在 kubectl 语句中添加了以下内容: --kubeconfig
  • 修复了另一个问题。我用来注入额外内容的文件需要位于我使用的根模块的根目录中。 @MarkoE 现在可以使用,非常感谢!请提交我可以标记为解决方案的答案

标签: terraform jq kubectl


【解决方案1】:

根据我的评论:KUBECONFIG 环境变量需要是配置文件列表,而不是文件本身的内容 [1]:

KUBECONFIG 环境变量是配置文件的路径列表。

最初的问题是文件的内容以base64 格式[2] 编码,并以该格式使用,而之前没有对其进行解码。幸运的是,Terraform 内置了这两个函数,因此使用 base64decode [3] 将返回“正常”文件内容。不过,它将是文件内容,而不是配置文件的路径。基于其他 cmets,我想需要注意的重要一点是 additional_roles_aws_auth.yaml 文件必须与根模块位于同一目录中。由于命令有点复杂,我不确定你是否可以使用 Terraform 内置的path object [4] 来确保在模块的根目录中搜索文件:

kubectl get cm aws-auth -n kube-system -o json | jq --arg add "`cat ${path.root}/additional_roles_aws_auth.yaml`" '.data.mapRoles += $add' | kubectl apply -f -

[1]https://kubernetes.io/docs/tasks/access-application-cluster/configure-access-multiple-clusters/#set-the-kubeconfig-environment-variable

[2]https://www.terraform.io/language/functions/base64encode

[3]https://www.terraform.io/language/functions/base64decode

[4]https://www.terraform.io/language/expressions/references#filesystem-and-workspace-info

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-08
    • 2021-08-14
    • 2021-12-07
    • 2020-03-22
    • 1970-01-01
    • 1970-01-01
    • 2014-05-30
    • 2015-04-18
    相关资源
    最近更新 更多