Skip to content

Commit 241d31d

Browse files
authored
Add GRPC liveness/readiness/startup_probe (#1915)
1 parent 4ce1273 commit 241d31d

12 files changed

+192
-0
lines changed

.changelog/1915.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
Add a new optional attribute `grpc` to `pod.spec.container.liveness_probe`, `pod.spec.container.readiness_probe`, and `pod.spec.container.startup_probe`. That affects all resources and data sources that use mentioned `pod.spec.container` probes directly or as a template.
3+
```

kubernetes/resource_kubernetes_pod_test.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,42 @@ func TestAccKubernetesPod_with_container_liveness_probe_using_tcp(t *testing.T)
542542
})
543543
}
544544

545+
func TestAccKubernetesPod_with_container_liveness_probe_using_grpc(t *testing.T) {
546+
var conf api.Pod
547+
548+
podName := acctest.RandomWithPrefix("tf-acc-test")
549+
imageName := "gcr.io/google_containers/liveness"
550+
resourceName := "kubernetes_pod.test"
551+
552+
resource.Test(t, resource.TestCase{
553+
PreCheck: func() {
554+
testAccPreCheck(t)
555+
skipIfClusterVersionLessThan(t, "1.24.0")
556+
},
557+
ProviderFactories: testAccProviderFactories,
558+
CheckDestroy: testAccCheckKubernetesPodDestroy,
559+
Steps: []resource.TestStep{
560+
{
561+
Config: testAccKubernetesPodConfigWithLivenessProbeUsingGRPC(podName, imageName),
562+
Check: resource.ComposeAggregateTestCheckFunc(
563+
testAccCheckKubernetesPodExists(resourceName, &conf),
564+
resource.TestCheckResourceAttr(resourceName, "spec.0.container.0.args.#", "1"),
565+
resource.TestCheckResourceAttr(resourceName, "spec.0.container.0.liveness_probe.#", "1"),
566+
resource.TestCheckResourceAttr(resourceName, "spec.0.container.0.liveness_probe.0.grpc.#", "1"),
567+
resource.TestCheckResourceAttr(resourceName, "spec.0.container.0.liveness_probe.0.grpc.0.port", "8888"),
568+
resource.TestCheckResourceAttr(resourceName, "spec.0.container.0.liveness_probe.0.grpc.0.service", "EchoService"),
569+
),
570+
},
571+
{
572+
ResourceName: resourceName,
573+
ImportState: true,
574+
ImportStateVerify: true,
575+
ImportStateVerifyIgnore: []string{"metadata.0.resource_version"},
576+
},
577+
},
578+
})
579+
}
580+
545581
func TestAccKubernetesPod_with_container_lifecycle(t *testing.T) {
546582
var conf api.Pod
547583

@@ -1894,6 +1930,37 @@ func testAccKubernetesPodConfigWithLivenessProbeUsingTCP(podName, imageName stri
18941930
`, podName, imageName)
18951931
}
18961932

1933+
func testAccKubernetesPodConfigWithLivenessProbeUsingGRPC(podName, imageName string) string {
1934+
return fmt.Sprintf(`resource "kubernetes_pod" "test" {
1935+
metadata {
1936+
labels = {
1937+
app = "pod_label"
1938+
}
1939+
1940+
name = "%s"
1941+
}
1942+
1943+
spec {
1944+
container {
1945+
image = "%s"
1946+
name = "containername"
1947+
args = ["/server"]
1948+
1949+
liveness_probe {
1950+
grpc {
1951+
port = 8888
1952+
service = "EchoService"
1953+
}
1954+
1955+
initial_delay_seconds = 30
1956+
period_seconds = 30
1957+
}
1958+
}
1959+
}
1960+
}
1961+
`, podName, imageName)
1962+
}
1963+
18971964
func testAccKubernetesPodConfigWithLifeCycle(podName, imageName string) string {
18981965
return fmt.Sprintf(`resource "kubernetes_pod" "test" {
18991966
metadata {

kubernetes/schema_container.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,26 @@ func containerFields(isUpdatable bool) map[string]*schema.Schema {
660660

661661
func probeSchema() *schema.Resource {
662662
h := lifecycleHandlerFields()
663+
h["grpc"] = &schema.Schema{
664+
Type: schema.TypeList,
665+
Optional: true,
666+
Description: "GRPC specifies an action involving a GRPC port.",
667+
Elem: &schema.Resource{
668+
Schema: map[string]*schema.Schema{
669+
"port": {
670+
Type: schema.TypeInt,
671+
Required: true,
672+
ValidateFunc: validatePortNum,
673+
Description: "Number of the port to access on the container. Number must be in the range 1 to 65535.",
674+
},
675+
"service": {
676+
Type: schema.TypeString,
677+
Optional: true,
678+
Description: "Name of the service to place in the gRPC HealthCheckRequest (see https://github.com/grpc/grpc/blob/master/doc/health-checking.md). If this is not specified, the default behavior is defined by gRPC.",
679+
},
680+
},
681+
},
682+
}
663683
h["failure_threshold"] = &schema.Schema{
664684
Type: schema.TypeInt,
665685
Optional: true,

kubernetes/structures_container.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,15 @@ func flattenTCPSocket(in *v1.TCPSocketAction) []interface{} {
122122
return []interface{}{att}
123123
}
124124

125+
func flattenGRPC(in *v1.GRPCAction) []interface{} {
126+
att := make(map[string]interface{})
127+
att["port"] = in.Port
128+
if in.Service != nil {
129+
att["service"] = *in.Service
130+
}
131+
return []interface{}{att}
132+
}
133+
125134
func flattenExec(in *v1.ExecAction) []interface{} {
126135
att := make(map[string]interface{})
127136
if len(in.Command) > 0 {
@@ -161,6 +170,9 @@ func flattenProbe(in *v1.Probe) []interface{} {
161170
if in.TCPSocket != nil {
162171
att["tcp_socket"] = flattenTCPSocket(in.TCPSocket)
163172
}
173+
if in.GRPC != nil {
174+
att["grpc"] = flattenGRPC(in.GRPC)
175+
}
164176

165177
return []interface{}{att}
166178
}
@@ -656,6 +668,21 @@ func expandTCPSocket(l []interface{}) *v1.TCPSocketAction {
656668
return &obj
657669
}
658670

671+
func expandGRPC(l []interface{}) *v1.GRPCAction {
672+
if len(l) == 0 || l[0] == nil {
673+
return &v1.GRPCAction{}
674+
}
675+
in := l[0].(map[string]interface{})
676+
obj := v1.GRPCAction{}
677+
if v, ok := in["port"].(int); ok {
678+
obj.Port = int32(v)
679+
}
680+
if v, ok := in["service"].(string); ok {
681+
obj.Service = ptrToString(v)
682+
}
683+
return &obj
684+
}
685+
659686
func expandHTTPGet(l []interface{}) *v1.HTTPGetAction {
660687
if len(l) == 0 || l[0] == nil {
661688
return &v1.HTTPGetAction{}
@@ -697,6 +724,9 @@ func expandProbe(l []interface{}) *v1.Probe {
697724
if v, ok := in["tcp_socket"].([]interface{}); ok && len(v) > 0 {
698725
obj.TCPSocket = expandTCPSocket(v)
699726
}
727+
if v, ok := in["grpc"].([]interface{}); ok && len(v) > 0 {
728+
obj.GRPC = expandGRPC(v)
729+
}
700730
if v, ok := in["failure_threshold"].(int); ok {
701731
obj.FailureThreshold = int32(v)
702732
}

website/docs/d/pod.html.markdown

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,13 @@ The `option` block supports the following:
180180

181181
* `command` - Command is the command line to execute inside the container, the working directory for the command is root ('/') in the container's filesystem. The command is simply exec'd, it is not run inside a shell, so traditional shell instructions. To use a shell, you need to explicitly call out to that shell. Exit status of 0 is treated as live/healthy and non-zero is unhealthy.
182182

183+
### `grpc`
184+
185+
#### Arguments
186+
187+
* `port` - Number of the port to access on the container. Number must be in the range 1 to 65535.
188+
* `service` - Name of the service to place in the gRPC HealthCheckRequest (see https://github.com/grpc/grpc/blob/master/doc/health-checking.md). If this is not specified, the default behavior is defined by gRPC.
189+
183190
### `image_pull_secrets`
184191

185192
#### Attributes
@@ -207,6 +214,7 @@ The `option` block supports the following:
207214
* `exec` - exec specifies the action to take.
208215
* `failure_threshold` - Minimum consecutive failures for the probe to be considered failed after having succeeded.
209216
* `http_get` - Specifies the http request to perform.
217+
* `grpc` - GRPC specifies an action involving a GRPC port.
210218
* `initial_delay_seconds` - Number of seconds after the container has started before liveness probes are initiated. For more info see [Kubernetes reference](http://kubernetes.io/docs/user-guide/pod-states#container-probes)
211219
* `period_seconds` - How often (in seconds) to perform the probe
212220
* `success_threshold` - Minimum consecutive successes for the probe to be considered successful after having failed.
@@ -290,6 +298,7 @@ The `option` block supports the following:
290298

291299
* `exec` - exec specifies the action to take.
292300
* `failure_threshold` - Minimum consecutive failures for the probe to be considered failed after having succeeded.
301+
* `grpc` - GRPC specifies an action involving a GRPC port.
293302
* `http_get` - Specifies the http request to perform.
294303
* `initial_delay_seconds` - Number of seconds after the container has started before readiness probes are initiated. For more info see [Kubernetes reference](http://kubernetes.io/docs/user-guide/pod-states#container-probes)
295304
* `period_seconds` - How often (in seconds) to perform the probe

website/docs/d/pod_v1.html.markdown

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,13 @@ The `option` block supports the following:
180180

181181
* `command` - Command is the command line to execute inside the container, the working directory for the command is root ('/') in the container's filesystem. The command is simply exec'd, it is not run inside a shell, so traditional shell instructions. To use a shell, you need to explicitly call out to that shell. Exit status of 0 is treated as live/healthy and non-zero is unhealthy.
182182

183+
### `grpc`
184+
185+
#### Arguments
186+
187+
* `port` - Number of the port to access on the container. Number must be in the range 1 to 65535.
188+
* `service` - Name of the service to place in the gRPC HealthCheckRequest (see https://github.com/grpc/grpc/blob/master/doc/health-checking.md). If this is not specified, the default behavior is defined by gRPC.
189+
183190
### `image_pull_secrets`
184191

185192
#### Attributes
@@ -206,6 +213,7 @@ The `option` block supports the following:
206213

207214
* `exec` - exec specifies the action to take.
208215
* `failure_threshold` - Minimum consecutive failures for the probe to be considered failed after having succeeded.
216+
* `grpc` - GRPC specifies an action involving a GRPC port.
209217
* `http_get` - Specifies the http request to perform.
210218
* `initial_delay_seconds` - Number of seconds after the container has started before liveness probes are initiated. For more info see [Kubernetes reference](http://kubernetes.io/docs/user-guide/pod-states#container-probes)
211219
* `period_seconds` - How often (in seconds) to perform the probe
@@ -290,6 +298,7 @@ The `option` block supports the following:
290298

291299
* `exec` - exec specifies the action to take.
292300
* `failure_threshold` - Minimum consecutive failures for the probe to be considered failed after having succeeded.
301+
* `grpc` - GRPC specifies an action involving a GRPC port.
293302
* `http_get` - Specifies the http request to perform.
294303
* `initial_delay_seconds` - Number of seconds after the container has started before readiness probes are initiated. For more info see [Kubernetes reference](http://kubernetes.io/docs/user-guide/pod-states#container-probes)
295304
* `period_seconds` - How often (in seconds) to perform the probe

website/docs/r/daemon_set_v1.html.markdown

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,13 @@ The `option` block supports the following:
474474
* `path` - (Required) The Glusterfs volume path. For more info see https://github.com/kubernetes/examples/tree/master/volumes/glusterfs#create-a-pod.
475475
* `read_only` - (Optional) Whether to force the Glusterfs volume to be mounted with read-only permissions. Defaults to false. For more info see https://github.com/kubernetes/examples/tree/master/volumes/glusterfs#create-a-pod.
476476

477+
### `grpc`
478+
479+
#### Arguments
480+
481+
* `port` - (Required) Number of the port to access on the container. Number must be in the range 1 to 65535.
482+
* `service` - (Optional) Name of the service to place in the gRPC HealthCheckRequest (see https://github.com/grpc/grpc/blob/master/doc/health-checking.md). If this is not specified, the default behavior is defined by gRPC.
483+
477484
### `host_aliases`
478485

479486
#### Arguments
@@ -543,6 +550,7 @@ The `option` block supports the following:
543550

544551
* `exec` - (Optional) exec specifies the action to take.
545552
* `failure_threshold` - (Optional) Minimum consecutive failures for the probe to be considered failed after having succeeded.
553+
* `grpc` - (Optional) GRPC specifies an action involving a GRPC port. **NOTE: This field is behind a [feature gate](https://kubernetes.io/docs/reference/command-line-tools-reference/feature-gates/) prior to v1.24**
546554
* `http_get` - (Optional) Specifies the http request to perform.
547555
* `initial_delay_seconds` - (Optional) Number of seconds after the container has started before liveness probes are initiated. For more info see [Kubernetes reference](http://kubernetes.io/docs/user-guide/pod-states#container-probes)
548556
* `period_seconds` - (Optional) How often (in seconds) to perform the probe
@@ -627,6 +635,7 @@ The `option` block supports the following:
627635

628636
* `exec` - (Optional) exec specifies the action to take.
629637
* `failure_threshold` - (Optional) Minimum consecutive failures for the probe to be considered failed after having succeeded.
638+
* `grpc` - (Optional) GRPC specifies an action involving a GRPC port. **NOTE: This field is behind a [feature gate](https://kubernetes.io/docs/reference/command-line-tools-reference/feature-gates/) prior to v1.24**
630639
* `http_get` - (Optional) Specifies the http request to perform.
631640
* `initial_delay_seconds` - (Optional) Number of seconds after the container has started before readiness probes are initiated. For more info see [Kubernetes reference](http://kubernetes.io/docs/user-guide/pod-states#container-probes)
632641
* `period_seconds` - (Optional) How often (in seconds) to perform the probe

website/docs/r/daemonset.html.markdown

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,13 @@ The `option` block supports the following:
484484
* `path` - (Required) The Glusterfs volume path. For more info see https://github.com/kubernetes/examples/tree/master/volumes/glusterfs#create-a-pod.
485485
* `read_only` - (Optional) Whether to force the Glusterfs volume to be mounted with read-only permissions. Defaults to false. For more info see https://github.com/kubernetes/examples/tree/master/volumes/glusterfs#create-a-pod.
486486

487+
### `grpc`
488+
489+
#### Arguments
490+
491+
* `port` - (Required) Number of the port to access on the container. Number must be in the range 1 to 65535.
492+
* `service` - (Optional) Name of the service to place in the gRPC HealthCheckRequest (see https://github.com/grpc/grpc/blob/master/doc/health-checking.md). If this is not specified, the default behavior is defined by gRPC.
493+
487494
### `host_aliases`
488495

489496
#### Arguments
@@ -553,6 +560,7 @@ The `option` block supports the following:
553560

554561
* `exec` - (Optional) exec specifies the action to take.
555562
* `failure_threshold` - (Optional) Minimum consecutive failures for the probe to be considered failed after having succeeded.
563+
* `grpc` - (Optional) GRPC specifies an action involving a GRPC port. **NOTE: This field is behind a [feature gate](https://kubernetes.io/docs/reference/command-line-tools-reference/feature-gates/) prior to v1.24**
556564
* `http_get` - (Optional) Specifies the http request to perform.
557565
* `initial_delay_seconds` - (Optional) Number of seconds after the container has started before liveness probes are initiated. For more info see [Kubernetes reference](http://kubernetes.io/docs/user-guide/pod-states#container-probes)
558566
* `period_seconds` - (Optional) How often (in seconds) to perform the probe
@@ -637,6 +645,7 @@ The `option` block supports the following:
637645

638646
* `exec` - (Optional) exec specifies the action to take.
639647
* `failure_threshold` - (Optional) Minimum consecutive failures for the probe to be considered failed after having succeeded.
648+
* `grpc` - (Optional) GRPC specifies an action involving a GRPC port. **NOTE: This field is behind a [feature gate](https://kubernetes.io/docs/reference/command-line-tools-reference/feature-gates/) prior to v1.24**
640649
* `http_get` - (Optional) Specifies the http request to perform.
641650
* `initial_delay_seconds` - (Optional) Number of seconds after the container has started before readiness probes are initiated. For more info see [Kubernetes reference](http://kubernetes.io/docs/user-guide/pod-states#container-probes)
642651
* `period_seconds` - (Optional) How often (in seconds) to perform the probe

website/docs/r/deployment.html.markdown

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,13 @@ The `option` block supports the following:
495495
* `path` - (Required) The Glusterfs volume path. For more info see https://github.com/kubernetes/examples/tree/master/volumes/glusterfs#create-a-pod.
496496
* `read_only` - (Optional) Whether to force the Glusterfs volume to be mounted with read-only permissions. Defaults to false. For more info see https://github.com/kubernetes/examples/tree/master/volumes/glusterfs#create-a-pod.
497497

498+
### `grpc`
499+
500+
#### Arguments
501+
502+
* `port` - (Required) Number of the port to access on the container. Number must be in the range 1 to 65535.
503+
* `service` - (Optional) Name of the service to place in the gRPC HealthCheckRequest (see https://github.com/grpc/grpc/blob/master/doc/health-checking.md). If this is not specified, the default behavior is defined by gRPC.
504+
498505
### `host_aliases`
499506

500507
#### Arguments
@@ -564,6 +571,7 @@ The `option` block supports the following:
564571

565572
* `exec` - (Optional) exec specifies the action to take.
566573
* `failure_threshold` - (Optional) Minimum consecutive failures for the probe to be considered failed after having succeeded.
574+
* `grpc` - (Optional) GRPC specifies an action involving a GRPC port. **NOTE: This field is behind a [feature gate](https://kubernetes.io/docs/reference/command-line-tools-reference/feature-gates/) prior to v1.24**
567575
* `http_get` - (Optional) Specifies the http request to perform.
568576
* `initial_delay_seconds` - (Optional) Number of seconds after the container has started before liveness probes are initiated. For more info see [Kubernetes reference](http://kubernetes.io/docs/user-guide/pod-states#container-probes)
569577
* `period_seconds` - (Optional) How often (in seconds) to perform the probe
@@ -648,6 +656,7 @@ The `option` block supports the following:
648656

649657
* `exec` - (Optional) exec specifies the action to take.
650658
* `failure_threshold` - (Optional) Minimum consecutive failures for the probe to be considered failed after having succeeded.
659+
* `grpc` - (Optional) GRPC specifies an action involving a GRPC port. **NOTE: This field is behind a [feature gate](https://kubernetes.io/docs/reference/command-line-tools-reference/feature-gates/) prior to v1.24**
651660
* `http_get` - (Optional) Specifies the http request to perform.
652661
* `initial_delay_seconds` - (Optional) Number of seconds after the container has started before readiness probes are initiated. For more info see [Kubernetes reference](http://kubernetes.io/docs/user-guide/pod-states#container-probes)
653662
* `period_seconds` - (Optional) How often (in seconds) to perform the probe

website/docs/r/deployment_v1.html.markdown

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,13 @@ The `option` block supports the following:
485485
* `path` - (Required) The Glusterfs volume path. For more info see https://github.com/kubernetes/examples/tree/master/volumes/glusterfs#create-a-pod.
486486
* `read_only` - (Optional) Whether to force the Glusterfs volume to be mounted with read-only permissions. Defaults to false. For more info see https://github.com/kubernetes/examples/tree/master/volumes/glusterfs#create-a-pod.
487487

488+
### `grpc`
489+
490+
#### Arguments
491+
492+
* `port` - (Required) Number of the port to access on the container. Number must be in the range 1 to 65535.
493+
* `service` - (Optional) Name of the service to place in the gRPC HealthCheckRequest (see https://github.com/grpc/grpc/blob/master/doc/health-checking.md). If this is not specified, the default behavior is defined by gRPC.
494+
488495
### `host_aliases`
489496

490497
#### Arguments
@@ -554,6 +561,7 @@ The `option` block supports the following:
554561

555562
* `exec` - (Optional) exec specifies the action to take.
556563
* `failure_threshold` - (Optional) Minimum consecutive failures for the probe to be considered failed after having succeeded.
564+
* `grpc` - (Optional) GRPC specifies an action involving a GRPC port. **NOTE: This field is behind a [feature gate](https://kubernetes.io/docs/reference/command-line-tools-reference/feature-gates/) prior to v1.24**
557565
* `http_get` - (Optional) Specifies the http request to perform.
558566
* `initial_delay_seconds` - (Optional) Number of seconds after the container has started before liveness probes are initiated. For more info see [Kubernetes reference](http://kubernetes.io/docs/user-guide/pod-states#container-probes)
559567
* `period_seconds` - (Optional) How often (in seconds) to perform the probe
@@ -638,6 +646,7 @@ The `option` block supports the following:
638646

639647
* `exec` - (Optional) exec specifies the action to take.
640648
* `failure_threshold` - (Optional) Minimum consecutive failures for the probe to be considered failed after having succeeded.
649+
* `grpc` - (Optional) GRPC specifies an action involving a GRPC port. **NOTE: This field is behind a [feature gate](https://kubernetes.io/docs/reference/command-line-tools-reference/feature-gates/) prior to v1.24**
641650
* `http_get` - (Optional) Specifies the http request to perform.
642651
* `initial_delay_seconds` - (Optional) Number of seconds after the container has started before readiness probes are initiated. For more info see [Kubernetes reference](http://kubernetes.io/docs/user-guide/pod-states#container-probes)
643652
* `period_seconds` - (Optional) How often (in seconds) to perform the probe

0 commit comments

Comments
 (0)