-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
252 lines (221 loc) · 7.48 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
terraform {
backend "s3" {
endpoint = "https://frd8bsyrgar7.compat.objectstorage.eu-frankfurt-1.oraclecloud.com"
skip_metadata_api_check = true
skip_region_validation = true
force_path_style = true
skip_credentials_validation = true
bucket = "tf-state"
key = "arm-k8s/terraform.tfstate"
region = "eu-frankfurt-1"
}
required_providers {
oci = {
source = "hashicorp/oci"
version = ">= 4.0.0"
}
sops = {
source = "carlpett/sops"
version = "~> 0.5"
}
}
}
locals {
security_list_ids = "${split(",", oci_core_security_list.k8s_security_list.id)}"
}
data "oci_identity_availability_domains" "ads" {
compartment_id = data.oci_identity_compartments.vpn_compartments.compartments[0].id
}
data "oci_identity_compartments" "vpn_compartments" {
compartment_id = data.sops_file.secret.data["tenancy"]
access_level = "ACCESSIBLE"
name = var.compartment_name
state = var.compartment_state
}
data "oci_core_vcns" "existing_vcns" {
compartment_id = data.oci_identity_compartments.vpn_compartments.compartments[0].id
display_name = var.vcn_display_name
state = var.vcn_state
}
data "oci_core_instances" "existing_instance" {
compartment_id = data.oci_identity_compartments.vpn_compartments.compartments[0].id
display_name = var.vpn_instance_name
}
data "oci_core_subnets" "public_subnets" {
compartment_id = data.oci_identity_compartments.vpn_compartments.compartments[0].id
display_name = var.subnet_display_name
vcn_id = data.oci_core_vcns.existing_vcns.virtual_networks[0].id
}
data "oci_core_private_ips" "private_ips_by_subnet" {
subnet_id = data.oci_core_subnets.public_subnets.subnets[0].id
}
data "oci_core_security_lists" "vpn_security_lists" {
compartment_id = data.oci_identity_compartments.vpn_compartments.compartments[0].id
display_name = var.vpn_security_list_display_name
vcn_id = data.oci_core_vcns.existing_vcns.virtual_networks[0].id
}
resource "oci_core_route_table" "private_subnet_route_table" {
compartment_id = data.oci_identity_compartments.vpn_compartments.compartments[0].id
vcn_id = data.oci_core_vcns.existing_vcns.virtual_networks[0].id
display_name = var.private_subnet_route_table_display_name
freeform_tags = var.app_tags
route_rules {
network_entity_id = data.oci_core_private_ips.private_ips_by_subnet.private_ips[0].id
description = "Route to vpn server"
destination = "0.0.0.0/0"
destination_type = "CIDR_BLOCK"
}
}
resource "oci_core_subnet" "private_subnet" {
cidr_block = var.private_subnet_cidr_block
compartment_id = data.oci_identity_compartments.vpn_compartments.compartments[0].id
vcn_id = data.oci_core_vcns.existing_vcns.virtual_networks[0].id
display_name = var.private_subnet_display_name
freeform_tags = var.app_tags
route_table_id = oci_core_route_table.private_subnet_route_table.id
security_list_ids = local.security_list_ids
}
resource "oci_core_security_list" "k8s_security_list" {
compartment_id = data.oci_identity_compartments.vpn_compartments.compartments[0].id
vcn_id = data.oci_core_vcns.existing_vcns.virtual_networks[0].id
display_name = var.k8s_security_list_name
freeform_tags = var.app_tags
ingress_security_rules {
protocol = "all"
source = var.private_subnet_cidr_block
description = "Allow all connection from private subnet"
}
ingress_security_rules {
protocol = "6"
source = "0.0.0.0/0"
description = "Allow http connection"
tcp_options {
min = "80"
max = "80"
}
}
ingress_security_rules {
protocol = "6"
source = "0.0.0.0/0"
description = "Allow https connection"
tcp_options {
min = "443"
max = "443"
}
}
ingress_security_rules {
protocol = "17"
source = "0.0.0.0/0"
description = "Allow ntp connection"
tcp_options {
min = "123"
max = "123"
}
}
ingress_security_rules {
protocol = "6"
source = data.oci_core_subnets.public_subnets.subnets[0].cidr_block
description = "Allow connection to k8s api from public subnet through OpenVPN"
tcp_options {
min = "6443"
max = "6443"
}
}
ingress_security_rules {
protocol = "6"
source = data.oci_core_subnets.public_subnets.subnets[0].cidr_block
description = "Allow ssh connection from public subnet"
tcp_options {
min = "22"
max = "22"
}
}
ingress_security_rules {
protocol = "17"
source = data.oci_core_subnets.public_subnets.subnets[0].cidr_block
description = "Allow vpn connection from public subnet"
tcp_options {
min = "1194"
max = "1194"
}
}
egress_security_rules {
protocol = "6"
destination = "0.0.0.0/0"
}
egress_security_rules {
protocol = "17"
destination = "0.0.0.0/0"
description = "OpenVPN egress"
udp_options {
min = "1194"
max = "1194"
}
}
egress_security_rules {
protocol = "17"
destination = "0.0.0.0/0"
description = "NTP egress"
udp_options {
min = "123"
max = "123"
}
}
egress_security_rules {
protocol = "4"
destination = var.private_subnet_cidr_block
description = "IPIP private subnet egress block"
}
}
resource "oci_core_instance" "k8s-cp-instance" {
availability_domain = data.oci_identity_availability_domains.ads.availability_domains[2].name
compartment_id = data.oci_identity_compartments.vpn_compartments.compartments[0].id
display_name = "k8s-cp"
shape = "VM.Standard.A1.Flex"
freeform_tags = var.app_tags
create_vnic_details {
subnet_id = oci_core_subnet.private_subnet.id
display_name = "k8s-cp VNIC"
assign_public_ip = false
hostname_label = "k8s-cp"
private_ip = var.k8s_cp_private_ip
skip_source_dest_check = true
}
shape_config {
memory_in_gbs = 12
ocpus = 2
}
source_details {
source_type = "image"
source_id = "${var.image_id[var.region]}"
}
metadata = {
ssh_authorized_keys = "${file(var.ssh_key_public)}"
}
}
resource "oci_core_instance" "k8s-worker-instance" {
availability_domain = data.oci_identity_availability_domains.ads.availability_domains[2].name
compartment_id = data.oci_identity_compartments.vpn_compartments.compartments[0].id
display_name = "k8s-worker"
shape = "VM.Standard.A1.Flex"
freeform_tags = var.app_tags
create_vnic_details {
subnet_id = oci_core_subnet.private_subnet.id
display_name = "k8s-worker VNIC"
assign_public_ip = false
hostname_label = "k8s-worker"
private_ip = var.k8s_worker_private_ip
skip_source_dest_check = true
}
shape_config {
memory_in_gbs = 12
ocpus = 2
}
source_details {
source_type = "image"
source_id = "${var.image_id[var.region]}"
}
metadata = {
ssh_authorized_keys = "${file(var.ssh_key_public)}"
}
}