How to use a token created in a secret in another resource?

╄→尐↘猪︶ㄣ 提交于 2020-04-07 08:03:23

问题


I'm trying to create a service account secret in order to populate a secret with the token:

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}"
  }
}

But it gives me an error: Resource 'data.kubernetes_secret.k8s-api-token' does not have attribute 'data.token' for variable 'data.kubernetes_secret.k8s-api-token.data.token'

I can verify the secret is created, but even running terraform state show kubernetes_secret.k8s_api_token doesn't return anything

What am I doing wrong?


回答1:


The solution is to use a lookup:

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

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



来源:https://stackoverflow.com/questions/60574055/how-to-use-a-token-created-in-a-secret-in-another-resource

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!