hashicorp / terraform-provider-google

Terraform Provider for Google Cloud Platform
https://registry.terraform.io/providers/hashicorp/google/latest/docs
Mozilla Public License 2.0
2.35k stars 1.75k forks source link

cloud_sql_instance volumes in google_cloud_run_v2_service inconsistent ordering #20360

Open lyricnz opened 5 hours ago

lyricnz commented 5 hours ago

Community Note

Terraform Version & Provider Version(s)

❯ terraform version
OpenTofu v1.8.5
on darwin_amd64
+ provider registry.opentofu.org/hashicorp/google v6.11.2

Affected Resource(s)

google_cloud_run_v2_service

Terraform Configuration

resource "google_cloud_run_v2_service" "default" {
  name = "cloudrun-service"

  location            = "us-central1"
  project             = "innablr-dev"

  template {
    execution_environment = "EXECUTION_ENVIRONMENT_GEN2"

    containers {
      image = "us-docker.pkg.dev/cloudrun/container/hello"
      volume_mounts {
        name       = "bucket"
        mount_path = "/var/www"
      }
      volume_mounts {
        name       = "cloudsql"
        mount_path = "/cloudsql"
      }
    }

    volumes {
      name = "cloudsql"
      cloud_sql_instance {
        instances = ["innablr-dev:australia-southeast2:innablr-dev"]
      }
    }

    volumes {
      name = "bucket"
      gcs {
        bucket    = google_storage_bucket.default.name
        read_only = false
      }
    }
  }
}

resource "google_storage_bucket" "default" {
  name     = "innablr-test-junk-cloudrun-service"
  location = "US"
  project  = "innablr-dev"
}

Debug Output

terraform2.log

Expected Behavior

terraform plan should show no changes

Actual Behavior

terraform plan wants to swap the order of the volumes. even if I apply this, the next plan/apply is the same.

❯ TF_LOG=debug TF_LOG_PATH=terraform2.log terraform apply
google_storage_bucket.default: Refreshing state... [id=innablr-test-junk-cloudrun-service]
google_cloud_run_v2_service.default: Refreshing state... [id=projects/innablr-dev/locations/us-central1/services/cloudrun-service]

OpenTofu used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  ~ update in-place

OpenTofu will perform the following actions:

  # google_cloud_run_v2_service.default will be updated in-place
  ~ resource "google_cloud_run_v2_service" "default" {
      ~ deletion_protection     = false -> true
        id                      = "projects/innablr-dev/locations/us-central1/services/cloudrun-service"
        name                    = "cloudrun-service"
        # (26 unchanged attributes hidden)

      ~ template {
            # (7 unchanged attributes hidden)

          ~ volumes {
              ~ name = "bucket" -> "cloudsql"

              + cloud_sql_instance {
                  + instances = [
                      + "innablr-dev:australia-southeast2:innablr-dev",
                    ]
                }

              - gcs {
                  - bucket    = "innablr-test-junk-cloudrun-service" -> null
                  - read_only = false -> null
                }
            }
          ~ volumes {
              ~ name = "cloudsql" -> "bucket"

              - cloud_sql_instance {
                  - instances = [
                      - "innablr-dev:australia-southeast2:innablr-dev",
                    ] -> null
                }

              + gcs {
                  + bucket    = "innablr-test-junk-cloudrun-service"
                  + read_only = false
                }
            }

            # (2 unchanged blocks hidden)
        }

        # (1 unchanged block hidden)
    }

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

Steps to reproduce

  1. terraform plan

Important Factoids

It looks like google-side API is returning the volumes in a particular order (cloudsql last?) which doesn't match what we're setting. If we SWAP the order of declaration of the volumes, then it plan/apply clean.

2024-11-16T15:58:17.128+1100 [DEBUG] provider.terraform-provider-google:     "volumes": [
2024-11-16T15:58:17.128+1100 [DEBUG] provider.terraform-provider-google:       {
2024-11-16T15:58:17.128+1100 [DEBUG] provider.terraform-provider-google:         "name": "bucket",
2024-11-16T15:58:17.128+1100 [DEBUG] provider.terraform-provider-google:         "gcs": {
2024-11-16T15:58:17.128+1100 [DEBUG] provider.terraform-provider-google:           "bucket": "innablr-test-junk-cloudrun-service"
2024-11-16T15:58:17.128+1100 [DEBUG] provider.terraform-provider-google:         }
2024-11-16T15:58:17.128+1100 [DEBUG] provider.terraform-provider-google:       },
2024-11-16T15:58:17.128+1100 [DEBUG] provider.terraform-provider-google:       {
2024-11-16T15:58:17.128+1100 [DEBUG] provider.terraform-provider-google:         "name": "cloudsql",
2024-11-16T15:58:17.128+1100 [DEBUG] provider.terraform-provider-google:         "cloudSqlInstance": {
2024-11-16T15:58:17.128+1100 [DEBUG] provider.terraform-provider-google:           "instances": [
2024-11-16T15:58:17.128+1100 [DEBUG] provider.terraform-provider-google: "innablr-dev:australia-southeast2:innablr-dev"
2024-11-16T15:58:17.128+1100 [DEBUG] provider.terraform-provider-google:           ]
2024-11-16T15:58:17.128+1100 [DEBUG] provider.terraform-provider-google:         }
2024-11-16T15:58:17.128+1100 [DEBUG] provider.terraform-provider-google:       }

The same thing happens with the volume_mounts attribute.

References

https://github.com/hashicorp/terraform-provider-google/issues/13368 https://github.com/GoogleCloudPlatform/magic-modules/pull/8929

wiktorn commented 2 hours ago

Looks like the API is returning the entries in the lexicographical order by name and requires the same order in the resource. While it is possible to comply with this requirement when using bare resources, it is impossible to do that within modules that use dynamic to provide module mounts depending on the inputs without requiring users to specify correct volume names.