Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resource import of kubernetes_namespace not working #2146

Closed
darlinchen opened this issue Jun 16, 2023 · 5 comments
Closed

Resource import of kubernetes_namespace not working #2146

darlinchen opened this issue Jun 16, 2023 · 5 comments
Labels

Comments

@darlinchen
Copy link

darlinchen commented Jun 16, 2023

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 darlinchen added the bug label Jun 16, 2023
@github-actions github-actions bot removed the bug label Jun 16, 2023
@BBBmau
Copy link
Contributor

BBBmau commented Jun 21, 2023

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
Copy link

Cr4ig101 commented Nov 2, 2023

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
Copy link
Member

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
Copy link

Cr4ig101 commented Nov 2, 2023

Thank you, it works when using kubernetes_namespace_v1 instead of kubernetes_namespace

Copy link

github-actions bot commented Nov 2, 2024

Marking this issue as stale due to inactivity. If this issue receives no comments in the next 30 days it will automatically be closed. If this issue was automatically closed and you feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. This helps our maintainers find and focus on the active issues. Maintainers may also remove the stale label at their discretion. Thank you!

@github-actions github-actions bot added the stale label Nov 2, 2024
@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Dec 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants