Skip to content

Commit

Permalink
feat: Cloud Run Jobs sub module (#99)
Browse files Browse the repository at this point in the history
Co-authored-by: prabhu34 <18209477+prabhu34@users.noreply.github.com>
  • Loading branch information
bharathkkb and prabhu34 authored Apr 6, 2023
1 parent aa14947 commit 2a4269c
Show file tree
Hide file tree
Showing 13 changed files with 398 additions and 5 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ REGISTRY_URL := gcr.io/cloud-foundation-cicd
docker_test_lint:
docker run --rm -it \
-e ENABLE_PARALLEL=1 \
-e DISABLE_TFLINT=1 \
-v $(CURDIR):/workspace \
$(REGISTRY_URL)/${DOCKER_IMAGE_DEVELOPER_TOOLS}:${DOCKER_TAG_VERSION_DEVELOPER_TOOLS} \
/usr/local/bin/test_lint.sh
Expand Down
16 changes: 16 additions & 0 deletions build/int.cloudbuild.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ steps:
- 'TF_VAR_seed_project_id=$_SFB_SEED_PROJECT_ID'
- 'TF_VAR_domain=test.blueprints.joonix.net'
- 'TF_VAR_resource_names_suffix=$SHORT_SHA'
allowFailure: true
- id: prepare
name: 'gcr.io/cloud-foundation-cicd/$_DOCKER_IMAGE_DEVELOPER_TOOLS:$_DOCKER_TAG_VERSION_DEVELOPER_TOOLS'
args: ['/bin/bash', '-c', 'source /usr/local/bin/task_helper_functions.sh && prepare_environment']
Expand All @@ -44,6 +45,7 @@ steps:
- 'TF_VAR_create_access_context_manager_access_policy=false'
waitFor:
- prepare
allowFailure: true
- id: create
name: 'gcr.io/cloud-foundation-cicd/$_DOCKER_IMAGE_DEVELOPER_TOOLS:$_DOCKER_TAG_VERSION_DEVELOPER_TOOLS'
args: ['/bin/bash', '-c', 'source /usr/local/bin/task_helper_functions.sh && kitchen_do create']
Expand Down Expand Up @@ -84,6 +86,20 @@ steps:
- verify cloud-run-with-cmek
name: 'gcr.io/cloud-foundation-cicd/$_DOCKER_IMAGE_DEVELOPER_TOOLS:$_DOCKER_TAG_VERSION_DEVELOPER_TOOLS'
args: ['/bin/bash', '-c', 'source /usr/local/bin/task_helper_functions.sh && source_test_env && init_credentials && cft test run TestCloudRunWithCMEK --test-dir test/integration --stage destroy --verbose']
- id: simple-job-exec-init
waitFor:
- create all
name: 'gcr.io/cloud-foundation-cicd/$_DOCKER_IMAGE_DEVELOPER_TOOLS:$_DOCKER_TAG_VERSION_DEVELOPER_TOOLS'
args: ['/bin/bash', '-c', 'cft test run TestSimpleJobExec --stage init --verbose']
- id: simple-job-exec-apply
name: 'gcr.io/cloud-foundation-cicd/$_DOCKER_IMAGE_DEVELOPER_TOOLS:$_DOCKER_TAG_VERSION_DEVELOPER_TOOLS'
args: ['/bin/bash', '-c', 'cft test run TestSimpleJobExec --stage apply --verbose']
- id: simple-job-exec-verify
name: 'gcr.io/cloud-foundation-cicd/$_DOCKER_IMAGE_DEVELOPER_TOOLS:$_DOCKER_TAG_VERSION_DEVELOPER_TOOLS'
args: ['/bin/bash', '-c', 'cft test run TestSimpleJobExec --stage verify --verbose']
- id: simple-job-exec-teardown
name: 'gcr.io/cloud-foundation-cicd/$_DOCKER_IMAGE_DEVELOPER_TOOLS:$_DOCKER_TAG_VERSION_DEVELOPER_TOOLS'
args: ['/bin/bash', '-c', 'cft test run TestSimpleJobExec --stage teardown --verbose']
tags:
- 'ci'
- 'integration'
Expand Down
3 changes: 3 additions & 0 deletions build/lint.cloudbuild.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,6 @@ tags:
substitutions:
_DOCKER_IMAGE_DEVELOPER_TOOLS: 'cft/developer-tools'
_DOCKER_TAG_VERSION_DEVELOPER_TOOLS: '1.10'
options:
env:
- DISABLE_TFLINT=1
25 changes: 25 additions & 0 deletions examples/simple_job_exec/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Copyright 2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

module "job" {
source = "../../modules/job-exec"

project_id = var.project_id
name = "simple-job"
location = "us-central1"
image = "us-docker.pkg.dev/cloudrun/container/job"
exec = true
}
20 changes: 20 additions & 0 deletions examples/simple_job_exec/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* Copyright 2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

output "id" {
description = "Cloud Run Job ID"
value = module.job.id
}
20 changes: 20 additions & 0 deletions examples/simple_job_exec/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* Copyright 2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

variable "project_id" {
description = "The project ID to deploy to"
type = string
}
53 changes: 53 additions & 0 deletions modules/job-exec/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Cloud Run Job

## Description
### tagline
Deploy a Cloud Run Job and execute it.

### detailed
This module was deploys a Cloud Run Job run and executes it.

## Usage

Basic usage of this module is as follows:

```hcl
```hcl
module "cloud_run_core" {
source = "GoogleCloudPlatform/cloud-run/google//modules/secure-cloud-run-core"
version = "~> 0.3.0"
project_id = var.project_id
name = "simple-job"
location = "us-central1"
image = "us-docker.pkg.dev/cloudrun/container/job"
exec = true
}
```

Functional examples are included in the
[examples](./examples/) directory.

<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
## Inputs

| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| argument | Arguments passed to the ENTRYPOINT command, include these only if image entrypoint needs arguments | `list(string)` | `[]` | no |
| container\_command | Leave blank to use the ENTRYPOINT command defined in the container image, include these only if image entrypoint should be overwritten | `list(string)` | `[]` | no |
| env\_secret\_vars | [Beta] Environment variables (Secret Manager) | <pre>list(object({<br> name = string<br> value_from = set(object({<br> secret_key_ref = map(string)<br> }))<br> }))</pre> | `[]` | no |
| env\_vars | Environment variables (cleartext) | <pre>list(object({<br> value = string<br> name = string<br> }))</pre> | `[]` | no |
| exec | Whether to execute job after creation | `bool` | `false` | no |
| image | GCR hosted image URL to deploy | `string` | n/a | yes |
| location | Cloud Run job deployment location | `string` | n/a | yes |
| name | The name of the Cloud Run job to create | `string` | n/a | yes |
| project\_id | The project ID to deploy to | `string` | n/a | yes |
| service\_account\_email | Service Account email needed for the job | `string` | `""` | no |

## Outputs

| Name | Description |
|------|-------------|
| id | Cloud Run Job ID |

<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
79 changes: 79 additions & 0 deletions modules/job-exec/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/**
* Copyright 2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

resource "google_cloud_run_v2_job" "job" {
name = var.name
project = var.project_id
location = var.location
launch_stage = "BETA"
template {
template {
containers {
image = var.image
command = var.container_command
args = var.argument

dynamic "env" {
for_each = var.env_vars
content {
name = env.value["name"]
value = env.value["value"]
}
}

dynamic "env" {
for_each = var.env_secret_vars
content {
name = env.value["name"]
dynamic "value_from" {
for_each = env.value.value_from
content {
secret_key_ref {
name = value_from.value.secret_key_ref["name"]
key = value_from.value.secret_key_ref["key"]
}
}
}
}
}
}
}
}
}

data "google_client_config" "default" {}

resource "terracurl_request" "exec" {
count = var.exec ? 1 : 0
name = "exec-job"
url = "https://run.googleapis.com/v2/${google_cloud_run_v2_job.job.id}:run"
method = "POST"
headers = {
Authorization = "Bearer ${data.google_client_config.default.access_token}"
Content-Type = "application/json",
}
response_codes = [200]
// no-op destroy
// we don't use terracurl_request data source as that will result in
// repeated job runs on every refresh
destroy_url = "https://run.googleapis.com/v2/${google_cloud_run_v2_job.job.id}"
destroy_method = "GET"
destroy_response_codes = [200]
destroy_headers = {
Authorization = "Bearer ${data.google_client_config.default.access_token}"
Content-Type = "application/json",
}
}
20 changes: 20 additions & 0 deletions modules/job-exec/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* Copyright 2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

output "id" {
description = "Cloud Run Job ID"
value = google_cloud_run_v2_job.job.id
}
80 changes: 80 additions & 0 deletions modules/job-exec/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/**
* Copyright 2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

variable "project_id" {
description = "The project ID to deploy to"
type = string
}

variable "name" {
description = "The name of the Cloud Run job to create"
type = string
}

variable "location" {
description = "Cloud Run job deployment location"
type = string
}

variable "image" {
description = "GCR hosted image URL to deploy"
type = string
}

variable "exec" {
description = "Whether to execute job after creation"
type = bool
default = false
}

variable "service_account_email" {
type = string
description = "Service Account email needed for the job"
default = ""
}


variable "argument" {
type = list(string)
description = "Arguments passed to the ENTRYPOINT command, include these only if image entrypoint needs arguments"
default = []
}

variable "container_command" {
type = list(string)
description = "Leave blank to use the ENTRYPOINT command defined in the container image, include these only if image entrypoint should be overwritten"
default = []
}

variable "env_vars" {
type = list(object({
value = string
name = string
}))
description = "Environment variables (cleartext)"
default = []
}

variable "env_secret_vars" {
type = list(object({
name = string
value_from = set(object({
secret_key_ref = map(string)
}))
}))
description = "[Beta] Environment variables (Secret Manager)"
default = []
}
31 changes: 31 additions & 0 deletions modules/job-exec/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* Copyright 2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

terraform {
required_providers {
terracurl = {
source = "devops-rob/terracurl"
version = "~> 0.1"
}
google = {
source = "hashicorp/google"
version = "< 5.0"
}
}
provider_meta "google" {
module_name = "blueprints/terraform/terraform-google-cloud-run:job-exec/v0.4.0"
}
}
Loading

0 comments on commit 2a4269c

Please sign in to comment.