Skip to content

Commit 3f7527e

Browse files
authored
Merge pull request #61 from terraform-google-modules/internal/submodule-structure
Set up submodule structure
2 parents a0cfbae + 0f780cc commit 3f7527e

23 files changed

+1038
-11
lines changed

Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ test_integration:
8585
generate_docs:
8686
@source test/make.sh && generate_docs
8787

88+
.PHONY: generate
89+
generate:
90+
@pip install --user -r ./helpers/generate_modules/requirements.txt
91+
@./helpers/generate_modules/generate_modules.py
92+
8893
# Versioning
8994
.PHONY: version
9095
version:

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,13 +199,20 @@ The project has the following folders and files:
199199

200200
- /: root folder
201201
- /examples: examples for using this module
202+
- /helpers: Helper scripts
202203
- /scripts: Scripts for specific tasks on module (see Infrastructure section on this file)
203204
- /test: Folders with files for testing the module (see Testing section on this file)
204205
- /main.tf: main file for this module, contains all the resources to create
205206
- /variables.tf: all the variables for the module
206207
- /output.tf: the outputs of the module
207208
- /readme.MD: this file
208209

210+
## Templating
211+
212+
To more cleanly handle cases where desired functionality would require complex duplication of Terraform resources (i.e. [PR 51](https://github.com/terraform-google-modules/terraform-google-kubernetes-engine/pull/51)), this repository is largely generated from the [`autogen`](./autogen) directory.
213+
214+
The root module is generated by running `make generate`. Changes to this repository should be made in the [`autogen`](./autogen) directory where appropriate.
215+
209216
## Testing
210217

211218
### Requirements

auth.tf

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
* limitations under the License.
1515
*/
1616

17+
// This file was automatically generated from a template in ./autogen
18+
1719
/******************************************
1820
Retrieve authentication token
1921
*****************************************/
@@ -27,4 +29,4 @@ provider "kubernetes" {
2729
host = "https://${local.cluster_endpoint}"
2830
token = "${data.google_client_config.default.access_token}"
2931
cluster_ca_certificate = "${base64decode(local.cluster_ca_certificate)}"
30-
}
32+
}

autogen/auth.tf

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
/******************************************
20+
Retrieve authentication token
21+
*****************************************/
22+
data "google_client_config" "default" {}
23+
24+
/******************************************
25+
Configure provider
26+
*****************************************/
27+
provider "kubernetes" {
28+
load_config_file = false
29+
host = "https://${local.cluster_endpoint}"
30+
token = "${data.google_client_config.default.access_token}"
31+
cluster_ca_certificate = "${base64decode(local.cluster_ca_certificate)}"
32+
}

autogen/cluster_regional.tf

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
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+
/******************************************
20+
Create regional cluster
21+
*****************************************/
22+
resource "google_container_cluster" "primary" {
23+
count = "${var.regional ? 1 : 0}"
24+
name = "${var.name}"
25+
description = "${var.description}"
26+
project = "${var.project_id}"
27+
28+
region = "${var.region}"
29+
additional_zones = ["${coalescelist(compact(var.zones), sort(random_shuffle.available_zones.result))}"]
30+
31+
network = "${replace(data.google_compute_network.gke_network.self_link, "https://www.googleapis.com/compute/v1/", "")}"
32+
subnetwork = "${replace(data.google_compute_subnetwork.gke_subnetwork.self_link, "https://www.googleapis.com/compute/v1/", "")}"
33+
min_master_version = "${local.kubernetes_version}"
34+
35+
logging_service = "${var.logging_service}"
36+
monitoring_service = "${var.monitoring_service}"
37+
38+
master_authorized_networks_config = "${var.master_authorized_networks_config}"
39+
40+
addons_config {
41+
http_load_balancing {
42+
disabled = "${var.http_load_balancing ? 0 : 1}"
43+
}
44+
45+
horizontal_pod_autoscaling {
46+
disabled = "${var.horizontal_pod_autoscaling ? 0 : 1}"
47+
}
48+
49+
kubernetes_dashboard {
50+
disabled = "${var.kubernetes_dashboard ? 0 : 1}"
51+
}
52+
53+
network_policy_config {
54+
disabled = "${var.network_policy ? 0 : 1}"
55+
}
56+
}
57+
58+
ip_allocation_policy {
59+
cluster_secondary_range_name = "${var.ip_range_pods}"
60+
services_secondary_range_name = "${var.ip_range_services}"
61+
}
62+
63+
maintenance_policy {
64+
daily_maintenance_window {
65+
start_time = "${var.maintenance_start_time}"
66+
}
67+
}
68+
69+
lifecycle {
70+
ignore_changes = ["node_pool"]
71+
}
72+
73+
timeouts {
74+
create = "30m"
75+
update = "30m"
76+
delete = "30m"
77+
}
78+
79+
node_pool {
80+
name = "default-pool"
81+
82+
node_config {
83+
service_account = "${lookup(var.node_pools[0], "service_account", var.service_account)}"
84+
}
85+
}
86+
87+
remove_default_node_pool = "${var.remove_default_node_pool}"
88+
}
89+
90+
/******************************************
91+
Create regional node pools
92+
*****************************************/
93+
resource "google_container_node_pool" "pools" {
94+
count = "${var.regional ? length(var.node_pools) : 0}"
95+
name = "${lookup(var.node_pools[count.index], "name")}"
96+
project = "${var.project_id}"
97+
region = "${var.region}"
98+
cluster = "${var.name}"
99+
version = "${lookup(var.node_pools[count.index], "auto_upgrade", false) ? "" : lookup(var.node_pools[count.index], "version", local.node_version)}"
100+
initial_node_count = "${lookup(var.node_pools[count.index], "initial_node_count", lookup(var.node_pools[count.index], "min_count", 1))}"
101+
102+
autoscaling {
103+
min_node_count = "${lookup(var.node_pools[count.index], "min_count", 1)}"
104+
max_node_count = "${lookup(var.node_pools[count.index], "max_count", 100)}"
105+
}
106+
107+
management {
108+
auto_repair = "${lookup(var.node_pools[count.index], "auto_repair", true)}"
109+
auto_upgrade = "${lookup(var.node_pools[count.index], "auto_upgrade", true)}"
110+
}
111+
112+
node_config {
113+
image_type = "${lookup(var.node_pools[count.index], "image_type", "COS")}"
114+
machine_type = "${lookup(var.node_pools[count.index], "machine_type", "n1-standard-2")}"
115+
labels = "${merge(map("cluster_name", var.name), map("node_pool", lookup(var.node_pools[count.index], "name")), var.node_pools_labels["all"], var.node_pools_labels[lookup(var.node_pools[count.index], "name")])}"
116+
metadata = "${merge(map("cluster_name", var.name), map("node_pool", lookup(var.node_pools[count.index], "name")), var.node_pools_metadata["all"], var.node_pools_metadata[lookup(var.node_pools[count.index], "name")])}"
117+
taint = "${concat(var.node_pools_taints["all"], var.node_pools_taints[lookup(var.node_pools[count.index], "name")])}"
118+
tags = ["${concat(list("gke-${var.name}"), list("gke-${var.name}-${lookup(var.node_pools[count.index], "name")}"), var.node_pools_tags["all"], var.node_pools_tags[lookup(var.node_pools[count.index], "name")])}"]
119+
120+
disk_size_gb = "${lookup(var.node_pools[count.index], "disk_size_gb", 100)}"
121+
disk_type = "${lookup(var.node_pools[count.index], "disk_type", "pd-standard")}"
122+
service_account = "${lookup(var.node_pools[count.index], "service_account", var.service_account)}"
123+
preemptible = "${lookup(var.node_pools[count.index], "preemptible", false)}"
124+
125+
oauth_scopes = [
126+
"https://www.googleapis.com/auth/cloud-platform",
127+
]
128+
}
129+
130+
lifecycle {
131+
ignore_changes = ["initial_node_count"]
132+
}
133+
134+
timeouts {
135+
create = "30m"
136+
update = "30m"
137+
delete = "30m"
138+
}
139+
140+
depends_on = ["google_container_cluster.primary"]
141+
}
142+
143+
resource "null_resource" "wait_for_regional_cluster" {
144+
count = "${var.regional ? 1 : 0}"
145+
146+
provisioner "local-exec" {
147+
command = "${path.module}/scripts/wait-for-cluster.sh ${var.project_id} ${var.name}"
148+
}
149+
150+
provisioner "local-exec" {
151+
when = "destroy"
152+
command = "${path.module}/scripts/wait-for-cluster.sh ${var.project_id} ${var.name}"
153+
}
154+
155+
depends_on = ["google_container_cluster.primary", "google_container_node_pool.pools"]
156+
}

autogen/cluster_zonal.tf

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
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+
/******************************************
20+
Create zonal cluster
21+
*****************************************/
22+
resource "google_container_cluster" "zonal_primary" {
23+
count = "${var.regional ? 0 : 1}"
24+
name = "${var.name}"
25+
description = "${var.description}"
26+
project = "${var.project_id}"
27+
28+
zone = "${var.zones[0]}"
29+
additional_zones = ["${slice(var.zones,1,length(var.zones))}"]
30+
31+
network = "${replace(data.google_compute_network.gke_network.self_link, "https://www.googleapis.com/compute/v1/", "")}"
32+
subnetwork = "${replace(data.google_compute_subnetwork.gke_subnetwork.self_link, "https://www.googleapis.com/compute/v1/", "")}"
33+
min_master_version = "${local.kubernetes_version}"
34+
35+
logging_service = "${var.logging_service}"
36+
monitoring_service = "${var.monitoring_service}"
37+
38+
master_authorized_networks_config = "${var.master_authorized_networks_config}"
39+
40+
addons_config {
41+
http_load_balancing {
42+
disabled = "${var.http_load_balancing ? 0 : 1}"
43+
}
44+
45+
horizontal_pod_autoscaling {
46+
disabled = "${var.horizontal_pod_autoscaling ? 0 : 1}"
47+
}
48+
49+
kubernetes_dashboard {
50+
disabled = "${var.kubernetes_dashboard ? 0 : 1}"
51+
}
52+
53+
network_policy_config {
54+
disabled = "${var.network_policy ? 0 : 1}"
55+
}
56+
}
57+
58+
ip_allocation_policy {
59+
cluster_secondary_range_name = "${var.ip_range_pods}"
60+
services_secondary_range_name = "${var.ip_range_services}"
61+
}
62+
63+
maintenance_policy {
64+
daily_maintenance_window {
65+
start_time = "${var.maintenance_start_time}"
66+
}
67+
}
68+
69+
lifecycle {
70+
ignore_changes = ["node_pool"]
71+
}
72+
73+
timeouts {
74+
create = "30m"
75+
update = "30m"
76+
delete = "30m"
77+
}
78+
79+
node_pool {
80+
name = "default-pool"
81+
82+
node_config {
83+
service_account = "${lookup(var.node_pools[0], "service_account", var.service_account)}"
84+
}
85+
}
86+
87+
remove_default_node_pool = "${var.remove_default_node_pool}"
88+
}
89+
90+
/******************************************
91+
Create zonal node pools
92+
*****************************************/
93+
resource "google_container_node_pool" "zonal_pools" {
94+
count = "${var.regional ? 0 : length(var.node_pools)}"
95+
name = "${lookup(var.node_pools[count.index], "name")}"
96+
project = "${var.project_id}"
97+
zone = "${var.zones[0]}"
98+
cluster = "${var.name}"
99+
version = "${lookup(var.node_pools[count.index], "auto_upgrade", false) ? "" : lookup(var.node_pools[count.index], "version", local.node_version)}"
100+
initial_node_count = "${lookup(var.node_pools[count.index], "initial_node_count", lookup(var.node_pools[count.index], "min_count", 1))}"
101+
102+
autoscaling {
103+
min_node_count = "${lookup(var.node_pools[count.index], "min_count", 1)}"
104+
max_node_count = "${lookup(var.node_pools[count.index], "max_count", 100)}"
105+
}
106+
107+
management {
108+
auto_repair = "${lookup(var.node_pools[count.index], "auto_repair", true)}"
109+
auto_upgrade = "${lookup(var.node_pools[count.index], "auto_upgrade", false)}"
110+
}
111+
112+
node_config {
113+
image_type = "${lookup(var.node_pools[count.index], "image_type", "COS")}"
114+
machine_type = "${lookup(var.node_pools[count.index], "machine_type", "n1-standard-2")}"
115+
labels = "${merge(map("cluster_name", var.name), map("node_pool", lookup(var.node_pools[count.index], "name")), var.node_pools_labels["all"], var.node_pools_labels[lookup(var.node_pools[count.index], "name")])}"
116+
metadata = "${merge(map("cluster_name", var.name), map("node_pool", lookup(var.node_pools[count.index], "name")), var.node_pools_metadata["all"], var.node_pools_metadata[lookup(var.node_pools[count.index], "name")])}"
117+
taint = "${concat(var.node_pools_taints["all"], var.node_pools_taints[lookup(var.node_pools[count.index], "name")])}"
118+
tags = ["${concat(list("gke-${var.name}"), list("gke-${var.name}-${lookup(var.node_pools[count.index], "name")}"), var.node_pools_tags["all"], var.node_pools_tags[lookup(var.node_pools[count.index], "name")])}"]
119+
120+
disk_size_gb = "${lookup(var.node_pools[count.index], "disk_size_gb", 100)}"
121+
disk_type = "${lookup(var.node_pools[count.index], "disk_type", "pd-standard")}"
122+
service_account = "${lookup(var.node_pools[count.index], "service_account", var.service_account)}"
123+
preemptible = "${lookup(var.node_pools[count.index], "preemptible", false)}"
124+
125+
oauth_scopes = [
126+
"https://www.googleapis.com/auth/cloud-platform",
127+
]
128+
}
129+
130+
lifecycle {
131+
ignore_changes = ["initial_node_count"]
132+
}
133+
134+
timeouts {
135+
create = "30m"
136+
update = "30m"
137+
delete = "30m"
138+
}
139+
140+
depends_on = ["google_container_cluster.zonal_primary"]
141+
}
142+
143+
resource "null_resource" "wait_for_zonal_cluster" {
144+
count = "${var.regional ? 0 : 1}"
145+
146+
provisioner "local-exec" {
147+
command = "${path.module}/scripts/wait-for-cluster.sh ${var.project_id} ${var.name}"
148+
}
149+
150+
provisioner "local-exec" {
151+
when = "destroy"
152+
command = "${path.module}/scripts/wait-for-cluster.sh ${var.project_id} ${var.name}"
153+
}
154+
155+
depends_on = ["google_container_cluster.zonal_primary", "google_container_node_pool.zonal_pools"]
156+
}

0 commit comments

Comments
 (0)