Skip to content

Commit ee441be

Browse files
committed
Allow service account for cluster to be created by the module
1 parent ee8fe0e commit ee441be

File tree

10 files changed

+48
-20
lines changed

10 files changed

+48
-20
lines changed

README.md

Lines changed: 1 addition & 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 |

autogen/README.md

Lines changed: 1 addition & 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

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 |

modules/private-cluster/README.md

Lines changed: 1 addition & 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 |

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: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
}

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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@
1818

1919
locals {
2020
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}"
21+
service_account = "${var.service_account == "create" ? element(local.service_account_list, 0) : var.service_account}"
2222
}
2323

2424
resource "google_service_account" "cluster_service_account" {
2525
count = "${var.service_account == "create" ? 1 : 0}"
2626
project = "${var.project_id}"
2727
account_id = "tf-gke-${substr(var.name, 0, 20)}"
2828
display_name = "Terraform-managed service account for cluster ${var.name}"
29-
}
29+
}

test/fixtures/simple_zonal/example.tf

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +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}"
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}"
2929
}

0 commit comments

Comments
 (0)