From efc17cd57c435fb9a8e37f0af7f37857424de24c Mon Sep 17 00:00:00 2001
From: Arun Patel <ap2261@cam.ac.uk>
Date: Tue, 29 Jun 2021 13:02:11 +0100
Subject: [PATCH] Add explicit image name and reads current deployed image

This works with Terraform v0.14.10; the data source is read before
the plan walk, so the existing deployed image can be determined.
However, it is suggested that future Terraform may combine the
refresh and plan walks which *may* result in a cyclic dependancy.
---
 README.md    | 15 +--------------
 locals.tf    |  7 +++++++
 main.tf      | 10 +++++++++-
 variables.tf | 13 ++-----------
 4 files changed, 19 insertions(+), 26 deletions(-)

diff --git a/README.md b/README.md
index b556ecb..bf44158 100644
--- a/README.md
+++ b/README.md
@@ -171,18 +171,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 8b59663..5578dd3 100644
--- a/locals.tf
+++ b/locals.tf
@@ -23,4 +23,11 @@ locals {
     [trimsuffix(trimprefix(google_cloud_run_service.webapp.status[0].url, "https://"), "/")],
     local.can_monitor_custom_dns ? [var.dns_name] : []
   )
+
+  # 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 cd9dad2..0194526 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 4165abc..6544989 100644
--- a/variables.tf
+++ b/variables.tf
@@ -243,16 +243,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
 }
-- 
GitLab