Skip to content

Commit ffda1d5

Browse files
Add support for readiness probe for services (#15626)
1 parent 35d0491 commit ffda1d5

File tree

3 files changed

+274
-0
lines changed

3 files changed

+274
-0
lines changed

mmv1/products/cloudrun/Service.yaml

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,10 @@ examples:
139139
primary_resource_id: 'default'
140140
vars:
141141
cloud_run_service_name: 'cloudrun-srv'
142+
- name: 'cloud_run_service_readiness_probe'
143+
primary_resource_id: 'default'
144+
vars:
145+
cloud_run_service_name: 'cloudrun-srv-rp'
142146
test_env_vars:
143147
project: 'PROJECT_NAME'
144148
- name: 'cloud_run_service_multicontainer'
@@ -687,6 +691,78 @@ properties:
687691
The name of the service to place in the gRPC HealthCheckRequest
688692
(see https://github.com/grpc/grpc/blob/master/doc/health-checking.md).
689693
If this is not specified, the default behavior is defined by gRPC.
694+
- name: 'readinessProbe'
695+
type: NestedObject
696+
description: |-
697+
Periodic probe of container readiness.
698+
properties:
699+
- name: 'timeoutSeconds'
700+
type: Integer
701+
description: |-
702+
Number of seconds after which the probe times out.
703+
Defaults to 1 second. Must be smaller than period_seconds.
704+
default_from_api: true
705+
- name: 'periodSeconds'
706+
type: Integer
707+
description: |-
708+
How often (in seconds) to perform the probe.
709+
Default to 10 seconds.
710+
default_from_api: true
711+
- name: 'failureThreshold'
712+
type: Integer
713+
description: |-
714+
Minimum consecutive failures for the probe to be considered failed after
715+
having succeeded. Defaults to 3.
716+
default_from_api: true
717+
- name: 'successThreshold'
718+
type: Integer
719+
description: |-
720+
Minimum consecutive successes for the probe to be considered successful after having failed.
721+
Defaults to 2.
722+
default_from_api: true
723+
- name: 'httpGet'
724+
type: NestedObject
725+
description: |-
726+
HttpGet specifies the http request to perform.
727+
send_empty_value: true
728+
allow_empty_object: true
729+
exactly_one_of:
730+
- 'template.0.spec.0.containers.0.readiness_probe.0.http_get'
731+
- 'template.0.spec.0.containers.0.readiness_probe.0.grpc'
732+
properties:
733+
- name: 'path'
734+
type: String
735+
description: |-
736+
Path to access on the HTTP server. If set, it should not be empty string.
737+
default_from_api: true
738+
- name: 'port'
739+
type: Integer
740+
description: |-
741+
Port number to access on the container. Number must be in the range 1 to 65535.
742+
If not specified, defaults to the same value as container.ports[0].containerPort.
743+
default_from_api: true
744+
- name: 'grpc'
745+
type: NestedObject
746+
description: |-
747+
GRPC specifies an action involving a GRPC port.
748+
send_empty_value: true
749+
allow_empty_object: true
750+
exactly_one_of:
751+
- 'template.0.spec.0.containers.0.readiness_probe.0.http_get'
752+
- 'template.0.spec.0.containers.0.readiness_probe.0.grpc'
753+
properties:
754+
- name: 'port'
755+
type: Integer
756+
description: |-
757+
Port number to access on the container. Number must be in the range 1 to 65535.
758+
If not specified, defaults to the same value as container.ports[0].containerPort.
759+
default_from_api: true
760+
- name: 'service'
761+
type: String
762+
description: |-
763+
The name of the service to place in the gRPC HealthCheckRequest
764+
(see https://github.com/grpc/grpc/blob/master/doc/health-checking.md).
765+
If this is not specified, the default behavior is defined by gRPC.
690766
- name: 'livenessProbe'
691767
type: NestedObject
692768
description: |-
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
resource "google_cloud_run_service" "{{$.PrimaryResourceId}}" {
2+
name = "{{index $.Vars "cloud_run_service_name"}}"
3+
location = "us-central1"
4+
5+
metadata {
6+
annotations = {
7+
"run.googleapis.com/launch-stage" = "BETA"
8+
}
9+
}
10+
11+
template {
12+
spec {
13+
containers {
14+
image = "us-docker.pkg.dev/cloudrun/container/hello"
15+
readiness_probe {
16+
timeout_seconds = 20
17+
period_seconds = 30
18+
success_threshold = 3
19+
failure_threshold = 2
20+
grpc {
21+
port = 8080
22+
}
23+
}
24+
}
25+
}
26+
}
27+
28+
traffic {
29+
percent = 100
30+
latest_revision = true
31+
}
32+
33+
lifecycle {
34+
ignore_changes = [
35+
metadata.0.annotations,
36+
]
37+
}
38+
}

mmv1/third_party/terraform/services/cloudrun/resource_cloud_run_service_test.go.tmpl

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1089,6 +1089,166 @@ resource "google_cloud_run_service" "default" {
10891089

10901090
{{ end }}
10911091

1092+
func TestAccCloudRunService_readinessProbe(t *testing.T) {
1093+
t.Parallel()
1094+
1095+
project := envvar.GetTestProjectFromEnv()
1096+
name := "tftest-cloudrun-" + acctest.RandString(t, 6)
1097+
1098+
acctest.VcrTest(t, resource.TestCase{
1099+
PreCheck: func() { acctest.AccTestPreCheck(t) },
1100+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
1101+
Steps: []resource.TestStep{
1102+
{
1103+
Config: testAccCloudRunService_cloudRunServiceWithHTTPReadinessProbe(name, project),
1104+
},
1105+
{
1106+
ResourceName: "google_cloud_run_service.default",
1107+
ImportState: true,
1108+
ImportStateVerify: true,
1109+
ImportStateVerifyIgnore: []string{"metadata.0.resource_version", "metadata.0.annotations", "metadata.0.labels", "metadata.0.terraform_labels", "status.0.conditions"},
1110+
},
1111+
{
1112+
Config: testAccCloudRunService_cloudRunServiceUpdateWithHTTPReadinessProbe(name, project),
1113+
},
1114+
{
1115+
ResourceName: "google_cloud_run_service.default",
1116+
ImportState: true,
1117+
ImportStateVerify: true,
1118+
ImportStateVerifyIgnore: []string{"metadata.0.resource_version", "metadata.0.annotations", "metadata.0.labels", "metadata.0.terraform_labels", "status.0.conditions"},
1119+
},
1120+
{
1121+
Config: testAccCloudRunService_cloudRunServiceUpdateWithGRPCReadinessProbe(name, project),
1122+
},
1123+
{
1124+
ResourceName: "google_cloud_run_service.default",
1125+
ImportState: true,
1126+
ImportStateVerify: true,
1127+
ImportStateVerifyIgnore: []string{"metadata.0.resource_version", "metadata.0.annotations", "metadata.0.labels", "metadata.0.terraform_labels", "status.0.conditions"},
1128+
},
1129+
},
1130+
})
1131+
}
1132+
1133+
func testAccCloudRunService_cloudRunServiceWithHTTPReadinessProbe(name, project string) string {
1134+
return fmt.Sprintf(`
1135+
resource "google_cloud_run_service" "default" {
1136+
name = "%s"
1137+
location = "us-central1"
1138+
1139+
metadata {
1140+
namespace = "%s"
1141+
annotations = {
1142+
generated-by = "magic-modules"
1143+
"run.googleapis.com/launch-stage": "BETA"
1144+
1145+
}
1146+
}
1147+
1148+
template {
1149+
spec {
1150+
containers {
1151+
image = "gcr.io/cloudrun/hello"
1152+
ports {
1153+
container_port = 8080
1154+
}
1155+
readiness_probe {
1156+
http_get {}
1157+
}
1158+
}
1159+
}
1160+
}
1161+
1162+
lifecycle {
1163+
ignore_changes = [
1164+
metadata.0.annotations,
1165+
]
1166+
}
1167+
}
1168+
`, name, project)
1169+
}
1170+
1171+
func testAccCloudRunService_cloudRunServiceUpdateWithHTTPReadinessProbe(name, project string) string {
1172+
return fmt.Sprintf(`
1173+
resource "google_cloud_run_service" "default" {
1174+
name = "%s"
1175+
location = "us-central1"
1176+
1177+
metadata {
1178+
namespace = "%s"
1179+
annotations = {
1180+
generated-by = "magic-modules"
1181+
"run.googleapis.com/launch-stage": "BETA"
1182+
}
1183+
}
1184+
1185+
template {
1186+
spec {
1187+
containers {
1188+
image = "gcr.io/cloudrun/hello"
1189+
ports {
1190+
container_port = 8080
1191+
}
1192+
readiness_probe {
1193+
period_seconds = 30
1194+
timeout_seconds = 20
1195+
failure_threshold = 1
1196+
success_threshold = 1
1197+
http_get {
1198+
path = "/some-path"
1199+
port = 8080
1200+
}
1201+
}
1202+
}
1203+
}
1204+
}
1205+
1206+
lifecycle {
1207+
ignore_changes = [
1208+
metadata.0.annotations,
1209+
]
1210+
}
1211+
}
1212+
`, name, project)
1213+
}
1214+
1215+
func testAccCloudRunService_cloudRunServiceUpdateWithGRPCReadinessProbe(name, project string) string {
1216+
return fmt.Sprintf(`
1217+
resource "google_cloud_run_service" "default" {
1218+
name = "%s"
1219+
location = "us-central1"
1220+
1221+
metadata {
1222+
namespace = "%s"
1223+
annotations = {
1224+
generated-by = "magic-modules"
1225+
"run.googleapis.com/launch-stage": "BETA"
1226+
}
1227+
}
1228+
1229+
template {
1230+
spec {
1231+
containers {
1232+
image = "gcr.io/cloudrun/hello"
1233+
readiness_probe {
1234+
grpc {
1235+
port = 8080
1236+
service = "health"
1237+
}
1238+
}
1239+
}
1240+
}
1241+
}
1242+
1243+
lifecycle {
1244+
ignore_changes = [
1245+
metadata.0.annotations,
1246+
]
1247+
}
1248+
}
1249+
`, name, project)
1250+
}
1251+
10921252
func TestAccCloudRunService_withCreationOnlyAttribution(t *testing.T) {
10931253
t.Parallel()
10941254

0 commit comments

Comments
 (0)