Skip to content

Commit

Permalink
feat: add support for liveness and readiness probes (GoogleCloudPlatf…
Browse files Browse the repository at this point in the history
…orm#101)

Co-authored-by: Prabhu <18209477+prabhu34@users.noreply.github.com>
Co-authored-by: Amanda Karina Lopes de Oliveira <amandak@ciandt.com>
Co-authored-by: Andrew Peabody <andrewpeabody@google.com>
Co-authored-by: Grant Sorbo <gtsorbo@google.com>
  • Loading branch information
5 people authored Aug 4, 2023
1 parent b6ba267 commit 0299bfe
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ module "cloud_run" {
| generate\_revision\_name | Option to enable revision name generation | `bool` | `true` | no |
| image | GCR hosted image URL to deploy | `string` | n/a | yes |
| limits | Resource limits to the container | `map(string)` | `null` | no |
| liveness\_probe | Periodic probe of container liveness. Container will be restarted if the probe fails.<br>More info: https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle#container-probes | <pre>object({<br> failure_threshold = optional(number, null)<br> initial_delay_seconds = optional(number, null)<br> timeout_seconds = optional(number, null)<br> period_seconds = optional(number, null)<br> http_get = optional(object({<br> path = optional(string)<br> http_headers = optional(list(object({<br> name = string<br> value = string<br> })), null)<br> }), null)<br> grpc = optional(object({<br> port = optional(number)<br> service = optional(string)<br> }), null)<br> })</pre> | `null` | no |
| location | Cloud Run service deployment location | `string` | n/a | yes |
| members | Users/SAs to be given invoker access to the service | `list(string)` | `[]` | no |
| ports | Port which the container listens to (http1 or h2c) | <pre>object({<br> name = string<br> port = number<br> })</pre> | <pre>{<br> "name": "http1",<br> "port": 8080<br>}</pre> | no |
Expand All @@ -72,6 +73,7 @@ module "cloud_run" {
| service\_annotations | Annotations to the service. Acceptable values all, internal, internal-and-cloud-load-balancing | `map(string)` | <pre>{<br> "run.googleapis.com/ingress": "all"<br>}</pre> | no |
| service\_labels | A set of key/value label pairs to assign to the service | `map(string)` | `{}` | no |
| service\_name | The name of the Cloud Run service to create | `string` | n/a | yes |
| startup\_probe | Startup probe of application within the container.<br>All other probes are disabled if a startup probe is provided, until it succeeds.<br>Container will not be added to service endpoints if the probe fails.<br>More info: https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle#container-probes | <pre>object({<br> failure_threshold = optional(number, null)<br> initial_delay_seconds = optional(number, null)<br> timeout_seconds = optional(number, null)<br> period_seconds = optional(number, null)<br> http_get = optional(object({<br> path = optional(string)<br> http_headers = optional(list(object({<br> name = string<br> value = string<br> })), null)<br> }), null)<br> tcp_socket = optional(object({<br> port = optional(number)<br> }), null)<br> grpc = optional(object({<br> port = optional(number)<br> service = optional(string)<br> }), null)<br> })</pre> | `null` | no |
| template\_annotations | Annotations to the container metadata including VPC Connector and SQL. See [more details](https://cloud.google.com/run/docs/reference/rpc/google.cloud.run.v1#revisiontemplate) | `map(string)` | <pre>{<br> "autoscaling.knative.dev/maxScale": 2,<br> "autoscaling.knative.dev/minScale": 1,<br> "generated-by": "terraform",<br> "run.googleapis.com/client-name": "terraform"<br>}</pre> | no |
| template\_labels | A set of key/value label pairs to assign to the container metadata | `map(string)` | `{}` | no |
| timeout\_seconds | Timeout for each request | `number` | `120` | no |
Expand Down
66 changes: 66 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,72 @@ resource "google_cloud_run_service" "main" {
requests = var.requests
}

dynamic "startup_probe" {
for_each = var.startup_probe != null ? [1] : []
content {
failure_threshold = var.startup_probe.failure_threshold
initial_delay_seconds = var.startup_probe.initial_delay_seconds
timeout_seconds = var.startup_probe.timeout_seconds
period_seconds = var.startup_probe.period_seconds
dynamic "http_get" {
for_each = var.startup_probe.http_get != null ? [1] : []
content {
path = var.startup_probe.http_get.path
dynamic "http_headers" {
for_each = var.startup_probe.http_get.http_headers != null ? var.startup_probe.http_get.http_headers : []
content {
name = http_headers.value["name"]
value = http_headers.value["value"]
}
}
}
}
dynamic "tcp_socket" {
for_each = var.startup_probe.tcp_socket != null ? [1] : []
content {
port = var.startup_probe.tcp_socket.port
}
}
dynamic "grpc" {
for_each = var.startup_probe.grpc != null ? [1] : []
content {
port = var.startup_probe.grpc.port
service = var.startup_probe.grpc.service
}
}
}
}

dynamic "liveness_probe" {
for_each = var.liveness_probe != null ? [1] : []
content {
failure_threshold = var.liveness_probe.failure_threshold
initial_delay_seconds = var.liveness_probe.initial_delay_seconds
timeout_seconds = var.liveness_probe.timeout_seconds
period_seconds = var.liveness_probe.period_seconds
dynamic "http_get" {
for_each = var.liveness_probe.http_get != null ? [1] : []
content {
path = var.liveness_probe.http_get.path
dynamic "http_headers" {
for_each = var.liveness_probe.http_get.http_headers != null ? var.liveness_probe.http_get.http_headers : []
content {
name = http_headers.value["name"]
value = http_headers.value["value"]
}
}
}
}
dynamic "grpc" {
for_each = var.liveness_probe.grpc != null ? [1] : []
content {
port = var.liveness_probe.grpc.port
service = var.liveness_probe.grpc.service
}
}
}
}

dynamic "env" {
for_each = var.env_vars
content {
Expand Down
54 changes: 54 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,60 @@ variable "container_command" {
default = []
}

variable "startup_probe" {
type = object({
failure_threshold = optional(number, null)
initial_delay_seconds = optional(number, null)
timeout_seconds = optional(number, null)
period_seconds = optional(number, null)
http_get = optional(object({
path = optional(string)
http_headers = optional(list(object({
name = string
value = string
})), null)
}), null)
tcp_socket = optional(object({
port = optional(number)
}), null)
grpc = optional(object({
port = optional(number)
service = optional(string)
}), null)
})
default = null
description = <<-EOF
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.
More info: https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle#container-probes
EOF
}

variable "liveness_probe" {
type = object({
failure_threshold = optional(number, null)
initial_delay_seconds = optional(number, null)
timeout_seconds = optional(number, null)
period_seconds = optional(number, null)
http_get = optional(object({
path = optional(string)
http_headers = optional(list(object({
name = string
value = string
})), null)
}), null)
grpc = optional(object({
port = optional(number)
service = optional(string)
}), null)
})
default = null
description = <<-EOF
Periodic probe of container liveness. Container will be restarted if the probe fails.
More info: https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle#container-probes
EOF
}
variable "env_vars" {
type = list(object({
value = string
Expand Down
2 changes: 1 addition & 1 deletion versions.tf
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

terraform {
required_version = ">= 0.13"
required_version = ">= 1.3"

required_providers {
google = {
Expand Down

0 comments on commit 0299bfe

Please sign in to comment.