forked from OJFord/terraform-module-kubernetes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
232 lines (193 loc) · 5.47 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
locals {
masters_idx = toset(range(length(var.masters)))
masters = { for idx in local.masters_idx : idx => var.masters[idx] }
workers_idx = toset(range(length(var.workers)))
workers = { for idx in local.workers_idx : idx => var.workers[idx] }
define_cluster_ready = <<EOD
cluster_ready() {
if [ $${CLUSTER_READY:-1} -gt 0 ]; then
curl --silent 'https://${var.control_plane_endpoint}:6443' | grep '"kind": "Status"' >/dev/null
CLUSTER_READY=$?
fi
return $CLUSTER_READY
}
EOD
kubeadm_config_file = "/tmp/kubeadm-conf.yaml"
kubeadm_join_script_file = "/tmp/kubeadm-join"
# Certs paths source: https://kubernetes.io/docs/setup/best-practices/certificates
ca_cert = "/etc/kubernetes/pki/ca.crt"
certs = [
local.ca_cert,
"/etc/kubernetes/pki/ca.key",
"/etc/kubernetes/pki/etcd/ca.crt",
"/etc/kubernetes/pki/etcd/ca.key",
"/etc/kubernetes/pki/front-proxy-ca.crt",
"/etc/kubernetes/pki/front-proxy-ca.key",
"/etc/kubernetes/pki/sa.key",
"/etc/kubernetes/pki/sa.pub",
]
kubectl_taint_cmd = "kubectl --kubeconfig=/etc/kubernetes/admin.conf taint --overwrite node \"$(cat /etc/hostname)\""
}
resource "null_resource" "cluster" {
# Init on (arbitrarily) the first master.
# *Do not* trigger on changes to it,
# so we don't re-init if the node is replaced.
connection {
host = local.masters[0].ssh_host
user = local.masters[0].ssh_user
}
provisioner "file" {
content = <<EOC
apiVersion: kubeadm.k8s.io/v1beta2
kind: ClusterConfiguration
controlPlaneEndpoint: ${var.control_plane_endpoint}
networking:
podSubnet: ${var.pod_subnet}
serviceSubnet: ${var.service_subnet}
---
apiVersion: kubeadm.k8s.io/v1beta2
kind: InitConfiguration
bootstrapTokens:
- token: ${var.bootstrap_token}
localAPIEndpoint:
advertiseAddress: ${var.apiserver_advertise_address}
bindPort: 6443
EOC
destination = local.kubeadm_config_file
}
provisioner "remote-exec" {
inline = [
"kubeadm reset --force",
"kubeadm init --config=${local.kubeadm_config_file} ${join(" ", var.kubeadm_init_extra_args)}",
]
}
}
resource "null_resource" "certs" {
for_each = local.masters
triggers = {
master = each.value.id
cluster = null_resource.cluster.id
}
depends_on = [
null_resource.cluster,
]
connection {
host = each.value.ssh_host
user = each.value.ssh_user
}
provisioner "local-exec" {
command = <<EOC
joiner='${each.value.ssh_user}@${each.value.ssh_host}'
>&2 echo Seeking certs...
for source in ${join(" ", [for m in local.masters : "${m.ssh_user}@${m.ssh_host}"])}; do
if ssh "$source" test -f '${local.certs[0]}'; then
>&2 echo Found extant master with certificates
%{for f in local.certs}
ssh "$joiner" -- mkdir -p "$(dirname '${f}')"
scp -3 "$source:${f}" "$joiner:${f}"
%{endfor}
fi
done
EOC
}
provisioner "remote-exec" {
inline = [
"if grep 'Arch Linux' /usr/lib/os-release; then",
" cp '${local.ca_cert}' /etc/ca-certificates/trust-source/anchors/k8s-apiserver.crt && trust extract-compat",
"else",
" cp ${local.ca_cert} /usr/local/share/ca-certificates/k8s-apiserver.crt && update-ca-certificates",
"fi",
]
}
}
resource "null_resource" "master" {
for_each = local.masters
triggers = {
node = each.value.id
}
depends_on = [
null_resource.cluster,
null_resource.certs,
]
connection {
host = each.value.ssh_host
user = each.value.ssh_user
}
provisioner "file" {
content = <<EOC
#!/bin/sh
set -e
${local.define_cluster_ready}
while ! cluster_ready; do sleep 2; done
kubeadm join --control-plane --token='${var.bootstrap_token}' \
${join(" ", var.kubeadm_join_master_extra_args)}
EOC
destination = local.kubeadm_join_script_file
}
provisioner "remote-exec" {
inline = [
"chmod 0100 ${local.kubeadm_join_script_file}",
local.kubeadm_join_script_file,
"rm ${local.kubeadm_join_script_file}",
]
}
}
resource "null_resource" "worker" {
for_each = local.workers
triggers = {
node = each.value.id
}
depends_on = [
null_resource.cluster,
]
connection {
host = each.value.ssh_host
user = each.value.ssh_user
}
provisioner "file" {
content = <<EOC
#!/bin/sh
set -e
${local.define_cluster_ready}
while ! cluster_ready; do sleep 2; done
kubeadm join --token='${var.bootstrap_token}' \
${join(" ", var.kubeadm_join_worker_extra_args)}
EOC
destination = local.kubeadm_join_script_file
}
provisioner "remote-exec" {
inline = [
"kubeadm reset --force",
"chmod 0100 ${local.kubeadm_join_script_file}",
local.kubeadm_join_script_file,
"rm ${local.kubeadm_join_script_file}",
]
}
}
module "kubeconfig" {
source = "OJFord/ssh_file/module"
connection = {
host = local.masters[0].ssh_host
user = local.masters[0].ssh_user
}
filename = "/etc/kubernetes/admin.conf"
}
resource "null_resource" "master_taints" {
for_each = local.masters
depends_on = [
null_resource.master,
]
triggers = {
node = each.value.id
taint = each.value.no_schedule
}
connection {
host = each.value.ssh_host
user = each.value.ssh_user
}
provisioner "remote-exec" {
inline = [
"${local.kubectl_taint_cmd} node-role.kubernetes.io/master${each.value.no_schedule ? "=:NoSchedule" : ":NoSchedule-"}",
]
}
}