2

我们正在尝试terraform apply在我们的 terraform 文件中使用以下 kubernetes 提供程序设置运行:

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_LOGDEBUG我们看到创建 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 标头中发送的令牌位于 terraform 容器中/run/secrets/kubernetes.io/serviceaccount/token

terraform 是否有任何理由会用 kubernetes 生成的令牌覆盖此令牌?我们可以尝试其他设置吗?

4

1 回答 1

1

这是 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
}
于 2019-11-20T13:07:28.657 回答