Skip to content

Commit

Permalink
Add Cloud Run v1 Multi-Container Example with New Fields (#7971) (#14647
Browse files Browse the repository at this point in the history
)

* 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 <magic-modules@google.com>
  • Loading branch information
modular-magician committed May 18, 2023
1 parent ad1bc60 commit e8f711c
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 11 deletions.
6 changes: 6 additions & 0 deletions .changelog/7971.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
```release-note:enhancement
cloudrun: Added field `template.spec.containers.name` to `google_cloud_run_service`
```
```release-note:enhancement
cloudrun: Added beta field `template.spec.columes.empty_dir` to `google_cloud_run_service`
```
34 changes: 27 additions & 7 deletions google/resource_cloud_run_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,10 @@ responsible for materializing the container image from source.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"containers": {
Type: schema.TypeList,
Computed: true,
Optional: true,
Description: `Container defines the unit of execution for this Revision.
In the context of a Revision, we disallow a number of the fields of
this Container, including: name, ports, and volumeMounts.`,
Type: schema.TypeList,
Computed: true,
Optional: true,
Description: `Containers defines the unit of execution for this Revision.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"image": {
Expand Down Expand Up @@ -385,6 +383,12 @@ Must be smaller than period_seconds.`,
},
},
},
"name": {
Type: schema.TypeString,
Computed: true,
Optional: true,
Description: `Name of the container`,
},
"ports": {
Type: schema.TypeList,
Computed: true,
Expand Down Expand Up @@ -643,7 +647,7 @@ will use the project's default service account.`,
},
"secret": {
Type: schema.TypeList,
Required: true,
Optional: true,
Description: `The secret's value will be presented as the content of a file whose
name is defined in the item path. If no items are defined, the name of
the file is the secret_name.`,
Expand Down Expand Up @@ -1649,6 +1653,7 @@ func flattenCloudRunServiceSpecTemplateSpecContainers(v interface{}, d *schema.R
continue
}
transformed = append(transformed, map[string]interface{}{
"name": flattenCloudRunServiceSpecTemplateSpecContainersName(original["name"], d, config),
"working_dir": flattenCloudRunServiceSpecTemplateSpecContainersWorkingDir(original["workingDir"], d, config),
"args": flattenCloudRunServiceSpecTemplateSpecContainersArgs(original["args"], d, config),
"env_from": flattenCloudRunServiceSpecTemplateSpecContainersEnvFrom(original["envFrom"], d, config),
Expand All @@ -1664,6 +1669,10 @@ func flattenCloudRunServiceSpecTemplateSpecContainers(v interface{}, d *schema.R
}
return transformed
}
func flattenCloudRunServiceSpecTemplateSpecContainersName(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
return v
}

func flattenCloudRunServiceSpecTemplateSpecContainersWorkingDir(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
return v
}
Expand Down Expand Up @@ -2940,6 +2949,13 @@ func expandCloudRunServiceSpecTemplateSpecContainers(v interface{}, d tpgresourc
original := raw.(map[string]interface{})
transformed := make(map[string]interface{})

transformedName, err := expandCloudRunServiceSpecTemplateSpecContainersName(original["name"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedName); val.IsValid() && !tpgresource.IsEmptyValue(val) {
transformed["name"] = transformedName
}

transformedWorkingDir, err := expandCloudRunServiceSpecTemplateSpecContainersWorkingDir(original["working_dir"], d, config)
if err != nil {
return nil, err
Expand Down Expand Up @@ -3022,6 +3038,10 @@ func expandCloudRunServiceSpecTemplateSpecContainers(v interface{}, d tpgresourc
return req, nil
}

func expandCloudRunServiceSpecTemplateSpecContainersName(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
return v, nil
}

func expandCloudRunServiceSpecTemplateSpecContainersWorkingDir(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
return v, nil
}
Expand Down
91 changes: 87 additions & 4 deletions website/docs/r/cloud_run_service.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,72 @@ resource "google_cloud_run_service" "default" {
}
}
```
## Example Usage - Cloud Run Service Multicontainer


```hcl
resource "google_cloud_run_service" "default" {
name = "cloudrun-srv"
location = "us-central1"
provider = google-beta
metadata {
annotations = {
"run.googleapis.com/launch-stage" = "BETA"
}
}
template {
metadata {
annotations = {
"run.googleapis.com/container-dependencies" = jsonencode({hello-1 = ["hello-2"]})
}
}
spec {
containers {
name = "hello-1"
ports {
container_port = 8080
}
image = "us-docker.pkg.dev/cloudrun/container/hello"
volume_mounts {
name = "shared-volume"
mount_path = "/mnt/shared"
}
}
containers {
name = "hello-2"
image = "us-docker.pkg.dev/cloudrun/container/hello"
env {
name = "PORT"
value = "8081"
}
startup_probe {
http_get {
port = 8081
}
}
volume_mounts {
name = "shared-volume"
mount_path = "/mnt/shared"
}
}
volumes {
name = "shared-volume"
empty_dir {
medium = "Memory"
size_limit = "128Mi"
}
}
}
}
lifecycle {
ignore_changes = [
metadata[0].annotations["run.googleapis.com/launch-stage"],
]
}
}
```

## Argument Reference

Expand Down Expand Up @@ -321,9 +387,7 @@ The following arguments are supported:

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

* `container_concurrency` -
Expand Down Expand Up @@ -361,6 +425,10 @@ The following arguments are supported:

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

* `name` -
(Optional)
Name of the container

* `working_dir` -
(Optional, Deprecated)
Container's working directory.
Expand Down Expand Up @@ -727,12 +795,17 @@ The following arguments are supported:
Volume's name.

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

* `empty_dir` -
(Optional, [Beta](https://terraform.io/docs/providers/google/guides/provider_versions.html))
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).
Structure is [documented below](#nested_empty_dir).


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

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

<a name="nested_empty_dir"></a>The `empty_dir` block supports:

* `medium` -
(Optional)
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.

* `size_limit` -
(Optional)
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.

- - -


Expand Down

0 comments on commit e8f711c

Please sign in to comment.