Skip to content

Commit

Permalink
feat: move emptydir to GA for cloudrun and cloudrunv2 (#12230) (#20185)
Browse files Browse the repository at this point in the history
[upstream:8a6f281bad55b80c3e9f6d739729cb5d78646ab3]

Signed-off-by: Modular Magician <magic-modules@google.com>
  • Loading branch information
modular-magician authored Nov 4, 2024
1 parent 23c558c commit 422e87f
Show file tree
Hide file tree
Showing 10 changed files with 451 additions and 11 deletions.
6 changes: 6 additions & 0 deletions .changelog/12230.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
```release-note:enhancement
cloudrun: promoted `empty_dir` field in `google_cloud_run_service` to GA
```
```release-note:enhancement
cloudrunv2: promoted `empty_dir` field in `google_cloud_run_v2_service` and `google_cloud_run_v2_job` to GA
```
93 changes: 89 additions & 4 deletions google/services/cloudrun/resource_cloud_run_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,26 @@ will use the project's default service account.`,
},
},
},
"empty_dir": {
Type: schema.TypeList,
Optional: true,
Description: `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).`,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"medium": {
Type: schema.TypeString,
Optional: true,
Description: `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": {
Type: schema.TypeString,
Optional: true,
Description: `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: https://kubernetes.io/docs/concepts/storage/volumes/#emptydir.`,
},
},
},
},
"nfs": {
Type: schema.TypeList,
Optional: true,
Expand Down Expand Up @@ -2524,10 +2544,11 @@ func flattenCloudRunServiceSpecTemplateSpecVolumes(v interface{}, d *schema.Reso
continue
}
transformed = append(transformed, map[string]interface{}{
"name": flattenCloudRunServiceSpecTemplateSpecVolumesName(original["name"], d, config),
"secret": flattenCloudRunServiceSpecTemplateSpecVolumesSecret(original["secret"], d, config),
"csi": flattenCloudRunServiceSpecTemplateSpecVolumesCsi(original["csi"], d, config),
"nfs": flattenCloudRunServiceSpecTemplateSpecVolumesNfs(original["nfs"], d, config),
"name": flattenCloudRunServiceSpecTemplateSpecVolumesName(original["name"], d, config),
"secret": flattenCloudRunServiceSpecTemplateSpecVolumesSecret(original["secret"], d, config),
"empty_dir": flattenCloudRunServiceSpecTemplateSpecVolumesEmptyDir(original["emptyDir"], d, config),
"csi": flattenCloudRunServiceSpecTemplateSpecVolumesCsi(original["csi"], d, config),
"nfs": flattenCloudRunServiceSpecTemplateSpecVolumesNfs(original["nfs"], d, config),
})
}
return transformed
Expand Down Expand Up @@ -2619,6 +2640,29 @@ func flattenCloudRunServiceSpecTemplateSpecVolumesSecretItemsMode(v interface{},
return v // let terraform core handle it otherwise
}

func flattenCloudRunServiceSpecTemplateSpecVolumesEmptyDir(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
if v == nil {
return nil
}
original := v.(map[string]interface{})
if len(original) == 0 {
return nil
}
transformed := make(map[string]interface{})
transformed["medium"] =
flattenCloudRunServiceSpecTemplateSpecVolumesEmptyDirMedium(original["medium"], d, config)
transformed["size_limit"] =
flattenCloudRunServiceSpecTemplateSpecVolumesEmptyDirSizeLimit(original["sizeLimit"], d, config)
return []interface{}{transformed}
}
func flattenCloudRunServiceSpecTemplateSpecVolumesEmptyDirMedium(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
return v
}

func flattenCloudRunServiceSpecTemplateSpecVolumesEmptyDirSizeLimit(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
return v
}

func flattenCloudRunServiceSpecTemplateSpecVolumesCsi(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
if v == nil {
return nil
Expand Down Expand Up @@ -4186,6 +4230,13 @@ func expandCloudRunServiceSpecTemplateSpecVolumes(v interface{}, d tpgresource.T
transformed["secret"] = transformedSecret
}

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

transformedCsi, err := expandCloudRunServiceSpecTemplateSpecVolumesCsi(original["csi"], d, config)
if err != nil {
return nil, err
Expand Down Expand Up @@ -4298,6 +4349,40 @@ func expandCloudRunServiceSpecTemplateSpecVolumesSecretItemsMode(v interface{},
return v, nil
}

func expandCloudRunServiceSpecTemplateSpecVolumesEmptyDir(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
l := v.([]interface{})
if len(l) == 0 || l[0] == nil {
return nil, nil
}
raw := l[0]
original := raw.(map[string]interface{})
transformed := make(map[string]interface{})

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

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

return transformed, nil
}

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

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

func expandCloudRunServiceSpecTemplateSpecVolumesCsi(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
l := v.([]interface{})
if len(l) == 0 || l[0] == nil {
Expand Down
56 changes: 56 additions & 0 deletions google/services/cloudrun/resource_cloud_run_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1108,3 +1108,59 @@ resource "google_cloud_run_service" "default" {
}
`, name, project)
}

func TestAccCloudRunService_emptyDirVolume(t *testing.T) {
t.Parallel()

project := envvar.GetTestProjectFromEnv()
name := "tftest-cloudrun-" + acctest.RandString(t, 6)

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
Steps: []resource.TestStep{
{
Config: testAccCloudRunService_cloudRunServiceWithEmptyDirVolume(name, project),
},
{
ResourceName: "google_cloud_run_service.default",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"metadata.0.resource_version", "metadata.0.annotations", "metadata.0.labels", "metadata.0.terraform_labels", "status.0.conditions"},
},
},
})
}

func testAccCloudRunService_cloudRunServiceWithEmptyDirVolume(name, project string) string {
return fmt.Sprintf(`
resource "google_cloud_run_service" "default" {
name = "%s"
location = "us-central1"
metadata {
namespace = "%s"
annotations = {
generated-by = "magic-modules"
}
}
template {
spec {
containers {
image = "gcr.io/cloudrun/hello"
volume_mounts {
name = "vol1"
mount_path = "/mnt/vol1"
}
}
volumes {
name = "vol1"
empty_dir { size_limit = "256Mi" }
}
}
}
}
`, name, project)
}
87 changes: 87 additions & 0 deletions google/services/cloudrunv2/resource_cloud_run_v2_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,28 @@ A duration in seconds with up to nine fractional digits, ending with 's'. Exampl
},
},
},
"empty_dir": {
Type: schema.TypeList,
Optional: true,
Description: `Ephemeral storage used as a shared volume.`,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"medium": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: verify.ValidateEnum([]string{"MEMORY", ""}),
Description: `The different types of medium supported for EmptyDir. Default value: "MEMORY" Possible values: ["MEMORY"]`,
Default: "MEMORY",
},
"size_limit": {
Type: schema.TypeString,
Optional: true,
Description: `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: https://kubernetes.io/docs/concepts/storage/volumes/#emptydir.`,
},
},
},
},
"gcs": {
Type: schema.TypeList,
Optional: true,
Expand Down Expand Up @@ -1624,6 +1646,7 @@ func flattenCloudRunV2JobTemplateTemplateVolumes(v interface{}, d *schema.Resour
"name": flattenCloudRunV2JobTemplateTemplateVolumesName(original["name"], d, config),
"secret": flattenCloudRunV2JobTemplateTemplateVolumesSecret(original["secret"], d, config),
"cloud_sql_instance": flattenCloudRunV2JobTemplateTemplateVolumesCloudSqlInstance(original["cloudSqlInstance"], d, config),
"empty_dir": flattenCloudRunV2JobTemplateTemplateVolumesEmptyDir(original["emptyDir"], d, config),
"gcs": flattenCloudRunV2JobTemplateTemplateVolumesGcs(original["gcs"], d, config),
"nfs": flattenCloudRunV2JobTemplateTemplateVolumesNfs(original["nfs"], d, config),
})
Expand Down Expand Up @@ -1734,6 +1757,29 @@ func flattenCloudRunV2JobTemplateTemplateVolumesCloudSqlInstanceInstances(v inte
return v
}

func flattenCloudRunV2JobTemplateTemplateVolumesEmptyDir(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
if v == nil {
return nil
}
original := v.(map[string]interface{})
if len(original) == 0 {
return nil
}
transformed := make(map[string]interface{})
transformed["medium"] =
flattenCloudRunV2JobTemplateTemplateVolumesEmptyDirMedium(original["medium"], d, config)
transformed["size_limit"] =
flattenCloudRunV2JobTemplateTemplateVolumesEmptyDirSizeLimit(original["sizeLimit"], d, config)
return []interface{}{transformed}
}
func flattenCloudRunV2JobTemplateTemplateVolumesEmptyDirMedium(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
return v
}

func flattenCloudRunV2JobTemplateTemplateVolumesEmptyDirSizeLimit(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
return v
}

func flattenCloudRunV2JobTemplateTemplateVolumesGcs(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
if v == nil {
return nil
Expand Down Expand Up @@ -2606,6 +2652,13 @@ func expandCloudRunV2JobTemplateTemplateVolumes(v interface{}, d tpgresource.Ter
transformed["cloudSqlInstance"] = transformedCloudSqlInstance
}

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

transformedGcs, err := expandCloudRunV2JobTemplateTemplateVolumesGcs(original["gcs"], d, config)
if err != nil {
return nil, err
Expand Down Expand Up @@ -2741,6 +2794,40 @@ func expandCloudRunV2JobTemplateTemplateVolumesCloudSqlInstanceInstances(v inter
return v, nil
}

func expandCloudRunV2JobTemplateTemplateVolumesEmptyDir(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
l := v.([]interface{})
if len(l) == 0 || l[0] == nil {
return nil, nil
}
raw := l[0]
original := raw.(map[string]interface{})
transformed := make(map[string]interface{})

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

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

return transformed, nil
}

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

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

func expandCloudRunV2JobTemplateTemplateVolumesGcs(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
l := v.([]interface{})
if len(l) == 0 || l[0] == nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,59 @@ resource "google_secret_manager_secret_iam_member" "secret-access" {
`, context)
}

func TestAccCloudRunV2Job_cloudrunv2JobEmptydirExample(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: testAccCheckCloudRunV2JobDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccCloudRunV2Job_cloudrunv2JobEmptydirExample(context),
},
{
ResourceName: "google_cloud_run_v2_job.default",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"annotations", "deletion_protection", "labels", "location", "name", "terraform_labels"},
},
},
})
}

func testAccCloudRunV2Job_cloudrunv2JobEmptydirExample(context map[string]interface{}) string {
return acctest.Nprintf(`
resource "google_cloud_run_v2_job" "default" {
name = "tf-test-cloudrun-job%{random_suffix}"
location = "us-central1"
deletion_protection = false
template {
template {
containers {
image = "us-docker.pkg.dev/cloudrun/container/job"
volume_mounts {
name = "empty-dir-volume"
mount_path = "/mnt"
}
}
volumes {
name = "empty-dir-volume"
empty_dir {
medium = "MEMORY"
size_limit = "128Mi"
}
}
}
}
}
`, context)
}

func testAccCheckCloudRunV2JobDestroyProducer(t *testing.T) func(s *terraform.State) error {
return func(s *terraform.State) error {
for name, rs := range s.RootModule().Resources {
Expand Down
Loading

0 comments on commit 422e87f

Please sign in to comment.