From 5e17b335efb441e7bcef732b405c948fb9322a68 Mon Sep 17 00:00:00 2001
From: Abraham Martin <amc203@cam.ac.uk>
Date: Fri, 14 Jul 2023 19:16:31 +0100
Subject: [PATCH] Add support for a http_get.value["http_headers"]

---
 main.tf      | 39 +++++++++++++++++++++++++++++++++++++++
 variables.tf | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 87 insertions(+)

diff --git a/main.tf b/main.tf
index b0eadf3..7464582 100644
--- a/main.tf
+++ b/main.tf
@@ -112,6 +112,45 @@ resource "google_cloud_run_service" "webapp" {
       containers {
         image = var.image_name
 
+        dynamic "startup_probe" {
+          for_each = var.startup_probe != {} ? [var.startup_probe] : []
+          content {
+            initial_delay_seconds = startup_probe.value["initial_delay_seconds"]
+            timeout_seconds       = startup_probe.value["timeout_seconds"]
+            period_seconds        = startup_probe.value["period_seconds"]
+            failure_threshold     = startup_probe.value["failure_threshold"]
+            dynamic "tcp_socket" {
+              # Check if tcp_socket has been defined in the startup_probe variable
+              for_each = contains(keys(startup_probe.value), "tcp_socket") ? [startup_probe.value["tcp_socket"]] : []
+              content {
+                port = tcp_socket.value["port"]
+              }
+            }
+            dynamic "http_get" {
+              for_each = contains(keys(startup_probe.value), "http_get") ? [startup_probe.value["http_get"]] : []
+              content {
+                path = http_get.value["path"]
+                # port = http_get.value["port"]
+                # port only supported on latest version of Google Cloud Terraform module
+                dynamic "http_headers" {
+                  for_each = contains(keys(http_get.value), "http_headers") ? http_get.value["http_headers"] : []
+                  content {
+                    name  = http_headers.value["name"]
+                    value = http_headers.value["value"]
+                  }
+                }
+              }
+            }
+            dynamic "grpc" {
+              for_each = contains(keys(startup_probe.value), "grpc") ? [startup_probe.value["grpc"]] : []
+              content {
+                port    = grpc.value["port"]
+                service = grpc.value["service"]
+              }
+            }
+          }
+        }
+
         resources {
           limits = {
             cpu    = var.cpu_limit
diff --git a/variables.tf b/variables.tf
index 880a158..d833690 100644
--- a/variables.tf
+++ b/variables.tf
@@ -435,3 +435,51 @@ variable "image_name" {
     error_message = "The image_name value must be a valid URL to a container image."
   }
 }
+
+variable "startup_probe" {
+  type = object({
+    initial_delay_seconds = optional(number) # Number of seconds after the container has started before
+    # the probe is initiated. Defaults to 0 seconds. Minimum value is 0. Maximum value is 240.
+    timeout_seconds = optional(number) # Number of seconds after which the probe times out. Defaults
+    # to 1 second. Minimum value is 1. Maximum value is 3600. Must be smaller than periodSeconds.
+    period_seconds = optional(number) # How often (in seconds) to perform the probe. Default to 10 seconds.
+    # Minimum value is 1. Maximum value is 240.
+    failure_threshold = optional(number) #  Minimum consecutive failures for the probe to be considered
+    # failed after having succeeded. Defaults to 3. Minimum value is 1.
+    tcp_socket = optional(object({
+      port = optional(number) # (Optional) Port number to access on the container. Number must be in the
+      # range 1 to 65535. If not specified, defaults to the same value as container.ports[0].containerPort.
+    }))
+    # (Optional) TcpSocket specifies an action involving a TCP port. Structure is documented below.
+    http_get = optional(object({
+      path = optional(string) # (Optional) Path to access on the HTTP server. Defaults to /.
+
+      # port only supported on latest version of Google Cloud Terraform module
+      # port = optional(number) # (Optional) Port number to access on the container. Number must be in
+      # the range 1 to 65535. If not specified, defaults to the same value as container.ports[0].containerPort.
+
+      http_headers = optional(list(object({
+        name  = string           # (Optional) The header field name
+        value = optional(string) # (Optional) The header field value
+      })))
+      # (Optional) Custom headers to set in the request. HTTP allows repeated headers.
+    }))
+    # (Optional) HttpGet specifies the http request to perform. Structure is documented below.
+    grpc = optional(object({
+      port = optional(number) # (Optional) Port number to access on the container. Number must be in the
+      # range 1 to 65535. If not specified, defaults to the same value as container.ports[0].containerPort.
+      service = optional(string) # (Optional) The name of the service to place in the gRPC HealthCheckRequest
+      # (see https://github.com/grpc/grpc/blob/master/doc/health-checking.md).
+      # If this is not specified, the default behavior is defined by gRPC.
+    }))
+    # (Optional) GRPC specifies an action involving a GRPC port. Structure is documented below.
+  })
+
+  default     = {}
+  description = <<-EOL
+    Optional. Startup probe of application within the container. 
+    All other probes are disabled if a startup probe is provided, until it succeeds. 
+    Container will not be added to service endpoints if the probe fails. 
+    Structure is documented at https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/cloud_run_service#nested_startup_probe
+EOL
+}
-- 
GitLab