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

for_each in rancher2_registry keeps changing the order of the list that has not changed

Open sarg3nt opened this issue 2 years ago • 0 comments

Each time we terraform apply registries keep getting updated because the list order is getting changed. Our local data is not changing, you can terraform apply over and over and watch it flip back and forth. Very annoying because we deploy this with CD and it's saying there are changes every time when there are not.

Here's our code:

variable "containerRepositories" {
  type = list(object({
    url      = string
    username = string
    password = string
  }))
  default     = []
  description = "A list of Docker container repositories."
}

 containerRepositories = [
    {
      url      = "dcc-docker-release.<redacted>"
      username = "bld_dcc"
      password = "<redacted>"
    },
    {
      url      = "dcc-docker-dev.<redacted>"
      username = "bld_dcc"
      password = "<redacted>"
    },
    {
      url      = "dcsws-docker-release.<redacted>"
      username = "bld_dcc"
      password = "<redacted>"
    },
    {
      url      = "dcsws-docker-dev.<redacted>"
      username = "bld_dcc"
      password = "<redacted>"
    }
  ]

resource "rancher2_registry" "regcred-build" {
  count        = length(var.containerRepositories) > 0 ? 1 : 0
  name         = "regcred"
  description  = "Registry entry for k8s to talk to customers Artifactory registries"
  project_id   = rancher2_project.jenkins-project-build.id
  namespace_id = rancher2_namespace.jenkins-namespace-build.id
  dynamic "registries" {
    for_each = var.containerRepositories
    content {
      address  = registries.value["url"]
      username = registries.value["username"]
      password = registries.value["password"]
    }
  }
}

The result of running terraform apply when there have been no updates. If we apply this, then run it again, they will “switch back”

~ resource "rancher2_registry" "regcred" {
        id           = "jenkins-dcs:regcred"
        name         = "regcred"
        # (5 unchanged attributes hidden)

      ~ registries {
          ~ address  = "dcsws-docker-release.<redacted>" -> "dcc-docker-release.<redacted>"
            # (2 unchanged attributes hidden)
        }
      ~ registries {
          ~ address  = "dcc-docker-release.<redacted>" -> "dcsws-docker-release.<redacted>"
            # (2 unchanged attributes hidden)
        }
        # (2 unchanged blocks hidden)
    }

We are running Terraform v1.1.9 and rancher/rancher2 v1.23.0

I posted this on the Terraform forum here https://discuss.hashicorp.com/t/for-each-keeps-changing-the-order-of-a-list-that-has-not-changed/39440 And was told it's a bug in the provider.

Thoughts?

sarg3nt avatar May 12 '22 20:05 sarg3nt