From 4c1e6d3677897fef875a6debaceafadc8d7dfe52 Mon Sep 17 00:00:00 2001 From: Anouar Chattouna Date: Mon, 12 Apr 2021 10:49:21 +0200 Subject: [PATCH 1/2] Enhancement: Use configurable resources * Cloud Run Service - adding variable for maximum instances - adding variable for timeout seconds * Cloud Scheduler Job - adding variable for attempt deadline - adding variable for retry count --- examples/complete/main.tf | 16 ++++++++++------ main.tf | 20 ++++++++++++++------ outputs.tf | 5 +---- variables.tf | 30 +++++++++++++++++++++++++++++- 4 files changed, 54 insertions(+), 17 deletions(-) diff --git a/examples/complete/main.tf b/examples/complete/main.tf index 59c5339..59f5fe3 100644 --- a/examples/complete/main.tf +++ b/examples/complete/main.tf @@ -4,12 +4,16 @@ module "gcr_cleaner" { # If you want to create your App Engine Application using terraform, uncomment the following # create_app_engine_app = true - app_engine_application_location = "europe-west3" - cloud_run_service_name = "gcr-cleaner-helsinki" - cloud_run_service_location = "europe-north1" - gcr_cleaner_image = "europe-docker.pkg.dev/gcr-cleaner/gcr-cleaner/gcr-cleaner" - cloud_scheduler_job_schedule = "0 2 * * 5" - cloud_scheduler_job_time_zone = "Europe/Helsinki" + app_engine_application_location = "europe-west3" + cloud_run_service_name = "gcr-cleaner-helsinki" + cloud_run_service_location = "europe-north1" + cloud_run_service_maximum_instances = 300 + cloud_run_service_timeout_seconds = 300 + gcr_cleaner_image = "europe-docker.pkg.dev/gcr-cleaner/gcr-cleaner/gcr-cleaner" + cloud_scheduler_job_schedule = "0 2 * * 5" + cloud_scheduler_job_time_zone = "Europe/Helsinki" + cloud_scheduler_job_attempt_deadline = 600 + cloud_scheduler_job_retry_count = 3 gcr_repositories = [ { storage_region = "eu" diff --git a/main.tf b/main.tf index 9e34eb2..6e46ede 100644 --- a/main.tf +++ b/main.tf @@ -5,12 +5,19 @@ resource "google_cloud_run_service" "this" { location = var.cloud_run_service_location template { + metadata { + annotations = { + "autoscaling.knative.dev/maxScale" = var.cloud_run_service_maximum_instances + "run.googleapis.com/client-name" = "cloud-scheduler" + } + } + spec { containers { image = var.gcr_cleaner_image } service_account_name = google_service_account.cleaner.email - timeout_seconds = 60 + timeout_seconds = var.cloud_run_service_timeout_seconds } } @@ -52,10 +59,11 @@ resource "google_cloud_scheduler_job" "this" { # name must match the RE2 regular expression "[a-zA-Z\d_-]{1,500}" # and be no more than 500 characters. - name = "gcr-cleaner_${replace(each.value, "/[.\\/]/", "_")}" - description = "Cleanup ${each.value}" - schedule = var.cloud_scheduler_job_schedule - time_zone = var.cloud_scheduler_job_time_zone + name = "gcr-cleaner_${replace(each.value, "/[.\\/]/", "_")}" + description = "Cleanup ${each.value}" + schedule = var.cloud_scheduler_job_schedule + time_zone = var.cloud_scheduler_job_time_zone + attempt_deadline = "${var.cloud_scheduler_job_attempt_deadline}s" # Location must equal to the one of the App Engine app that is associated with this project # /!\ Note that two locations, called europe-west and us-central in App Engine commands, # are called, respectively, europe-west1 and us-central1 in Cloud Scheduler commands. @@ -63,7 +71,7 @@ resource "google_cloud_scheduler_job" "this" { region = contains(["europe-west", "us-central"], var.app_engine_application_location) == true ? "${var.app_engine_application_location}1" : var.app_engine_application_location retry_config { - retry_count = 1 + retry_count = var.cloud_scheduler_job_retry_count } http_target { diff --git a/outputs.tf b/outputs.tf index 820b686..651a2da 100644 --- a/outputs.tf +++ b/outputs.tf @@ -11,9 +11,6 @@ output "app_engine_application_name" { output "cloud_scheduler_jobs" { description = "List of the created scheduler jobs." value = [ - for job in google_cloud_scheduler_job.this : { - id : job.id - name : job.name - } + for job in google_cloud_scheduler_job.this : job.id ] } diff --git a/variables.tf b/variables.tf index 223da62..99bd21f 100644 --- a/variables.tf +++ b/variables.tf @@ -17,11 +17,23 @@ variable "cloud_run_service_name" { } variable "cloud_run_service_location" { - description = "The location of the cloud run instance. Make sure to provide a valid location. More at https://cloud.google.com/run/docs/locations" + description = "The location of the cloud run instance. Make sure to provide a valid location. More at https://cloud.google.com/run/docs/locations." type = string default = "europe-west1" } +variable "cloud_run_service_maximum_instances" { + description = "The number of maximum instances to set for this revision. This value will be used in the `autoscaling.knative.dev/maxScale` annotation key." + type = number + default = 100 +} + +variable "cloud_run_service_timeout_seconds" { + description = "TimeoutSeconds holds the max duration the instance is allowed for responding to a request." + type = number + default = 60 +} + variable "create_app_engine_app" { description = "Whether to create an App Engine application." type = bool @@ -79,3 +91,19 @@ variable "cloud_scheduler_job_time_zone" { type = string default = "Europe/Brussels" } + +variable "cloud_scheduler_job_attempt_deadline" { + description = "The deadline for job attempts in seconds. If the request handler does not respond by this deadline then the request is cancelled and the attempt is marked as a `DEADLINE_EXCEEDED` failure. The failed attempt can be viewed in execution logs. Cloud Scheduler will retry the job according to the `RetryConfig`. Value must be between 15 seconds and 24 hours" + type = number + default = 320 + validation { + condition = var.cloud_scheduler_job_attempt_deadline >= 15 && var.cloud_scheduler_job_attempt_deadline <= 86400 + error_message = "Value must be between 15 seconds and 24 hours." + } +} + +variable "cloud_scheduler_job_retry_count" { + description = "The number of attempts that the system will make to run a job using the exponential backoff procedure described by maxDoublings. Values greater than 5 and negative values are not allowed." + type = number + default = 1 +} From 4a681abc4bed1a74770860784e88cb3d0c389e44 Mon Sep 17 00:00:00 2001 From: Anouar Chattouna Date: Mon, 12 Apr 2021 10:54:14 +0200 Subject: [PATCH 2/2] Running pre-commit run -a --- README.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 1db959b..a150af6 100644 --- a/README.md +++ b/README.md @@ -98,8 +98,12 @@ No modules. | Name | Description | Type | Default | Required | |------|-------------|------|---------|:--------:| | [app\_engine\_application\_location](#input\_app\_engine\_application\_location) | The location to serve the app from. | `string` | `"europe-west1"` | no | -| [cloud\_run\_service\_location](#input\_cloud\_run\_service\_location) | The location of the cloud run instance. Make sure to provide a valid location. More at https://cloud.google.com/run/docs/locations | `string` | `"europe-west1"` | no | +| [cloud\_run\_service\_location](#input\_cloud\_run\_service\_location) | The location of the cloud run instance. Make sure to provide a valid location. More at https://cloud.google.com/run/docs/locations. | `string` | `"europe-west1"` | no | +| [cloud\_run\_service\_maximum\_instances](#input\_cloud\_run\_service\_maximum\_instances) | The number of maximum instances to set for this revision. This value will be used in the `autoscaling.knative.dev/maxScale` annotation key. | `number` | `100` | no | | [cloud\_run\_service\_name](#input\_cloud\_run\_service\_name) | The name of the cloud run service. | `string` | `"gcr-cleaner"` | no | +| [cloud\_run\_service\_timeout\_seconds](#input\_cloud\_run\_service\_timeout\_seconds) | TimeoutSeconds holds the max duration the instance is allowed for responding to a request. | `number` | `60` | no | +| [cloud\_scheduler\_job\_attempt\_deadline](#input\_cloud\_scheduler\_job\_attempt\_deadline) | The deadline for job attempts in seconds. If the request handler does not respond by this deadline then the request is cancelled and the attempt is marked as a `DEADLINE_EXCEEDED` failure. The failed attempt can be viewed in execution logs. Cloud Scheduler will retry the job according to the `RetryConfig`. Value must be between 15 seconds and 24 hours | `number` | `320` | no | +| [cloud\_scheduler\_job\_retry\_count](#input\_cloud\_scheduler\_job\_retry\_count) | The number of attempts that the system will make to run a job using the exponential backoff procedure described by maxDoublings. Values greater than 5 and negative values are not allowed. | `number` | `1` | no | | [cloud\_scheduler\_job\_schedule](#input\_cloud\_scheduler\_job\_schedule) | Describes the schedule on which the job will be executed. | `string` | `"0 4 * * 1"` | no | | [cloud\_scheduler\_job\_time\_zone](#input\_cloud\_scheduler\_job\_time\_zone) | Specifies the time zone to be used in interpreting schedule. The value of this field must be a time zone name from the tz database. More on https://en.wikipedia.org/wiki/List_of_tz_database_time_zones | `string` | `"Europe/Brussels"` | no | | [create\_app\_engine\_app](#input\_create\_app\_engine\_app) | Whether to create an App Engine application. | `bool` | `false` | no |