This repository has been archived by the owner on Apr 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 88
/
Copy pathmain.tf
299 lines (244 loc) · 10.4 KB
/
main.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
# Copyright 2019 Jetstack Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Resource definitions for cluster and node pools
# Each argument is explained, most details are pulled from the Terraform
# documentation. Arguments set by input variables are documented in the
# variables.tf file.
terraform {
required_version = "~> 0.12"
}
# Local values assign a name to an expression, that can then be used multiple
# times within a module. They are used here to determine the GCP region from
# the given location, which can be either a region or zone.
locals {
gcp_location_parts = split("-", var.gcp_location)
gcp_region = format("%s-%s", local.gcp_location_parts[0], local.gcp_location_parts[1])
}
# https://www.terraform.io/docs/providers/google/index.html
provider "google" {
version = "~> 3.5"
project = var.gcp_project_id
region = local.gcp_region
}
# To make use of beta features the google-beta provider is also used. Only
# resources that use beta features use the beta provider. All resources have
# the provider set explicitly for clarity.
# https://www.terraform.io/docs/providers/google/guides/provider_versions.html#using-the-google-beta-provider
provider "google-beta" {
version = "~> 3.5"
project = var.gcp_project_id
region = local.gcp_region
}
locals {
release_channel = var.release_channel == "" ? [] : [var.release_channel]
min_master_version = var.release_channel == "" ? var.min_master_version : ""
identity_namespace = var.identity_namespace == "" ? [] : [var.identity_namespace]
}
locals {
authenticator_security_group = var.authenticator_security_group == "" ? [] : [var.authenticator_security_group]
}
# https://www.terraform.io/docs/providers/google/r/container_cluster.html
resource "google_container_cluster" "cluster" {
provider = google-beta
location = var.gcp_location
name = var.cluster_name
min_master_version = local.min_master_version
dynamic "release_channel" {
for_each = toset(local.release_channel)
content {
channel = release_channel.value
}
}
dynamic "authenticator_groups_config" {
for_each = toset(local.authenticator_security_group)
content {
security_group = authenticator_groups_config.value
}
}
# Configure workload identity if set
dynamic "workload_identity_config" {
for_each = toset(local.identity_namespace)
content {
identity_namespace = workload_identity_config.value
}
}
maintenance_policy {
daily_maintenance_window {
start_time = var.daily_maintenance_window_start_time
}
}
# A set of options for creating a private cluster.
private_cluster_config {
enable_private_endpoint = var.private_endpoint
enable_private_nodes = var.private_nodes
master_ipv4_cidr_block = var.master_ipv4_cidr_block
}
# Enable the PodSecurityPolicy admission controller for the cluster.
pod_security_policy_config {
enabled = var.pod_security_policy_enabled
}
# Configuration options for the NetworkPolicy feature.
network_policy {
# Whether network policy is enabled on the cluster. Defaults to false.
# In GKE this also enables the ip masquerade agent
# https://cloud.google.com/kubernetes-engine/docs/how-to/ip-masquerade-agent
enabled = true
# The selected network policy provider. Defaults to PROVIDER_UNSPECIFIED.
provider = "CALICO"
}
master_auth {
# Setting an empty username and password explicitly disables basic auth
username = ""
password = ""
# Whether client certificate authorization is enabled for this cluster.
client_certificate_config {
issue_client_certificate = false
}
}
# The configuration for addons supported by GKE.
addons_config {
http_load_balancing {
disabled = var.http_load_balancing_disabled
}
# Whether we should enable the network policy addon for the master. This must be
# enabled in order to enable network policy for the nodes. It can only be disabled
# if the nodes already do not have network policies enabled. Defaults to disabled;
# set disabled = false to enable.
network_policy_config {
disabled = false
}
}
network = var.vpc_network_name
subnetwork = var.vpc_subnetwork_name
# Configuration for cluster IP allocation. As of now, only pre-allocated
# subnetworks (custom type with secondary ranges) are supported. This will
# activate IP aliases.
ip_allocation_policy {
cluster_secondary_range_name = var.cluster_secondary_range_name
services_secondary_range_name = var.services_secondary_range_name
}
# It's not possible to create a cluster with no node pool defined, but we
# want to only use separately managed node pools. So we create the smallest
# possible default node pool and immediately delete it.
remove_default_node_pool = true
# The number of nodes to create in this cluster (not including the Kubernetes master).
initial_node_count = 1
# The desired configuration options for master authorized networks. Omit the
# nested cidr_blocks attribute to disallow external access (except the
# cluster node IPs, which GKE automatically whitelists).
master_authorized_networks_config {
dynamic "cidr_blocks" {
for_each = var.master_authorized_networks_cidr_blocks
content {
cidr_block = cidr_blocks.value.cidr_block
display_name = cidr_blocks.value.display_name
}
}
}
# The loggingservice that the cluster should write logs to. Using the
# 'logging.googleapis.com/kubernetes' option makes use of new Stackdriver
# Kubernetes integration.
logging_service = var.stackdriver_logging != "false" ? "logging.googleapis.com/kubernetes" : ""
# The monitoring service that the cluster should write metrics to. Using the
# 'monitoring.googleapis.com/kubernetes' option makes use of new Stackdriver
# Kubernetes integration.
monitoring_service = var.stackdriver_monitoring != "false" ? "monitoring.googleapis.com/kubernetes" : ""
# Change how long update operations on the cluster are allowed to take
# before being considered to have failed. The default is 10 mins.
# https://www.terraform.io/docs/configuration/resources.html#operation-timeouts
timeouts {
update = "20m"
}
}
# https://www.terraform.io/docs/providers/google/r/container_node_pool.html
resource "google_container_node_pool" "node_pool" {
provider = google
# The location (region or zone) in which the cluster resides
location = google_container_cluster.cluster.location
count = length(var.node_pools)
# The name of the node pool. Instance groups created will have the cluster
# name prefixed automatically.
name = format("%s-pool", lookup(var.node_pools[count.index], "name", format("%03d", count.index + 1)))
# The cluster to create the node pool for.
cluster = google_container_cluster.cluster.name
initial_node_count = lookup(var.node_pools[count.index], "initial_node_count", 1)
# Configuration required by cluster autoscaler to adjust the size of the node pool to the current cluster usage.
autoscaling {
# Minimum number of nodes in the NodePool. Must be >=0 and <= max_node_count.
min_node_count = lookup(var.node_pools[count.index], "autoscaling_min_node_count", 2)
# Maximum number of nodes in the NodePool. Must be >= min_node_count.
max_node_count = lookup(var.node_pools[count.index], "autoscaling_max_node_count", 3)
}
# Target a specific Kubernetes version.
version = lookup(var.node_pools[count.index], "version", "")
# Node management configuration, wherein auto-repair and auto-upgrade is configured.
management {
# Whether the nodes will be automatically repaired.
auto_repair = lookup(var.node_pools[count.index], "auto_repair", true)
# Whether the nodes will be automatically upgraded.
auto_upgrade = lookup(var.node_pools[count.index], "version", "") == "" ? lookup(var.node_pools[count.index], "auto_upgrade", true) : false
}
# Parameters used in creating the cluster's nodes.
node_config {
# The name of a Google Compute Engine machine type. Defaults to
# n1-standard-1.
machine_type = lookup(
var.node_pools[count.index],
"node_config_machine_type",
"n1-standard-1",
)
service_account = google_service_account.default.email
# Size of the disk attached to each node, specified in GB. The smallest
# allowed disk size is 10GB. Defaults to 100GB.
disk_size_gb = lookup(
var.node_pools[count.index],
"node_config_disk_size_gb",
100
)
# Type of the disk attached to each node (e.g. 'pd-standard' or 'pd-ssd').
# If unspecified, the default disk type is 'pd-standard'
disk_type = lookup(
var.node_pools[count.index],
"node_config_disk_type",
"pd-standard",
)
# A boolean that represents whether or not the underlying node VMs are
# preemptible. See the official documentation for more information.
# Defaults to false.
preemptible = lookup(
var.node_pools[count.index],
"node_config_preemptible",
false,
)
# The set of Google API scopes to be made available on all of the node VMs
# under the "default" service account. These can be either FQDNs, or scope
# aliases. The cloud-platform access scope authorizes access to all Cloud
# Platform services, and then limit the access by granting IAM roles
# https://cloud.google.com/compute/docs/access/service-accounts#service_account_permissions
oauth_scopes = [
"https://www.googleapis.com/auth/cloud-platform",
]
# The metadata key/value pairs assigned to instances in the cluster.
metadata = {
# https://cloud.google.com/kubernetes-engine/docs/how-to/protecting-cluster-metadata
disable-legacy-endpoints = "true"
}
}
# Change how long update operations on the node pool are allowed to take
# before being considered to have failed. The default is 10 mins.
# https://www.terraform.io/docs/configuration/resources.html#operation-timeouts
timeouts {
update = "20m"
}
}