【发布时间】: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 现在可以使用,非常感谢!请提交我可以标记为解决方案的答案