Skip to content

Commit 4251695

Browse files
Add manual scaling for cloudrunv2 services (#13742) (#9908)
[upstream:ff861852732f9d6d86749b48f876ed1a3ebc8b15] Signed-off-by: Modular Magician <[email protected]>
1 parent 59dd552 commit 4251695

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-beta/services/cloudrunv2/resource_cloud_run_v2_service.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -971,11 +971,22 @@ For example, if ALPHA is provided as input, but only BETA and GA-level features
971971
MaxItems: 1,
972972
Elem: &schema.Resource{
973973
Schema: map[string]*schema.Schema{
974+
"manual_instance_count": {
975+
Type: schema.TypeInt,
976+
Optional: true,
977+
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.`,
978+
},
974979
"min_instance_count": {
975980
Type: schema.TypeInt,
976981
Optional: true,
977982
Description: `Minimum number of instances for the service, to be divided among all revisions receiving traffic.`,
978983
},
984+
"scaling_mode": {
985+
Type: schema.TypeString,
986+
Optional: true,
987+
ValidateFunc: verify.ValidateEnum([]string{"AUTOMATIC", "MANUAL", ""}),
988+
Description: `The [scaling mode](https://cloud.google.com/run/docs/reference/rest/v2/projects.locations.services#scalingmode) for the service. Possible values: ["AUTOMATIC", "MANUAL"]`,
989+
},
979990
},
980991
},
981992
},
@@ -2013,6 +2024,10 @@ func flattenCloudRunV2ServiceScaling(v interface{}, d *schema.ResourceData, conf
20132024
transformed := make(map[string]interface{})
20142025
transformed["min_instance_count"] =
20152026
flattenCloudRunV2ServiceScalingMinInstanceCount(original["minInstanceCount"], d, config)
2027+
transformed["scaling_mode"] =
2028+
flattenCloudRunV2ServiceScalingScalingMode(original["scalingMode"], d, config)
2029+
transformed["manual_instance_count"] =
2030+
flattenCloudRunV2ServiceScalingManualInstanceCount(original["manualInstanceCount"], d, config)
20162031
return []interface{}{transformed}
20172032
}
20182033
func flattenCloudRunV2ServiceScalingMinInstanceCount(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
@@ -2032,6 +2047,27 @@ func flattenCloudRunV2ServiceScalingMinInstanceCount(v interface{}, d *schema.Re
20322047
return v // let terraform core handle it otherwise
20332048
}
20342049

2050+
func flattenCloudRunV2ServiceScalingScalingMode(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
2051+
return v
2052+
}
2053+
2054+
func flattenCloudRunV2ServiceScalingManualInstanceCount(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
2055+
// Handles the string fixed64 format
2056+
if strVal, ok := v.(string); ok {
2057+
if intVal, err := tpgresource.StringToFixed64(strVal); err == nil {
2058+
return intVal
2059+
}
2060+
}
2061+
2062+
// number values are represented as float64
2063+
if floatVal, ok := v.(float64); ok {
2064+
intVal := int(floatVal)
2065+
return intVal
2066+
}
2067+
2068+
return v // let terraform core handle it otherwise
2069+
}
2070+
20352071
func flattenCloudRunV2ServiceDefaultUriDisabled(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
20362072
return v
20372073
}
@@ -3588,13 +3624,35 @@ func expandCloudRunV2ServiceScaling(v interface{}, d tpgresource.TerraformResour
35883624
transformed["minInstanceCount"] = transformedMinInstanceCount
35893625
}
35903626

3627+
transformedScalingMode, err := expandCloudRunV2ServiceScalingScalingMode(original["scaling_mode"], d, config)
3628+
if err != nil {
3629+
return nil, err
3630+
} else if val := reflect.ValueOf(transformedScalingMode); val.IsValid() && !tpgresource.IsEmptyValue(val) {
3631+
transformed["scalingMode"] = transformedScalingMode
3632+
}
3633+
3634+
transformedManualInstanceCount, err := expandCloudRunV2ServiceScalingManualInstanceCount(original["manual_instance_count"], d, config)
3635+
if err != nil {
3636+
return nil, err
3637+
} else if val := reflect.ValueOf(transformedManualInstanceCount); val.IsValid() && !tpgresource.IsEmptyValue(val) {
3638+
transformed["manualInstanceCount"] = transformedManualInstanceCount
3639+
}
3640+
35913641
return transformed, nil
35923642
}
35933643

35943644
func expandCloudRunV2ServiceScalingMinInstanceCount(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
35953645
return v, nil
35963646
}
35973647

3648+
func expandCloudRunV2ServiceScalingScalingMode(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
3649+
return v, nil
3650+
}
3651+
3652+
func expandCloudRunV2ServiceScalingManualInstanceCount(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
3653+
return v, nil
3654+
}
3655+
35983656
func expandCloudRunV2ServiceDefaultUriDisabled(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
35993657
return v, nil
36003658
}

google-beta/services/cloudrunv2/resource_cloud_run_v2_service_generated_meta.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ fields:
5757
provider_only: true
5858
- field: 'observed_generation'
5959
- field: 'reconciling'
60+
- field: 'scaling.manual_instance_count'
6061
- field: 'scaling.min_instance_count'
62+
- field: 'scaling.scaling_mode'
6163
- field: 'template.annotations'
6264
- field: 'template.containers.args'
6365
- field: 'template.containers.base_image_uri'

google-beta/services/cloudrunv2/resource_cloud_run_v2_service_test.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1549,6 +1549,88 @@ resource "google_project_iam_member" "logs_writer" {
15491549
`, context)
15501550
}
15511551

1552+
func TestAccCloudRunV2Service_cloudrunv2ServiceWithManualScaling(t *testing.T) {
1553+
t.Parallel()
1554+
context := map[string]interface{}{
1555+
"random_suffix": acctest.RandString(t, 10),
1556+
}
1557+
acctest.VcrTest(t, resource.TestCase{
1558+
PreCheck: func() { acctest.AccTestPreCheck(t) },
1559+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
1560+
CheckDestroy: testAccCheckCloudRunV2ServiceDestroyProducer(t),
1561+
Steps: []resource.TestStep{
1562+
{
1563+
Config: testAccCloudRunV2Service_cloudrunv2ServiceWithManualScaling(context),
1564+
},
1565+
{
1566+
ResourceName: "google_cloud_run_v2_service.default",
1567+
ImportState: true,
1568+
ImportStateVerify: true,
1569+
ImportStateVerifyIgnore: []string{"name", "location", "annotations", "labels", "terraform_labels", "launch_stage", "deletion_protection"},
1570+
},
1571+
{
1572+
Config: testAccCloudRunV2Service_cloudrunv2ServiceUpdateWithManualScaling(context),
1573+
},
1574+
{
1575+
ResourceName: "google_cloud_run_v2_service.default",
1576+
ImportState: true,
1577+
ImportStateVerify: true,
1578+
ImportStateVerifyIgnore: []string{"name", "location", "annotations", "labels", "terraform_labels", "launch_stage", "deletion_protection"},
1579+
},
1580+
},
1581+
})
1582+
}
1583+
1584+
func testAccCloudRunV2Service_cloudrunv2ServiceWithManualScaling(context map[string]interface{}) string {
1585+
return acctest.Nprintf(`
1586+
resource "google_cloud_run_v2_service" "default" {
1587+
name = "tf-test-cloudrun-manual-scaling-service%{random_suffix}"
1588+
description = "description creating"
1589+
location = "us-central1"
1590+
deletion_protection = false
1591+
annotations = {
1592+
generated-by = "magic-modules"
1593+
}
1594+
ingress = "INGRESS_TRAFFIC_ALL"
1595+
launch_stage = "BETA"
1596+
scaling {
1597+
scaling_mode = "MANUAL"
1598+
manual_instance_count = 2
1599+
}
1600+
template {
1601+
containers {
1602+
image = "us-docker.pkg.dev/cloudrun/container/hello"
1603+
}
1604+
}
1605+
}
1606+
`, context)
1607+
}
1608+
1609+
func testAccCloudRunV2Service_cloudrunv2ServiceUpdateWithManualScaling(context map[string]interface{}) string {
1610+
return acctest.Nprintf(`
1611+
resource "google_cloud_run_v2_service" "default" {
1612+
name = "tf-test-cloudrun-manual-scaling-service%{random_suffix}"
1613+
description = "description creating"
1614+
location = "us-central1"
1615+
deletion_protection = false
1616+
annotations = {
1617+
generated-by = "magic-modules"
1618+
}
1619+
ingress = "INGRESS_TRAFFIC_ALL"
1620+
launch_stage = "BETA"
1621+
scaling {
1622+
scaling_mode = "MANUAL"
1623+
manual_instance_count = 10
1624+
}
1625+
template {
1626+
containers {
1627+
image = "us-docker.pkg.dev/cloudrun/container/hello"
1628+
}
1629+
}
1630+
}
1631+
`, context)
1632+
}
1633+
15521634
func TestAccCloudRunV2Service_cloudrunv2ServiceIapUpdate(t *testing.T) {
15531635
t.Parallel()
15541636

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)