-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Cloud Run Jobs sub module (#99)
Co-authored-by: prabhu34 <18209477+prabhu34@users.noreply.github.com>
- Loading branch information
1 parent
aa14947
commit 2a4269c
Showing
13 changed files
with
398 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 --> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = [] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
Oops, something went wrong.