如何使用在另一个资源的秘密中创建的令牌?

时间:2020-03-07 03:12:21

标签: terraform terraform-provider-kubernetes

我正在尝试创建服务帐户密码,以便使用令牌填充密码:

resource "kubernetes_service_account" "k8s-api-token" {
  metadata {
    namespace = "${var.whatever_namespace}"
    name = "api-service-account"
  }
  secret {
    name = "api-service-account-secret"
  }
}

resource "kubernetes_secret" "k8s-api-token" {
  metadata {
    namespace = "${var.whatever_namespace}"
    name = "${kubernetes_service_account.k8s-api-token.metadata.0.name}-secret"
    annotations = {
      "kubernetes.io/service-account.name" = "${kubernetes_service_account.k8s-api-token.metadata.0.name}"
    }
  }
  type = "kubernetes.io/service-account-token"
}

data "kubernetes_secret" "k8s-api-token" {
  depends_on = ["kubernetes_secret.k8s-api-token"]
  metadata {
    namespace = "${var.whatever_namespace}"
    name = "${kubernetes_secret.k8s-api-token.metadata.0.name}"
  }
}

resource "kubernetes_secret" "whatever-secrets" {
  depends_on = ["kubernetes_secret.k8s-api-token"]
  metadata {
    name = "botfront-secrets"
    namespace = "${var.whatever_namespace}"
  }

  data = {
    K8S_API   = "${data.kubernetes_secret.k8s-api-token.data.token}"
  }
}

但是它给了我一个错误: Resource 'data.kubernetes_secret.k8s-api-token' does not have attribute 'data.token' for variable 'data.kubernetes_secret.k8s-api-token.data.token'

我可以确认密码已创建,但是即使运行terraform state show kubernetes_secret.k8s_api_token也不会返回任何信息

我在做什么错了?

1 个答案:

答案 0 :(得分:0)

解决方案是使用查找:

K8S_API = ${lookup(data.kubernetes_secret.k8s-api-token-data.data, "token","")}

来源:http://blog.crashtest-security.com/resource-does-not-have-attribute