Terraform Kubernetes 供应商“local-exec” kubectl apply -f -<<EOF 在 Windows 上不起作用

Kir*_*kiy 1 yaml kubernetes terraform kubectl

我正在尝试 通过 Terraform 的 null_resource创建/应用这个 kubectl .yaml 文件https://github.com/Azure/aad-pod-identity/blob/master/deploy/infra/deployment.yaml到 AKS 以安装 Azure AD Pod 标识。它需要部署 Azure 网关入口控制器。将 Windows 10 与 VS Code 结合使用

主要.tf:

data "template_file" "aad_pod" {
  template = "${file("${path.module}/templates/aad_pod.yaml")}"
}

resource "null_resource" "aad_pod_deploy" {
  triggers = {
    manifest_sha1 = "${sha1("${data.template_file.aad_pod.rendered}")}"
  }

  provisioner "local-exec" {
    command = "kubectl apply -f -<<EOF\n${data.template_file.aad_pod.rendered}\nEOF"
  }
}
Run Code Online (Sandbox Code Playgroud)

在 terraform apply 之后我有这个错误:

Error: Error running command 'kubectl apply -f -<<EOF
'cutted listing of yaml file'
EOF': exit status 1. Output: << was unexpected at this time.
Run Code Online (Sandbox Code Playgroud)

任何帮助将不胜感激

Mar*_*ins 5

由于类 Unix 操作系统和 Windows 之间的差异,local-exec除非您的用例非常简单,否则很难以可移植的方式使用。这是供应商是最后手段的原因之一。

我认为最便携的答案是使用官方的 Kubernetes 供应商在这里与 Kubernetes 进行交互。或者,如果 usingkubectl的输入格式对您正在做的事情特别重要,您可以使用社区维护的kubectlprovider

resource "kubectl_manifest" "example" {
  yaml_body = data.template_file.aad_pod.rendered
}
Run Code Online (Sandbox Code Playgroud)

如果您有充分的理由使用local-exec配置程序而不是本机 Terraform 提供程序,您需要找到一种方法来编写一个命令,该命令可以由 Unix 样式的 shell 和 Windows 的命令行约定以兼容的方式解释. 我希望通过首先将文件写入磁盘并将文件名传递给 来实现这一点会更容易kubectl,因为这避免了使用 shell 的任何特殊功能的需要,并让一切都由kubectl自己处理:

resource "local_file" "aad_pod_deploy" {
  filename = "${path.module}/aad_pod.yaml"
  content  = data.template_file.aad_pod.rendered

  provisioner "local-exec" {
    command = "kubectl apply -f ${self.filename}"
  }
}
Run Code Online (Sandbox Code Playgroud)

对于这种方法,仍有一些注意事项需要注意。例如,如果您在包含空格的目录路径下运行 Terraform,那么self.filename将包含空格,因此可能不会被 Unix shell 或kubectlWindows 可执行文件解析为您想要的。