Skip to content

Commit 8bea339

Browse files
Add manual scaling for cloudrunv2 services (#13742) (#22561)
[upstream:ff861852732f9d6d86749b48f876ed1a3ebc8b15] Signed-off-by: Modular Magician <magic-modules@google.com>
1 parent 615b98a commit 8bea339

File tree

5 files changed

+154
-0
lines changed

5 files changed

+154
-0
lines changed

.changelog/13742.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
cloudrunv2: added `scaling_mode` and `manual_instance_count` fields to `google_cloud_run_v2_service` resource
3+
```

google/services/cloudrunv2/resource_cloud_run_v2_service.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -937,11 +937,22 @@ For example, if ALPHA is provided as input, but only BETA and GA-level features
937937
MaxItems: 1,
938938
Elem: &schema.Resource{
939939
Schema: map[string]*schema.Schema{
940+
"manual_instance_count": {
941+
Type: schema.TypeInt,
942+
Optional: true,
943+
Description: `Total instance count for the service in manual scaling mode. This number of instances is divided among all revisions with specified traffic based on the percent of traffic they are receiving.`,
944+
},
940945
"min_instance_count": {
941946
Type: schema.TypeInt,
942947
Optional: true,
943948
Description: `Minimum number of instances for the service, to be divided among all revisions receiving traffic.`,
944949
},
950+
"scaling_mode": {
951+
Type: schema.TypeString,
952+
Optional: true,
953+
ValidateFunc: verify.ValidateEnum([]string{"AUTOMATIC", "MANUAL", ""}),
954+
Description: `The [scaling mode](https://cloud.google.com/run/docs/reference/rest/v2/projects.locations.services#scalingmode) for the service. Possible values: ["AUTOMATIC", "MANUAL"]`,
955+
},
945956
},
946957
},
947958
},
@@ -1949,6 +1960,10 @@ func flattenCloudRunV2ServiceScaling(v interface{}, d *schema.ResourceData, conf
19491960
transformed := make(map[string]interface{})
19501961
transformed["min_instance_count"] =
19511962
flattenCloudRunV2ServiceScalingMinInstanceCount(original["minInstanceCount"], d, config)
1963+
transformed["scaling_mode"] =
1964+
flattenCloudRunV2ServiceScalingScalingMode(original["scalingMode"], d, config)
1965+
transformed["manual_instance_count"] =
1966+
flattenCloudRunV2ServiceScalingManualInstanceCount(original["manualInstanceCount"], d, config)
19521967
return []interface{}{transformed}
19531968
}
19541969
func flattenCloudRunV2ServiceScalingMinInstanceCount(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
@@ -1968,6 +1983,27 @@ func flattenCloudRunV2ServiceScalingMinInstanceCount(v interface{}, d *schema.Re
19681983
return v // let terraform core handle it otherwise
19691984
}
19701985

1986+
func flattenCloudRunV2ServiceScalingScalingMode(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
1987+
return v
1988+
}
1989+
1990+
func flattenCloudRunV2ServiceScalingManualInstanceCount(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
1991+
// Handles the string fixed64 format
1992+
if strVal, ok := v.(string); ok {
1993+
if intVal, err := tpgresource.StringToFixed64(strVal); err == nil {
1994+
return intVal
1995+
}
1996+
}
1997+
1998+
// number values are represented as float64
1999+
if floatVal, ok := v.(float64); ok {
2000+
intVal := int(floatVal)
2001+
return intVal
2002+
}
2003+
2004+
return v // let terraform core handle it otherwise
2005+
}
2006+
19712007
func flattenCloudRunV2ServiceTemplate(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
19722008
if v == nil {
19732009
return nil
@@ -3491,13 +3527,35 @@ func expandCloudRunV2ServiceScaling(v interface{}, d tpgresource.TerraformResour
34913527
transformed["minInstanceCount"] = transformedMinInstanceCount
34923528
}
34933529

3530+
transformedScalingMode, err := expandCloudRunV2ServiceScalingScalingMode(original["scaling_mode"], d, config)
3531+
if err != nil {
3532+
return nil, err
3533+
} else if val := reflect.ValueOf(transformedScalingMode); val.IsValid() && !tpgresource.IsEmptyValue(val) {
3534+
transformed["scalingMode"] = transformedScalingMode
3535+
}
3536+
3537+
transformedManualInstanceCount, err := expandCloudRunV2ServiceScalingManualInstanceCount(original["manual_instance_count"], d, config)
3538+
if err != nil {
3539+
return nil, err
3540+
} else if val := reflect.ValueOf(transformedManualInstanceCount); val.IsValid() && !tpgresource.IsEmptyValue(val) {
3541+
transformed["manualInstanceCount"] = transformedManualInstanceCount
3542+
}
3543+
34943544
return transformed, nil
34953545
}
34963546

34973547
func expandCloudRunV2ServiceScalingMinInstanceCount(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
34983548
return v, nil
34993549
}
35003550

3551+
func expandCloudRunV2ServiceScalingScalingMode(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
3552+
return v, nil
3553+
}
3554+
3555+
func expandCloudRunV2ServiceScalingManualInstanceCount(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
3556+
return v, nil
3557+
}
3558+
35013559
func expandCloudRunV2ServiceTemplate(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
35023560
l := v.([]interface{})
35033561
if len(l) == 0 || l[0] == nil {

google/services/cloudrunv2/resource_cloud_run_v2_service_generated_meta.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ fields:
5555
provider_only: true
5656
- field: 'observed_generation'
5757
- field: 'reconciling'
58+
- field: 'scaling.manual_instance_count'
5859
- field: 'scaling.min_instance_count'
60+
- field: 'scaling.scaling_mode'
5961
- field: 'template.annotations'
6062
- field: 'template.containers.args'
6163
- field: 'template.containers.base_image_uri'

google/services/cloudrunv2/resource_cloud_run_v2_service_test.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1359,3 +1359,85 @@ resource "google_project_iam_member" "logs_writer" {
13591359
}
13601360
`, context)
13611361
}
1362+
1363+
func TestAccCloudRunV2Service_cloudrunv2ServiceWithManualScaling(t *testing.T) {
1364+
t.Parallel()
1365+
context := map[string]interface{}{
1366+
"random_suffix": acctest.RandString(t, 10),
1367+
}
1368+
acctest.VcrTest(t, resource.TestCase{
1369+
PreCheck: func() { acctest.AccTestPreCheck(t) },
1370+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
1371+
CheckDestroy: testAccCheckCloudRunV2ServiceDestroyProducer(t),
1372+
Steps: []resource.TestStep{
1373+
{
1374+
Config: testAccCloudRunV2Service_cloudrunv2ServiceWithManualScaling(context),
1375+
},
1376+
{
1377+
ResourceName: "google_cloud_run_v2_service.default",
1378+
ImportState: true,
1379+
ImportStateVerify: true,
1380+
ImportStateVerifyIgnore: []string{"name", "location", "annotations", "labels", "terraform_labels", "launch_stage", "deletion_protection"},
1381+
},
1382+
{
1383+
Config: testAccCloudRunV2Service_cloudrunv2ServiceUpdateWithManualScaling(context),
1384+
},
1385+
{
1386+
ResourceName: "google_cloud_run_v2_service.default",
1387+
ImportState: true,
1388+
ImportStateVerify: true,
1389+
ImportStateVerifyIgnore: []string{"name", "location", "annotations", "labels", "terraform_labels", "launch_stage", "deletion_protection"},
1390+
},
1391+
},
1392+
})
1393+
}
1394+
1395+
func testAccCloudRunV2Service_cloudrunv2ServiceWithManualScaling(context map[string]interface{}) string {
1396+
return acctest.Nprintf(`
1397+
resource "google_cloud_run_v2_service" "default" {
1398+
name = "tf-test-cloudrun-manual-scaling-service%{random_suffix}"
1399+
description = "description creating"
1400+
location = "us-central1"
1401+
deletion_protection = false
1402+
annotations = {
1403+
generated-by = "magic-modules"
1404+
}
1405+
ingress = "INGRESS_TRAFFIC_ALL"
1406+
launch_stage = "BETA"
1407+
scaling {
1408+
scaling_mode = "MANUAL"
1409+
manual_instance_count = 2
1410+
}
1411+
template {
1412+
containers {
1413+
image = "us-docker.pkg.dev/cloudrun/container/hello"
1414+
}
1415+
}
1416+
}
1417+
`, context)
1418+
}
1419+
1420+
func testAccCloudRunV2Service_cloudrunv2ServiceUpdateWithManualScaling(context map[string]interface{}) string {
1421+
return acctest.Nprintf(`
1422+
resource "google_cloud_run_v2_service" "default" {
1423+
name = "tf-test-cloudrun-manual-scaling-service%{random_suffix}"
1424+
description = "description creating"
1425+
location = "us-central1"
1426+
deletion_protection = false
1427+
annotations = {
1428+
generated-by = "magic-modules"
1429+
}
1430+
ingress = "INGRESS_TRAFFIC_ALL"
1431+
launch_stage = "BETA"
1432+
scaling {
1433+
scaling_mode = "MANUAL"
1434+
manual_instance_count = 10
1435+
}
1436+
template {
1437+
containers {
1438+
image = "us-docker.pkg.dev/cloudrun/container/hello"
1439+
}
1440+
}
1441+
}
1442+
`, context)
1443+
}

website/docs/r/cloud_run_v2_service.html.markdown

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1378,6 +1378,15 @@ When the field is set to false, deleting the service is allowed.
13781378
(Optional)
13791379
Minimum number of instances for the service, to be divided among all revisions receiving traffic.
13801380

1381+
* `scaling_mode` -
1382+
(Optional)
1383+
The [scaling mode](https://cloud.google.com/run/docs/reference/rest/v2/projects.locations.services#scalingmode) for the service.
1384+
Possible values are: `AUTOMATIC`, `MANUAL`.
1385+
1386+
* `manual_instance_count` -
1387+
(Optional)
1388+
Total instance count for the service in manual scaling mode. This number of instances is divided among all revisions with specified traffic based on the percent of traffic they are receiving.
1389+
13811390
<a name="nested_traffic"></a>The `traffic` block supports:
13821391

13831392
* `type` -

0 commit comments

Comments
 (0)