-
Notifications
You must be signed in to change notification settings - Fork 28
/
privatek8s.tf
430 lines (380 loc) · 16.6 KB
/
privatek8s.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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
resource "azurerm_resource_group" "privatek8s" {
name = "prod-privatek8s"
location = var.location
tags = local.default_tags
}
resource "random_pet" "suffix_privatek8s" {
# You want to taint this resource in order to get a new pet
}
data "azurerm_subnet" "privatek8s_tier" {
name = "privatek8s-tier"
resource_group_name = data.azurerm_resource_group.private.name
virtual_network_name = data.azurerm_virtual_network.private.name
}
data "azurerm_subnet" "privatek8s_release_tier" {
name = "privatek8s-release-tier"
resource_group_name = data.azurerm_resource_group.private.name
virtual_network_name = data.azurerm_virtual_network.private.name
}
data "azurerm_subnet" "privatek8s_infra_ci_controller_tier" {
name = "privatek8s-infraci-ctrl-tier"
resource_group_name = data.azurerm_resource_group.private.name
virtual_network_name = data.azurerm_virtual_network.private.name
}
data "azurerm_subnet" "privatek8s_release_ci_controller_tier" {
name = "privatek8s-releaseci-ctrl-tier"
resource_group_name = data.azurerm_resource_group.private.name
virtual_network_name = data.azurerm_virtual_network.private.name
}
#trivy:ignore:azure-container-logging #trivy:ignore:azure-container-limit-authorized-ips
resource "azurerm_kubernetes_cluster" "privatek8s" {
name = "privatek8s-${random_pet.suffix_privatek8s.id}"
location = azurerm_resource_group.privatek8s.location
resource_group_name = azurerm_resource_group.privatek8s.name
kubernetes_version = local.kubernetes_versions["privatek8s"]
dns_prefix = "privatek8s-${random_pet.suffix_privatek8s.id}"
role_based_access_control_enabled = true # default value but made explicit to please trivy
api_server_access_profile {
authorized_ip_ranges = setunion(
formatlist(
"%s/32",
flatten(
concat(
[for key, value in module.jenkins_infra_shared_data.admin_public_ips : value],
# privatek8s outbound IPs (traffic routed through gateways or outbound LBs)
module.jenkins_infra_shared_data.outbound_ips["privatek8s.jenkins.io"],
)
)
),
data.azurerm_subnet.private_vnet_data_tier.address_prefixes,
)
}
network_profile {
network_plugin = "azure"
network_policy = "azure"
outbound_type = "loadBalancer"
load_balancer_sku = "standard"
load_balancer_profile {
outbound_ports_allocated = "1088" # Max 58 Nodes, <64000 total
idle_timeout_in_minutes = "4"
managed_outbound_ip_count = "1"
}
}
default_node_pool {
name = "syspool"
vm_size = "Standard_D2as_v4"
os_sku = "Ubuntu"
os_disk_type = "Ephemeral"
os_disk_size_gb = 50 # Ref. Cache storage size at https://learn.microsoft.com/en-us/azure/virtual-machines/dav4-dasv4-series#dasv4-series (depends on the instance size)
orchestrator_version = local.kubernetes_versions["privatek8s"]
kubelet_disk_type = "OS"
enable_auto_scaling = true
min_count = 1
max_count = 3
vnet_subnet_id = data.azurerm_subnet.privatek8s_tier.id
tags = local.default_tags
zones = [3]
}
identity {
type = "SystemAssigned"
}
lifecycle {
ignore_changes = [default_node_pool[0].node_count]
}
tags = local.default_tags
}
resource "azurerm_kubernetes_cluster_node_pool" "linuxpool" {
name = "linuxpool"
vm_size = "Standard_D4s_v3"
os_disk_type = "Ephemeral"
os_disk_size_gb = 100 # Ref. Cache storage size at https://learn.microsoft.com/en-us/azure/virtual-machines/dv3-dsv3-series#dsv3-series (depends on the instance size)
orchestrator_version = local.kubernetes_versions["privatek8s"]
kubernetes_cluster_id = azurerm_kubernetes_cluster.privatek8s.id
enable_auto_scaling = true
min_count = 0
max_count = 5
zones = [3]
vnet_subnet_id = data.azurerm_subnet.privatek8s_tier.id
lifecycle {
ignore_changes = [node_count]
}
tags = local.default_tags
}
resource "azurerm_kubernetes_cluster_node_pool" "infracipool" {
name = "infracipool"
vm_size = "Standard_D8s_v3"
os_disk_type = "Ephemeral"
os_disk_size_gb = 200 # Ref. Cache storage size at https://learn.microsoft.com/en-us/azure/virtual-machines/dv3-dsv3-series#dsv3-series (depends on the instance size)
orchestrator_version = local.kubernetes_versions["privatek8s"]
kubernetes_cluster_id = azurerm_kubernetes_cluster.privatek8s.id
enable_auto_scaling = true
min_count = 0
max_count = 20
zones = [3]
vnet_subnet_id = data.azurerm_subnet.privatek8s_tier.id
# Spot instances
priority = "Spot"
eviction_policy = "Delete"
spot_max_price = "-1" # in $, -1 = On demand pricing
# Note: label and taint added automatically when in "Spot" priority, putting it here to explicit them
node_labels = {
"kubernetes.azure.com/scalesetpriority" = "spot"
}
node_taints = [
"jenkins=infra.ci.jenkins.io:NoSchedule",
"kubernetes.azure.com/scalesetpriority=spot:NoSchedule",
]
lifecycle {
ignore_changes = [node_count]
}
tags = local.default_tags
}
resource "azurerm_kubernetes_cluster_node_pool" "infraciarm64" {
name = "arm64small"
vm_size = "Standard_D4pds_v5" # 4 vCPU, 16 GB RAM, local disk: 150 GB and 19000 IOPS
os_disk_type = "Ephemeral"
os_disk_size_gb = 150 # Ref. Cache storage size at https://learn.microsoft.com/en-us/azure/virtual-machines/dpsv5-dpdsv5-series#dpdsv5-series (depends on the instance size)
orchestrator_version = local.kubernetes_versions["privatek8s"]
kubernetes_cluster_id = azurerm_kubernetes_cluster.privatek8s.id
enable_auto_scaling = true
min_count = 1
max_count = 10
zones = [1] # Linux arm64 VMs are only available in the Zone 1 in this region (undocumented by Azure)
vnet_subnet_id = data.azurerm_subnet.privatek8s_tier.id
# Spot instances
priority = "Spot"
eviction_policy = "Delete"
spot_max_price = "-1" # in $, -1 = On demand pricing
# Note: label and taint added automatically when in "Spot" priority, putting it here to explicit them
node_labels = {
"kubernetes.azure.com/scalesetpriority" = "spot"
}
node_taints = [
"jenkins=infra.ci.jenkins.io:NoSchedule",
"kubernetes.azure.com/scalesetpriority=spot:NoSchedule",
]
lifecycle {
ignore_changes = [node_count]
}
tags = local.default_tags
}
# nodepool dedicated for the infra.ci.jenkins.io controller
resource "azurerm_kubernetes_cluster_node_pool" "infraci_controller" {
name = "infracictrl"
vm_size = "Standard_D4pds_v5" # 4 vCPU, 16 GB RAM, local disk: 150 GB and 19000 IOPS
os_sku = "AzureLinux"
os_disk_type = "Ephemeral"
os_disk_size_gb = 150 # Ref. Cache storage size at https://learn.microsoft.com/en-us/azure/virtual-machines/dpsv5-dpdsv5-series#dpdsv5-series (depends on the instance size)
orchestrator_version = local.kubernetes_versions["privatek8s"]
kubernetes_cluster_id = azurerm_kubernetes_cluster.privatek8s.id
enable_auto_scaling = true
min_count = 1
max_count = 2
zones = [1] # Linux arm64 VMs are only available in the Zone 1 in this region (undocumented by Azure)
vnet_subnet_id = data.azurerm_subnet.privatek8s_infra_ci_controller_tier.id
node_taints = [
"jenkins=infra.ci.jenkins.io:NoSchedule",
"jenkins-component=controller:NoSchedule"
]
lifecycle {
ignore_changes = [node_count]
}
tags = local.default_tags
}
# nodepool dedicated for the release.ci.jenkins.io controller
resource "azurerm_kubernetes_cluster_node_pool" "releaseci_controller" {
name = "releacictrl"
vm_size = "Standard_D4pds_v5" # 4 vCPU, 16 GB RAM, local disk: 150 GB and 19000 IOPS
os_sku = "AzureLinux"
os_disk_type = "Ephemeral"
os_disk_size_gb = 150 # Ref. Cache storage size at https://learn.microsoft.com/en-us/azure/virtual-machines/dpsv5-dpdsv5-series#dpdsv5-series (depends on the instance size)
orchestrator_version = local.kubernetes_versions["privatek8s"]
kubernetes_cluster_id = azurerm_kubernetes_cluster.privatek8s.id
enable_auto_scaling = true
min_count = 1
max_count = 2
zones = [1] # Linux arm64 VMs are only available in the Zone 1 in this region (undocumented by Azure)
vnet_subnet_id = data.azurerm_subnet.privatek8s_release_ci_controller_tier.id
node_taints = [
"jenkins=release.ci.jenkins.io:NoSchedule",
"jenkins-component=controller:NoSchedule"
]
lifecycle {
ignore_changes = [node_count]
}
tags = local.default_tags
}
resource "azurerm_kubernetes_cluster_node_pool" "releasepool" {
name = "releasepool"
vm_size = "Standard_D8s_v3" # 8 vCPU 32 GiB RAM
os_disk_type = "Ephemeral"
os_disk_size_gb = 200 # Ref. Cache storage size at https://learn.microsoft.com/en-us/azure/virtual-machines/dv3-dsv3-series#dsv3-series (depends on the instance size)
orchestrator_version = local.kubernetes_versions["privatek8s"]
kubernetes_cluster_id = azurerm_kubernetes_cluster.privatek8s.id
enable_auto_scaling = true
min_count = 0
max_count = 3
zones = [3]
vnet_subnet_id = data.azurerm_subnet.privatek8s_release_tier.id
node_taints = [
"jenkins=release.ci.jenkins.io:NoSchedule",
]
lifecycle {
ignore_changes = [node_count]
}
tags = local.default_tags
}
resource "azurerm_kubernetes_cluster_node_pool" "windows2019pool" {
name = "w2019"
vm_size = "Standard_D4s_v3" # 4 vCPU 16 GiB RAM
os_disk_type = "Ephemeral"
os_disk_size_gb = 100 # Ref. Cache storage size at https://learn.microsoft.com/en-us/azure/virtual-machines/dv3-dsv3-series#dsv3-series (depends on the instance size)
orchestrator_version = local.kubernetes_versions["privatek8s"]
os_type = "Windows"
os_sku = "Windows2019"
kubernetes_cluster_id = azurerm_kubernetes_cluster.privatek8s.id
enable_auto_scaling = true
min_count = 0
max_count = 3
zones = [3]
vnet_subnet_id = data.azurerm_subnet.privatek8s_release_tier.id
node_taints = [
"os=windows:NoSchedule",
"jenkins=release.ci.jenkins.io:NoSchedule",
]
lifecycle {
ignore_changes = [node_count]
}
tags = local.default_tags
}
# Allow cluster to manage LBs in the privatek8s-tier subnet (Public LB)
resource "azurerm_role_assignment" "privatek8s_networkcontributor" {
scope = data.azurerm_subnet.privatek8s_tier.id
role_definition_name = "Network Contributor"
principal_id = azurerm_kubernetes_cluster.privatek8s.identity[0].principal_id
skip_service_principal_aad_check = true
}
# Allow cluster to manage LBs in the data-tier subnet (internal LBs)
resource "azurerm_role_assignment" "datatier_networkcontributor" {
scope = data.azurerm_subnet.private_vnet_data_tier.id
role_definition_name = "Network Contributor"
principal_id = azurerm_kubernetes_cluster.privatek8s.identity[0].principal_id
skip_service_principal_aad_check = true
}
# Allow cluster to manage LBs in the data-tier subnet (internal LBs)
resource "azurerm_role_assignment" "publicip_networkcontributor" {
scope = azurerm_public_ip.public_privatek8s.id
role_definition_name = "Network Contributor"
principal_id = azurerm_kubernetes_cluster.privatek8s.identity[0].principal_id
skip_service_principal_aad_check = true
}
# Allow cluster to manage get.jenkins.io storage account
resource "azurerm_role_assignment" "getjenkinsio_storage_account_contributor" {
scope = azurerm_storage_account.get_jenkins_io.id
role_definition_name = "Storage Account Contributor"
principal_id = azurerm_kubernetes_cluster.privatek8s.identity[0].principal_id
skip_service_principal_aad_check = true
}
resource "kubernetes_storage_class" "managed_csi_premium_retain" {
metadata {
name = "managed-csi-premium-retain"
}
storage_provisioner = "disk.csi.azure.com"
reclaim_policy = "Retain"
parameters = {
skuname = "Premium_LRS"
}
provider = kubernetes.privatek8s
}
resource "kubernetes_storage_class" "azurefile_csi_premium_retain" {
metadata {
name = "azurefile-csi-premium-retain"
}
storage_provisioner = "file.csi.azure.com"
reclaim_policy = "Retain"
parameters = {
skuname = "Premium_LRS"
}
mount_options = ["dir_mode=0777", "file_mode=0777", "uid=1000", "gid=1000", "mfsymlinks", "nobrl"]
provider = kubernetes.privatek8s
}
resource "kubernetes_storage_class" "managed_csi_premium_ZRS_retain_private" {
metadata {
name = "managed-csi-premium-zrs-retain"
}
storage_provisioner = "disk.csi.azure.com"
reclaim_policy = "Retain"
parameters = {
skuname = "Premium_ZRS"
}
provider = kubernetes.privatek8s
allow_volume_expansion = true
}
# https://learn.microsoft.com/en-us/java/api/com.microsoft.azure.management.storage.skuname?view=azure-java-legacy#field-summary
resource "kubernetes_storage_class" "managed_csi_standard_ZRS_retain_private" {
metadata {
name = "managed-csi-standard-zrs-retain"
}
storage_provisioner = "disk.csi.azure.com"
reclaim_policy = "Retain"
parameters = {
skuname = " Standard_ZRS"
}
provider = kubernetes.privatek8s
allow_volume_expansion = true
}
# Used later by the load balancer deployed on the cluster, see https://github.com/jenkins-infra/kubernetes-management/config/privatek8s.yaml
resource "azurerm_public_ip" "public_privatek8s" {
name = "public-privatek8s"
resource_group_name = azurerm_resource_group.prod_public_ips.name
location = var.location
allocation_method = "Static"
sku = "Standard" # Needed to fix the error "PublicIPAndLBSkuDoNotMatch"
tags = local.default_tags
}
resource "azurerm_management_lock" "public_privatek8s_publicip" {
name = "public-privatek8s-publicip"
scope = azurerm_public_ip.public_privatek8s.id
lock_level = "CanNotDelete"
notes = "Locked because this is a sensitive resource that should not be removed when privatek8s is removed"
}
resource "azurerm_dns_a_record" "public_privatek8s" {
name = "public.privatek8s"
zone_name = data.azurerm_dns_zone.jenkinsio.name
resource_group_name = data.azurerm_resource_group.proddns_jenkinsio.name
ttl = 300
records = [azurerm_public_ip.public_privatek8s.ip_address]
tags = local.default_tags
}
resource "azurerm_dns_a_record" "private_privatek8s" {
name = "private.privatek8s"
zone_name = data.azurerm_dns_zone.jenkinsio.name
resource_group_name = data.azurerm_resource_group.proddns_jenkinsio.name
ttl = 300
records = ["10.248.1.5"] # External IP of the private-nginx ingress LoadBalancer, created by https://github.com/jenkins-infra/kubernetes-management/blob/54a0d4aa72b15f4236abcfbde00a080905bbb890/clusters/privatek8s.yaml#L112-L118
tags = local.default_tags
}
output "privatek8s_kube_config" {
value = azurerm_kubernetes_cluster.privatek8s.kube_config_raw
sensitive = true
}
output "privatek8s_public_ip_address" {
value = azurerm_public_ip.public_privatek8s.ip_address
}
# Configure the jenkins-infra/kubernetes-management admin service account
module "privatek8s_admin_sa" {
providers = {
kubernetes = kubernetes.privatek8s
}
source = "./.shared-tools/terraform/modules/kubernetes-admin-sa"
cluster_name = azurerm_kubernetes_cluster.privatek8s.name
cluster_hostname = azurerm_kubernetes_cluster.privatek8s.kube_config.0.host
cluster_ca_certificate_b64 = azurerm_kubernetes_cluster.privatek8s.kube_config.0.cluster_ca_certificate
}
output "kubeconfig_privatek8s" {
sensitive = true
value = module.privatek8s_admin_sa.kubeconfig
}
output "privatek8s_kube_config_command" {
value = "az aks get-credentials --name ${azurerm_kubernetes_cluster.privatek8s.name} --resource-group ${azurerm_kubernetes_cluster.privatek8s.resource_group_name}"
}