Skip to content

Commit ff71123

Browse files
authored
fix: switch ASM API and IAM flags to use native resources (terraform-google-modules#914)
* fix: switch ASM api, iam flags to native resources * fmt
1 parent c32c5d1 commit ff71123

File tree

10 files changed

+81
-48
lines changed

10 files changed

+81
-48
lines changed

examples/simple_zonal_with_asm/README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ This example illustrates how to create a simple zonal cluster with ASM.
1313
| network | The VPC network to host the cluster in | `any` | n/a | yes |
1414
| project\_id | The project ID to host the cluster in | `any` | n/a | yes |
1515
| region | The region to host the cluster in | `any` | n/a | yes |
16-
| service\_account | The GCP Service Account email address used to deploy ASM. | `string` | `""` | no |
1716
| subnetwork | The subnetwork to host the cluster in | `any` | n/a | yes |
1817
| zones | The zone to host the cluster in (required if is a zonal cluster) | `list(string)` | n/a | yes |
1918

examples/simple_zonal_with_asm/main.tf

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ provider "google-beta" {
2424
}
2525

2626
provider "google" {
27-
version = "~> 3.42.0"
27+
version = "~> 3.63.0"
2828
region = var.region
2929
}
3030

@@ -75,7 +75,6 @@ module "asm" {
7575
enable_cluster_roles = true
7676
enable_cluster_labels = true
7777
enable_gcp_apis = true
78-
enable_gcp_iam_roles = true
7978
enable_gcp_components = true
8079
options = ["envoy-access-log"]
8180
outdir = "./${module.gke.name}-outdir"

examples/simple_zonal_with_asm/variables.tf

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,3 @@ variable "ip_range_pods" {
4747
variable "ip_range_services" {
4848
description = "The secondary ip range to use for services"
4949
}
50-
51-
variable "service_account" {
52-
description = "The GCP Service Account email address used to deploy ASM."
53-
type = string
54-
default = ""
55-
}

modules/asm/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,10 @@ To deploy this config:
6161
| enable\_cluster\_roles | Sets `--enable_cluster_roles` option if true. | `bool` | `false` | no |
6262
| enable\_gcp\_apis | Sets `--enable_gcp_apis` option if true. | `bool` | `false` | no |
6363
| enable\_gcp\_components | Sets --enable\_gcp\_components option if true. Can be true or false. Available versions are documented in https://github.com/GoogleCloudPlatform/anthos-service-mesh-packages | `bool` | `false` | no |
64-
| enable\_gcp\_iam\_roles | Sets `--enable_gcp_iam_roles` option if true. | `bool` | `false` | no |
64+
| enable\_gcp\_iam\_roles | Grants IAM roles required for ASM if true. If enable\_gcp\_iam\_roles, one of impersonate\_service\_account, service\_account, or iam\_member must be set. | `bool` | `false` | no |
6565
| enable\_registration | Sets `--enable_registration` option if true. | `bool` | `false` | no |
6666
| gcloud\_sdk\_version | The gcloud sdk version to use. Minimum required version is 293.0.0 | `string` | `"296.0.1"` | no |
67+
| iam\_member | The GCP member email address to grant IAM roles to. If impersonate\_service\_account or service\_account is set, roles are granted to that SA. | `string` | `""` | no |
6768
| impersonate\_service\_account | An optional service account to impersonate for gcloud commands. If this service account is not specified, the module will use Application Default Credentials. | `string` | `""` | no |
6869
| key\_file | The GCP Service Account credentials file path used to deploy ASM. | `string` | `""` | no |
6970
| location | The location (zone or region) this cluster has been created in. | `string` | n/a | yes |

modules/asm/main.tf

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,63 @@ locals {
2828
ca_key = lookup(var.ca_certs, "ca_key", "none")
2929
root_cert = lookup(var.ca_certs, "root_cert", "none")
3030
cert_chain = lookup(var.ca_certs, "cert_chain", "none")
31+
# https://github.com/GoogleCloudPlatform/anthos-service-mesh-packages/blob/1cf61b679cd369f42a0e735f8e201de1a6a6433b/scripts/asm-installer/install_asm#L1970
32+
iam_roles = [
33+
"roles/container.admin",
34+
"roles/meshconfig.admin",
35+
"roles/gkehub.admin",
36+
]
37+
# https://github.com/GoogleCloudPlatform/anthos-service-mesh-packages/blob/1cf61b679cd369f42a0e735f8e201de1a6a6433b/scripts/asm-installer/install_asm#L1958
38+
mcp_iam_roles = [
39+
"roles/serviceusage.serviceUsageConsumer",
40+
"roles/container.admin",
41+
"roles/monitoring.metricWriter",
42+
"roles/logging.logWriter",
43+
"roles/gkehub.viewer",
44+
"roles/gkehub.gatewayAdmin",
45+
]
46+
# if enable_gcp_iam_roles is set, grant IAM roles to first non null principal in the order below
47+
asm_iam_member = var.enable_gcp_iam_roles ? coalesce(var.impersonate_service_account, var.service_account, var.iam_member) : ""
48+
# compute any additonal resources that ASM provisioner should depend on
49+
additional_depends_on = concat(var.enable_gcp_apis ? [module.asm-services[0].project_id] : [], local.asm_iam_member != "" ? [for k, v in google_project_iam_member.asm_iam : v.etag] : [])
50+
}
51+
52+
resource "google_project_iam_member" "asm_iam" {
53+
for_each = toset(local.asm_iam_member != "" ? (var.managed_control_plane ? local.mcp_iam_roles : local.iam_roles) : [])
54+
project = var.project_id
55+
role = each.value
56+
member = "serviceAccount:${local.asm_iam_member}"
57+
}
58+
59+
module "asm-services" {
60+
source = "terraform-google-modules/project-factory/google//modules/project_services"
61+
version = "~> 10.0"
62+
count = var.enable_gcp_apis ? 1 : 0
63+
64+
project_id = var.project_id
65+
disable_services_on_destroy = false
66+
disable_dependent_services = false
67+
68+
# https://github.com/GoogleCloudPlatform/anthos-service-mesh-packages/blob/1cf61b679cd369f42a0e735f8e201de1a6a6433b/scripts/asm-installer/install_asm#L2005
69+
activate_apis = [
70+
"container.googleapis.com",
71+
"monitoring.googleapis.com",
72+
"logging.googleapis.com",
73+
"cloudtrace.googleapis.com",
74+
"meshtelemetry.googleapis.com",
75+
"meshconfig.googleapis.com",
76+
"iamcredentials.googleapis.com",
77+
"gkeconnect.googleapis.com",
78+
"gkehub.googleapis.com",
79+
"cloudresourcemanager.googleapis.com",
80+
"stackdriver.googleapis.com",
81+
]
3182
}
3283

3384
module "asm_install" {
3485
source = "terraform-google-modules/gcloud/google//modules/kubectl-wrapper"
3586
version = "~> 2.1.0"
36-
module_depends_on = [var.cluster_endpoint]
87+
module_depends_on = concat([var.cluster_endpoint], local.additional_depends_on)
3788

3889
gcloud_sdk_version = var.gcloud_sdk_version
3990
upgrade = true
@@ -44,6 +95,6 @@ module "asm_install" {
4495
service_account_key_file = var.service_account_key_file
4596
impersonate_service_account = var.impersonate_service_account
4697

47-
kubectl_create_command = "${path.module}/scripts/install_asm.sh ${var.project_id} ${var.cluster_name} ${var.location} ${var.asm_version} ${var.mode} ${var.managed_control_plane} ${var.skip_validation} ${local.options_string} ${local.custom_overlays_string} ${var.enable_all} ${var.enable_cluster_roles} ${var.enable_cluster_labels} ${var.enable_gcp_apis} ${var.enable_gcp_iam_roles} ${var.enable_gcp_components} ${var.enable_registration} ${var.outdir} ${var.ca} ${local.ca_cert} ${local.ca_key} ${local.root_cert} ${local.cert_chain} ${local.service_account_string} ${local.key_file_string} ${local.asm_git_tag_string}"
98+
kubectl_create_command = "${path.module}/scripts/install_asm.sh ${var.project_id} ${var.cluster_name} ${var.location} ${var.asm_version} ${var.mode} ${var.managed_control_plane} ${var.skip_validation} ${local.options_string} ${local.custom_overlays_string} ${var.enable_all} ${var.enable_cluster_roles} ${var.enable_cluster_labels} ${var.enable_gcp_components} ${var.enable_registration} ${var.outdir} ${var.ca} ${local.ca_cert} ${local.ca_key} ${local.root_cert} ${local.cert_chain} ${local.service_account_string} ${local.key_file_string} ${local.asm_git_tag_string}"
4899
kubectl_destroy_command = "kubectl delete ns istio-system"
49100
}

modules/asm/scripts/install_asm.sh

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,17 @@ CUSTOM_OVERLAYS_LIST=$9
3333
ENABLE_ALL=${10}
3434
ENABLE_CLUSTER_ROLES=${11}
3535
ENABLE_CLUSTER_LABELS=${12}
36-
ENABLE_GCP_APIS=${13}
37-
ENABLE_GCP_IAM_ROLES=${14}
38-
ENABLE_GCP_COMPONENTS=${15}
39-
ENABLE_REGISTRATION=${16}
40-
OUTDIR=${17}
41-
CA=${18}
42-
CA_CERT=${19}
43-
CA_KEY=${20}
44-
ROOT_CERT=${21}
45-
CERT_CHAIN=${22}
46-
SERVICE_ACCOUNT=${23}
47-
KEY_FILE=${24}
48-
ASM_GIT_TAG=${25}
36+
ENABLE_GCP_COMPONENTS=${13}
37+
ENABLE_REGISTRATION=${14}
38+
OUTDIR=${15}
39+
CA=${16}
40+
CA_CERT=${17}
41+
CA_KEY=${18}
42+
ROOT_CERT=${19}
43+
CERT_CHAIN=${20}
44+
SERVICE_ACCOUNT=${21}
45+
KEY_FILE=${22}
46+
ASM_GIT_TAG=${23}
4947

5048
# Set SKIP_VALIDATION variable
5149
if [[ ${SKIP_VALIDATION} = "true" ]]; then
@@ -86,8 +84,6 @@ echo -e "CUSTOM_OVERLAYS_COMMAND is $CUSTOM_OVERLAYS_COMMAND"
8684
echo -e "ENABLE_ALL is $ENABLE_ALL"
8785
echo -e "ENABLE_CLUSTER_ROLES is $ENABLE_CLUSTER_ROLES"
8886
echo -e "ENABLE_CLUSTER_LABELS is $ENABLE_CLUSTER_LABELS"
89-
echo -e "ENABLE_GCP_APIS is $ENABLE_GCP_APIS"
90-
echo -e "ENABLE_GCP_IAM_ROLES is $ENABLE_GCP_IAM_ROLES"
9187
echo -e "ENABLE_GCP_COMPONENTS is $ENABLE_GCP_COMPONENTS"
9288
echo -e "ENABLE_REGISTRATION is $ENABLE_REGISTRATION"
9389
echo -e "OUTDIR is $OUTDIR"
@@ -158,18 +154,6 @@ else
158154
ENABLE_CLUSTER_LABELS_COMMAND_SNIPPET="--enable_cluster_labels"
159155
fi
160156

161-
if [[ "${ENABLE_GCP_APIS}" = false ]]; then
162-
ENABLE_GCP_APIS_COMMAND_SNIPPET=""
163-
else
164-
ENABLE_GCP_APIS_COMMAND_SNIPPET="--enable_gcp_apis"
165-
fi
166-
167-
if [[ "${ENABLE_GCP_IAM_ROLES}" = false ]]; then
168-
ENABLE_GCP_IAM_ROLES_COMMAND_SNIPPET=""
169-
else
170-
ENABLE_GCP_IAM_ROLES_COMMAND_SNIPPET="--enable_gcp_iam_roles"
171-
fi
172-
173157
if [[ "${ENABLE_GCP_COMPONENTS}" = false ]]; then
174158
ENABLE_GCP_COMPONENTS_COMMAND_SNIPPET=""
175159
else
@@ -196,8 +180,8 @@ else
196180
fi
197181

198182
# Echo the command before executing
199-
echo -e "install_asm_${ASM_VERSION} --verbose --project_id ${PROJECT_ID} --cluster_name ${CLUSTER_NAME} --cluster_location ${CLUSTER_LOCATION} --mode ${MODE} ${MCP_COMMAND_SNIPPET} ${OPTIONS_COMMAND_SNIPPET} ${CUSTOM_OVERLAYS_COMMAND_SNIPPET} ${OUTDIR_COMMAND_SNIPPET} ${ENABLE_ALL_COMMAND_SNIPPET} ${ENABLE_CLUSTER_ROLES_COMMAND_SNIPPET} ${ENABLE_CLUSTER_LABELS_COMMAND_SNIPPET} ${ENABLE_GCP_APIS_COMMAND_SNIPPET} ${ENABLE_GCP_IAM_ROLES_COMMAND_SNIPPET} ${ENABLE_GCP_COMPONENTS_COMMAND_SNIPPET} ${ENABLE_REGISTRATION_COMMAND_SNIPPET} ${CA_COMMAND_SNIPPET} ${SERVICE_ACCOUNT_COMMAND_SNIPPET} ${KEY_FILE_COMMAND_SNIPPET}"
183+
echo -e "install_asm_${ASM_VERSION} --verbose --project_id ${PROJECT_ID} --cluster_name ${CLUSTER_NAME} --cluster_location ${CLUSTER_LOCATION} --mode ${MODE} ${MCP_COMMAND_SNIPPET} ${OPTIONS_COMMAND_SNIPPET} ${CUSTOM_OVERLAYS_COMMAND_SNIPPET} ${OUTDIR_COMMAND_SNIPPET} ${ENABLE_ALL_COMMAND_SNIPPET} ${ENABLE_CLUSTER_ROLES_COMMAND_SNIPPET} ${ENABLE_CLUSTER_LABELS_COMMAND_SNIPPET} ${ENABLE_GCP_COMPONENTS_COMMAND_SNIPPET} ${ENABLE_REGISTRATION_COMMAND_SNIPPET} ${CA_COMMAND_SNIPPET} ${SERVICE_ACCOUNT_COMMAND_SNIPPET} ${KEY_FILE_COMMAND_SNIPPET}"
200184

201185
# run the script with appropriate flags
202186
# shellcheck disable=SC2086
203-
./install_asm_${ASM_VERSION} --verbose --project_id ${PROJECT_ID} --cluster_name ${CLUSTER_NAME} --cluster_location ${CLUSTER_LOCATION} --mode ${MODE} ${MCP_COMMAND_SNIPPET} ${OPTIONS_COMMAND_SNIPPET} ${CUSTOM_OVERLAYS_COMMAND_SNIPPET} ${OUTDIR_COMMAND_SNIPPET} ${ENABLE_ALL_COMMAND_SNIPPET} ${ENABLE_CLUSTER_ROLES_COMMAND_SNIPPET} ${ENABLE_CLUSTER_LABELS_COMMAND_SNIPPET} ${ENABLE_GCP_APIS_COMMAND_SNIPPET} ${ENABLE_GCP_IAM_ROLES_COMMAND_SNIPPET} ${ENABLE_GCP_COMPONENTS_COMMAND_SNIPPET} ${ENABLE_REGISTRATION_COMMAND_SNIPPET} ${CA_COMMAND_SNIPPET} ${SERVICE_ACCOUNT_COMMAND_SNIPPET} ${KEY_FILE_COMMAND_SNIPPET}
187+
./install_asm_${ASM_VERSION} --verbose --project_id ${PROJECT_ID} --cluster_name ${CLUSTER_NAME} --cluster_location ${CLUSTER_LOCATION} --mode ${MODE} ${MCP_COMMAND_SNIPPET} ${OPTIONS_COMMAND_SNIPPET} ${CUSTOM_OVERLAYS_COMMAND_SNIPPET} ${OUTDIR_COMMAND_SNIPPET} ${ENABLE_ALL_COMMAND_SNIPPET} ${ENABLE_CLUSTER_ROLES_COMMAND_SNIPPET} ${ENABLE_CLUSTER_LABELS_COMMAND_SNIPPET} ${ENABLE_GCP_COMPONENTS_COMMAND_SNIPPET} ${ENABLE_REGISTRATION_COMMAND_SNIPPET} ${CA_COMMAND_SNIPPET} ${SERVICE_ACCOUNT_COMMAND_SNIPPET} ${KEY_FILE_COMMAND_SNIPPET}

modules/asm/variables.tf

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ variable "enable_gcp_apis" {
136136
}
137137

138138
variable "enable_gcp_iam_roles" {
139-
description = "Sets `--enable_gcp_iam_roles` option if true."
139+
description = "Grants IAM roles required for ASM if true. If enable_gcp_iam_roles, one of impersonate_service_account, service_account, or iam_member must be set."
140140
type = bool
141141
default = false
142142
}
@@ -176,3 +176,9 @@ variable "ca_certs" {
176176
# "cert_chain" = "none"
177177
# }
178178
}
179+
180+
variable "iam_member" {
181+
description = "The GCP member email address to grant IAM roles to. If impersonate_service_account or service_account is set, roles are granted to that SA."
182+
type = string
183+
default = ""
184+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# asm outdir
2+
simple-zonal-*
3+
# install asm script
4+
install_asm*

test/fixtures/simple_zonal_with_asm/network.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ resource "random_string" "suffix" {
2121
}
2222

2323
provider "google" {
24-
version = "~> 3.42.0"
24+
version = "~> 3.63.0"
2525
project = var.project_ids[2]
2626
}
2727

test/fixtures/simple_zonal_with_asm/variables.tf

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,3 @@ variable "zones" {
3030
default = ["us-central1-a", "us-central1-b", "us-central1-c"]
3131
}
3232

33-
variable "service_account" {
34-
description = "The GCP Service Account email address used to deploy ASM."
35-
type = string
36-
default = ""
37-
}

0 commit comments

Comments
 (0)