From c96e4317fb566735d8fe4de08ee21e200e3beec6 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 f77daa6..35184a2 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 ce0b188..07c2f12 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 bde013e..e21642c 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 1336827..27b9a28 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
 }
-- 
GitLab