diff --git a/README.md b/README.md index f77daa62adf6b03969fad3c59a996b3b08e8d252..35184a2d7e1a6531d8b840c101e43b68091c64a8 100644 --- a/README.md +++ b/README.md @@ -181,18 +181,5 @@ module "webapp" { Originally, the module did not deploy images except on the very first use (using `gcr.io/cloudrun/hello:latest`). -Currently, deploy the image from `image_name` if specified, otherwise keep the +Currently, the module deploys the image from `image_name` if specified, otherwise keep the current image, if that doesn't exist fall-back to the "hello" image. - -The name of the currently deployed image needs to be sourced outside of -this module and supplied as `existing_image_name` to prevent a cyclic dependency -problem. This can be extracted from an existing deployment and included in the -root module with: - -```tf -existing_image_name = ( - data.google_cloud_run_service.webapp.template != null ? - data.google_cloud_run_service.webapp.template[0].spec[0].containers[0].image : - "" -) -``` diff --git a/locals.tf b/locals.tf index ce0b188ba911aa7a69fb843aec59e06d4b73be2b..07c2f120a4d1717eed2897d20cef31429482bca7 100644 --- a/locals.tf +++ b/locals.tf @@ -51,4 +51,11 @@ locals { [trimsuffix(trimprefix(google_cloud_run_service.webapp.status[0].url, "https://"), "/")], var.allow_unauthenticated_invocations ? local.dns_names : [], ) + + # If a cloud run revision is present, get the full URL of the deployed image. + existing_image_name = ( + data.google_cloud_run_service.webapp.template != null ? + data.google_cloud_run_service.webapp.template[0].spec[0].containers[0].image : + "" + ) } diff --git a/main.tf b/main.tf index bde013e84f6a152dfca1bcd0e18598f35562abdb..e21642c8368409e506d59ca5a2ced51dd082cf5b 100644 --- a/main.tf +++ b/main.tf @@ -113,7 +113,7 @@ resource "google_cloud_run_service" "webapp" { containers { image = coalesce( var.image_name, - var.existing_image_name, + local.existing_image_name, "gcr.io/cloudrun/hello:latest", ) @@ -275,3 +275,11 @@ module "uptime_monitoring" { google = google.stackdriver } } + +# This extracts information about any currently running Cloud Run revision before +# starting the plan walk. This is current behaviour, but may change in future see +# https://github.com/hashicorp/terraform/issues/17034. +data "google_cloud_run_service" "webapp" { + name = "webapp" + location = var.cloud_run_region +} diff --git a/variables.tf b/variables.tf index 1336827c248689b56a481fad7510334d54155e36..27b9a281ee243f5a56c28a6239c76fabc701d109 100644 --- a/variables.tf +++ b/variables.tf @@ -308,16 +308,7 @@ variable "image_name" { type = string default = "" description = <<-EOL - The docker image that will be deployed. If unset, the value of existing_image_name - is preferred. -EOL -} - -variable "existing_image_name" { - type = string - default = "" - description = <<-EOL - The image currently deployed, which needs to be passed into this module to - avoid a cyclic dependency. If unset, the hello container will be deployed. + Optional. The docker image that will be deployed. If unset, the value of + existing_image_name is preferred. EOL }