This repository has been archived by the owner on Jan 5, 2022. It is now read-only.
forked from poseidon/typhoon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontrollers.tf
167 lines (140 loc) · 4.55 KB
/
controllers.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
# Discrete DNS records for each controller's private IPv4 for etcd usage
resource "aws_route53_record" "etcds" {
count = var.controller_count
# DNS Zone where record should be created
zone_id = var.dns_zone_id
name = format("%s-etcd%d.%s.", var.cluster_name, count.index, var.dns_zone)
type = "A"
ttl = 300
# private IPv4 address for etcd
records = [aws_instance.controllers.*.private_ip[count.index]]
}
# Controller instances
resource "aws_instance" "controllers" {
count = var.controller_count
tags = map(
"Name", "${var.cluster_name}-controller-${count.index}",
"kubernetes.io/cluster/${var.cluster_name}", "owned"
)
instance_type = var.controller_type
ami = coalesce(var.ami, data.aws_ami.fedora-coreos.image_id)
iam_instance_profile = aws_iam_instance_profile.controller.name
user_data = <<EOF
{
"ignition": {
"version": "3.1.0",
"config": {
"merge": [
{
"source": "${var.base_ignition_config_path}"
},
{
"source": "s3://${aws_s3_bucket.ignition_configs.id}/${aws_s3_bucket_object.controller-ignitions.*.id[count.index]}"
}
]
}
}
}
EOF
# storage
root_block_device {
volume_type = var.disk_type
volume_size = var.disk_size
iops = var.disk_iops
encrypted = true
}
# network
subnet_id = element(aws_subnet.private.*.id, count.index)
vpc_security_group_ids = [
aws_security_group.controller.id,
aws_security_group.bastion_internal.id
]
lifecycle {
ignore_changes = [
ami,
user_data,
]
}
}
resource "aws_s3_bucket_object" "controller-ignitions" {
count = var.controller_count
bucket = aws_s3_bucket.ignition_configs.id
key = "controllers/${count.index}.json"
content = data.ct_config.controller-ignitions.*.rendered[count.index]
}
# Controller Ignition configs
data "ct_config" "controller-ignitions" {
count = var.controller_count
content = data.template_file.controller-configs.*.rendered[count.index]
strict = true
snippets = var.controller_snippets
}
# Controller Fedora CoreOS configs
data "template_file" "controller-configs" {
count = var.controller_count
template = file("${path.module}/fcc/controller.yaml")
vars = {
# Cannot use cyclic dependencies on controllers or their DNS records
etcd_name = "etcd${count.index}"
etcd_domain = "${var.cluster_name}-etcd${count.index}.${var.dns_zone}"
# etcd0=https://cluster-etcd0.example.com,etcd1=https://cluster-etcd1.example.com,...
etcd_initial_cluster = join(",", data.template_file.etcds.*.rendered)
kubeconfig = indent(10, module.bootstrap.kubeconfig-kubelet)
cluster_dns_service_ip = cidrhost(var.service_cidr, 10)
cluster_domain_suffix = var.cluster_domain_suffix
}
}
data "template_file" "etcds" {
count = var.controller_count
template = "etcd$${index}=https://$${cluster_name}-etcd$${index}.$${dns_zone}:2380"
vars = {
index = count.index
cluster_name = var.cluster_name
dns_zone = var.dns_zone
}
}
resource "aws_iam_role_policy" "controller_read_base_ignition_config" {
name = "read-base-ignition-config"
role = aws_iam_role.controller.id
policy = var.base_ignition_config_read_policy
}
resource "aws_iam_role_policy" "controller_read_ignition_configs" {
name = "read-ignition-configs"
role = aws_iam_role.controller.id
policy = data.aws_iam_policy_document.controller_read_ignition_configs.json
}
data "aws_iam_policy_document" "controller_read_ignition_configs" {
statement {
effect = "Allow"
actions = ["s3:GetObject"]
resources = formatlist("${aws_s3_bucket.ignition_configs.arn}/%s", aws_s3_bucket_object.controller-ignitions.*.id)
}
}
resource "aws_iam_role_policy" "controller_instance_read_ec2" {
name = "instance-read-ec2"
role = aws_iam_role.controller.id
policy = data.aws_iam_policy_document.controller_instance_read_ec2.json
}
data "aws_iam_policy_document" "controller_instance_read_ec2" {
statement {
actions = ["ec2:Describe*"]
resources = ["*"]
}
}
data "aws_iam_policy_document" "controller_assume_role" {
statement {
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["ec2.amazonaws.com"]
}
}
}
resource "aws_iam_role" "controller" {
name = "${var.cluster_name}-controller"
assume_role_policy = data.aws_iam_policy_document.controller_assume_role.json
}
resource "aws_iam_instance_profile" "controller" {
name = "${var.cluster_name}-controller"
role = aws_iam_role.controller.id
}