-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathfgcp-ha-ap-multilb.jinja
293 lines (277 loc) · 9.9 KB
/
fgcp-ha-ap-multilb.jinja
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
{% import "utils-netcalc.jinja" as netcalc %}
{# If no prefix is defined in properties, set it to deployment name #}
{% set prefix = properties.prefix|default(env.deployment) %}
{# Derive region name from the first zone #}
{% set region=properties.zones[0].split('-')[0]~'-'~properties.zones[0].split('-')[1] %}
{# Indeces used for building HA config #}
{% set mgmtNicIndex0 = 3 %}
{% set hasyncNicIndex0 = 2 %}
resources:
{% for vm in ["Primary", "Secondary"] %}
{% set vmLoop=loop %}
{% set fgtName="fgt"~loop.index%}
- name: Fortigate_{{vm}}
type: singlevm2.jinja
properties:
prefix: {{ prefix }}
name: {{ fgtName }}
zone: {{ properties.zones[loop.index0]}}
instanceType: {{ properties.instanceType }}
{% if 0==loop.index0 %}
createCloudFirewallRules: true
{% else %}
createCloudFirewallRules: false
{% endif %}
license:
type: {{ properties.license.type }}
{% if ('byol'==properties.license.type) and ("lics" in properties.license)%}
lic: {{ properties.license.lics[ loop.index0 ]}}
{% endif %}
version: {{ properties.version }}
serviceAccount: {{ properties.serviceAccount }}
serialPortEnable: {{ properties.serialPortEnable }}
{# Routes will be created later for ILBs. Disable for VMs #}
routes: []
networks:
{% for nic in properties.networks %}
- name: {{ nic.name }}
vpcLink: {{ nic.vpcLink }}
subnetLink: {{ nic.subnetLink }}
ipCidrRange: {{ nic.ipCidrRange }}
networkIP: {{ netcalc.getNthIpWithMask( nic.ipCidrRange, vmLoop.index+nic.addressOffset ) }}
{%- if loop.index0 in [mgmtNicIndex0, hasyncNicIndex0]%}
reserved: true
{% endif %}
{%- if nic.allowaccess %}
allowaccess: {{ nic.allowaccess }}
{%- endif %}
{%- if nic.description %}
description: {{ nic.description }}
{%- endif %}
{% if "externalIP" in nic %}
externalIP:
name: {{ fgtName }}-{{ nic.externalIP.name }}
{% endif %}
{% if "serviceIPs" in nic %}
additionalExternalIPs:
{% for serviceIP in nic.serviceIPs %}
- name: {{ serviceIP.name }}
createAddressAndFwdRule: false
address: $(ref.{{ prefix }}-{{ serviceIP.name }}.address)
{% if 'description' in serviceIP %}
description: {{ serviceIP.description }}
{% endif %}
{% endfor %}
{% endif %}
{% endfor %}
fwConfig: |
config system probe-response
set port {{ properties.probePort }}
set mode http-probe
set http-probe-value OK
end
config system ha
set group-name "cluster1"
set mode a-p
set hbdev "port{{ hasyncNicIndex0+1 }}" 50
set session-pickup enable
set ha-mgmt-status enable
config ha-mgmt-interfaces
edit 1
set interface "port{{ mgmtNicIndex0+1 }}"
set gateway {{ netcalc.getNthIp( properties.networks[mgmtNicIndex0].ipCidrRange, 0 )}}
next
end
set override disable
set priority {{ loop.index%2 }}
set unicast-hb enable
set unicast-hb-peerip {{ netcalc.getNthIp( properties.networks[ hasyncNicIndex0 ].ipCidrRange, (loop.index%2)+1 )}}
set unicast-hb-netmask {{ netcalc.getNetmaskLong( properties.networks[ hasyncNicIndex0 ].ipCidrRange )}}
end
config system interface
{%- for network in properties.networks %}
{%- if "routes" in network or "serviceIPs" in network %}
edit port{{ loop.index }}
set secondary-IP enable
config secondaryip
{%- if "routes" in network %}
edit 1
set ip $(ref.{{ prefix }}-{{ network.name | default( 'net'~loop.index )}}-ilb.IPAddress) 255.255.255.255
set allowaccess probe-response
next
{%- endif %}
{%- if "serviceIPs" in network %}
{%- for serviceIP in network.serviceIPs %}
edit {{ loop.index+100 }}
set ip $(ref.{{ prefix }}-{{ serviceIP.name }}.address) 255.255.255.255
set allowaccess probe-response
next
{%- endfor %}
{%- endif %}
end
next
{%- endif %}
{%- endfor %}
end
config router static
{%- for network in properties.networks %}
{# do NOT concatenate "for" with "if" or you'll break index #}
{%- if "routes" in network %}
edit 10{{loop.index}}
set dst 35.191.0.0/16
set device port{{ loop.index }}
set gateway {{ netcalc.getNthIp( network.ipCidrRange, 0 )}}
next
edit 11{{loop.index}}
set dst 130.211.0.0/22
set device port{{ loop.index }}
set gateway {{ netcalc.getNthIp( network.ipCidrRange, 0 )}}
next
{%- endif %}
{%- endfor %}
end
{{properties.fwConfig | indent(7)}}
{% endfor%}
{% for zoneindx in [0,1] %}
- name: {{ prefix }}-hagroup{{ zoneindx }}
type: compute.v1.instanceGroup
properties:
zone: {{ properties.zones[ zoneindx ]}}
- name: {{ prefix }}-hagroup{{zoneindx}}-add
action: gcp-types/compute-v1:compute.instanceGroups.addInstances
metadata:
runtimePolicy:
- CREATE
properties:
zone: {{ properties.zones[ zoneindx ]}}
instanceGroup: $(ref.{{ prefix }}-hagroup{{ zoneindx }}.name)
instances:
- instance: $(ref.{{ prefix }}-fgt{{ loop.index }}.selfLink)
{#}
- name: {{ prefix }}-hagroup{{zoneindx}}-setPort
action: gcp-types/compute-v1:compute.instanceGroups.setNamedPorts
metadata:
runtimePolicy:
- CREATE
properties:
zone: {{ properties.zones[ zoneindx ]}}
instanceGroup: $(ref.{{ prefix }}-hagroup{{ zoneindx }}.name)
namedPorts:
- name: http
port: 80
{#}
{% endfor %}
- name: {{ prefix }}-ilb-probe{{ properties.probePort }}
type: compute.v1.regionHealthCheck
properties:
type: HTTP
region: {{region}}
httpHealthCheck:
port: {{ properties.probePort }}
checkIntervalSec: 4
timeoutSec: 2
unhealthyThreshold: 2
healthyThreshold: 1
{# ILB resources #}
{% for network in properties.networks if "routes" in network %}
{# Regional backend service and forwarding rule must be created per VPC #}
- name: {{ prefix }}-{{ network.name | default( 'net'~loop.index ) }}-lbbes
type: compute.v1.regionBackendService
properties:
region: {{ region }}
loadBalancingScheme: INTERNAL
network: {{ network.vpcLink }}
protocol: TCP
healthChecks:
- $(ref.{{ prefix }}-ilb-probe{{ properties.probePort }}.selfLink)
backends:
- group: $(ref.{{ prefix }}-hagroup0.selfLink)
- group: $(ref.{{ prefix }}-hagroup1.selfLink)
connectionTrackingPolicy:
connectionPersistenceOnUnhealthyBackends: NEVER_PERSIST
- name: {{ prefix }}-{{ network.name | default( 'net'~loop.index )}}-ilb
type: compute.v1.forwardingRule
properties:
region: {{ region }}
loadBalancingScheme: INTERNAL
IPAddress: {{ netcalc.getNthIp( network.ipCidrRange, 3+network.addressOffset )}}
IPProtocol: TCP
allPorts: true
backendService: $(ref.{{ prefix }}-{{ network.name | default( 'net'~loop.index )}}-lbbes.selfLink)
network: {{ network.vpcLink }}
subnetwork: {{ network.subnetLink }}
{% set netloop=loop %}
{% for route in network.routes %}
- name: {{ prefix }}-{{ network.name | default( 'net'~netloop.index )}}-rt-{{ route.name | default( loop.index )}}-via-fgt
type: compute.v1.route
properties:
destRange: {{ route.destRange }}
{% if "priority" in route %}
priority: {{ route.priority }}
{% endif %}
network: {{ network.vpcLink }}
nextHopIlb: $(ref.{{ prefix }}-{{ network.name | default( 'net'~netloop.index )}}-ilb.selfLink )
description: {{ route.description | default("route via FGT") }}
{% endfor %}{# for each route #}
{% endfor %}{# for each network #}
{% for network in properties.networks if "serviceIPs" in network %}
- name: {{ prefix }}-{{ network.name | default( 'net'~loop.index ) }}-elb-bes
type: compute.v1.regionBackendService
properties:
region: {{ region }}
loadBalancingScheme: EXTERNAL
protocol: UNSPECIFIED
healthChecks:
- $(ref.{{ prefix }}-ilb-probe{{ properties.probePort }}.selfLink)
backends:
- group: $(ref.{{ prefix }}-hagroup0.selfLink)
- group: $(ref.{{ prefix }}-hagroup1.selfLink)
connectionTrackingPolicy:
connectionPersistenceOnUnhealthyBackends: NEVER_PERSIST
{# add ELB forwarding rules for each address in serviceIPs #}
{% for serviceIP in network.serviceIPs %}
- name: {{ prefix }}-{{ serviceIP.name }}
type: compute.v1.address
properties:
region: {{ region }}
{% if "description" in serviceIP %}
description: {{ serviceIP.description }}
{% endif %}
{% if "networkTier" in serviceIP %}
networkTier: {{ serviceIP.networkTier }}
{% endif %}
- name: {{prefix}}-{{serviceIP.name}}-elb
type: compute.v1.forwardingRule
properties:
region: {{region}}
IPAddress: $(ref.{{ prefix }}-{{ serviceIP.name }}.selfLink)
IPProtocol: L3_DEFAULT
allPorts: true
loadBalancingScheme: EXTERNAL
backendService: $(ref.{{prefix}}-{{ network.name | default( 'net'~loop.index ) }}-elb-bes.selfLink)
{% if "networkTier" in serviceIP %}
networkTier: {{ serviceIP.networkTier }}
{% endif %}
{% endfor %} {# for each serviceIP #}
{% endfor %}
{# Cloud NAT needed for outbound connections to FortiGuard #}
- name: {{ prefix }}-cloudnat-router
type: compute.v1.router
properties:
region: {{ region }}
network: {{ properties.networks[0].vpcLink }}
nats:
- name: {{ prefix }}-cloudnat
sourceSubnetworkIpRangesToNat: ALL_SUBNETWORKS_ALL_IP_RANGES
natIpAllocateOption: AUTO_ONLY
outputs:
- name: defaultPassword
value: $(ref.Fortigate_Primary.defaultPassword)
- name: mgmtPublicIPs
{% if "externalIP" in properties.networks[ mgmtNicIndex0 ] %}
value:
- $(ref.{{ prefix }}-fgt1-{{ properties.networks[mgmtNicIndex0].externalIP.name }}.address)
- $(ref.{{ prefix }}-fgt2-{{ properties.networks[mgmtNicIndex0].externalIP.name }}.address)
{% else %}
value: undefined
{% endif %}