Repository navigation
Expand file tree
/
Copy pathnetwork.tf
More file actions
406 lines (354 loc) · 12.2 KB
/
Copy pathnetwork.tf
File metadata and controls
406 lines (354 loc) · 12.2 KB
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
## Copyright © 2022, Oracle and/or its affiliates.
## All rights reserved. The Universal Permissive License (UPL), Version 1.0 as shown at http://oss.oracle.com/licenses/upl
locals {
subnet_cidrs = cidrsubnets(var.vcn_cidr, 12, 8, 4, 4, 4) # API + 1 LB + 3 node pools
api_subnet_cidr = element(local.subnet_cidrs, 0)
lb_subnet_cidr = element(local.subnet_cidrs, 1)
node_pool_subnets_cidrs = slice(local.subnet_cidrs, 2, 5)
ADs = data.oci_identity_availability_domains.ADs.availability_domains.*.name
}
resource "oci_core_vcn" "oke_vcn" {
count = var.use_existing_vcn ? 0 : 1
cidr_blocks = [var.vcn_cidr]
compartment_id = var.vcn_compartment_id
dns_label = "vcn${random_string.deploy_id.result}"
display_name = "vcn${random_string.deploy_id.result}"
defined_tags = var.vcn_tags
}
resource "oci_core_service_gateway" "oke_sg" {
count = var.use_existing_vcn ? 0 : 1
compartment_id = var.vcn_compartment_id
display_name = "Service Gateway for vcn${random_string.deploy_id.result}"
vcn_id = oci_core_vcn.oke_vcn[0].id
services {
service_id = lookup(data.oci_core_services.all_oci_services[0].services[0], "id")
}
defined_tags = var.vcn_tags
}
resource "oci_core_nat_gateway" "oke_natgw" {
count = var.use_existing_vcn ? 0 : 1
compartment_id = var.vcn_compartment_id
display_name = "NAT Gateway for vcn${random_string.deploy_id.result}"
vcn_id = oci_core_vcn.oke_vcn[0].id
defined_tags = var.vcn_tags
}
resource "oci_core_route_table" "oke_rt_via_natgw_and_sg" {
count = var.use_existing_vcn ? 0 : 1
compartment_id = var.vcn_compartment_id
vcn_id = oci_core_vcn.oke_vcn[0].id
display_name = "via NAT Gateway"
defined_tags = var.vcn_tags
route_rules {
destination = "0.0.0.0/0"
destination_type = "CIDR_BLOCK"
network_entity_id = oci_core_nat_gateway.oke_natgw[0].id
}
route_rules {
destination = lookup(data.oci_core_services.all_oci_services[0].services[0], "cidr_block")
destination_type = "SERVICE_CIDR_BLOCK"
network_entity_id = oci_core_service_gateway.oke_sg[0].id
}
}
resource "oci_core_internet_gateway" "oke_igw" {
count = var.use_existing_vcn ? 0 : 1
compartment_id = var.vcn_compartment_id
display_name = "Internet Gateway for vcn${random_string.deploy_id.result}"
vcn_id = oci_core_vcn.oke_vcn[0].id
defined_tags = var.vcn_tags
}
resource "oci_core_route_table" "oke_rt_via_igw" {
count = var.use_existing_vcn ? 0 : 1
compartment_id = var.vcn_compartment_id
vcn_id = oci_core_vcn.oke_vcn[0].id
display_name = "via Internet Gateway"
defined_tags = var.vcn_tags
route_rules {
destination = "0.0.0.0/0"
destination_type = "CIDR_BLOCK"
network_entity_id = oci_core_internet_gateway.oke_igw[0].id
}
}
### Security Lists
resource "oci_core_security_list" "oke_api_endpoint_external_sec_list" {
count = var.use_existing_vcn ? 0 : 1
compartment_id = var.vcn_compartment_id
display_name = "API Endpoint External Comm"
vcn_id = oci_core_vcn.oke_vcn[0].id
defined_tags = var.vcn_tags
# TCP SSL to Services
egress_security_rules {
description = "Allow Kubernetes control plane to communicate with OKE"
protocol = "6"
destination_type = "SERVICE_CIDR_BLOCK"
destination = lookup(data.oci_core_services.all_oci_services[0].services[0], "cidr_block")
stateless = false
tcp_options {
min = 443
max = 443
}
}
# Incoming from outside to Endpoint 6443
ingress_security_rules {
description = "Client access to Kubernetes API endpoint"
protocol = "6" # TCP
source = "0.0.0.0/0"
stateless = false
tcp_options {
min = 6443
max = 6443
}
}
}
resource "oci_core_security_list" "oke_api_endpoint_nodes_sec_list" {
count = var.use_existing_vcn ? 0 : length([for x in [true, var.np2_create_new_subnet, var.np3_create_new_subnet] : x if x])
compartment_id = var.vcn_compartment_id
display_name = "API Endpoint - Node Pool ${count.index + 1} Comm"
vcn_id = oci_core_vcn.oke_vcn[0].id
defined_tags = var.vcn_tags
# TCP All to Nodes
egress_security_rules {
description = "TCP traffic to worker nodes"
protocol = "6" # TCP
destination_type = "CIDR_BLOCK"
destination = local.node_pool_subnets_cidrs[count.index]
stateless = false
}
# ICMP 3,4 to Nodes
egress_security_rules {
description = "Path Discovery."
protocol = "1" # ICMP
destination_type = "CIDR_BLOCK"
destination = local.node_pool_subnets_cidrs[count.index]
stateless = false
icmp_options {
type = 3
code = 4
}
}
# Incoming ICMP from Nodes
ingress_security_rules {
description = "Path Discovery"
protocol = "1" # ICMP
source = local.node_pool_subnets_cidrs[count.index]
stateless = false
icmp_options {
type = 3
code = 4
}
}
# Incoming TCP 6443 from Nodes
ingress_security_rules {
description = "Kubernetes worker to Kubernetes API endpoint communication"
protocol = "6" # TCP
source = local.node_pool_subnets_cidrs[count.index]
stateless = false
tcp_options {
min = 6443
max = 6443
}
}
# Incoming TCP 12250 from Nodes
ingress_security_rules {
description = "Kubernetes worker to control plane communication"
protocol = "6" # TCP
source = local.node_pool_subnets_cidrs[count.index]
stateless = false
tcp_options {
min = 12250
max = 12250
}
}
}
resource "oci_core_security_list" "oke_nodepool_internal_sec_list" {
count = var.use_existing_vcn ? 0 : 1
compartment_id = var.vcn_compartment_id
display_name = "Nodepool - Internal Comm"
vcn_id = oci_core_vcn.oke_vcn[0].id
defined_tags = var.vcn_tags
dynamic "egress_security_rules" {
iterator = cidr
for_each = local.node_pool_subnets_cidrs
content {
description = "Allow pods on one worker node to communicate with pods on other worker nodes"
protocol = "all"
destination_type = "CIDR_BLOCK"
destination = cidr.value
stateless = false
}
}
dynamic "ingress_security_rules" {
iterator = cidr
for_each = local.node_pool_subnets_cidrs
content {
description = "Node to Node"
protocol = "all"
source = cidr.value
stateless = false
}
}
}
resource "oci_core_security_list" "oke_nodepool_api_comm_sec_list" {
count = var.use_existing_vcn ? 0 : 1
compartment_id = var.vcn_compartment_id
display_name = "Nodepool - API Comm"
vcn_id = oci_core_vcn.oke_vcn[0].id
defined_tags = var.vcn_tags
egress_security_rules {
description = "Allow nodes to communicate with OKE"
protocol = "6"
destination_type = "SERVICE_CIDR_BLOCK"
destination = lookup(data.oci_core_services.all_oci_services[0].services[0], "cidr_block")
stateless = false
}
egress_security_rules {
description = "Kubernetes worker to Kubernetes API endpoint communication"
protocol = "6"
destination_type = "CIDR_BLOCK"
destination = local.api_subnet_cidr
stateless = false
tcp_options {
min = 6443
max = 6443
}
}
egress_security_rules {
description = "Kubernetes worker to control plane communication"
protocol = "6"
destination_type = "CIDR_BLOCK"
destination = local.api_subnet_cidr
stateless = false
tcp_options {
min = 12250
max = 12250
}
}
ingress_security_rules {
description = "Allow Kubernetes control plane to communicate with worker nodes"
protocol = "6"
source = local.api_subnet_cidr
stateless = false
}
}
resource "oci_core_security_list" "oke_nodepool_external_comm_sec_list" {
count = var.use_existing_vcn ? 0 : 1
compartment_id = var.vcn_compartment_id
display_name = "Nodepool External Comm"
vcn_id = oci_core_vcn.oke_vcn[0].id
defined_tags = var.vcn_tags
# ICMP out
egress_security_rules {
description = "Path Discovery."
protocol = 1
destination = "0.0.0.0/0"
stateless = false
icmp_options {
type = 3
code = 4
}
}
# Internet access
egress_security_rules {
description = "Allow worker nodes to communicate with internet"
protocol = "6"
destination_type = "CIDR_BLOCK"
destination = "0.0.0.0/0"
stateless = false
}
# ICMP
ingress_security_rules {
description = "Path Discovery"
protocol = 1
source = "0.0.0.0/0"
stateless = false
icmp_options {
type = 3
code = 4
}
}
# SSH
ingress_security_rules {
description = "Allow inbound SSH traffic to worker nodes"
protocol = "6"
source = "0.0.0.0/0"
stateless = false
tcp_options {
min = 22
max = 22
}
}
}
resource "oci_core_security_list" "oke_nodepool_lb_comm_sec_list" {
count = var.use_existing_vcn ? 0 : 1
compartment_id = var.vcn_compartment_id
display_name = "Nodepool - Load Balancer Comm"
vcn_id = oci_core_vcn.oke_vcn[0].id
defined_tags = var.vcn_tags
egress_security_rules {
# iterator = cidr
# for_each = local.lb_subnets_cidrs
# content {
description = "TCP to LBs"
protocol = "6"
destination_type = "CIDR_BLOCK"
destination = local.lb_subnet_cidr
stateless = false
# }
}
ingress_security_rules {
# iterator = cidr
# for_each = local.lb_subnets_cidrs
# content {
description = "TCP from LBs"
protocol = "6"
source = local.lb_subnet_cidr
stateless = false
# }
}
}
resource "oci_core_subnet" "oke_api_endpoint_subnet" {
count = var.use_existing_vcn ? 0 : 1
cidr_block = local.api_subnet_cidr
compartment_id = var.vcn_compartment_id
vcn_id = oci_core_vcn.oke_vcn[0].id
dns_label = "api"
display_name = "API Endpoint Subnet"
security_list_ids = flatten([
[oci_core_vcn.oke_vcn[0].default_security_list_id],
[oci_core_security_list.oke_api_endpoint_external_sec_list[0].id],
oci_core_security_list.oke_api_endpoint_nodes_sec_list.*.id
])
route_table_id = var.is_endpoint_public ? oci_core_route_table.oke_rt_via_igw[0].id : oci_core_route_table.oke_rt_via_natgw_and_sg[0].id
prohibit_public_ip_on_vnic = var.is_endpoint_public ? false : true
defined_tags = var.vcn_tags
}
resource "oci_core_subnet" "oke_lb_subnet" {
count = (var.use_existing_vcn) ? 0 : 1
cidr_block = local.lb_subnet_cidr
compartment_id = var.vcn_compartment_id
availability_domain = null
vcn_id = oci_core_vcn.oke_vcn[0].id
dns_label = "lb"
display_name = "Services LBs Subnet"
security_list_ids = [oci_core_vcn.oke_vcn[0].default_security_list_id]
route_table_id = oci_core_route_table.oke_rt_via_igw[0].id
prohibit_public_ip_on_vnic = !var.allow_deploy_public_lb
defined_tags = var.vcn_tags
}
resource "oci_core_subnet" "oke_nodepool_subnet" {
count = (var.use_existing_vcn && var.node_pool_count == 0) ? 0 : length([for x in [true, var.np2_create_new_subnet, var.np3_create_new_subnet] : x if x])
cidr_block = local.node_pool_subnets_cidrs[count.index]
compartment_id = var.vcn_compartment_id
vcn_id = oci_core_vcn.oke_vcn[0].id
dns_label = "nodes${count.index + 1}"
display_name = "Node Pool ${count.index + 1} Subnet"
security_list_ids = [
oci_core_vcn.oke_vcn[0].default_security_list_id,
oci_core_security_list.oke_nodepool_lb_comm_sec_list[0].id,
oci_core_security_list.oke_nodepool_external_comm_sec_list[0].id,
oci_core_security_list.oke_nodepool_api_comm_sec_list[0].id,
oci_core_security_list.oke_nodepool_internal_sec_list[0].id
]
route_table_id = oci_core_route_table.oke_rt_via_natgw_and_sg[0].id
prohibit_public_ip_on_vnic = true
defined_tags = var.vcn_tags
}