Skip to content

Commit 093b229

Browse files
committed
WIP
1 parent 6189fef commit 093b229

File tree

12 files changed

+61
-16
lines changed

12 files changed

+61
-16
lines changed

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", (var.service_account == "create") ? element(concat(google_service_account.cluster_service_account.*.email, list("")), 0) : var.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", (var.service_account == "create") ? element(concat(google_service_account.cluster_service_account.*.email, list("")), 0) : var.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", (var.service_account == "create") ? element(concat(google_service_account.cluster_service_account.*.email, list("")), 0) : var.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", (var.service_account == "create") ? element(concat(google_service_account.cluster_service_account.*.email, list("")), 0) : var.service_account)}"
131131
preemptible = "${lookup(var.node_pools[count.index], "preemptible", false)}"
132132

133133
oauth_scopes = [

autogen/sa.tf

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
resource "google_service_account" "cluster_service_account" {
20+
count = "${(var.service_account == "create") ? 1 : 0}"
21+
account_id = "tf-gke-${var.name}"
22+
display_name = "Terraform-managed service account for cluster ${var.name}"
23+
}

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", (var.service_account == "create") ? element(concat(google_service_account.cluster_service_account.*.email, list("")), 0) : var.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", (var.service_account == "create") ? element(concat(google_service_account.cluster_service_account.*.email, list("")), 0) : var.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", (var.service_account == "create") ? element(concat(google_service_account.cluster_service_account.*.email, list("")), 0) : var.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", (var.service_account == "create") ? element(concat(google_service_account.cluster_service_account.*.email, list("")), 0) : var.service_account)}"
125125
preemptible = "${lookup(var.node_pools[count.index], "preemptible", false)}"
126126

127127
oauth_scopes = [

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-
}

sa.tf

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
resource "google_service_account" "cluster_service_account" {
20+
count = "${(var.service_account == "create") ? 1 : 0}"
21+
account_id = "tf-gke-${var.name}"
22+
display_name = "Terraform-managed service account for cluster ${var.name}"
23+
}

test/fixtures/simple_zonal/example.tf

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,4 @@ module "example" {
2626
subnetwork = "${google_compute_subnetwork.main.name}"
2727
ip_range_pods = "${google_compute_subnetwork.main.secondary_ip_range.0.range_name}"
2828
ip_range_services = "${google_compute_subnetwork.main.secondary_ip_range.1.range_name}"
29-
compute_engine_service_account = "${var.compute_engine_service_account}"
3029
}

test/integration/simple_zonal/controls/gcloud.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@
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+
raise node_pools.to_json.inspect
84+
end
85+
8286
it "has autoscaling enabled" do
8387
expect(node_pools).to include(
8488
including(

variables.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,6 @@ 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
}

0 commit comments

Comments
 (0)