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

Fix connector create logic in cloud run module #729

Merged
merged 2 commits into from
Jul 10, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
77 changes: 56 additions & 21 deletions modules/cloud-run/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,21 @@
*/

locals {
annotations = merge(
var.ingress_settings == null ? {} : {
"run.googleapis.com/ingress" = var.ingress_settings
},
var.vpc_connector == null ? {} : {
"run.googleapis.com/vpc-access-connector" = (
try(var.vpc_connector.create, false)
? google_vpc_access_connector.connector.0.id
: var.vpc_connector.name
)
},
try(var.vpc_connector.egress_settings, null) == null ? {} : {
"run.googleapis.com/vpc-access-egress" = var.vpc_connector.egress_settings
}
)
prefix = var.prefix == null ? "" : "${var.prefix}-"
service_account_email = (
var.service_account_create
Expand All @@ -25,21 +40,10 @@ locals {
)
: var.service_account
)

annotations = merge(var.ingress_settings == null ? {} : { "run.googleapis.com/ingress" = var.ingress_settings },
var.vpc_connector == null
? {}
: try(var.vpc_connector.create, false)
? { "run.googleapis.com/vpc-access-connector" = var.vpc_connector.name }
: { "run.googleapis.com/vpc-access-connector" = google_vpc_access_connector.connector.0.id }
,
try(var.vpc_connector.egress_settings, null) == null
? {}
: { "run.googleapis.com/vpc-access-egress" = var.vpc_connector.egress_settings })
}

resource "google_vpc_access_connector" "connector" {
count = try(var.vpc_connector.create, false) == false ? 0 : 1
count = try(var.vpc_connector.create, false) ? 1 : 0
project = var.project_id
name = var.vpc_connector.name
region = var.region
Expand All @@ -56,20 +60,30 @@ resource "google_cloud_run_service" "service" {
template {
spec {
dynamic "containers" {
for_each = var.containers == null ? {} : { for i, container in var.containers : i => container }
for_each = var.containers == null ? {} : {
for i, container in var.containers : i => container
}
content {
image = containers.value["image"]
command = try(containers.value["options"]["command"], null)
args = try(containers.value["options"]["args"], null)
dynamic "env" {
for_each = try(containers.value["options"]["env"], null) == null ? {} : containers.value["options"]["env"]
for_each = (
try(containers.value["options"]["env"], null) == null
? {}
: containers.value["options"]["env"]
)
content {
name = env.key
value = env.value
}
}
dynamic "env" {
for_each = try(containers.value["options"]["env_from"], null) == null ? {} : containers.value["options"]["env_from"]
for_each = (
try(containers.value["options"]["env_from"], null) == null
? {}
: containers.value["options"]["env_from"]
)
content {
name = env.key
value_from {
Expand All @@ -81,7 +95,14 @@ resource "google_cloud_run_service" "service" {
}
}
dynamic "ports" {
for_each = containers.value["ports"] == null ? {} : { for port in containers.value["ports"] : "${port.name}-${port.container_port}" => port }
for_each = (
containers.value["ports"] == null
? {}
: {
for port in containers.value["ports"] :
"${port.name}-${port.container_port}" => port
}
)
content {
name = ports.value["name"]
protocol = ports.value["protocol"]
Expand All @@ -96,7 +117,11 @@ resource "google_cloud_run_service" "service" {
}
}
dynamic "volume_mounts" {
for_each = containers.value["volume_mounts"] == null ? {} : containers.value["volume_mounts"]
for_each = (
containers.value["volume_mounts"] == null
? {}
: containers.value["volume_mounts"]
)
content {
name = volume_mounts.key
mount_path = volume_mounts.value
Expand All @@ -112,7 +137,11 @@ resource "google_cloud_run_service" "service" {
secret {
secret_name = volumes.value["secret_name"]
dynamic "items" {
for_each = volumes.value["items"] == null ? [] : volumes.value["items"]
for_each = (
volumes.value["items"] == null
? []
: volumes.value["items"]
)
content {
key = items.value["key"]
path = items.value["path"]
Expand All @@ -130,7 +159,6 @@ resource "google_cloud_run_service" "service" {
}
}


metadata {
annotations = local.annotations
}
Expand Down Expand Up @@ -162,7 +190,10 @@ resource "google_service_account" "service_account" {
}

resource "google_eventarc_trigger" "audit_log_triggers" {
for_each = var.audit_log_triggers == null ? {} : { for trigger in var.audit_log_triggers : "${trigger.service_name}-${trigger.method_name}" => trigger }
for_each = var.audit_log_triggers == null ? {} : {
for trigger in var.audit_log_triggers :
"${trigger.service_name}-${trigger.method_name}" => trigger
}
name = "${local.prefix}${each.key}-audit-log-trigger"
location = google_cloud_run_service.service.location
project = google_cloud_run_service.service.project
Expand All @@ -188,7 +219,11 @@ resource "google_eventarc_trigger" "audit_log_triggers" {

resource "google_eventarc_trigger" "pubsub_triggers" {
for_each = var.pubsub_triggers == null ? [] : toset(var.pubsub_triggers)
name = each.value == "" ? "${local.prefix}default-pubsub-trigger" : "${local.prefix}${each.value}-pubsub-trigger"
name = (
each.value == ""
? "${local.prefix}default-pubsub-trigger"
: "${local.prefix}${each.value}-pubsub-trigger"
)
location = google_cloud_run_service.service.location
project = google_cloud_run_service.service.project
matching_criteria {
Expand Down
12 changes: 12 additions & 0 deletions tests/modules/cloud_run/fixture/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@
# See the License for the specific language governing permissions and
# limitations under the License.

variable "vpc_connector" {
type = any
default = null
}

variable "vpc_connector_config" {
type = any
default = null
}

module "cloud_run" {
source = "../../../../modules/cloud-run"
project_id = "my-project"
Expand All @@ -37,4 +47,6 @@ module "cloud_run" {
iam = {
"roles/run.invoker" = ["allUsers"]
}
vpc_connector = var.vpc_connector
vpc_connector_config = var.vpc_connector_config
}
13 changes: 0 additions & 13 deletions tests/modules/cloud_run/fixture/variables.tf

This file was deleted.

45 changes: 39 additions & 6 deletions tests/modules/cloud_run/test_plan.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,54 @@ def test_resource_count(resources):

def test_iam(resources):
"Test IAM binding resources."
bindings = [r['values'] for r in resources if r['type']
== 'google_cloud_run_service_iam_binding']
bindings = [
r['values']
for r in resources
if r['type'] == 'google_cloud_run_service_iam_binding'
]
assert len(bindings) == 1
assert bindings[0]['role'] == 'roles/run.invoker'


def test_audit_log_triggers(resources):
"Test audit logs Eventarc trigger resources."
audit_log_triggers = [r['values'] for r in resources if r['type']
== 'google_eventarc_trigger' and r['name'] == 'audit_log_triggers']
audit_log_triggers = [
r['values']
for r in resources
if r['type'] == 'google_eventarc_trigger' and
r['name'] == 'audit_log_triggers'
]
assert len(audit_log_triggers) == 1


def test_pubsub_triggers(resources):
"Test Pub/Sub Eventarc trigger resources."
pubsub_triggers = [r['values'] for r in resources if r['type']
== 'google_eventarc_trigger' and r['name'] == 'pubsub_triggers']
pubsub_triggers = [
r['values'] for r in resources if
r['type'] == 'google_eventarc_trigger' and r['name'] == 'pubsub_triggers'
]
assert len(pubsub_triggers) == 2


def test_vpc_connector_none(plan_runner):
"Test VPC connector creation."
_, resources = plan_runner()
assert len(
[r for r in resources if r['type'] == 'google_vpc_access_connector']) == 0


def test_vpc_connector_nocreate(plan_runner):
"Test VPC connector creation."
_, resources = plan_runner(
vpc_connector='{create=false, name="foo", egress_settings=null}')
assert len(
[r for r in resources if r['type'] == 'google_vpc_access_connector']) == 0


def test_vpc_connector_create(plan_runner):
"Test VPC connector creation."
_, resources = plan_runner(
vpc_connector='{create=true, name="foo", egress_settings=null}',
vpc_connector_config='{ip_cidr_range="10.0.0.0/28", network="default"}')
assert len(
[r for r in resources if r['type'] == 'google_vpc_access_connector']) == 1