Skip to content

Commit

Permalink
Added capability support for startupProbe, livenessProbe, and readine…
Browse files Browse the repository at this point in the history
…ssProbe. (#10)
  • Loading branch information
BSick7 authored Mar 29, 2024
1 parent ca75e23 commit 6f5e3ac
Show file tree
Hide file tree
Showing 5 changed files with 199 additions and 31 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# 0.6.17 (Mar 29, 2024)
* Added capability support for `startupProbe`, `readinessProbe`, and `livenessProbe`.

# 0.6.16 (Mar 27, 2024)
* Fixed service name from being blank.

Expand Down
1 change: 1 addition & 0 deletions app.tf
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ locals {
service_account_id = google_service_account.app.id
service_account_email = google_service_account.app.email
service_name = local.service_name
container_port = var.container_port
service_port = var.service_port
internal_subdomain = var.service_port == 0 ? "" : "${local.block_name}.${local.kubernetes_namespace}.svc.cluster.local"
})
Expand Down
165 changes: 146 additions & 19 deletions deployment.tf
Original file line number Diff line number Diff line change
Expand Up @@ -89,34 +89,161 @@ resource "kubernetes_deployment_v1" "this" {
}
}

dynamic "liveness_probe" {
for_each = local.has_service ? [1] : []
dynamic "startup_probe" {
for_each = local.startup_probes
iterator = sp

content {
failure_threshold = 3
success_threshold = 1
initial_delay_seconds = var.readiness_delay
period_seconds = 10
timeout_seconds = 1

tcp_socket {
port = var.container_port
initial_delay_seconds = sp.value.initial_delay_seconds
period_seconds = sp.value.period_seconds
timeout_seconds = sp.value.timeout_seconds
success_threshold = sp.value.success_threshold
failure_threshold = sp.value.failure_threshold

dynamic "exec" {
for_each = sp.value.exec
content {
command = exec.value.command
}
}

dynamic "grpc" {
for_each = sp.value.grpc
content {
port = grpc.value.port
service = lookup(grpc.value, "service", null)
}
}

dynamic "tcp_socket" {
for_each = sp.value.tcp_socket
content {
port = tcp_socket.value.port
}
}

dynamic "http_get" {
for_each = sp.value.http_get
content {
host = lookup(http_get.value, "host", null)
path = lookup(http_get.value, "path", null)
port = lookup(http_get.value, "port", null)
scheme = lookup(http_get.value, "scheme", null)

dynamic "http_header" {
for_each = coalesce(tomap(lookup(http_get.value, "http_headers", "null")), tomap({}))
content {
name = http_header.key
value = http_header.value
}
}
}
}
}
}

dynamic "readiness_probe" {
for_each = local.has_service ? [1] : []
for_each = local.readiness_probes
iterator = sp

content {
initial_delay_seconds = sp.value.initial_delay_seconds
period_seconds = sp.value.period_seconds
timeout_seconds = sp.value.timeout_seconds
success_threshold = sp.value.success_threshold
failure_threshold = sp.value.failure_threshold

dynamic "exec" {
for_each = sp.value.exec
content {
command = exec.value.command
}
}

dynamic "grpc" {
for_each = sp.value.grpc
content {
port = grpc.value.port
service = lookup(grpc.value, "service", null)
}
}

dynamic "tcp_socket" {
for_each = sp.value.tcp_socket
content {
port = tcp_socket.value.port
}
}

dynamic "http_get" {
for_each = sp.value.http_get
content {
host = lookup(http_get.value, "host", null)
path = lookup(http_get.value, "path", null)
port = lookup(http_get.value, "port", null)
scheme = lookup(http_get.value, "scheme", null)

dynamic "http_header" {
for_each = coalesce(tomap(lookup(http_get.value, "http_headers", "null")), tomap({}))
content {
name = http_header.key
value = http_header.value
}
}
}
}
}
}

dynamic "liveness_probe" {
for_each = local.liveness_probes
iterator = lp

content {
failure_threshold = 3
success_threshold = 1
initial_delay_seconds = var.readiness_delay
period_seconds = 10
timeout_seconds = 1

tcp_socket {
port = var.container_port
initial_delay_seconds = lp.value.initial_delay_seconds
period_seconds = lp.value.period_seconds
timeout_seconds = lp.value.timeout_seconds
success_threshold = lp.value.success_threshold
failure_threshold = lp.value.failure_threshold

dynamic "exec" {
for_each = lp.value.exec
content {
command = exec.value.command
}
}

dynamic "grpc" {
for_each = lp.value.grpc
content {
port = grpc.value.port
service = lookup(grpc.value, "service", null)
}
}

dynamic "tcp_socket" {
for_each = lp.value.tcp_socket
content {
port = tcp_socket.value.port
}
}

dynamic "http_get" {
for_each = lp.value.http_get
content {
host = lookup(http_get.value, "host", null)
path = lookup(http_get.value, "path", null)
port = lookup(http_get.value, "port", null)
scheme = lookup(http_get.value, "scheme", null)

dynamic "http_header" {
for_each = coalesce(tomap(lookup(http_get.value, "http_headers", "null")), tomap({}))
content {
name = http_header.key
value = http_header.value
}
}
}
}
}
}
Expand Down
48 changes: 48 additions & 0 deletions probes.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
locals {
cap_startup_probes = lookup(local.capabilities, "startup_probes", [])
cap_readiness_probes = lookup(local.capabilities, "readiness_probes", [])
cap_liveness_probes = lookup(local.capabilities, "liveness_probes", [])

startup_probes = [
for p in local.cap_startup_probes : {
initial_delay_seconds = lookup(p, "initial_delay_seconds")
period_seconds = lookup(p, "period_seconds")
timeout_seconds = lookup(p, "timeout_seconds")
success_threshold = lookup(p, "success_threshold")
failure_threshold = lookup(p, "failure_threshold")

exec = [for x in compact([lookup(p, "exec", null)]) : jsondecode(x)]
grpc = [for x in compact([lookup(p, "grpc", null)]) : jsondecode(x)]
http_get = [for x in compact([lookup(p, "http_get", null)]) : jsondecode(x)]
tcp_socket = [for x in compact([lookup(p, "tcp_socket", null)]) : jsondecode(x)]
}
]
readiness_probes = [
for p in local.cap_readiness_probes : {
initial_delay_seconds = lookup(p, "initial_delay_seconds")
period_seconds = lookup(p, "period_seconds")
timeout_seconds = lookup(p, "timeout_seconds")
success_threshold = lookup(p, "success_threshold")
failure_threshold = lookup(p, "failure_threshold")

exec = [for x in compact([lookup(p, "exec", null)]) : jsondecode(x)]
grpc = [for x in compact([lookup(p, "grpc", null)]) : jsondecode(x)]
http_get = [for x in compact([lookup(p, "http_get", null)]) : jsondecode(x)]
tcp_socket = [for x in compact([lookup(p, "tcp_socket", null)]) : jsondecode(x)]
}
]
liveness_probes = [
for p in local.cap_liveness_probes : {
initial_delay_seconds = lookup(p, "initial_delay_seconds")
period_seconds = lookup(p, "period_seconds")
timeout_seconds = lookup(p, "timeout_seconds")
success_threshold = lookup(p, "success_threshold")
failure_threshold = lookup(p, "failure_threshold")

exec = [for x in compact([lookup(p, "exec", null)]) : jsondecode(x)]
grpc = [for x in compact([lookup(p, "grpc", null)]) : jsondecode(x)]
http_get = [for x in compact([lookup(p, "http_get", null)]) : jsondecode(x)]
tcp_socket = [for x in compact([lookup(p, "tcp_socket", null)]) : jsondecode(x)]
}
]
}
13 changes: 1 addition & 12 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ variable "container_port" {
type = number
default = 8080
description = <<EOF
Set your container to listen on this port.
This is the port that your container is listening and will get mapped to var.service_port for external communication.
By default, this is set to 8080.
You cannot bind to a port <1024 a you will get permission errors.
EOF
Expand All @@ -55,14 +55,3 @@ Other services on the network can reach this app via `<app_name>:<service_port>`
Specify 0 to disable network connectivity to this app.
EOF
}

variable "readiness_delay" {
type = number
default = 0
description = <<EOF
The period of time (in seconds) to delay before performing a readiness check against the application.
If an application has a long start time, readiness_delay can be used to defer readiness checks on the application.
The default value is 0.
EOF
}

0 comments on commit 6f5e3ac

Please sign in to comment.