From 245f7ad6bc5dd9fb90bafb3e916ccd113f8aa119 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      |  2 +-
 variables.tf | 13 ++-----------
 4 files changed, 11 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 01842b9..99ff371 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",
         )
 
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