terraform-provider-kubernetes icon indicating copy to clipboard operation
terraform-provider-kubernetes copied to clipboard

Resource import of kubernetes_namespace not working

Open darlinchen opened this issue 2 years ago • 4 comments

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

  • Please vote on this issue by adding a 👍 reaction to the original issue to help the community and maintainers prioritize this request
  • If you are interested in working on this issue or have submitted a pull request, please leave a comment

darlinchen avatar Jun 16 '23 12:06 darlinchen

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.

BBBmau avatar Jun 21 '23 14:06 BBBmau

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

Cr4ig101 avatar Nov 02 '23 14:11 Cr4ig101

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

alexsomesan avatar Nov 02 '23 15:11 alexsomesan

Thank you, it works when using kubernetes_namespace_v1 instead of kubernetes_namespace

Cr4ig101 avatar Nov 02 '23 16:11 Cr4ig101