Skip to content

Commit 8205618

Browse files
authored
Merge pull request #80 from terraform-google-modules/feature/allow-creation-of-service-accounts
Allow creation of service accounts
2 parents 6189fef + 567e29a commit 8205618

File tree

20 files changed

+191
-34
lines changed

20 files changed

+191
-34
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ Then perform the following commands on the root folder:
125125
| region | The region to host the cluster in (required) | string | - | yes |
126126
| regional | Whether is a regional cluster (zonal cluster if set false. WARNING: changing this after cluster creation is destructive!) | string | `true` | no |
127127
| remove_default_node_pool | Remove default node pool while setting up the cluster | string | `false` | no |
128-
| service_account | The service account to default running nodes as if not overridden in `node_pools`. Defaults to the compute engine default service account | string | `` | no |
128+
| service_account | The service account to default running nodes as if not overridden in `node_pools`. Defaults to the compute engine default service account. May also specify `create` to automatically create a cluster-specific service account | string | `` | no |
129129
| stub_domains | Map of stub domains and their resolvers to forward DNS queries for a certain domain to an external DNS server | map | `<map>` | no |
130130
| subnetwork | The subnetwork to host the cluster in (required) | string | - | yes |
131131
| zones | The zones to host the cluster in (optional if regional cluster / required if zonal) | list | `<list>` | no |
@@ -179,6 +179,7 @@ following project roles:
179179
- roles/compute.viewer
180180
- roles/container.clusterAdmin
181181
- roles/container.developer
182+
- roles/iam.serviceAccountAdmin
182183
- roles/iam.serviceAccountUser
183184

184185
### Enable APIs

autogen/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Terraform Kubernetes Engine Module
22

3-
This module handles opinionated Google Cloud Platform Kubernetes Engine cluster creation and configuration with Node Pools, IP MASQ, Network Policy, etc. {% if private_cluster %}This particular submodule creates a [private cluster](https://cloud.google.com/kubernetes-engine/docs/how-to/private-clusters){% endif %}
3+
This module handles opinionated Google Cloud Platform Kubernetes Engine cluster creation and configuration with Node Pools, IP MASQ, Network Policy, etc.{% if private_cluster %} This particular submodule creates a [private cluster](https://cloud.google.com/kubernetes-engine/docs/how-to/private-clusters){% endif %}
44

55
The resources/services/activations/deletions that this module will create/trigger are:
66
- Create a GKE cluster with the provided addons
@@ -189,6 +189,7 @@ following project roles:
189189
- roles/compute.viewer
190190
- roles/container.clusterAdmin
191191
- roles/container.developer
192+
- roles/iam.serviceAccountAdmin
192193
- roles/iam.serviceAccountUser
193194

194195
### Enable APIs

autogen/cluster_regional.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ resource "google_container_cluster" "primary" {
8181
name = "default-pool"
8282

8383
node_config {
84-
service_account = "${lookup(var.node_pools[0], "service_account", var.service_account)}"
84+
service_account = "${lookup(var.node_pools[0], "service_account", local.service_account)}"
8585
}
8686
}
8787
{% if private_cluster %}
@@ -127,7 +127,7 @@ resource "google_container_node_pool" "pools" {
127127

128128
disk_size_gb = "${lookup(var.node_pools[count.index], "disk_size_gb", 100)}"
129129
disk_type = "${lookup(var.node_pools[count.index], "disk_type", "pd-standard")}"
130-
service_account = "${lookup(var.node_pools[count.index], "service_account", var.service_account)}"
130+
service_account = "${lookup(var.node_pools[count.index], "service_account", local.service_account)}"
131131
preemptible = "${lookup(var.node_pools[count.index], "preemptible", false)}"
132132

133133
oauth_scopes = [

autogen/cluster_zonal.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ resource "google_container_cluster" "zonal_primary" {
8181
name = "default-pool"
8282

8383
node_config {
84-
service_account = "${lookup(var.node_pools[0], "service_account", var.service_account)}"
84+
service_account = "${lookup(var.node_pools[0], "service_account", local.service_account)}"
8585
}
8686
}
8787
{% if private_cluster %}
@@ -127,7 +127,7 @@ resource "google_container_node_pool" "zonal_pools" {
127127

128128
disk_size_gb = "${lookup(var.node_pools[count.index], "disk_size_gb", 100)}"
129129
disk_type = "${lookup(var.node_pools[count.index], "disk_type", "pd-standard")}"
130-
service_account = "${lookup(var.node_pools[count.index], "service_account", var.service_account)}"
130+
service_account = "${lookup(var.node_pools[count.index], "service_account", local.service_account)}"
131131
preemptible = "${lookup(var.node_pools[count.index], "preemptible", false)}"
132132

133133
oauth_scopes = [

autogen/sa.tf

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* Copyright 2018 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
{{ autogeneration_note }}
18+
19+
locals {
20+
service_account_list = "${compact(concat(google_service_account.cluster_service_account.*.email, list("dummy")))}"
21+
service_account = "${var.service_account == "create" ? element(local.service_account_list, 0) : var.service_account}"
22+
}
23+
24+
resource "google_service_account" "cluster_service_account" {
25+
count = "${var.service_account == "create" ? 1 : 0}"
26+
project = "${var.project_id}"
27+
account_id = "tf-gke-${substr(var.name, 0, 20)}"
28+
display_name = "Terraform-managed service account for cluster ${var.name}"
29+
}
30+
31+
resource "google_project_iam_member" "cluster_service_account-log_writer" {
32+
count = "${var.service_account == "create" ? 1 : 0}"
33+
project = "${google_service_account.cluster_service_account.project}"
34+
role = "roles/logging.logWriter"
35+
member = "serviceAccount:${google_service_account.cluster_service_account.email}"
36+
}
37+
38+
resource "google_project_iam_member" "cluster_service_account-metric_writer" {
39+
count = "${var.service_account == "create" ? 1 : 0}"
40+
project = "${google_project_iam_member.cluster_service_account-log_writer.project}"
41+
role = "roles/monitoring.metricWriter"
42+
member = "serviceAccount:${google_service_account.cluster_service_account.email}"
43+
}
44+
45+
resource "google_project_iam_member" "cluster_service_account-monitoring_viewer" {
46+
count = "${var.service_account == "create" ? 1 : 0}"
47+
project = "${google_project_iam_member.cluster_service_account-metric_writer.project}"
48+
role = "roles/monitoring.viewer"
49+
member = "serviceAccount:${google_service_account.cluster_service_account.email}"
50+
}

autogen/variables.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ variable "monitoring_service" {
208208
}
209209

210210
variable "service_account" {
211-
description = "The service account to default running nodes as if not overridden in `node_pools`. Defaults to the compute engine default service account"
211+
description = "The service account to default running nodes as if not overridden in `node_pools`. Defaults to the compute engine default service account. May also specify `create` to automatically create a cluster-specific service account"
212212
default = ""
213213
}
214214
{% if private_cluster %}

cluster_regional.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ resource "google_container_cluster" "primary" {
8181
name = "default-pool"
8282

8383
node_config {
84-
service_account = "${lookup(var.node_pools[0], "service_account", var.service_account)}"
84+
service_account = "${lookup(var.node_pools[0], "service_account", local.service_account)}"
8585
}
8686
}
8787

@@ -121,7 +121,7 @@ resource "google_container_node_pool" "pools" {
121121

122122
disk_size_gb = "${lookup(var.node_pools[count.index], "disk_size_gb", 100)}"
123123
disk_type = "${lookup(var.node_pools[count.index], "disk_type", "pd-standard")}"
124-
service_account = "${lookup(var.node_pools[count.index], "service_account", var.service_account)}"
124+
service_account = "${lookup(var.node_pools[count.index], "service_account", local.service_account)}"
125125
preemptible = "${lookup(var.node_pools[count.index], "preemptible", false)}"
126126

127127
oauth_scopes = [

cluster_zonal.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ resource "google_container_cluster" "zonal_primary" {
8181
name = "default-pool"
8282

8383
node_config {
84-
service_account = "${lookup(var.node_pools[0], "service_account", var.service_account)}"
84+
service_account = "${lookup(var.node_pools[0], "service_account", local.service_account)}"
8585
}
8686
}
8787

@@ -121,7 +121,7 @@ resource "google_container_node_pool" "zonal_pools" {
121121

122122
disk_size_gb = "${lookup(var.node_pools[count.index], "disk_size_gb", 100)}"
123123
disk_type = "${lookup(var.node_pools[count.index], "disk_type", "pd-standard")}"
124-
service_account = "${lookup(var.node_pools[count.index], "service_account", var.service_account)}"
124+
service_account = "${lookup(var.node_pools[count.index], "service_account", local.service_account)}"
125125
preemptible = "${lookup(var.node_pools[count.index], "preemptible", false)}"
126126

127127
oauth_scopes = [

examples/simple_zonal/README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ This example illustrates how to create a simple cluster.
1010
| Name | Description | Type | Default | Required |
1111
|------|-------------|:----:|:-----:|:-----:|
1212
| cluster_name_suffix | A suffix to append to the default cluster name | string | `` | no |
13-
| compute_engine_service_account | Service account to associate to the nodes in the cluster | string | - | yes |
1413
| credentials_path | The path to the GCP credentials JSON file | string | - | yes |
1514
| ip_range_pods | The secondary ip range to use for pods | string | - | yes |
1615
| ip_range_services | The secondary ip range to use for pods | string | - | yes |

examples/simple_zonal/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ module "gke" {
3434
subnetwork = "${var.subnetwork}"
3535
ip_range_pods = "${var.ip_range_pods}"
3636
ip_range_services = "${var.ip_range_services}"
37-
service_account = "${var.compute_engine_service_account}"
37+
service_account = "create"
3838
}
3939

4040
data "google_client_config" "default" {}

examples/simple_zonal/variables.tf

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,3 @@ variable "ip_range_pods" {
5151
variable "ip_range_services" {
5252
description = "The secondary ip range to use for pods"
5353
}
54-
55-
variable "compute_engine_service_account" {
56-
description = "Service account to associate to the nodes in the cluster"
57-
}

modules/private-cluster/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ Then perform the following commands on the root folder:
131131
| region | The region to host the cluster in (required) | string | - | yes |
132132
| regional | Whether is a regional cluster (zonal cluster if set false. WARNING: changing this after cluster creation is destructive!) | string | `true` | no |
133133
| remove_default_node_pool | Remove default node pool while setting up the cluster | string | `false` | no |
134-
| service_account | The service account to default running nodes as if not overridden in `node_pools`. Defaults to the compute engine default service account | string | `` | no |
134+
| service_account | The service account to default running nodes as if not overridden in `node_pools`. Defaults to the compute engine default service account. May also specify `create` to automatically create a cluster-specific service account | string | `` | no |
135135
| stub_domains | Map of stub domains and their resolvers to forward DNS queries for a certain domain to an external DNS server | map | `<map>` | no |
136136
| subnetwork | The subnetwork to host the cluster in (required) | string | - | yes |
137137
| zones | The zones to host the cluster in (optional if regional cluster / required if zonal) | list | `<list>` | no |
@@ -185,6 +185,7 @@ following project roles:
185185
- roles/compute.viewer
186186
- roles/container.clusterAdmin
187187
- roles/container.developer
188+
- roles/iam.serviceAccountAdmin
188189
- roles/iam.serviceAccountUser
189190

190191
### Enable APIs

modules/private-cluster/cluster_regional.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ resource "google_container_cluster" "primary" {
8181
name = "default-pool"
8282

8383
node_config {
84-
service_account = "${lookup(var.node_pools[0], "service_account", var.service_account)}"
84+
service_account = "${lookup(var.node_pools[0], "service_account", local.service_account)}"
8585
}
8686
}
8787

@@ -127,7 +127,7 @@ resource "google_container_node_pool" "pools" {
127127

128128
disk_size_gb = "${lookup(var.node_pools[count.index], "disk_size_gb", 100)}"
129129
disk_type = "${lookup(var.node_pools[count.index], "disk_type", "pd-standard")}"
130-
service_account = "${lookup(var.node_pools[count.index], "service_account", var.service_account)}"
130+
service_account = "${lookup(var.node_pools[count.index], "service_account", local.service_account)}"
131131
preemptible = "${lookup(var.node_pools[count.index], "preemptible", false)}"
132132

133133
oauth_scopes = [

modules/private-cluster/cluster_zonal.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ resource "google_container_cluster" "zonal_primary" {
8181
name = "default-pool"
8282

8383
node_config {
84-
service_account = "${lookup(var.node_pools[0], "service_account", var.service_account)}"
84+
service_account = "${lookup(var.node_pools[0], "service_account", local.service_account)}"
8585
}
8686
}
8787

@@ -127,7 +127,7 @@ resource "google_container_node_pool" "zonal_pools" {
127127

128128
disk_size_gb = "${lookup(var.node_pools[count.index], "disk_size_gb", 100)}"
129129
disk_type = "${lookup(var.node_pools[count.index], "disk_type", "pd-standard")}"
130-
service_account = "${lookup(var.node_pools[count.index], "service_account", var.service_account)}"
130+
service_account = "${lookup(var.node_pools[count.index], "service_account", local.service_account)}"
131131
preemptible = "${lookup(var.node_pools[count.index], "preemptible", false)}"
132132

133133
oauth_scopes = [

modules/private-cluster/sa.tf

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* Copyright 2018 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
// This file was automatically generated from a template in ./autogen
18+
19+
locals {
20+
service_account_list = "${compact(concat(google_service_account.cluster_service_account.*.email, list("dummy")))}"
21+
service_account = "${var.service_account == "create" ? element(local.service_account_list, 0) : var.service_account}"
22+
}
23+
24+
resource "google_service_account" "cluster_service_account" {
25+
count = "${var.service_account == "create" ? 1 : 0}"
26+
project = "${var.project_id}"
27+
account_id = "tf-gke-${substr(var.name, 0, 20)}"
28+
display_name = "Terraform-managed service account for cluster ${var.name}"
29+
}
30+
31+
resource "google_project_iam_member" "cluster_service_account-log_writer" {
32+
count = "${var.service_account == "create" ? 1 : 0}"
33+
project = "${google_service_account.cluster_service_account.project}"
34+
role = "roles/logging.logWriter"
35+
member = "serviceAccount:${google_service_account.cluster_service_account.email}"
36+
}
37+
38+
resource "google_project_iam_member" "cluster_service_account-metric_writer" {
39+
count = "${var.service_account == "create" ? 1 : 0}"
40+
project = "${google_project_iam_member.cluster_service_account-log_writer.project}"
41+
role = "roles/monitoring.metricWriter"
42+
member = "serviceAccount:${google_service_account.cluster_service_account.email}"
43+
}
44+
45+
resource "google_project_iam_member" "cluster_service_account-monitoring_viewer" {
46+
count = "${var.service_account == "create" ? 1 : 0}"
47+
project = "${google_project_iam_member.cluster_service_account-metric_writer.project}"
48+
role = "roles/monitoring.viewer"
49+
member = "serviceAccount:${google_service_account.cluster_service_account.email}"
50+
}

modules/private-cluster/variables.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ variable "monitoring_service" {
208208
}
209209

210210
variable "service_account" {
211-
description = "The service account to default running nodes as if not overridden in `node_pools`. Defaults to the compute engine default service account"
211+
description = "The service account to default running nodes as if not overridden in `node_pools`. Defaults to the compute engine default service account. May also specify `create` to automatically create a cluster-specific service account"
212212
default = ""
213213
}
214214

sa.tf

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* Copyright 2018 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
// This file was automatically generated from a template in ./autogen
18+
19+
locals {
20+
service_account_list = "${compact(concat(google_service_account.cluster_service_account.*.email, list("dummy")))}"
21+
service_account = "${var.service_account == "create" ? element(local.service_account_list, 0) : var.service_account}"
22+
}
23+
24+
resource "google_service_account" "cluster_service_account" {
25+
count = "${var.service_account == "create" ? 1 : 0}"
26+
project = "${var.project_id}"
27+
account_id = "tf-gke-${substr(var.name, 0, 20)}"
28+
display_name = "Terraform-managed service account for cluster ${var.name}"
29+
}
30+
31+
resource "google_project_iam_member" "cluster_service_account-log_writer" {
32+
count = "${var.service_account == "create" ? 1 : 0}"
33+
project = "${google_service_account.cluster_service_account.project}"
34+
role = "roles/logging.logWriter"
35+
member = "serviceAccount:${google_service_account.cluster_service_account.email}"
36+
}
37+
38+
resource "google_project_iam_member" "cluster_service_account-metric_writer" {
39+
count = "${var.service_account == "create" ? 1 : 0}"
40+
project = "${google_project_iam_member.cluster_service_account-log_writer.project}"
41+
role = "roles/monitoring.metricWriter"
42+
member = "serviceAccount:${google_service_account.cluster_service_account.email}"
43+
}
44+
45+
resource "google_project_iam_member" "cluster_service_account-monitoring_viewer" {
46+
count = "${var.service_account == "create" ? 1 : 0}"
47+
project = "${google_project_iam_member.cluster_service_account-metric_writer.project}"
48+
role = "roles/monitoring.viewer"
49+
member = "serviceAccount:${google_service_account.cluster_service_account.email}"
50+
}

test/fixtures/simple_zonal/example.tf

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,13 @@
1717
module "example" {
1818
source = "../../../examples/simple_zonal"
1919

20-
project_id = "${var.project_id}"
21-
credentials_path = "${local.credentials_path}"
22-
cluster_name_suffix = "-${random_string.suffix.result}"
23-
region = "${var.region}"
24-
zones = ["${slice(var.zones,0,1)}"]
25-
network = "${google_compute_network.main.name}"
26-
subnetwork = "${google_compute_subnetwork.main.name}"
27-
ip_range_pods = "${google_compute_subnetwork.main.secondary_ip_range.0.range_name}"
28-
ip_range_services = "${google_compute_subnetwork.main.secondary_ip_range.1.range_name}"
29-
compute_engine_service_account = "${var.compute_engine_service_account}"
20+
project_id = "${var.project_id}"
21+
credentials_path = "${local.credentials_path}"
22+
cluster_name_suffix = "-${random_string.suffix.result}"
23+
region = "${var.region}"
24+
zones = ["${slice(var.zones,0,1)}"]
25+
network = "${google_compute_network.main.name}"
26+
subnetwork = "${google_compute_subnetwork.main.name}"
27+
ip_range_pods = "${google_compute_subnetwork.main.secondary_ip_range.0.range_name}"
28+
ip_range_services = "${google_compute_subnetwork.main.secondary_ip_range.1.range_name}"
3029
}

test/integration/simple_zonal/controls/gcloud.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,16 @@
7979
describe "node pool" do
8080
let(:node_pools) { data['nodePools'].reject { |p| p['name'] == "default-pool" } }
8181

82+
it "uses an automatically created service account" do
83+
expect(node_pools).to include(
84+
including(
85+
"config" => including(
86+
"serviceAccount" => starting_with("tf-gke-simple-zonal-cluster@"),
87+
),
88+
),
89+
)
90+
end
91+
8292
it "has autoscaling enabled" do
8393
expect(node_pools).to include(
8494
including(

0 commit comments

Comments
 (0)