Skip to content

Commit

Permalink
Adds grpc field to liveness_probe and startup_probe to `google_…
Browse files Browse the repository at this point in the history
…cloud_run_v2_service` resource (#6987) (#5259)

Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
The Magician authored Feb 27, 2023
1 parent 4407599 commit ff75441
Show file tree
Hide file tree
Showing 6 changed files with 541 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .changelog/6987.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
added `template.0.containers0.liveness_probe.grpc`, `template.0.containers0.startup_probe.grpc` fields to `google_cloud_run_v2_service` resource
```
15 changes: 15 additions & 0 deletions google-beta/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import (
iamcredentials "google.golang.org/api/iamcredentials/v1"
cloudlogging "google.golang.org/api/logging/v2"
"google.golang.org/api/pubsub/v1"
runadminv2 "google.golang.org/api/run/v2"
runtimeconfig "google.golang.org/api/runtimeconfig/v1beta1"
"google.golang.org/api/servicemanagement/v1"
"google.golang.org/api/servicenetworking/v1"
Expand Down Expand Up @@ -1200,6 +1201,20 @@ func (c *Config) NewBigTableProjectsInstancesTablesClient(userAgent string) *big
return clientBigtableProjectsInstancesTables
}

func (c *Config) NewCloudRunV2Client(userAgent string) *runadminv2.Service {
runAdminV2ClientBasePath := removeBasePathVersion(removeBasePathVersion(c.CloudRunV2BasePath))
log.Printf("[INFO] Instantiating Google Cloud Run Admin v2 client for path %s", runAdminV2ClientBasePath)
clientRunAdminV2, err := runadminv2.NewService(c.context, option.WithHTTPClient(c.client))
if err != nil {
log.Printf("[WARN] Error creating client run admin: %s", err)
return nil
}
clientRunAdminV2.UserAgent = userAgent
clientRunAdminV2.BasePath = runAdminV2ClientBasePath

return clientRunAdminV2
}

// staticTokenSource is used to be able to identify static token sources without reflection.
type staticTokenSource struct {
oauth2.TokenSource
Expand Down
209 changes: 209 additions & 0 deletions google-beta/resource_cloud_run_v2_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ func ResourceCloudRunV2Service() *schema.Resource {
},
"liveness_probe": {
Type: schema.TypeList,
Computed: true,
Optional: true,
Description: `Periodic probe of container liveness. Container will be restarted if the probe fails. More info: https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle#container-probes`,
MaxItems: 1,
Expand All @@ -150,6 +151,29 @@ func ResourceCloudRunV2Service() *schema.Resource {
Description: `Minimum consecutive failures for the probe to be considered failed after having succeeded. Defaults to 3. Minimum value is 1.`,
Default: 3,
},
"grpc": {
Type: schema.TypeList,
Optional: true,
Description: `GRPC specifies an action involving a GRPC port.`,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"port": {
Type: schema.TypeInt,
Computed: true,
Optional: true,
Description: `Port number to access on the container. Number must be in the range 1 to 65535. If not specified, defaults to the same value as container.ports[0].containerPort.`,
},
"service": {
Type: schema.TypeString,
Optional: true,
Description: `The 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.`,
},
},
},
},
"http_get": {
Type: schema.TypeList,
Optional: true,
Expand Down Expand Up @@ -288,6 +312,29 @@ If omitted, a port number will be chosen and passed to the container through the
Description: `Minimum consecutive failures for the probe to be considered failed after having succeeded. Defaults to 3. Minimum value is 1.`,
Default: 3,
},
"grpc": {
Type: schema.TypeList,
Optional: true,
Description: `GRPC specifies an action involving a GRPC port.`,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"port": {
Type: schema.TypeInt,
Computed: true,
Optional: true,
Description: `Port number to access on the container. Number must be in the range 1 to 65535. If not specified, defaults to the same value as container.ports[0].containerPort.`,
},
"service": {
Type: schema.TypeString,
Optional: true,
Description: `The 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.`,
},
},
},
},
"http_get": {
Type: schema.TypeList,
Optional: true,
Expand Down Expand Up @@ -1649,6 +1696,8 @@ func flattenCloudRunV2ServiceTemplateContainersLivenessProbe(v interface{}, d *s
flattenCloudRunV2ServiceTemplateContainersLivenessProbeHttpGet(original["httpGet"], d, config)
transformed["tcp_socket"] =
flattenCloudRunV2ServiceTemplateContainersLivenessProbeTcpSocket(original["tcpSocket"], d, config)
transformed["grpc"] =
flattenCloudRunV2ServiceTemplateContainersLivenessProbeGrpc(original["grpc"], d, config)
return []interface{}{transformed}
}
func flattenCloudRunV2ServiceTemplateContainersLivenessProbeInitialDelaySeconds(v interface{}, d *schema.ResourceData, config *Config) interface{} {
Expand Down Expand Up @@ -1789,6 +1838,39 @@ func flattenCloudRunV2ServiceTemplateContainersLivenessProbeTcpSocketPort(v inte
return v // let terraform core handle it otherwise
}

func flattenCloudRunV2ServiceTemplateContainersLivenessProbeGrpc(v interface{}, d *schema.ResourceData, config *Config) interface{} {
if v == nil {
return nil
}
original := v.(map[string]interface{})
transformed := make(map[string]interface{})
transformed["port"] =
flattenCloudRunV2ServiceTemplateContainersLivenessProbeGrpcPort(original["port"], d, config)
transformed["service"] =
flattenCloudRunV2ServiceTemplateContainersLivenessProbeGrpcService(original["service"], d, config)
return []interface{}{transformed}
}
func flattenCloudRunV2ServiceTemplateContainersLivenessProbeGrpcPort(v interface{}, d *schema.ResourceData, config *Config) interface{} {
// Handles the string fixed64 format
if strVal, ok := v.(string); ok {
if intVal, err := stringToFixed64(strVal); err == nil {
return intVal
}
}

// number values are represented as float64
if floatVal, ok := v.(float64); ok {
intVal := int(floatVal)
return intVal
}

return v // let terraform core handle it otherwise
}

func flattenCloudRunV2ServiceTemplateContainersLivenessProbeGrpcService(v interface{}, d *schema.ResourceData, config *Config) interface{} {
return v
}

func flattenCloudRunV2ServiceTemplateContainersStartupProbe(v interface{}, d *schema.ResourceData, config *Config) interface{} {
if v == nil {
return nil
Expand All @@ -1810,6 +1892,8 @@ func flattenCloudRunV2ServiceTemplateContainersStartupProbe(v interface{}, d *sc
flattenCloudRunV2ServiceTemplateContainersStartupProbeHttpGet(original["httpGet"], d, config)
transformed["tcp_socket"] =
flattenCloudRunV2ServiceTemplateContainersStartupProbeTcpSocket(original["tcpSocket"], d, config)
transformed["grpc"] =
flattenCloudRunV2ServiceTemplateContainersStartupProbeGrpc(original["grpc"], d, config)
return []interface{}{transformed}
}
func flattenCloudRunV2ServiceTemplateContainersStartupProbeInitialDelaySeconds(v interface{}, d *schema.ResourceData, config *Config) interface{} {
Expand Down Expand Up @@ -1950,6 +2034,39 @@ func flattenCloudRunV2ServiceTemplateContainersStartupProbeTcpSocketPort(v inter
return v // let terraform core handle it otherwise
}

func flattenCloudRunV2ServiceTemplateContainersStartupProbeGrpc(v interface{}, d *schema.ResourceData, config *Config) interface{} {
if v == nil {
return nil
}
original := v.(map[string]interface{})
transformed := make(map[string]interface{})
transformed["port"] =
flattenCloudRunV2ServiceTemplateContainersStartupProbeGrpcPort(original["port"], d, config)
transformed["service"] =
flattenCloudRunV2ServiceTemplateContainersStartupProbeGrpcService(original["service"], d, config)
return []interface{}{transformed}
}
func flattenCloudRunV2ServiceTemplateContainersStartupProbeGrpcPort(v interface{}, d *schema.ResourceData, config *Config) interface{} {
// Handles the string fixed64 format
if strVal, ok := v.(string); ok {
if intVal, err := stringToFixed64(strVal); err == nil {
return intVal
}
}

// number values are represented as float64
if floatVal, ok := v.(float64); ok {
intVal := int(floatVal)
return intVal
}

return v // let terraform core handle it otherwise
}

func flattenCloudRunV2ServiceTemplateContainersStartupProbeGrpcService(v interface{}, d *schema.ResourceData, config *Config) interface{} {
return v
}

func flattenCloudRunV2ServiceTemplateVolumes(v interface{}, d *schema.ResourceData, config *Config) interface{} {
if v == nil {
return v
Expand Down Expand Up @@ -2993,6 +3110,13 @@ func expandCloudRunV2ServiceTemplateContainersLivenessProbe(v interface{}, d Ter
transformed["tcpSocket"] = transformedTcpSocket
}

transformedGrpc, err := expandCloudRunV2ServiceTemplateContainersLivenessProbeGrpc(original["grpc"], d, config)
if err != nil {
return nil, err
} else {
transformed["grpc"] = transformedGrpc
}

return transformed, nil
}

Expand Down Expand Up @@ -3112,6 +3236,45 @@ func expandCloudRunV2ServiceTemplateContainersLivenessProbeTcpSocketPort(v inter
return v, nil
}

func expandCloudRunV2ServiceTemplateContainersLivenessProbeGrpc(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
l := v.([]interface{})
if len(l) == 0 {
return nil, nil
}

if l[0] == nil {
transformed := make(map[string]interface{})
return transformed, nil
}
raw := l[0]
original := raw.(map[string]interface{})
transformed := make(map[string]interface{})

transformedPort, err := expandCloudRunV2ServiceTemplateContainersLivenessProbeGrpcPort(original["port"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedPort); val.IsValid() && !isEmptyValue(val) {
transformed["port"] = transformedPort
}

transformedService, err := expandCloudRunV2ServiceTemplateContainersLivenessProbeGrpcService(original["service"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedService); val.IsValid() && !isEmptyValue(val) {
transformed["service"] = transformedService
}

return transformed, nil
}

func expandCloudRunV2ServiceTemplateContainersLivenessProbeGrpcPort(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}

func expandCloudRunV2ServiceTemplateContainersLivenessProbeGrpcService(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}

func expandCloudRunV2ServiceTemplateContainersStartupProbe(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
l := v.([]interface{})
if len(l) == 0 || l[0] == nil {
Expand Down Expand Up @@ -3163,6 +3326,13 @@ func expandCloudRunV2ServiceTemplateContainersStartupProbe(v interface{}, d Terr
transformed["tcpSocket"] = transformedTcpSocket
}

transformedGrpc, err := expandCloudRunV2ServiceTemplateContainersStartupProbeGrpc(original["grpc"], d, config)
if err != nil {
return nil, err
} else {
transformed["grpc"] = transformedGrpc
}

return transformed, nil
}

Expand Down Expand Up @@ -3282,6 +3452,45 @@ func expandCloudRunV2ServiceTemplateContainersStartupProbeTcpSocketPort(v interf
return v, nil
}

func expandCloudRunV2ServiceTemplateContainersStartupProbeGrpc(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
l := v.([]interface{})
if len(l) == 0 {
return nil, nil
}

if l[0] == nil {
transformed := make(map[string]interface{})
return transformed, nil
}
raw := l[0]
original := raw.(map[string]interface{})
transformed := make(map[string]interface{})

transformedPort, err := expandCloudRunV2ServiceTemplateContainersStartupProbeGrpcPort(original["port"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedPort); val.IsValid() && !isEmptyValue(val) {
transformed["port"] = transformedPort
}

transformedService, err := expandCloudRunV2ServiceTemplateContainersStartupProbeGrpcService(original["service"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedService); val.IsValid() && !isEmptyValue(val) {
transformed["service"] = transformedService
}

return transformed, nil
}

func expandCloudRunV2ServiceTemplateContainersStartupProbeGrpcPort(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}

func expandCloudRunV2ServiceTemplateContainersStartupProbeGrpcService(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}

func expandCloudRunV2ServiceTemplateVolumes(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
l := v.([]interface{})
req := make([]interface{}, 0, len(l))
Expand Down
Loading

0 comments on commit ff75441

Please sign in to comment.