-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Deprecate name_prefix
#1054
Comments
👍 |
@sl1pm4t here's what I'm thinking about for your scenario:
The |
Yeah, that could be a pretty elegant solution @danawillow. |
That matches my understanding. |
@danawillow I tried out the alternative and it works well. Thanks! |
I tested this flow out with a build of the random provider with my changes and it works:
where originally, Imported
The base64 encoding came from this script: https://play.golang.org/p/9KrkDoxRTOw
|
Fixed in #1035 |
@danawillow I'm in the process of updating my If I'm not mistaken, given instance templates are immutable, the combination of I appreciate the goal of simpler code, and the idea of reusable config blocks rather than bespoke and spotty name_prefix support, but I have to say this new pattern feels quite awkward/unintuitive for what I imagine is a pretty common use case. Here's an example: resource "random_id" "template" {
byte_length = 11
prefix = "${local.pool_id}-"
// This randomly generated ID is stable until one of the below variables
// changes. This means any variable that should trigger the creation of a new
// instance template when it changes must be defined here and read 'through'
// this block from the below google_compute_instance_template.
keepers = {
machine_type = "${var.machine_type}"
service_account_email = "${google_service_account.account.email}"
subnetwork = "${var.gcp_shared_vpc_subnet}"
subnetwork_project = "${var.gcp_shared_vpc_project}"
subnetwork_range_name = "${var.gcp_shared_vpc_secondary_ip_range}"
source_image = "${var.image}"
disk_type = "${var.disk_type}"
disk_size_gb = "${var.disk_size}"
tag_region = "${data.google_client_config.current.region}"
metadata_user-data = "${data.ignition_config.config.rendered}"
}
}
resource "google_compute_instance_template" "template" {
name = "${random_id.template.dec}"
// Instance templates cannot be updated. They must instead be destroyed and
// replaced. This block instructs Terraform that it must successfully create
// the updated template before it destroys the old one.
lifecycle = {
create_before_destroy = true
}
machine_type = "${random_id.template.keepers.machine_type}"
instance_description = "Kubernetes control instance for cluster ${var.cluster_id}"
service_account {
email = "${random_id.template.keepers.service_account_email}"
scopes = ["https://www.googleapis.com/auth/cloud-platform"]
}
// Ensure we can pull from GCR at boot time.
depends_on = ["google_project_iam_member.allow_gcr_pull"]
// Our primary interface is in the shared VPC, and can access the internet via
// its ephemeral external IP.
network_interface {
subnetwork = "${random_id.template.keepers.subnetwork}"
subnetwork_project = "${random_id.template.keepers.subnetwork_project}"
// This is Terraform for 'give this NIC an external IP too please'.
access_config {}
alias_ip_range {
subnetwork_range_name = "${random_id.template.keepers.subnetwork_range_name}"
ip_cidr_range = "/24"
}
}
// Required to accept packets for Alias IP ranges.
can_ip_forward = true
disk {
boot = true
source_image = "${random_id.template.keepers.source_image}"
disk_type = "${random_id.template.keepers.disk_type}"
disk_size_gb = "${random_id.template.keepers.disk_size_gb}"
}
// We use a local SSD for /var/lib/docker, i.e. where Docker stores images,
// containers, etc. Local SSDs are faster and cheaper than pd-ssds, but
// prevent us from being able to restart the instance they are attached to.
// Local SSDs are always 375GB.
// https://cloud.google.com/compute/docs/disks/local-ssd
disk {
device_name = "varlibdocker" // Will be exposed at /dev/varlibdocker
disk_type = "local-ssd"
type = "SCRATCH"
}
tags = [
"${var.cluster_id}",
"${local.pool_id}",
"terraflop",
"kubernetes",
// Required to route traffic to instances outside us-central1
"${random_id.template.keepers.tag_region}",
]
labels = {
component = "kubernetes"
cluster = "${var.cluster_id}"
}
metadata = {
component = "kubernetes"
cluster = "${var.cluster_id}"
block-project-ssh-keys = "TRUE"
user-data = "${random_id.template.keepers.metadata_user-data}"
}
} |
If other folks find themselves here, you might want to be aware of hashicorp/terraform-provider-random#21. It seems that reading variables 'through' the |
@negz great writeup. I've run into hashicorp/terraform-provider-random#21 as well. And also noticed that it doesn't seem So far it seems like the best option is to read the same values I pass into the random_id keepers. |
Hi, thx for the good write up. The main HashiCorp docs on google_compute_ssl_certificate still uses I used this code: resource "random_id" "certificate" {
byte_length = 4
prefix = "my-certificate-"
}
resource "google_compute_ssl_certificate" "default" {
name = "${random_id.certificate.hex}"
private_key = "${var.domain_key}"
certificate = "${var.domain_cert}"
lifecycle {
create_before_destroy = true
}
} |
@nyurik, what if random_id.hex begins with a number (0-9), not letter (a-f)? Names can't start with a number, AFAIR. |
@Chupaka there is a prefix, so the name would be |
@nyurik ah, sorry, misread :) |
@danawillow thx! We must have the older version. Would it still make sense to mention both approaches on the ssl docs page? I wasn't even aware of the random id generation until I saw the deprecation warning, and also when i tried |
Totally! If you open a PR with the proposed changes, I'd be happy to review it :) |
@danawillow thx, I created a PR at #1731 |
I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. If you feel I made an error 🤖 🙉 , please reach out to my human friends 👉 [email protected]. Thanks! |
Right now, a few of our resources support specifying the name in multiple ways:
name
field, which is for the name explicitlyname_prefix
field, which specifies a prefix and generates a random string for the restBecause of the existence of the random provider, we don't need to have this logic inside the google resources, and getting rid of it will make the entire provider more consistent (since only some resources support name_prefix anyway).
We can do this safely in two steps:
random_id
resource to allow setting its output to a user-specified value. This would allow users to transition to therandom_id
resource without needing to recreate their google resources, allowing them to do that transition on their own time instead of waiting for an opportunity when the resource would need to be recreated to make the change.name_prefix
field and make it computed so users can remove it from their configs without recreating the resource@rosbo @paddycarver to make sure this checks out based on our conversation
The text was updated successfully, but these errors were encountered: