-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.tf
295 lines (254 loc) · 10.1 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
/**
* Copyright 2020 Quortex
*
* 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.
*/
locals {
vpc_cni_configuration_values = var.handle_eni_configs ? {
"eniConfig" : {
"create" : true,
"region" : data.aws_region.current.name,
"subnets" : { for e in var.pods_subnets :
e.availability_zone => {
id = e.id
securityGroups = [aws_eks_cluster.quortex.vpc_config[0].cluster_security_group_id]
}
}
}
} : null
# The Quortex cluster OIDC issuer.
cluster_oidc_issuer = trimprefix(aws_eks_cluster.quortex.identity[0].oidc[0].issuer, "https://")
node_group_labels = [
for key, node_group in var.node_groups :
{
for k, v in lookup(node_group, "labels", {}) :
key => {
"k8s.io/cluster-autoscaler/node-template/label/${k}" : v
(k) : v
}...
}
]
node_groups_tags = {
for key, node_group in var.node_groups :
key => {
for k, v in lookup(node_group, "tags", {}) :
k => v
}
}
asg_custom_tags_chunks = chunklist(flatten([
for key, node_group in var.node_groups : [
for k, v in merge({
nodegroup : key,
"k8s.io/cluster-autoscaler/node-template/label/nodegroup" : key
},
merge(local.node_group_labels[0][key]...),
merge(local.node_groups_tags[key]),
var.tags,
var.compute_tags
) :
{
node_group : key,
key : k,
tag : v
}
]
]), 5)
}
# This data source is used to get the access to the effective Account ID, User ID, and ARN in which Terraform is authorized.
data "aws_caller_identity" "current" {}
# This datasource is used to get the region currently used by the AWS provider
data "aws_region" "current" {}
# Cluster
resource "aws_eks_cluster" "quortex" {
name = var.cluster_name
role_arn = var.handle_iam_resources ? aws_iam_role.quortex_role_master[0].arn : var.master_role_arn
version = var.kubernetes_version
vpc_config {
subnet_ids = var.master_subnet_ids
# Public endpoint: enabled but restricted to an IP range list
endpoint_public_access = true
public_access_cidrs = [for label, cidr_block in var.master_authorized_networks : cidr_block]
# Private endpoint: enabled for communication between worker nodes and the API server (since public endpoint is restricted)
endpoint_private_access = true
# Note: for private endpoint to work, DNS hostnames must be enabled in the VPC
}
enabled_cluster_log_types = var.enabled_cluster_log_types
tags = var.tags
depends_on = [
aws_iam_role_policy_attachment.quortex_amazon_eks_cluster_policy,
aws_iam_role_policy_attachment.quortex_amazon_eks_service_policy,
aws_cloudwatch_log_group.cluster_logs
]
}
resource "aws_security_group_rule" "cluster_security_group_additional" {
for_each = var.cluster_security_group_additional_rules
security_group_id = aws_eks_cluster.quortex.vpc_config[0].cluster_security_group_id
description = each.value.description
protocol = each.value.protocol
type = each.value.type
from_port = each.value.from_port
to_port = each.value.to_port
cidr_blocks = each.value.cidr_blocks
ipv6_cidr_blocks = each.value.ipv6_cidr_blocks
prefix_list_ids = each.value.prefix_list_ids
source_security_group_id = each.value.source_security_group_id
}
data "tls_certificate" "quortex_cluster" {
url = aws_eks_cluster.quortex.identity[0].oidc[0].issuer
}
# Provides an IAM OpenID Connect provider for the cluster.
resource "aws_iam_openid_connect_provider" "quortex_cluster" {
count = var.handle_iam_resources ? 1 : 0
client_id_list = ["sts.amazonaws.com"]
thumbprint_list = [data.tls_certificate.quortex_cluster.certificates[0].sha1_fingerprint]
url = aws_eks_cluster.quortex.identity[0].oidc[0].issuer
tags = var.tags
}
# Worker nodes
resource "aws_eks_node_group" "quortex" {
for_each = var.node_groups
cluster_name = aws_eks_cluster.quortex.name
version = var.kubernetes_worker_nodes_version
node_group_name = lookup(each.value, "name", "${var.cluster_name}_${each.key}")
node_role_arn = var.handle_iam_resources ? aws_iam_role.quortex_role_worker[0].arn : var.worker_role_arn
subnet_ids = lookup(each.value, "subnet_ids", [])
scaling_config {
desired_size = lookup(each.value, "scaling_desired_size", lookup(each.value, "scaling_min_size", 1))
min_size = lookup(each.value, "scaling_min_size", 1)
max_size = lookup(each.value, "scaling_max_size", 1)
}
lifecycle {
ignore_changes = [
# ignore changes to the cluster size, because it can be changed by autoscaling
scaling_config[0].desired_size,
]
}
instance_types = lookup(each.value, "instance_types", ["t3.medium"])
disk_size = lookup(each.value, "disk_size", 20)
dynamic "remote_access" {
for_each = var.remote_access_ssh_key != null ? [true] : []
content {
ec2_ssh_key = var.remote_access_ssh_key
source_security_group_ids = aws_security_group.remote_access[*].id
}
}
tags = merge(
lookup(each.value, "cluster_autoscaler_enabled", true) ? {
# tag the node group so that it can be auto-discovered by the cluster autoscaler
"k8s.io/cluster-autoscaler/${var.cluster_name}" = "owned",
"k8s.io/cluster-autoscaler/enabled" = lookup(each.value, "cluster_autoscaler_enabled", true),
"k8s.io/cluster-autoscaler/node-template/label/nodegroup" = each.key, # tag required for scaling to/from 0
} : {},
{ "nodegroup" = each.key },
lookup(each.value, "labels", {}),
lookup(each.value, "tags", {}),
var.tags
)
labels = merge(
{
"nodegroup" = each.key
},
lookup(each.value, "labels", {})
)
depends_on = [
aws_iam_role_policy_attachment.quortex_amazon_eks_worker_node_policy,
aws_iam_role_policy_attachment.quortex_amazon_ec2_container_registry_readonly,
kubernetes_config_map_v1_data.aws_auth
]
}
locals {
addon_irsa_service_account_arn = {
vpc-cni = try(aws_iam_role.aws_vpc_cni[0].arn, null)
aws-ebs-csi-driver = try(aws_iam_role.ebs_csi_driver[0].arn, null)
}
}
resource "aws_eks_addon" "vpc_cni_addon" {
count = var.vpc_cni_addon != null ? 1 : 0
cluster_name = aws_eks_cluster.quortex.name
addon_name = "vpc-cni"
addon_version = var.vpc_cni_addon.version
configuration_values = jsonencode(merge(local.vpc_cni_configuration_values, var.vpc_cni_addon.configuration_values))
preserve = var.vpc_cni_addon.preserve
resolve_conflicts_on_update = var.vpc_cni_addon.resolve_conflicts
resolve_conflicts_on_create = var.vpc_cni_addon.resolve_conflicts
service_account_role_arn = lookup(local.addon_irsa_service_account_arn, "vpc-cni", null)
tags = var.tags
}
# Eks addons
resource "aws_eks_addon" "quortex_addon" {
for_each = { for k, v in var.cluster_addons : k => v if k != "vpc-cni" }
cluster_name = aws_eks_cluster.quortex.name
addon_name = each.key
addon_version = each.value.version
configuration_values = try(each.value.configuration_values, null)
preserve = try(each.value.preserve, null)
resolve_conflicts_on_update = try(each.value.resolve_conflicts, "OVERWRITE")
resolve_conflicts_on_create = try(each.value.resolve_conflicts, "OVERWRITE")
service_account_role_arn = lookup(local.addon_irsa_service_account_arn, each.key, null)
tags = var.tags
}
# This AWS CLI command will add tags to the ASG created by EKS
#
# The tags specified on the resource type "aws_eks_node_group" are not propagated to the ASG that
# represents this node group (issue https://github.com/aws/containers-roadmap/issues/608).
#
# As a workaround, we add tags to the ASG after the nodegroup creation/updates using the AWS
# command-line.
#
# Thanks to the PropagateAtLaunch=true argument, these tags will also be propagated to instances
# created in this ASG.
#
# Note: existing tags on the ASGs will not be removed
resource "null_resource" "add_custom_tags_to_asg" {
count = length(local.asg_custom_tags_chunks)
triggers = {
asg_custom_tags_chunk = jsonencode(local.asg_custom_tags_chunks[count.index])
}
provisioner "local-exec" {
command = <<EOF
aws autoscaling create-or-update-tags \
--region ${data.aws_region.current.name} \
--tags \
%{for v in local.asg_custom_tags_chunks[count.index]~}
"ResourceId=${aws_eks_node_group.quortex[v.node_group]["resources"][0]["autoscaling_groups"][0]["name"]},ResourceType=auto-scaling-group,Key='${v.key}',Value='${v.tag}',PropagateAtLaunch=true" \
%{endfor~}
EOF
}
}
resource "aws_security_group" "remote_access" {
# Create this security group only if remote access is requested
count = var.remote_access_ssh_key != null ? 1 : 0
name = "${var.cluster_name}-ssh"
description = "Allow remote access (SSH)"
vpc_id = var.vpc_id
ingress {
description = "SSH access from specified IP"
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = var.remote_access_allowed_ip_ranges
}
tags = merge(
{
"Name" = "${var.cluster_name}-ssh"
},
var.tags
)
}
resource "aws_cloudwatch_log_group" "cluster_logs" {
# The log group name format is /aws/eks/<cluster-name>/cluster
# Reference: https://docs.aws.amazon.com/eks/latest/userguide/control-plane-logs.html
name = "/aws/eks/${var.cluster_name}/cluster"
retention_in_days = var.cluster_logs_retention
tags = var.tags
}