Skip to content

Commit e8f711c

Browse files
Add Cloud Run v1 Multi-Container Example with New Fields (#7971) (#14647)
* cloudrun: Add container name field for Services Adds the template.spec.containers.name field for specifying container names in Cloud Run v1 Services. * cloudrun: Update the description of the Service containers field Updates the description of the Cloud Run v1 Service field template.spec.containers to reflect that the field is repeated and to remove other obsolete information. * cloudrun: Add empty dir volume type for Services Adds the beta template.spec.volumes.empty_dir field for configuring ephemeral volumes in Cloud Run v1 Services. * cloudrun: Add multi-container Service example Adds an example google_cloud_run_service resource with multiple containers and a shared empty dir volume. Container dependencies are specified as a json encoded annotation. Signed-off-by: Modular Magician <[email protected]>
1 parent ad1bc60 commit e8f711c

File tree

3 files changed

+120
-11
lines changed

3 files changed

+120
-11
lines changed

.changelog/7971.txt

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
```release-note:enhancement
2+
cloudrun: Added field `template.spec.containers.name` to `google_cloud_run_service`
3+
```
4+
```release-note:enhancement
5+
cloudrun: Added beta field `template.spec.columes.empty_dir` to `google_cloud_run_service`
6+
```

google/resource_cloud_run_service.go

+27-7
Original file line numberDiff line numberDiff line change
@@ -157,12 +157,10 @@ responsible for materializing the container image from source.`,
157157
Elem: &schema.Resource{
158158
Schema: map[string]*schema.Schema{
159159
"containers": {
160-
Type: schema.TypeList,
161-
Computed: true,
162-
Optional: true,
163-
Description: `Container defines the unit of execution for this Revision.
164-
In the context of a Revision, we disallow a number of the fields of
165-
this Container, including: name, ports, and volumeMounts.`,
160+
Type: schema.TypeList,
161+
Computed: true,
162+
Optional: true,
163+
Description: `Containers defines the unit of execution for this Revision.`,
166164
Elem: &schema.Resource{
167165
Schema: map[string]*schema.Schema{
168166
"image": {
@@ -385,6 +383,12 @@ Must be smaller than period_seconds.`,
385383
},
386384
},
387385
},
386+
"name": {
387+
Type: schema.TypeString,
388+
Computed: true,
389+
Optional: true,
390+
Description: `Name of the container`,
391+
},
388392
"ports": {
389393
Type: schema.TypeList,
390394
Computed: true,
@@ -643,7 +647,7 @@ will use the project's default service account.`,
643647
},
644648
"secret": {
645649
Type: schema.TypeList,
646-
Required: true,
650+
Optional: true,
647651
Description: `The secret's value will be presented as the content of a file whose
648652
name is defined in the item path. If no items are defined, the name of
649653
the file is the secret_name.`,
@@ -1649,6 +1653,7 @@ func flattenCloudRunServiceSpecTemplateSpecContainers(v interface{}, d *schema.R
16491653
continue
16501654
}
16511655
transformed = append(transformed, map[string]interface{}{
1656+
"name": flattenCloudRunServiceSpecTemplateSpecContainersName(original["name"], d, config),
16521657
"working_dir": flattenCloudRunServiceSpecTemplateSpecContainersWorkingDir(original["workingDir"], d, config),
16531658
"args": flattenCloudRunServiceSpecTemplateSpecContainersArgs(original["args"], d, config),
16541659
"env_from": flattenCloudRunServiceSpecTemplateSpecContainersEnvFrom(original["envFrom"], d, config),
@@ -1664,6 +1669,10 @@ func flattenCloudRunServiceSpecTemplateSpecContainers(v interface{}, d *schema.R
16641669
}
16651670
return transformed
16661671
}
1672+
func flattenCloudRunServiceSpecTemplateSpecContainersName(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
1673+
return v
1674+
}
1675+
16671676
func flattenCloudRunServiceSpecTemplateSpecContainersWorkingDir(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
16681677
return v
16691678
}
@@ -2940,6 +2949,13 @@ func expandCloudRunServiceSpecTemplateSpecContainers(v interface{}, d tpgresourc
29402949
original := raw.(map[string]interface{})
29412950
transformed := make(map[string]interface{})
29422951

2952+
transformedName, err := expandCloudRunServiceSpecTemplateSpecContainersName(original["name"], d, config)
2953+
if err != nil {
2954+
return nil, err
2955+
} else if val := reflect.ValueOf(transformedName); val.IsValid() && !tpgresource.IsEmptyValue(val) {
2956+
transformed["name"] = transformedName
2957+
}
2958+
29432959
transformedWorkingDir, err := expandCloudRunServiceSpecTemplateSpecContainersWorkingDir(original["working_dir"], d, config)
29442960
if err != nil {
29452961
return nil, err
@@ -3022,6 +3038,10 @@ func expandCloudRunServiceSpecTemplateSpecContainers(v interface{}, d tpgresourc
30223038
return req, nil
30233039
}
30243040

3041+
func expandCloudRunServiceSpecTemplateSpecContainersName(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
3042+
return v, nil
3043+
}
3044+
30253045
func expandCloudRunServiceSpecTemplateSpecContainersWorkingDir(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
30263046
return v, nil
30273047
}

website/docs/r/cloud_run_service.html.markdown

+87-4
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,72 @@ resource "google_cloud_run_service" "default" {
172172
}
173173
}
174174
```
175+
## Example Usage - Cloud Run Service Multicontainer
176+
177+
178+
```hcl
179+
resource "google_cloud_run_service" "default" {
180+
name = "cloudrun-srv"
181+
location = "us-central1"
182+
provider = google-beta
183+
184+
metadata {
185+
annotations = {
186+
"run.googleapis.com/launch-stage" = "BETA"
187+
}
188+
}
189+
template {
190+
metadata {
191+
annotations = {
192+
"run.googleapis.com/container-dependencies" = jsonencode({hello-1 = ["hello-2"]})
193+
}
194+
}
195+
spec {
196+
containers {
197+
name = "hello-1"
198+
ports {
199+
container_port = 8080
200+
}
201+
image = "us-docker.pkg.dev/cloudrun/container/hello"
202+
volume_mounts {
203+
name = "shared-volume"
204+
mount_path = "/mnt/shared"
205+
}
206+
}
207+
containers {
208+
name = "hello-2"
209+
image = "us-docker.pkg.dev/cloudrun/container/hello"
210+
env {
211+
name = "PORT"
212+
value = "8081"
213+
}
214+
startup_probe {
215+
http_get {
216+
port = 8081
217+
}
218+
}
219+
volume_mounts {
220+
name = "shared-volume"
221+
mount_path = "/mnt/shared"
222+
}
223+
}
224+
volumes {
225+
name = "shared-volume"
226+
empty_dir {
227+
medium = "Memory"
228+
size_limit = "128Mi"
229+
}
230+
}
231+
}
232+
}
233+
234+
lifecycle {
235+
ignore_changes = [
236+
metadata[0].annotations["run.googleapis.com/launch-stage"],
237+
]
238+
}
239+
}
240+
```
175241

176242
## Argument Reference
177243

@@ -321,9 +387,7 @@ The following arguments are supported:
321387

322388
* `containers` -
323389
(Required)
324-
Container defines the unit of execution for this Revision.
325-
In the context of a Revision, we disallow a number of the fields of
326-
this Container, including: name, ports, and volumeMounts.
390+
Containers defines the unit of execution for this Revision.
327391
Structure is [documented below](#nested_containers).
328392

329393
* `container_concurrency` -
@@ -361,6 +425,10 @@ The following arguments are supported:
361425

362426
<a name="nested_containers"></a>The `containers` block supports:
363427

428+
* `name` -
429+
(Optional)
430+
Name of the container
431+
364432
* `working_dir` -
365433
(Optional, Deprecated)
366434
Container's working directory.
@@ -727,12 +795,17 @@ The following arguments are supported:
727795
Volume's name.
728796

729797
* `secret` -
730-
(Required)
798+
(Optional)
731799
The secret's value will be presented as the content of a file whose
732800
name is defined in the item path. If no items are defined, the name of
733801
the file is the secret_name.
734802
Structure is [documented below](#nested_secret).
735803

804+
* `empty_dir` -
805+
(Optional, [Beta](https://terraform.io/docs/providers/google/guides/provider_versions.html))
806+
Ephemeral storage which can be backed by real disks (HD, SSD), network storage or memory (i.e. tmpfs). For now only in memory (tmpfs) is supported. It is ephemeral in the sense that when the sandbox is taken down, the data is destroyed with it (it does not persist across sandbox runs).
807+
Structure is [documented below](#nested_empty_dir).
808+
736809

737810
<a name="nested_secret"></a>The `secret` block supports:
738811

@@ -786,6 +859,16 @@ The following arguments are supported:
786859
conflict with other options that affect the file mode, like fsGroup, and
787860
the result can be other mode bits set.
788861

862+
<a name="nested_empty_dir"></a>The `empty_dir` block supports:
863+
864+
* `medium` -
865+
(Optional)
866+
The medium on which the data is stored. The default is "" which means to use the node's default medium. Must be an empty string (default) or Memory.
867+
868+
* `size_limit` -
869+
(Optional)
870+
Limit on the storage usable by this EmptyDir volume. The size limit is also applicable for memory medium. The maximum usage on memory medium EmptyDir would be the minimum value between the SizeLimit specified here and the sum of memory limits of all containers in a pod. This field's values are of the 'Quantity' k8s type: https://kubernetes.io/docs/reference/kubernetes-api/common-definitions/quantity/. The default is nil which means that the limit is undefined. More info: http://kubernetes.io/docs/user-guide/volumes#emptydir.
871+
789872
- - -
790873

791874

0 commit comments

Comments
 (0)