Skip to content

Commit cb8794f

Browse files
authored
Support optionally grant GKE SA network admin on VPC host project (#483)
* Support optionally grant GKE SA network admin on VPC host project
1 parent 6553449 commit cb8794f

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

modules/shared_vpc_access/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ module "shared_vpc_access" {
2828
| Name | Description | Type | Default | Required |
2929
|------|-------------|------|---------|:--------:|
3030
| active\_apis | The list of active apis on the service project. If api is not active this module will not try to activate it | `list(string)` | `[]` | no |
31+
| grant\_services\_security\_admin\_role | Whether or not to grant Kubernetes Engine Service Agent the Security Admin role on the host project so it can manage firewall rules | `bool` | `false` | no |
3132
| host\_project\_id | The ID of the host project which hosts the shared VPC | `string` | n/a | yes |
3233
| service\_project\_id | The ID of the service project | `string` | n/a | yes |
3334
| shared\_vpc\_subnets | List of subnets fully qualified subnet IDs (ie. projects/$project\_id/regions/$region/subnetworks/$subnet\_id) | `list(string)` | `[]` | no |

modules/shared_vpc_access/main.tf

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,24 @@ resource "google_project_iam_member" "service_shared_vpc_user" {
6868
}
6969

7070
/******************************************
71-
container.hostServiceAgentUser role granted to GKE service account for GKE on shared VPC
72-
See:https://cloud.google.com/kubernetes-engine/docs/how-to/cluster-shared-vpc
71+
container.hostServiceAgentUser role granted to GKE service account for GKE on shared VPC host project
72+
See: https://cloud.google.com/kubernetes-engine/docs/how-to/cluster-shared-vpc
7373
*****************************************/
7474
resource "google_project_iam_member" "gke_host_agent" {
7575
count = local.gke_shared_vpc_enabled ? 1 : 0
7676
project = var.host_project_id
7777
role = "roles/container.hostServiceAgentUser"
7878
member = format("serviceAccount:%s", local.apis["container.googleapis.com"])
7979
}
80+
81+
/******************************************
82+
roles/compute.securityAdmin role granted to GKE service account for GKE on shared VPC host project
83+
See: https://cloud.google.com/kubernetes-engine/docs/how-to/cluster-shared-vpc#enabling_and_granting_roles
84+
and https://cloud.google.com/kubernetes-engine/docs/how-to/cluster-shared-vpc#creating_additional_firewall_rules
85+
*****************************************/
86+
resource "google_project_iam_member" "gke_security_admin" {
87+
count = local.gke_shared_vpc_enabled && var.grant_services_security_admin_role ? 1 : 0
88+
project = var.host_project_id
89+
role = "roles/compute.securityAdmin"
90+
member = format("serviceAccount:%s", local.apis["container.googleapis.com"])
91+
}

modules/shared_vpc_access/variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,9 @@ variable "active_apis" {
3535
type = list(string)
3636
default = []
3737
}
38+
39+
variable "grant_services_security_admin_role" {
40+
description = "Whether or not to grant Kubernetes Engine Service Agent the Security Admin role on the host project so it can manage firewall rules"
41+
type = bool
42+
default = false
43+
}

0 commit comments

Comments
 (0)