我们正试图在我们的地形文件中使用以下kubernetes提供程序设置来运行terraform apply
:
data "google_client_config" "current" {
}
data "google_container_cluster" "onboarding_cluster" {
name = var.cluster_name
location = var.cluster_location
}
provider "kubernetes" {
load_config_file = false
host = data.google_container_cluster.onboarding_cluster.endpoint
cluster_ca_certificate = base64decode(data.google_container_cluster.onboarding_cluster.master_auth[0].cluster_ca_certificate)
token = data.google_client_config.current.access_token
}
resource "kubernetes_service_account" "service_account" {
metadata {
name = var.kubernetes_service_account_name
namespace = var.kubernetes_service_account_namespace
}
}
但我们得到了以下错误:
Error: Unauthorized
on main.tf line 85, in resource "kubernetes_service_account" "service_account":
85: resource "kubernetes_service_account" "service_account" {
在将TF_LOG
设置为DEBUG
之后,我们看到以下创建kubernetes服务帐户的请求:
---[ REQUEST ]---------------------------------------
POST /api/v1/namespaces/default/serviceaccounts HTTP/1.1
...
Authorization: Bearer <SOME_KUBERNETES_JWT>
即使我们在提供者中硬编码令牌,身份验证承载令牌也会被覆盖!例如:
provider "kubernetes" {
load_config_file = false
host = data.google_container_cluster.onboarding_cluster.endpoint
cluster_ca_certificate = base64decode(data.google_container_cluster.onboarding_cluster.master_auth[0].cluster_ca_certificate)
token = "some.hardcoded.token"
}
即使有了以上内容,令牌在HTTP请求中也将保持不变。
我们发现在auth头中发送的令牌在位于/run/secrets/kubernetes.io/serviceaccount/token
的terraform容器中找到。
terraform会用kubernetes生成的令牌覆盖这个令牌吗?我们还可以尝试其他设置吗?
这是kubernetes提供程序的问题。Github问题在这里:https://github.com/terraform-providers/terraform-provider-kubernetes/issues/679
要修复,请将您的提供商版本设置为1.9,如下所示:
provider "kubernetes" {
version = "1.9"
cluster_ca_certificate = base64decode(
data.google_container_cluster.this.master_auth[0].cluster_ca_certificate,
)
host = data.google_container_cluster.this.endpoint
token = data.external.get_token.result["token"]
load_config_file = false
}