-
Notifications
You must be signed in to change notification settings - Fork 390
/
haproxy_config.py
289 lines (244 loc) · 8.75 KB
/
haproxy_config.py
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
from svc_monitor.config_db import *
PROTO_HTTP = 'HTTP'
PROTO_HTTPS = 'HTTPS'
PROTO_TERMINATED_HTTPS = 'TERMINATED_HTTPS'
PROTO_MAP_V1 = {
'TCP': 'tcp',
'HTTP': 'http',
'HTTPS': 'http',
'TERMINATED_HTTPS': 'http'
}
PROTO_MAP_V2 = {
'TCP': 'tcp',
'HTTP': 'http',
'HTTPS': 'tcp',
'TERMINATED_HTTPS': 'http'
}
LB_METHOD_MAP = {
'ROUND_ROBIN': 'roundrobin',
'LEAST_CONNECTIONS': 'leastconn',
'SOURCE_IP': 'source'
}
HEALTH_MONITOR_PING = 'PING'
HEALTH_MONITOR_TCP = 'TCP'
HEALTH_MONITOR_HTTP = 'HTTP'
HEALTH_MONITOR_HTTPS = 'HTTPS'
PERSISTENCE_SOURCE_IP = 'SOURCE_IP'
PERSISTENCE_HTTP_COOKIE = 'HTTP_COOKIE'
PERSISTENCE_APP_COOKIE = 'APP_COOKIE'
def get_config_v2(lb):
sock_path = '/var/lib/contrail/loadbalancer/haproxy/'
sock_path += lb.uuid + '/haproxy.sock'
conf = set_globals(sock_path) + '\n\n'
conf += set_defaults() + '\n\n'
conf += set_v2_frontend_backend(lb)
return conf
def get_config_v1(pool):
sock_path = '/var/lib/contrail/loadbalancer/haproxy/'
sock_path += pool.uuid + '/haproxy.sock'
conf = set_globals(sock_path) + '\n\n'
conf += set_defaults() + '\n\n'
conf += set_v1_frontend_backend(pool)
return conf
def set_globals(sock_path):
maxconn = 65000
ssl_ciphers = \
'ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:' \
'ECDH+AES128:DH+AES:ECDH+3DES:DH+3DES:RSA+AESGCM:' \
'RSA+AES:RSA+3DES:!aNULL:!MD5:!DSS'
conf = [
'global',
'daemon',
'user haproxy',
'group haproxy',
'log /dev/log local0',
'log /dev/log local1 notice',
'tune.ssl.default-dh-param 2048',
'ssl-default-bind-ciphers %s' % ssl_ciphers,
'ulimit-n 200000',
'maxconn %d' % maxconn
]
conf.append('stats socket %s mode 0666 level user' % sock_path)
res = "\n\t".join(conf)
return res
def set_defaults():
client_timeout = 300000
server_timeout = 300000
connect_timeout = 5000
conf = [
'defaults',
'log global',
'retries 3',
'option redispatch',
'timeout connect %d' % connect_timeout,
'timeout client %d' % client_timeout,
'timeout server %d' % server_timeout,
]
res = "\n\t".join(conf)
return res
def set_v1_frontend_backend(pool):
conf = []
vip = VirtualIpSM.get(pool.virtual_ip)
if not vip or not vip.params['admin_state']:
return "\n"
ssl = ''
if vip.params['protocol'] == PROTO_HTTPS:
ssl = 'ssl crt haproxy_ssl_cert_path no-sslv3'
lconf = [
'frontend %s' % vip.uuid,
'option tcplog',
'bind %s:%s %s' % (vip.params['address'],
vip.params['protocol_port'], ssl),
'mode %s' % PROTO_MAP_V1[vip.params['protocol']],
]
if 'connection_limit' in vip.params and vip.params['connection_limit'] > 0:
lconf.append('maxconn %d' % vip.params['connection_limit'])
if vip.params['protocol'] == PROTO_HTTP or \
vip.params['protocol'] == PROTO_HTTPS:
lconf.append('option forwardfor')
if pool and pool.params['admin_state']:
lconf.append('default_backend %s' % pool.uuid)
res = "\n\t".join(lconf) + '\n\n'
res += set_backend_v1(pool)
conf.append(res)
return "\n".join(conf)
def set_v2_frontend_backend(lb):
conf = []
for ll_id in lb.loadbalancer_listeners:
ll = LoadbalancerListenerSM.get(ll_id)
if not ll:
continue
if not ll.params['admin_state']:
continue
ssl = 'ssl'
tls_sni_presence = False
if ll.params['protocol'] == PROTO_TERMINATED_HTTPS:
if ll.params['default_tls_container']:
ssl += ' crt %s' % ll.params['default_tls_container']
tls_sni_presence = True
for sni_container in ll.params['sni_containers']:
ssl += ' crt %s' % sni_container
tls_sni_presence = True
if (tls_sni_presence == False):
ssl = ''
else:
ssl += ' no-sslv3'
lconf = [
'frontend %s' % ll.uuid,
'option tcplog',
'bind %s:%s %s' % (lb.params['vip_address'],
ll.params['protocol_port'], ssl),
'mode %s' % PROTO_MAP_V2[ll.params['protocol']],
]
if 'connection_limit' in ll.params and ll.params['connection_limit'] > 0:
lconf.append('maxconn %d' % ll.params['connection_limit'])
if ll.params['protocol'] == PROTO_HTTP:
lconf.append('option forwardfor')
pool = LoadbalancerPoolSM.get(ll.loadbalancer_pool)
if pool and pool.params['admin_state']:
lconf.append('default_backend %s' % pool.uuid)
res = "\n\t".join(lconf) + '\n\n'
res += set_backend_v2(pool)
conf.append(res)
return "\n".join(conf)
def set_backend_v1(pool):
conf = [
'backend %s' % pool.uuid,
'mode %s' % PROTO_MAP_V1[pool.params['protocol']],
'balance %s' % LB_METHOD_MAP[pool.params['loadbalancer_method']]
]
if pool.params['protocol'] == PROTO_HTTP:
conf.append('option forwardfor')
server_suffix = ''
for hm_id in pool.loadbalancer_healthmonitors:
hm = HealthMonitorSM.get(hm_id)
if not hm:
continue
server_suffix, monitor_conf = set_health_monitor(hm)
conf.extend(monitor_conf)
session_conf = set_session_persistence(pool)
conf.extend(session_conf)
for member_id in pool.members:
member = LoadbalancerMemberSM.get(member_id)
if not member or not member.params['admin_state']:
continue
server = (('server %s %s:%s weight %s') % (member.uuid,
member.params['address'], member.params['protocol_port'],
member.params['weight'])) + server_suffix
conf.append(server)
return "\n\t".join(conf) + '\n'
def set_backend_v2(pool):
conf = [
'backend %s' % pool.uuid,
'mode %s' % PROTO_MAP_V2[pool.params['protocol']],
'balance %s' % LB_METHOD_MAP[pool.params['loadbalancer_method']]
]
if pool.params['protocol'] == PROTO_HTTP:
conf.append('option forwardfor')
server_suffix = ''
for hm_id in pool.loadbalancer_healthmonitors:
hm = HealthMonitorSM.get(hm_id)
if not hm:
continue
server_suffix, monitor_conf = set_health_monitor(hm)
conf.extend(monitor_conf)
session_conf = set_session_persistence(pool)
conf.extend(session_conf)
for member_id in pool.members:
member = LoadbalancerMemberSM.get(member_id)
if not member or not member.params['admin_state']:
continue
server = (('server %s %s:%s weight %s') % (member.uuid,
member.params['address'], member.params['protocol_port'],
member.params['weight'])) + server_suffix
conf.append(server)
return "\n\t".join(conf) + '\n'
def set_health_monitor(hm):
if not hm.params['admin_state']:
return '', []
server_suffix = ' check inter %ss fall %s' % \
(hm.params['delay'], hm.params['max_retries'])
conf = [
'timeout check %ss' % hm.params['timeout']
]
if hm.params['monitor_type'] in (HEALTH_MONITOR_HTTP, HEALTH_MONITOR_HTTPS):
conf.append('option httpchk %s %s' %
(hm.params['http_method'], hm.params['url_path']))
conf.append(
'http-check expect rstatus %s' %
'|'.join(_get_codes(hm.params['expected_codes']))
)
if hm.params['monitor_type'] == HEALTH_MONITOR_HTTPS:
conf.append('option ssl-hello-chk')
return server_suffix, conf
def set_session_persistence(pool):
conf = []
if pool.virtual_ip:
vip = VirtualIpSM.get(pool.virtual_ip)
if not vip:
return
persistence = vip.params.get('persistence_type', None)
cookie = vip.params.get('persistence_cookie_name', None)
else:
persistence = pool.params.get('session_persistence', None)
cookie = pool.params.get('persistence_cookie_name', None)
if persistence == PERSISTENCE_SOURCE_IP:
conf.append('stick-table type ip size 10k')
conf.append('stick on src')
elif persistence == PERSISTENCE_HTTP_COOKIE:
conf.append('cookie SRV insert indirect nocache')
elif (persistence == PERSISTENCE_APP_COOKIE and cookie):
conf.append('appsession %s len 56 timeout 3h' % cookie)
return conf
def _get_codes(codes):
response = set()
for code in codes.replace(',', ' ').split(' '):
code = code.strip()
if not code:
continue
elif '-' in code:
low, hi = code.split('-')[:2]
response.update(str(i) for i in xrange(int(low), int(hi) + 1))
else:
response.add(code)
return response