banzaicloud/terraform-provider-k8s

Unable to update Namespaces metadata: Error: the namespace of the object (default) does not match the namespace on the request (foo)

thomas-riccardi opened this issue · 0 comments

Describe the bug
Updating a namespace using k8s_manifest fails with Error: the namespace of the object (default) does not match the namespace on the request (foo)

Steps to reproduce the issue:

  • create a namespace:
Terraform will perform the following actions:

  # k8s_manifest.ns_foo will be created
  + resource "k8s_manifest" "ns_foo" {
      + content = <<~EOT
            apiVersion: v1
            kind: Namespace
            metadata:
              name: foo
        EOT
      + id      = (known after apply)
    }

Plan: 1 to add, 0 to change, 0 to destroy.
...
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

=> OK

  • update namespace definition:
Terraform will perform the following actions:

  # k8s_manifest.ns_foo will be updated in-place
  ~ resource "k8s_manifest" "ns_foo" {
      ~ content = <<~EOT
            apiVersion: v1
            kind: Namespace
            metadata:
              name: foo
          +   attributes:
          +     foo: bar
        EOT
        id      = "::v1::Namespace::foo"
    }

Plan: 0 to add, 1 to change, 0 to destroy.

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

k8s_manifest.ns_foo: Modifying... [id=::v1::Namespace::foo]

Error: the namespace of the object (default) does not match the namespace on the request (foo)

  on test.tf line 68, in resource "k8s_manifest" "ns_foo":
  68: resource "k8s_manifest" "ns_foo" {

Sources:

$ cat k8s-test.template.yaml
apiVersion: v1
kind: Namespace
metadata:
  name: ${namespace}
  attributes:
    foo: bar

provider "k8s" {}

$ cat main.tf
resource "k8s_manifest" "ns_foo" {
  content = templatefile("${path.module}/k8s-test.template.yaml", {
    namespace = "foo"
  })
}

Expected behavior
Handling namespaces metadata update should work like for other resources.

Additional context
plugin version: v0.7.2 linux

More tests
I tried to force the namespace argument to foo since the error message talked about default:

Terraform will perform the following actions:

  # k8s_manifest.ns_foo must be replaced
-/+ resource "k8s_manifest" "ns_foo" {
        content   = <<~EOT
            apiVersion: v1
            kind: Namespace
            metadata:
              name: foo
              attributes:
                foo: bar
        EOT
      ~ id        = "::v1::Namespace::foo" -> (known after apply)
      + namespace = "foo" # forces replacement
    }

Plan: 1 to add, 0 to change, 1 to destroy.

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

k8s_manifest.ns_foo: Destroying... [id=::v1::Namespace::foo]
k8s_manifest.ns_foo: Still destroying... [id=::v1::Namespace::foo, 10s elapsed]
k8s_manifest.ns_foo: Destruction complete after 10s
k8s_manifest.ns_foo: Creating...
k8s_manifest.ns_foo: Creation complete after 6s [id=::v1::Namespace::foo]

Apply complete! Resources: 1 added, 0 changed, 1 destroyed.

=> it deleted the whole namespace before recreating it, not a good solution...

Also, it didn't fix anything: when changing again a metetadata it failed again:

Terraform will perform the following actions:

  # k8s_manifest.ns_foo will be updated in-place
  ~ resource "k8s_manifest" "ns_foo" {
      ~ content   = <<~EOT
            apiVersion: v1
            kind: Namespace
            metadata:
              name: foo
              attributes:
          -     foo: bar
          +     foo: bar2
        EOT
        id        = "::v1::Namespace::foo"
        namespace = "foo"
    }

Plan: 0 to add, 1 to change, 0 to destroy.

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

k8s_manifest.ns_foo: Modifying... [id=::v1::Namespace::foo]

Error: the namespace of the object (default) does not match the namespace on the request (foo)

  on test.tf line 68, in resource "k8s_manifest" "ns_foo":
  68: resource "k8s_manifest" "ns_foo" {