forked from siderolabs/contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
407 lines (353 loc) · 13.5 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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
locals {
common_machine_config_patch = {
machine = {
kubelet = {
registerWithFQDN = true
}
}
}
ccm_patch_cp = {
cluster = {
externalCloudProvider = {
enabled = true
manifests = [
"https://raw.githubusercontent.com/siderolabs/contrib/main/examples/terraform/aws/manifests/ccm.yaml"
]
}
}
}
ccm_patch_worker = {
cluster = {
externalCloudProvider = {
enabled = true
}
}
}
config_patches_common = [
for path in var.config_patch_files : file(path)
]
config_patches_controlplane = var.ccm ? [yamlencode(local.ccm_patch_cp)] : []
config_patches_worker = var.ccm ? [yamlencode(local.ccm_patch_worker)] : []
cluster_required_tags = {
"kubernetes.io/cluster/${var.cluster_name}" = "owned"
}
}
data "aws_availability_zones" "available" {
state = "available"
}
data "aws_ami" "talos" {
owners = ["540036508848"] # Sidero Labs
most_recent = true
name_regex = "^talos-v\\d+\\.\\d+\\.\\d+-${data.aws_availability_zones.available.id}-amd64$"
}
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "~> 3.0"
name = var.cluster_name
cidr = var.vpc_cidr
tags = var.extra_tags
# lets pick utmost three AZ's since the CIDR bit is 2
azs = slice(data.aws_availability_zones.available.names, 0, 3)
public_subnets = [for i, v in slice(data.aws_availability_zones.available.names, 0, 3) : cidrsubnet(var.vpc_cidr, 2, i)]
}
module "cluster_sg" {
source = "terraform-aws-modules/security-group/aws"
version = "~> 4.0"
name = var.cluster_name
description = "Allow all intra-cluster and egress traffic"
vpc_id = module.vpc.vpc_id
tags = var.extra_tags
ingress_with_self = [
{
rule = "all-all"
},
]
ingress_with_cidr_blocks = [
{
from_port = 50000
to_port = 50000
protocol = "tcp"
cidr_blocks = var.talos_api_allowed_cidr
description = "Talos API Access"
},
]
egress_with_cidr_blocks = [
{
rule = "all-all"
cidr_blocks = "0.0.0.0/0"
},
]
}
module "kubernetes_api_sg" {
source = "terraform-aws-modules/security-group/aws//modules/https-443"
version = "~> 4.0"
name = "${var.cluster_name}-k8s-api"
description = "Allow access to the Kubernetes API"
vpc_id = module.vpc.vpc_id
ingress_cidr_blocks = [var.kubernetes_api_allowed_cidr]
tags = var.extra_tags
}
module "elb_k8s_elb" {
source = "terraform-aws-modules/elb/aws"
version = "~> 4.0"
name = substr("${var.cluster_name}-k8s-api", 0, 32)
subnets = module.vpc.public_subnets
tags = merge(var.extra_tags, local.cluster_required_tags)
security_groups = [
module.cluster_sg.security_group_id,
module.kubernetes_api_sg.security_group_id,
]
listener = [
{
lb_port = 443
lb_protocol = "tcp"
instance_port = 6443
instance_protocol = "tcp"
},
]
health_check = {
target = "tcp:6443"
interval = 30
healthy_threshold = 2
unhealthy_threshold = 2
timeout = 5
}
number_of_instances = var.control_plane.num_instances
instances = module.talos_control_plane_nodes.*.id
}
# https://cloud-provider-aws.sigs.k8s.io/prerequisites/
resource "aws_iam_policy" "control_plane_ccm_policy" {
count = var.ccm ? 1 : 0
name = "${var.cluster_name}-control-plane-ccm-policy"
path = "/"
description = "IAM policy for the control plane nodes to allow CCM to manage AWS resources"
policy = jsonencode(
{
Version = "2012-10-17",
Statement = [
{
Effect = "Allow",
Action = [
"autoscaling:DescribeAutoScalingGroups",
"autoscaling:DescribeLaunchConfigurations",
"autoscaling:DescribeTags",
"ec2:DescribeInstances",
"ec2:DescribeRegions",
"ec2:DescribeRouteTables",
"ec2:DescribeSecurityGroups",
"ec2:DescribeSubnets",
"ec2:DescribeVolumes",
"ec2:DescribeAvailabilityZones",
"ec2:CreateSecurityGroup",
"ec2:CreateTags",
"ec2:CreateVolume",
"ec2:ModifyInstanceAttribute",
"ec2:ModifyVolume",
"ec2:AttachVolume",
"ec2:AuthorizeSecurityGroupIngress",
"ec2:CreateRoute",
"ec2:DeleteRoute",
"ec2:DeleteSecurityGroup",
"ec2:DeleteVolume",
"ec2:DetachVolume",
"ec2:RevokeSecurityGroupIngress",
"ec2:DescribeVpcs",
"elasticloadbalancing:AddTags",
"elasticloadbalancing:AttachLoadBalancerToSubnets",
"elasticloadbalancing:ApplySecurityGroupsToLoadBalancer",
"elasticloadbalancing:CreateLoadBalancer",
"elasticloadbalancing:CreateLoadBalancerPolicy",
"elasticloadbalancing:CreateLoadBalancerListeners",
"elasticloadbalancing:ConfigureHealthCheck",
"elasticloadbalancing:DeleteLoadBalancer",
"elasticloadbalancing:DeleteLoadBalancerListeners",
"elasticloadbalancing:DescribeLoadBalancers",
"elasticloadbalancing:DescribeLoadBalancerAttributes",
"elasticloadbalancing:DetachLoadBalancerFromSubnets",
"elasticloadbalancing:DeregisterInstancesFromLoadBalancer",
"elasticloadbalancing:ModifyLoadBalancerAttributes",
"elasticloadbalancing:RegisterInstancesWithLoadBalancer",
"elasticloadbalancing:SetLoadBalancerPoliciesForBackendServer",
"elasticloadbalancing:AddTags",
"elasticloadbalancing:CreateListener",
"elasticloadbalancing:CreateTargetGroup",
"elasticloadbalancing:DeleteListener",
"elasticloadbalancing:DeleteTargetGroup",
"elasticloadbalancing:DescribeListeners",
"elasticloadbalancing:DescribeLoadBalancerPolicies",
"elasticloadbalancing:DescribeTargetGroups",
"elasticloadbalancing:DescribeTargetHealth",
"elasticloadbalancing:ModifyListener",
"elasticloadbalancing:ModifyTargetGroup",
"elasticloadbalancing:RegisterTargets",
"elasticloadbalancing:DeregisterTargets",
"elasticloadbalancing:SetLoadBalancerPoliciesOfListener",
"iam:CreateServiceLinkedRole",
"kms:DescribeKey"
],
Resource = [
"*"
]
}
]
}
)
}
# https://cloud-provider-aws.sigs.k8s.io/prerequisites/
resource "aws_iam_policy" "worker_ccm_policy" {
count = var.ccm ? 1 : 0
name = "${var.cluster_name}-worker-ccm-policy"
path = "/"
description = "IAM policy for the worker nodes to allow CCM to manage AWS resources"
policy = jsonencode(
{
Version : "2012-10-17",
Statement : [
{
Effect : "Allow",
Action : [
"ec2:DescribeInstances",
"ec2:DescribeRegions",
"ecr:GetAuthorizationToken",
"ecr:BatchCheckLayerAvailability",
"ecr:GetDownloadUrlForLayer",
"ecr:GetRepositoryPolicy",
"ecr:DescribeRepositories",
"ecr:ListImages",
"ecr:BatchGetImage"
],
Resource = "*"
}
]
})
}
module "talos_control_plane_nodes" {
source = "terraform-aws-modules/ec2-instance/aws"
version = "~> 4.0"
count = var.control_plane.num_instances
name = "${var.cluster_name}-control-plane-${count.index}"
ami = var.control_plane.ami_id == null ? data.aws_ami.talos.id : var.control_plane.ami_id
monitoring = true
instance_type = var.control_plane.instance_type
subnet_id = element(module.vpc.public_subnets, count.index)
iam_role_use_name_prefix = false
create_iam_instance_profile = var.ccm ? true : false
iam_role_policies = var.ccm ? {
"${var.cluster_name}-control-plane-ccm-policy" : aws_iam_policy.control_plane_ccm_policy[0].arn,
} : {}
tags = merge(var.extra_tags, var.control_plane.tags, local.cluster_required_tags)
vpc_security_group_ids = [module.cluster_sg.security_group_id]
root_block_device = [
{
volume_size = 100
}
]
}
module "talos_worker_group" {
source = "terraform-aws-modules/ec2-instance/aws"
version = "~> 4.0"
for_each = merge([for info in var.worker_groups : { for index in range(0, info.num_instances) : "${info.name}.${index}" => info }]...)
name = "${var.cluster_name}-worker-group-${each.value.name}-${trimprefix(each.key, "${each.value.name}.")}"
ami = each.value.ami_id == null ? data.aws_ami.talos.id : each.value.ami_id
monitoring = true
instance_type = each.value.instance_type
subnet_id = element(module.vpc.public_subnets, tonumber(trimprefix(each.key, "${each.value.name}.")))
iam_role_use_name_prefix = false
create_iam_instance_profile = var.ccm ? true : false
iam_role_policies = var.ccm ? {
"${var.cluster_name}-worker-ccm-policy" : aws_iam_policy.worker_ccm_policy[0].arn,
} : {}
tags = merge(each.value.tags, var.extra_tags, local.cluster_required_tags)
vpc_security_group_ids = [module.cluster_sg.security_group_id]
root_block_device = [
{
volume_size = 100
}
]
}
resource "talos_machine_secrets" "this" {}
data "talos_machine_configuration" "controlplane" {
cluster_name = var.cluster_name
cluster_endpoint = "https://${module.elb_k8s_elb.elb_dns_name}"
machine_type = "controlplane"
machine_secrets = talos_machine_secrets.this.machine_secrets
talos_version = var.talos_version_contract
kubernetes_version = var.kubernetes_version
docs = false
examples = false
config_patches = concat(
local.config_patches_common,
local.config_patches_controlplane,
[yamlencode(local.common_machine_config_patch)],
[for path in var.control_plane.config_patch_files : file(path)]
)
}
data "talos_machine_configuration" "worker_group" {
for_each = merge([for info in var.worker_groups : { "${info.name}" = info }]...)
cluster_name = var.cluster_name
cluster_endpoint = "https://${module.elb_k8s_elb.elb_dns_name}"
machine_type = "worker"
machine_secrets = talos_machine_secrets.this.machine_secrets
talos_version = var.talos_version_contract
kubernetes_version = var.kubernetes_version
docs = false
examples = false
config_patches = concat(
local.config_patches_common,
local.config_patches_worker,
[yamlencode(local.common_machine_config_patch)],
[for path in each.value.config_patch_files : file(path)]
)
}
resource "talos_machine_configuration_apply" "controlplane" {
count = var.control_plane.num_instances
client_configuration = talos_machine_secrets.this.client_configuration
machine_configuration_input = data.talos_machine_configuration.controlplane.machine_configuration
endpoint = module.talos_control_plane_nodes[count.index].public_ip
node = module.talos_control_plane_nodes[count.index].private_ip
}
resource "talos_machine_configuration_apply" "worker_group" {
for_each = merge([
for info in var.worker_groups : {
for index in range(0, info.num_instances) :
"${info.name}.${index}" => {
name = info.name,
public_ip = module.talos_worker_group["${info.name}.${index}"].public_ip,
private_ip = module.talos_worker_group["${info.name}.${index}"].private_ip
}
}
]...)
client_configuration = talos_machine_secrets.this.client_configuration
machine_configuration_input = data.talos_machine_configuration.worker_group[each.value.name].machine_configuration
endpoint = module.talos_worker_group[each.key].public_ip
node = module.talos_worker_group[each.key].private_ip
}
resource "talos_machine_bootstrap" "this" {
depends_on = [talos_machine_configuration_apply.controlplane]
client_configuration = talos_machine_secrets.this.client_configuration
endpoint = module.talos_control_plane_nodes.0.public_ip
node = module.talos_control_plane_nodes.0.private_ip
}
data "talos_client_configuration" "this" {
cluster_name = var.cluster_name
client_configuration = talos_machine_secrets.this.client_configuration
endpoints = module.talos_control_plane_nodes.*.public_ip
nodes = flatten([module.talos_control_plane_nodes.*.public_ip, flatten([for node in module.talos_worker_group : node.private_ip])])
}
resource "talos_cluster_kubeconfig" "this" {
depends_on = [talos_machine_bootstrap.this]
client_configuration = talos_machine_secrets.this.client_configuration
endpoint = module.talos_control_plane_nodes.0.public_ip
node = module.talos_control_plane_nodes.0.private_ip
}
data "talos_cluster_health" "this" {
depends_on = [
talos_machine_configuration_apply.controlplane,
talos_machine_configuration_apply.worker_group,
talos_cluster_kubeconfig.this
]
client_configuration = talos_machine_secrets.this.client_configuration
endpoints = module.talos_control_plane_nodes.*.public_ip
control_plane_nodes = module.talos_control_plane_nodes.*.private_ip
worker_nodes = [for node in module.talos_worker_group : node.private_ip]
}