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

Create secondary_ip_range without compute_subnetwork #6663

Open eraac opened 4 years ago

eraac commented 4 years ago

Community Note

Description

Add a new resource allowing to manage the secondary ip ranges in a subnetwork without the resource google_compute_subnetwork.

If you've an VPC with auto subnetworks on, you don't manage the subnetwork (meaning you don't have the resource in your terraform).

New or Affected Resource(s)

Potential Terraform Configuration

resource "google_compute_subnetwork_secondary_ip_range" "secondary_range" {
  subnetwork = "..."
  name = "..."
  cidr_range = ".../xx"
}

Workaround

resource "null_resource" "secondary_ip_range" {
  provisioner "local-exec" {
    command = format(
      "gcloud --project %s compute networks subnets update %s --region %s --add-secondary-ranges %s=%s,%s=%s",
    )
  }

  provisioner "local-exec" {
    when = destroy

    command = format(
      "gcloud --project %s compute networks subnets update %s --region %s --remove-secondary-ranges %s,%s",
    )
  }
}
eraac commented 3 years ago

Just find my own issue after facing the same problem again πŸ˜…

chrisghill commented 2 years ago

Running into this issue when trying to create a GKE module. The secondary ranges are specifically for the GKE pods and services, so it would be nice to be able to provision them and destroy them alongside the GKE cluster itself.

It seems the situation is a bit similar to the AWS route_table/route or security_group/security_group_rule resources. The AWS provider solves it by allowing you to provision the route_table resource, then separately you can provision individual routes within the route table. That same pattern could be used here, where the google_compute_subnetwork is provisioned as part of a network module, and the suggested google_compute_subnetwork_secondary_ip_range could be provisioned alongside the resource that uses it (google_container_cluster for example).

guhcampos commented 2 years ago

A new use case: I have a terraform config which must be applied in two different environments - say staging and production and for some reason (preexisting config, for example) the address ranges are not exactly the same in a subnet. Staging might have a different number of secondary ranges than Production, or in different order.

This makes it impossible to use an already bad strategy such as:

resource "google_container_cluster" "potato" {
  ...

  ip_allocation_policy {
    cluster_secondary_range_name  = data.google_compute_subnetwork.my_subnet.secondary_ip_range.0.range_name
    services_secondary_range_name = data.google_compute_subnetwork.my_subnet.secondary_ip_range.1.range_name  
  }
}

Ideally, I should be able to query the ranges individually, so in different environments I could do:

data "google_compute_subnetwork_secondary_ip_range" "pod_range" {
  network = ...
  subnet = ...
  cidr = "10.0.0.0/24" 
  # OR
  name = "pod-range-1234"
}
resource "google_container_cluster" "potato" {
  ...

  ip_allocation_policy {
    cluster_secondary_range_name  = data.google_compute_subnetwork_secondary_ip_range.pod_range.name
  }
}