Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Cloud Run functions by adding support to the BuildConfig V2 API field #12512

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions mmv1/products/cloudrunv2/Service.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,18 @@ examples:
cloud_run_service_name: 'cloudrun-service'
ignore_read_extra:
- 'deletion_protection'
- name: 'cloudrunv2_service_function'
primary_resource_id: 'default'
primary_resource_name: 'fmt.Sprintf("tf-test-cloudrun-srv%s", context["random_suffix"])'
vars:
cloud_run_service_name: 'cloudrun-service'
bucket_name: 'gcf-source'
zip_path: 'function_source.zip'
sa_name: 'build-sa'
test_vars_overrides:
'zip_path': '"./test-fixtures/function-source.zip"'
ignore_read_extra:
- 'deletion_protection'
virtual_fields:
- name: 'deletion_protection'
description: |
Expand Down Expand Up @@ -1118,6 +1130,61 @@ properties:
description: |-
All URLs serving traffic for this Service.
output: true
- name: 'buildConfig'
type: NestedObject
description: |-
Configuration for building a Cloud Run function.
properties:
- name: 'name'
type: String
description: |-
The Cloud Build name of the latest successful deployment of the function.
output: true
- name: 'sourceLocation'
type: String
description: |-
The Cloud Storage bucket URI where the function source code is located.
- name: 'functionTarget'
type: String
description: |-
The name of the function (as defined in source code) that will be executed. Defaults to the resource name suffix, if not specified. For backward compatibility, if function with given name is not found, then the system will try to use function named "function".
- name: 'imageUri'
type: String
description: |-
Artifact Registry URI to store the built image.
- name: 'baseImage'
type: String
description: |-
The base image used to build the function.
- name: 'enableAutomaticUpdates'
type: Boolean
description: |-
Sets whether the function will receive automatic base image updates.
- name: 'workerPool'
type: String
description: |-
Name of the Cloud Build Custom Worker Pool that should be used to build the Cloud Run function. The format of this field is `projects/{project}/locations/{region}/workerPools/{workerPool}` where {project} and {region} are the project id and region respectively where the worker pool is defined and {workerPool} is the short name of the worker pool.
- name: 'environmentVariables'
type: Array
description: |-
User-provided build-time environment variables for the function.
is_set: true
item_type:
type: NestedObject
properties:
- name: 'name'
type: String
description: |-
Name of the environment variable. Must match the regex [A-Z][A-Z0-9_]*
required: true
- name: 'value'
type: String
description: |-
Literal value of the environment variable. Variable references are not supported in Cloud Run.
- name: 'serviceAccount'
type: String
description: |-
Service account to be used for building the container. The format of this field is `projects/{projectId}/serviceAccounts/{serviceAccountEmail}`.
- name: 'reconciling'
type: Boolean
description: |
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
resource "google_cloud_run_v2_service" "{{$.PrimaryResourceId}}" {
name = "{{index $.Vars "cloud_run_service_name"}}"
location = "us-central1"
deletion_protection = false
ingress = "INGRESS_TRAFFIC_ALL"

template {
containers {
image = "us-docker.pkg.dev/cloudrun/container/hello"
}
}
build_config {
source_location = "gs://${google_storage_bucket.bucket.name}/${google_storage_bucket_object.object.name}"
function_target = "helloHttp"
image_uri = "us-docker.pkg.dev/cloudrun/container/hello"
base_image = "us-central1-docker.pkg.dev/serverless-runtimes/google-22-full/runtimes/nodejs22"
enable_automatic_updates = true
worker_pool = "worker-pool"
environment_variables {
name = "FOO_KEY"
value = "FOO_VALUE"
}
environment_variables {
name = "BAR_KEY"
value = "BAR_VALUE"
}
service_account = google_service_account.cloudbuild_service_account.id
}
depends_on = [
google_project_iam_member.act_as,
google_project_iam_member.logs_writer
]
}

data "google_project" "project" {
}

resource "google_storage_bucket" "bucket" {
name = "${data.google_project.project.project_id}-{{index $.Vars "bucket_name"}}" # Every bucket name must be globally unique
location = "US"
uniform_bucket_level_access = true
}

resource "google_storage_bucket_object" "object" {
name = "function-source.zip"
bucket = google_storage_bucket.bucket.name
source = "{{index $.Vars "zip_path"}}" # Add path to the zipped function source code
}

resource "google_service_account" "cloudbuild_service_account" {
account_id = "{{index $.Vars "sa_name"}}"
}

resource "google_project_iam_member" "act_as" {
project = data.google_project.project.project_id
role = "roles/iam.serviceAccountUser"
member = "serviceAccount:${google_service_account.cloudbuild_service_account.email}"
}

resource "google_project_iam_member" "logs_writer" {
project = data.google_project.project.project_id
role = "roles/logging.logWriter"
member = "serviceAccount:${google_service_account.cloudbuild_service_account.email}"
}
Binary file not shown.
Loading