Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for cloudrun ports #6873

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/3748.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
cloudrun: added `ports` field to `google_cloud_run_service` `templates.spec.containers`
```
128 changes: 128 additions & 0 deletions google/resource_cloud_run_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,33 @@ https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names`,
},
},
},
"ports": {
Type: schema.TypeList,
Computed: true,
Optional: true,
Description: `List of open ports in the container.
More Info:
https://cloud.google.com/run/docs/reference/rest/v1/RevisionSpec#ContainerPort`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"container_port": {
Type: schema.TypeInt,
Required: true,
Description: `Port number.`,
},
"name": {
Type: schema.TypeString,
Optional: true,
Description: `Name of the port.`,
},
"protocol": {
Type: schema.TypeString,
Optional: true,
Description: `Protocol used on port. Defaults to TCP.`,
},
},
},
},
"resources": {
Type: schema.TypeList,
Computed: true,
Expand Down Expand Up @@ -1028,6 +1055,7 @@ func flattenCloudRunServiceSpecTemplateSpecContainers(v interface{}, d *schema.R
"image": flattenCloudRunServiceSpecTemplateSpecContainersImage(original["image"], d, config),
"command": flattenCloudRunServiceSpecTemplateSpecContainersCommand(original["command"], d, config),
"env": flattenCloudRunServiceSpecTemplateSpecContainersEnv(original["env"], d, config),
"ports": flattenCloudRunServiceSpecTemplateSpecContainersPorts(original["ports"], d, config),
"resources": flattenCloudRunServiceSpecTemplateSpecContainersResources(original["resources"], d, config),
})
}
Expand Down Expand Up @@ -1172,6 +1200,51 @@ func flattenCloudRunServiceSpecTemplateSpecContainersEnvValue(v interface{}, d *
return v
}

func flattenCloudRunServiceSpecTemplateSpecContainersPorts(v interface{}, d *schema.ResourceData, config *Config) interface{} {
if v == nil {
return v
}
l := v.([]interface{})
transformed := make([]interface{}, 0, len(l))
for _, raw := range l {
original := raw.(map[string]interface{})
if len(original) < 1 {
// Do not include empty json objects coming back from the api
continue
}
transformed = append(transformed, map[string]interface{}{
"name": flattenCloudRunServiceSpecTemplateSpecContainersPortsName(original["name"], d, config),
"protocol": flattenCloudRunServiceSpecTemplateSpecContainersPortsProtocol(original["protocol"], d, config),
"container_port": flattenCloudRunServiceSpecTemplateSpecContainersPortsContainerPort(original["containerPort"], d, config),
})
}
return transformed
}
func flattenCloudRunServiceSpecTemplateSpecContainersPortsName(v interface{}, d *schema.ResourceData, config *Config) interface{} {
return v
}

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

func flattenCloudRunServiceSpecTemplateSpecContainersPortsContainerPort(v interface{}, d *schema.ResourceData, config *Config) interface{} {
// Handles the string fixed64 format
if strVal, ok := v.(string); ok {
if intVal, err := strconv.ParseInt(strVal, 10, 64); 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 flattenCloudRunServiceSpecTemplateSpecContainersResources(v interface{}, d *schema.ResourceData, config *Config) interface{} {
if v == nil {
return nil
Expand Down Expand Up @@ -1706,6 +1779,13 @@ func expandCloudRunServiceSpecTemplateSpecContainers(v interface{}, d TerraformR
transformed["env"] = transformedEnv
}

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

transformedResources, err := expandCloudRunServiceSpecTemplateSpecContainersResources(original["resources"], d, config)
if err != nil {
return nil, err
Expand Down Expand Up @@ -1917,6 +1997,54 @@ func expandCloudRunServiceSpecTemplateSpecContainersEnvValue(v interface{}, d Te
return v, nil
}

func expandCloudRunServiceSpecTemplateSpecContainersPorts(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
l := v.([]interface{})
req := make([]interface{}, 0, len(l))
for _, raw := range l {
if raw == nil {
continue
}
original := raw.(map[string]interface{})
transformed := make(map[string]interface{})

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

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

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

req = append(req, transformed)
}
return req, nil
}

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

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

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

func expandCloudRunServiceSpecTemplateSpecContainersResources(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
l := v.([]interface{})
if len(l) == 0 || l[0] == nil {
Expand Down
3 changes: 3 additions & 0 deletions google/resource_cloud_run_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ resource "google_cloud_run_service" "default" {
containers {
image = "gcr.io/cloudrun/hello"
args = ["arrgs"]
ports {
container_port = 8080
}
}
container_concurrency = %s
timeout_seconds = %s
Expand Down
20 changes: 20 additions & 0 deletions website/docs/r/cloud_run_service.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,12 @@ The `containers` block supports:
(Optional)
List of environment variables to set in the container. Structure is documented below.

* `ports` -
(Optional)
List of open ports in the container.
More Info:
https://cloud.google.com/run/docs/reference/rest/v1/RevisionSpec#ContainerPort Structure is documented below.

* `resources` -
(Optional)
Compute Resources required by this container. Used to set values such as max memory
Expand Down Expand Up @@ -480,6 +486,20 @@ The `env` block supports:
exists or not.
Defaults to "".

The `ports` block supports:

* `name` -
(Optional)
Name of the port.

* `protocol` -
(Optional)
Protocol used on port. Defaults to TCP.

* `container_port` -
(Required)
Port number.

The `resources` block supports:

* `limits` -
Expand Down