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.34k stars 1.74k forks source link

permadiff when setting target_size = 0 inside version block in google_compute_region_instance_group_manager #8949

Open bamartos opened 3 years ago

bamartos commented 3 years ago

Community Note

Terraform Version

Terraform v0.15.0

Affected Resource(s)

Terraform Configuration Files

# Copy-paste your Terraform configurations here.
#
# For large Terraform configs, please use a service like Dropbox and share a link to the ZIP file.
# For security, you can also encrypt the files using our GPG public key:
#    https://www.hashicorp.com/security
#
# If reproducing the bug involves modifying the config file (e.g., apply a config,
# change a value, apply the config again, see the bug), then please include both:
# * the version of the config before the change, and
# * the version of the config after the change.

resource "google_compute_region_instance_group_manager" "default" {

  name               = "test"
  base_instance_name = "tests-igm"
  region             = "europe-west1"
  description        = "Test"
  target_size        = 1
  project            = "my-project"

  update_policy {
    type                         = "OPPORTUNISTIC"
    minimal_action               = "REPLACE"
    instance_redistribution_type = "NONE"
    replacement_method           = "SUBSTITUTE"
    max_surge_fixed              = 3
    max_unavailable_fixed        = 3
  }

  version {
    name              = "default"
    instance_template = google_compute_instance_template.default.id
  }

  version {
    name              = "canary"
    instance_template = google_compute_instance_template.default.id
    target_size {
      percent = 0
    }
  }
}

resource "google_compute_instance_template" "default" {

  name         = "default-instance-template"
  machine_type = "f1-micro"
  project      = "my-project"

  disk {
    auto_delete  = true
    boot         = true
    source_image = "https://www.googleapis.com/compute/v1/projects/ubuntu-os-cloud/global/images/ubuntu-2004-focal-v20200729"
  }

  network_interface {
    network = "default"
  }
}

resource "google_compute_instance_template" "canary" {
  name         = "canary-instance-template"
  machine_type = "f1-micro"
  project      = "my-project"

  disk {
    auto_delete  = true
    boot         = true
    source_image = "https://www.googleapis.com/compute/v1/projects/ubuntu-os-cloud/global/images/ubuntu-2004-focal-v20200729"
  }

  network_interface {
    network = "default"
  }
}

Debug Output

Panic Output

Expected Behavior

Terraform applies normal the configuration the first time (1 instance group manager + 2 instance template versions), but when try to re-apply it shouldn't have any diff to apply.

Actual Behavior

Perma-diff the target_size

  # google_compute_region_instance_group_manager.default will be updated in-place
  ~ resource "google_compute_region_instance_group_manager" "default" {
        id                               = "myproject"
        name                             = "test"
        # (12 unchanged attributes hidden)

      ~ version {
            name              = "canary"
            # (1 unchanged attribute hidden)

          + target_size {
              + percent = 0
            }
        }
        # (2 unchanged blocks hidden)
    }

Steps to Reproduce

  1. terraform apply

Important Factoids

References

venkykuberan commented 3 years ago

version.target_size.percent will be set in the tf state only if its value greater than 0. Please use the percent value > 0 or not use in the config to avoid permadiff.

bamartos commented 3 years ago

Hey, if I will not use it, it will result an error since when you define multiple versions you need to provide at least one target size.

Using target size > 0 it will not create permadiff but it doesn't help on my use case.

Setting target_size = 0 it works and solves my usecase but it results in a permadiff thus the reason of this bug report.

Thank you

venkykuberan commented 3 years ago

@megan07 should we consider >= 0 here https://github.com/hashicorp/terraform-provider-google/blob/f4cc59ec3ab2859f424aa1d604f8b29af8f4c36b/google/resource_compute_instance_group_manager.go#L426?

What are your thoughts ?

megan07 commented 3 years ago

I'm taking a look at this. It's a bit more complicated since the API returns 0 for both percent and fixed when the entire block is unset, so adding >= 0 there will break any configs (in an undesirable way) that have target_size unset. We will need to fix this to allow for 0, I'm thinking we might need a diff suppress function with it, but haven't decided if that's the best approach yet or not.

zraider7 commented 2 years ago

@megan07 Our team is also running into this and it is tedious when investigating drift to find that it is a false alarm. Has there been any update on this?