hashicorp / terraform-provider-kubernetes

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

Resource import of kubernetes_namespace not working #2146

Open darlinchen opened 1 year ago

darlinchen commented 1 year ago

Terraform Version, Provider Version and Kubernetes Version

Terraform version: 1.4.6
Kubernetes provider version: 2.15.0
Kubernetes version: 1.24.9

Affected Resource(s)

Terraform Configuration Files

resource "kubernetes_namespace" "this" {
  for_each = var.namespaces
  metadata {
    name = each.key
  }
}

Debug Output

Import successful!

The resources that were imported are shown above. These resources are now in your Terraform state and will henceforth be managed by Terraform.

Steps to Reproduce

terraform import 'module.modulename.kubernetes_namespace.this["namespace"]' namespace

Expected Behavior

The resource was successful imported not only the command output

Actual Behavior

Terraform said import was successful but the resource is not in the terraform state

Important Factoids

Kubernetes Cluster running in Azure

References

Community Note

BBBmau commented 1 year ago

Hello @darlinchen! Could you give me an example of what's in your namespace variable?

I was able to import successfully using this tfconfig and the tf import command below

variable "namespaces" {
  default = {"namespace"= "terraform-test"}
}

resource "kubernetes_namespace" "this" {
  for_each = var.namespaces
  metadata {
    name = each.value
  }
}

terraform import 'kubernetes_namespace.this["namespace"]' terraform-test

The output should look like this:

mau@mau-JKDT676NCP  ~/Dev/Scratch/import_namespaces  terraform import 'kubernetes_namespace.this["namespace"]' terraform-test
kubernetes_namespace.this["namespace"]: Importing from ID "terraform-test"...
kubernetes_namespace.this["namespace"]: Import prepared!
  Prepared kubernetes_namespace for import
kubernetes_namespace.this["namespace"]: Refreshing state... [id=terraform-test]

Import successful!

The resources that were imported are shown above. These resources are now in
your Terraform state and will henceforth be managed by Terraform.
Cr4ig101 commented 11 months ago

I am also seeing this error when using a list of string to define my namespace names

resource "kubernetes_namespace" "namespace" {
  for_each = toset(var.namespace)
  metadata {
    name = each.value
  }
}
variable "namespace" {
  description = "EKS Namespace"
  type        = list(string)
}

I am using terragrunt so in my input values I have a list of names however I need to import a namespace that was already created.

inputs = {
   namespace  = ["project"]
}

Running the import comes back as successful but not seeing the namespace in the state file

terragrunt import kubernetes_namespace.namespace test

Import successful!

The resources that were imported are shown above. These resources are now in
your Terraform state and will henceforth be managed by Terraform.

terragrunt console output

> kubernetes_namespace.namespace
{
  "project" = {
    "id" = "project"
    "metadata" = tolist([
      {
        "annotations" = tomap(null) /* of string */
        "generate_name" = ""
        "generation" = 0
        "labels" = tomap(null) /* of string */
        "name" = "project"
        "resource_version" = "25816981"
        "uid" = "4f27-4b56-bb22-00cdc7cb35b0"
      },
    ])
    "timeouts" = null /* object */
    "wait_for_default_service_account" = false
  }
}
>
alexsomesan commented 11 months ago

Since for_each works on a set of strings, the correct syntax for the import command in this case would be:

terraform import 'kubernetes_namespace_v1.namespace["project"]' project
Cr4ig101 commented 11 months ago

Thank you, it works when using kubernetes_namespace_v1 instead of kubernetes_namespace