Skip to content

Commit

Permalink
Add node selector and gpu support with tests to Cloud Run Service v2 api
Browse files Browse the repository at this point in the history
  • Loading branch information
trshafer committed Aug 30, 2024
1 parent 242ef5c commit 18d53c2
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 1 deletion.
11 changes: 10 additions & 1 deletion mmv1/products/cloudrunv2/Service.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ properties:
- !ruby/object:Api::Type::KeyValuePairs
name: 'limits'
description: |-
Only memory and CPU are supported. Use key `cpu` for CPU limit and `memory` for memory limit. Note: The only supported values for CPU are '1', '2', '4', and '8'. Setting 4 CPU requires at least 2Gi of memory. The values of the map is string form of the 'quantity' k8s type: https://github.com/kubernetes/kubernetes/blob/master/staging/src/k8s.io/apimachinery/pkg/api/resource/quantity.go
Only memory, CPU, and nvidia.com/gpu are supported. Use key `cpu` for CPU limit, `memory` for memory limit, `nvidia.com/gpu` for gpu limit. Note: The only supported values for CPU are '1', '2', '4', and '8'. Setting 4 CPU requires at least 2Gi of memory. The values of the map is string form of the 'quantity' k8s type: https://github.com/kubernetes/kubernetes/blob/master/staging/src/k8s.io/apimachinery/pkg/api/resource/quantity.go
default_from_api: true
- !ruby/object:Api::Type::Boolean
name: 'cpuIdle'
Expand Down Expand Up @@ -877,6 +877,15 @@ properties:
name: 'sessionAffinity'
description: |-
Enables session affinity. For more information, go to https://cloud.google.com/run/docs/configuring/session-affinity
- !ruby/object:Api::Type::NestedObject
name: 'nodeSelector'
description: |-
Node Selector describes the hardware requirements of the resources.
properties:
- !ruby/object:Api::Type::String
name: 'accelerator'
description:
The GPU to attach to an instance. See https://cloud.google.com/run/docs/configuring/services/gpu for configuring GPU.
- !ruby/object:Api::Type::Array
name: 'traffic'
description: |-
Expand Down
27 changes: 27 additions & 0 deletions mmv1/templates/terraform/examples/cloudrunv2_service_gpu.tf.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
resource "google_cloud_run_v2_service" "<%= ctx[:primary_resource_id] %>" {
name = "<%= ctx[:vars]['cloud_run_service_name'] %>"
location = "us-central1"
deletion_protection = false
ingress = "INGRESS_TRAFFIC_ALL"
launch_stage = "BETA"

template {
containers {
image = "us-docker.pkg.dev/cloudrun/container/hello"
resources {
limits = {
"cpu" = "4"
"memory" = "16Gi"
"nvidia.com/gpu" = "1"
}
startup_cpu_boost = true
}
}
node_selector {
accelerator = "nvidia-l4"
}
scaling {
max_instance_count = 1
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1076,3 +1076,74 @@ resource "google_cloud_run_v2_service" "default" {
`, context)
}
<% end -%>

<% unless version == 'ga' -%>
func TestAccCloudRunV2Service_cloudrunv2ServiceWithResourcesRequirements(t *testing.T) {
t.Parallel()
context := map[string]interface{} {
"random_suffix" : acctest.RandString(t, 10),
}
acctest.VcrTest(t, resource.TestCase {
PreCheck: func() { acctest.AccTestPreCheck(t)},
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
CheckDestroy: testAccCheckCloudRunV2ServiceDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccCloudRunV2Service_cloudrunv2ServiceWithGpu(context),
},
{
ResourceName: "google_cloud_run_v2_service.default",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"name", "location", "annotations", "labels", "terraform_labels", "launch_stage", "deletion_protection"},
},
},
})
}

func testAccCloudRunV2Service_cloudrunv2ServiceWithGpu(context map[string]interface{}) string {
return acctest.Nprintf(`
resource "google_cloud_run_v2_service" "default" {
name = "tf-test-cloudrun-service%{random_suffix}"
description = "description creating"
location = "us-central1"
deletion_protection = false
launch_stage = "BETA"
annotations = {
generated-by = "magic-modules"
}
ingress = "INGRESS_TRAFFIC_ALL"
labels = {
label-1 = "value-1"
}
client = "client-1"
client_version = "client-version-1"
template {
containers {
image = "us-docker.pkg.dev/cloudrun/container/hello"
resources {
limits = {
"cpu" = "4"
"memory" = "16Gi"
"nvidia.com/gpu" = "1"
}
startup_cpu_boost = true
}
}
node_selector {
accelerator = "nvidia-l4"
}
scaling {
max_instance_count = 1
}
}
lifecycle {
ignore_changes = [
launch_stage,
]
}
}

`, context)
}
<% end -%>

0 comments on commit 18d53c2

Please sign in to comment.