hashicorp / terraform-provider-kubernetes

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

Failed to get namespace #2558

Open MonicaMagoniCom opened 4 months ago

MonicaMagoniCom commented 4 months ago

Terraform version: 1.7.4 Kubernetes provider version: 2.31.0 Kubernetes version: 1.28

I have a module called 'kube' where I create the following namespace:

resource "kubernetes_namespace" "pulsar-ns" {
  metadata {
    name = "pulsar2"
  }
}

Then in another module (which has a dependency on the kube module), so it is applied after the 'kube' module, I do:

resource "terraform_data" "prepare-pulsar-helm-release" {
  provisioner "local-exec" {
    command     = "./prepare_helm_release.sh -k pulsar -s -n ${var.pulsar-ns}"
    interpreter = ["bash", "-c"]
    working_dir = path.module
  }
  depends_on = [var.pulsar-ns]
}

When doing terraform apply, it correctly creates the namespace, but the local-exec fails with error (even though the ns exists):

exit status 1. Output: error: failed to get namespace 'pulsar2'
│ please check that this namespace exists, or use the '-c' option to create it
jrhouston commented 4 months ago

Thanks for opening an issue @MonicaMagoniCom – I suspect the problem may be with your depends_on configuration. Can you try the configuration with your terraform_data resource depending on the kubernetes_namespace directly instead of the var? This would tell us if there's a bug in the provider.

MonicaMagoniCom commented 4 months ago

Actually it is not deterministic, sometimes it happens, sometimes it works. Anyway I'm going to remove the var and let you know, thank you.

MonicaMagoniCom commented 3 months ago

I'm still experiecing the same issue. The namespace is created in the same apply command and it seems that the resource "prepare-pulsar-helm-release" sometimes does not find the namespace even if it was created some steps before. Maybe do I have to split it into 2 terraform apply?

JaylonmcShan03 commented 1 month ago

@jrhouston I can do some investigation on this issue for Q4, and will provide updates here depending on my findings! Are you okay with that?