hashicorp / terraform-provider-kubernetes

Terraform Kubernetes provider
https://www.terraform.io/docs/providers/kubernetes/
Mozilla Public License 2.0
1.58k stars 968 forks source link

Feature request: add a standard "name" attribute #2448

Open Manbeardo opened 6 months ago

Manbeardo commented 6 months ago

Description

It would be dramatically easier to DRY up k8s templates while implicitly ordering the resource DAG if every k8s resource had a "name" attribute. As-is, we can extract the name attribute by using coalesce(kubernetes_manifest.my_manifest.metadata[*].name...), but that's an awfully complicated way to access such a commonly-used attribute.

Potential Terraform Configuration

resource "kubernetes_namespace" "my_ns" {
  metadata {
    name = "my-namespace"
  }
}

# proposed option: name attribute
resource "kubernetes_secret" "secret_0" {
  metadata {
    name      = "secret-0"
    namespace = kubernetes_namespace.my_ns.name
  }

  data = {
    foo = "bar"
  }
}

# current option #1: use coalesce
resource "kubernetes_secret" "secret_1" {
  metadata {
    name      = "secret-1"
    namespace = coalesce(kubernetes_namespace.my_ns.metadata[*].name...)
  }

  data = {
    foo = "bar"
  }
}

# current option #2: use depends_on
resource "kubernetes_secret" "secret_2" {
  depends_on = [kubernetes_namespace.my_ns]
  metadata {
    name      = "secret-2"
    namespace = "my-namespace"
  }

  data = {
    foo = "bar"
  }
}

Community Note

sheneska commented 6 months ago

Hi @Manbeardo, thanks for opening this issue. A much simpler alternative to this would be setting the namespace without the coalesce function, and instead just referencing the name attribute in the metadata (metadata[0].name). Since name is the first and thus only attribute in the metadata this should always work. Hope this helped!

Manbeardo commented 6 months ago

What about code that uses generate_name instead of name?