Skip to content

Commit 0a85a5d

Browse files
Add traffic status field to cloud run v1 service. (#8410) (#15284)
Signed-off-by: Modular Magician <[email protected]>
1 parent f423e7c commit 0a85a5d

File tree

4 files changed

+158
-0
lines changed

4 files changed

+158
-0
lines changed

.changelog/8410.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
cloud-run: added `status.traffic` output fields to `google_cloud_run_service` resource
3+
```

google/resource_cloud_run_service_test.go

+25
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,31 @@ func TestAccCloudRunService_cloudRunServiceUpdate(t *testing.T) {
4343
})
4444
}
4545

46+
// test that the status fields are propagated correctly
47+
func TestAccCloudRunService_cloudRunServiceCreateHasStatus(t *testing.T) {
48+
t.Parallel()
49+
50+
project := envvar.GetTestProjectFromEnv()
51+
name := "tftest-cloudrun-" + acctest.RandString(t, 6)
52+
53+
acctest.VcrTest(t, resource.TestCase{
54+
PreCheck: func() { acctest.AccTestPreCheck(t) },
55+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
56+
Steps: []resource.TestStep{
57+
{
58+
Config: testAccCloudRunService_cloudRunServiceUpdate(name, project, "10", "600"),
59+
Check: resource.TestCheckResourceAttrSet("google_cloud_run_service.default", "status.0.traffic.0.revision_name"),
60+
},
61+
{
62+
ResourceName: "google_cloud_run_service.default",
63+
ImportState: true,
64+
ImportStateVerify: true,
65+
ImportStateVerifyIgnore: []string{"metadata.0.resource_version", "status.0.conditions"},
66+
},
67+
},
68+
})
69+
}
70+
4671
// this test checks that Terraform does not fail with a 409 recreating the same service
4772
func TestAccCloudRunService_foregroundDeletion(t *testing.T) {
4873
t.Parallel()

google/services/cloudrun/resource_cloud_run_service.go

+97
Original file line numberDiff line numberDiff line change
@@ -1031,6 +1031,46 @@ controller.
10311031
Clients polling for completed reconciliation should poll until observedGeneration =
10321032
metadata.generation and the Ready condition's status is True or False.`,
10331033
},
1034+
"traffic": {
1035+
Type: schema.TypeList,
1036+
Computed: true,
1037+
Description: `Traffic specifies how to distribute traffic over a collection of Knative Revisions
1038+
and Configurations`,
1039+
Elem: &schema.Resource{
1040+
Schema: map[string]*schema.Schema{
1041+
"latest_revision": {
1042+
Type: schema.TypeBool,
1043+
Computed: true,
1044+
Description: `LatestRevision may be optionally provided to indicate that the latest ready
1045+
Revision of the Configuration should be used for this traffic target. When
1046+
provided LatestRevision must be true if RevisionName is empty; it must be
1047+
false when RevisionName is non-empty.`,
1048+
},
1049+
"percent": {
1050+
Type: schema.TypeInt,
1051+
Computed: true,
1052+
Description: `Percent specifies percent of the traffic to this Revision or Configuration.`,
1053+
},
1054+
"revision_name": {
1055+
Type: schema.TypeString,
1056+
Computed: true,
1057+
Description: `RevisionName of a specific revision to which to send this portion of traffic.`,
1058+
},
1059+
"tag": {
1060+
Type: schema.TypeString,
1061+
Computed: true,
1062+
Description: `Tag is optionally used to expose a dedicated url for referencing this target exclusively.`,
1063+
},
1064+
"url": {
1065+
Type: schema.TypeString,
1066+
Computed: true,
1067+
Description: `URL displays the URL for accessing tagged traffic targets. URL is displayed in status,
1068+
and is disallowed on spec. URL must contain a scheme (e.g. http://) and a hostname,
1069+
but may not contain anything else (e.g. basic auth, url path, etc.)`,
1070+
},
1071+
},
1072+
},
1073+
},
10341074
"url": {
10351075
Type: schema.TypeString,
10361076
Computed: true,
@@ -2518,6 +2558,8 @@ func flattenCloudRunServiceStatus(v interface{}, d *schema.ResourceData, config
25182558
flattenCloudRunServiceStatusLatestCreatedRevisionName(original["latestCreatedRevisionName"], d, config)
25192559
transformed["latest_ready_revision_name"] =
25202560
flattenCloudRunServiceStatusLatestReadyRevisionName(original["latestReadyRevisionName"], d, config)
2561+
transformed["traffic"] =
2562+
flattenCloudRunServiceStatusTraffic(original["traffic"], d, config)
25212563
return []interface{}{transformed}
25222564
}
25232565
func flattenCloudRunServiceStatusConditions(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
@@ -2586,6 +2628,61 @@ func flattenCloudRunServiceStatusLatestReadyRevisionName(v interface{}, d *schem
25862628
return v
25872629
}
25882630

2631+
func flattenCloudRunServiceStatusTraffic(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
2632+
if v == nil {
2633+
return v
2634+
}
2635+
l := v.([]interface{})
2636+
transformed := make([]interface{}, 0, len(l))
2637+
for _, raw := range l {
2638+
original := raw.(map[string]interface{})
2639+
if len(original) < 1 {
2640+
// Do not include empty json objects coming back from the api
2641+
continue
2642+
}
2643+
transformed = append(transformed, map[string]interface{}{
2644+
"revision_name": flattenCloudRunServiceStatusTrafficRevisionName(original["revisionName"], d, config),
2645+
"percent": flattenCloudRunServiceStatusTrafficPercent(original["percent"], d, config),
2646+
"tag": flattenCloudRunServiceStatusTrafficTag(original["tag"], d, config),
2647+
"latest_revision": flattenCloudRunServiceStatusTrafficLatestRevision(original["latestRevision"], d, config),
2648+
"url": flattenCloudRunServiceStatusTrafficUrl(original["url"], d, config),
2649+
})
2650+
}
2651+
return transformed
2652+
}
2653+
func flattenCloudRunServiceStatusTrafficRevisionName(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
2654+
return v
2655+
}
2656+
2657+
func flattenCloudRunServiceStatusTrafficPercent(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
2658+
// Handles the string fixed64 format
2659+
if strVal, ok := v.(string); ok {
2660+
if intVal, err := tpgresource.StringToFixed64(strVal); err == nil {
2661+
return intVal
2662+
}
2663+
}
2664+
2665+
// number values are represented as float64
2666+
if floatVal, ok := v.(float64); ok {
2667+
intVal := int(floatVal)
2668+
return intVal
2669+
}
2670+
2671+
return v // let terraform core handle it otherwise
2672+
}
2673+
2674+
func flattenCloudRunServiceStatusTrafficTag(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
2675+
return v
2676+
}
2677+
2678+
func flattenCloudRunServiceStatusTrafficLatestRevision(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
2679+
return v
2680+
}
2681+
2682+
func flattenCloudRunServiceStatusTrafficUrl(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
2683+
return v
2684+
}
2685+
25892686
func flattenCloudRunServiceMetadata(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
25902687
if v == nil {
25912688
return nil

website/docs/r/cloud_run_service.html.markdown

+33
Original file line numberDiff line numberDiff line change
@@ -1005,6 +1005,12 @@ In addition to the arguments listed above, the following computed attributes are
10051005
stamped out from this Service's Configuration that has had its "Ready" condition become
10061006
"True".
10071007

1008+
* `traffic` -
1009+
(Output)
1010+
Traffic specifies how to distribute traffic over a collection of Knative Revisions
1011+
and Configurations
1012+
Structure is [documented below](#nested_traffic).
1013+
10081014

10091015
<a name="nested_conditions"></a>The `conditions` block contains:
10101016

@@ -1024,6 +1030,33 @@ In addition to the arguments listed above, the following computed attributes are
10241030
(Output)
10251031
Type of domain mapping condition.
10261032

1033+
<a name="nested_traffic"></a>The `traffic` block contains:
1034+
1035+
* `revision_name` -
1036+
(Output)
1037+
RevisionName of a specific revision to which to send this portion of traffic.
1038+
1039+
* `percent` -
1040+
(Output)
1041+
Percent specifies percent of the traffic to this Revision or Configuration.
1042+
1043+
* `tag` -
1044+
(Output)
1045+
Tag is optionally used to expose a dedicated url for referencing this target exclusively.
1046+
1047+
* `latest_revision` -
1048+
(Output)
1049+
LatestRevision may be optionally provided to indicate that the latest ready
1050+
Revision of the Configuration should be used for this traffic target. When
1051+
provided LatestRevision must be true if RevisionName is empty; it must be
1052+
false when RevisionName is non-empty.
1053+
1054+
* `url` -
1055+
(Output)
1056+
URL displays the URL for accessing tagged traffic targets. URL is displayed in status,
1057+
and is disallowed on spec. URL must contain a scheme (e.g. http://) and a hostname,
1058+
but may not contain anything else (e.g. basic auth, url path, etc.)
1059+
10271060
## Timeouts
10281061

10291062
This resource provides the following

0 commit comments

Comments
 (0)