/
registrator.py
executable file
·530 lines (456 loc) · 18.8 KB
/
registrator.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
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
#!/usr/bin/env python
"""
manages the upstream targets in Kong for Docker containers running on this host.
When a container is started, the registrator will create a upstream target for each
exposed port which has a matching SERVICE_<exposed-port>_NAME environment
variable. If the container exposes a single port, it suffices to have a SERVICE_NAME
environment variable.
The registrator has three commands: 'remove-all', 'sync' and 'daemon'.
remove-all - remove all targets pointing to this host
sync - synchronise the targets with the running containers
daemon - continuously update targets by subscribing to the Docker event stream
"""
import os
import socket
import json
import logging
import click
import docker
import requests
from jsondiff import diff
from jsondiff.symbols import Symbol
import urllib3
logging.basicConfig(level=os.getenv('LOG_LEVEL',logging.INFO))
log = logging.getLogger('KongServiceRegistrator')
#
# disable warnings on ssl usage (really irritating if you explicitly specify verify_ssl = False)
#
urllib3.disable_warnings()
class KongServiceRegistrator(object):
def __init__(self, admin_url, dns_name, hostname, verify_ssl):
"""
constructor.
"""
assert dns_name is not None
assert hostname is not None
assert admin_url is not None
self.dockr = docker.from_env()
self.hostname = hostname
self.dns_name = dns_name
self.admin_url = admin_url
self.verify_ssl = verify_ssl
self.upstreams = {}
self.targets = {}
self.apis = {}
self.load()
assert self.hostname == hostname
assert self.dns_name == dns_name
def sync_upstream(self, upstream, targets):
"""
synchronize all upstream targets on this machine with the targets registerted in Kong
"""
live = set(targets)
in_kong = set(map(lambda t: t['target'], self.targets[
upstream])) if upstream in self.targets else set()
to_delete = in_kong - live
to_add = live - in_kong
for target in to_delete:
self.remove_target(upstream, target)
for target in to_add:
self.add_target(upstream, target)
def load_apis(self):
"""
load all current API definition from Kong into self.apis
"""
self.apis = {}
next_page = '%s/apis?size=100' % self.admin_url
while next_page:
r = requests.get(next_page, verify=self.verify_ssl)
if r.status_code == 200:
response = r.json()
next_page = response['next'] if 'next' in response else None
for api in response['data']:
self.apis[api['name']] = api
elif r.status_code == 404:
next_page = None
else:
log.error('failed to get apis at %s, %s',
self.admin_url, r.text)
def load_upstreams(self):
"""
load all upstreams from Kong into self.upstreams
"""
self.upstreams = {}
next_page = '%s/upstreams?size=100' % self.admin_url
while next_page:
r = requests.get(next_page, verify=self.verify_ssl)
if r.status_code == 200:
response = r.json()
next_page = response['next'] if 'next' in response else None
for upstream in response['data']:
if upstream['name'].endswith(self.dns_name):
self.upstreams[upstream['name']] = upstream
elif r.status_code == 404:
next_page = None
else:
log.error('failed to get upstreams at %s, %s',
self.admin_url, r.text)
def load_targets(self, upstream):
"""
load all targets pointing to `self.hostname` for the upstream 'upstream'.
"""
self.targets[upstream] = []
url = '%s/upstreams/%s/targets/active/' % (self.admin_url, upstream)
r = requests.get(url, verify=self.verify_ssl)
if r.status_code == 200:
response = r.json()
own_targets = filter(lambda t: t['target'].startswith('%s:' % self.hostname), response['data'])
self.targets[upstream].extend(own_targets)
elif r.status_code == 404:
pass # no targets yet..
else:
log.error('failed to get targets of %s at %s, %d, %s',
upstream, r.url, r.status_code, r.text)
def load(self):
"""
load all upstream targets from Kong.
"""
self.load_upstreams()
for upstream in self.upstreams:
self.load_targets(upstream)
def add_upstream(self, name):
"""
add the upstream `name' to Kong.
"""
if name not in self.upstreams:
r = requests.post(
'%s/upstreams/' % self.admin_url, json={'name': name},
verify=self.verify_ssl)
if r.status_code == 409:
r = requests.get(
'%s/upstreams/%s' % (self.admin_url, name),
verify=self.verify_ssl)
if r.status_code == 200 or r.status_code == 201:
self.upstreams[name] = r.json()
self.targets[name] = []
else:
log.error(
'failed to add upstream %s at %s, status code %d, %s',
name, r.url, r.status_code, r.text)
else:
# upstream already exists
pass
def add_target(self, name, target):
"""
add the target `target` to the upstream `name` in Kong.
"""
log.info('adding target %s to upstream %s', target, name)
self.add_upstream(name)
targets = self.targets[name]
targets = filter(lambda t: t['target'] ==
target and t['weight'] != 0, targets)
if len(targets) == 0:
r = requests.post('%s/upstreams/%s/targets' %
(self.admin_url, name),
json={'target': target},
verify=self.verify_ssl)
if r.status_code == 200 or r.status_code == 201:
self.targets[name].append(r.json())
else:
log.error(
'failed to add target %s to upstream %s at %s: %d, %s',
target, name, self.admin_url, r.status_code, r.text)
else:
# target already exists
log.debug(
'target "%s" for upstream "%s" is already registered', target,
name)
pass
def remove_target(self, name, target):
"""
remove the target `target` from the upstream `name` in Kong.
"""
log.info('removing target %s from upstream %s',
target, name)
url = '%s/upstreams/%s/targets/%s' % (self.admin_url, name, target)
r = requests.delete(url, verify=self.verify_ssl)
if r.status_code != 204:
log.error(
'failed to remove target %s from upstream %s at %s: %d, %s',
target, name, r.url, r.status_code, r.text)
self.targets[name] = filter(
lambda t: t['target'] != target, self.targets[name])
def get_environment_of_container(self, container):
"""
returns the environment variables of the container as a dictionary.
"""
assert container is not None
result = {}
env = container.attrs['Config']['Env']
for e in env:
parameter = e.split('=', 1)
result[parameter[0]] = parameter[1]
assert len(env) == len(container.attrs['Config']['Env'])
return result
def sync_apis(self, apis):
"""
synchronizes the API definition defined on this machine with Kong.
"""
self.load_apis()
for name in apis:
definition = apis[name]
if name in self.apis:
# api with the same name already exists, check
# for update
current = self.apis[name]
differences = diff(current, definition, syntax='explicit')
has_update = filter(lambda k: k.label == 'update', differences.keys())
if len(has_update) > 0:
log.info('updating API definition %s.', name)
r = requests.patch(
'%s/apis/%s' % (self.admin_url, name),
json=definition, verify=self.verify_ssl)
if r.status_code == 200 or r.status_code == 201:
self.apis[name] = r.json()
else:
log.error('failed to update %s at %s, %s',
name, self.admin_url, r.text)
else:
log.info('API definition %s is up-to-date.', name)
else:
log.info('creating API definition %s.', name)
r = requests.put('%s/apis/' % self.admin_url,
json=definition, verify=self.verify_ssl)
if r.status_code == 200 or r.status_code == 201:
self.apis[name] = r.json()
else:
log.error('failed to create %s at %s, %s',
name, self.admin_url, r.text)
def get_all_exposed_tcp_ports(self, container):
"""
returns all TCP ports exposed by `container`.
"""
ports = container.attrs['NetworkSettings']['Ports']
return dict((k, v) for k, v in ports.items()
if k.endswith('/tcp') and v is not None)
def get_all_tcp_ports(self, container):
"""
returns all publishable TCP ports by `container`.
"""
ports = container.attrs['NetworkSettings']['Ports']
return dict((k, v) for k, v in ports.items() if k.endswith('/tcp'))
def get_environment_value_for_port(self, container, prefix, postfix, port):
"""
gets the environment variable for `prefix`_`port.split('/')[0]`_`postfix` or
for `prefix`_`postfix if the number of exposed ports == 1.
if no such environment variable exists or `prefix`_IGNORE is set, None is returned.
"""
env = self.get_environment_of_container(container)
tcp_ports = self.get_all_tcp_ports(container)
ignore_name = '%s_IGNORE' % prefix
if ignore_name in env:
return None
full_name = '%s_%s' % (prefix, postfix)
port_name = '%s_%s_%s' % (prefix, port.split('/')[0], postfix)
env = self.get_environment_of_container(container)
if port_name in env:
return env[port_name]
if full_name in env and len(tcp_ports) == 1:
return env[full_name]
return None
def get_service_name_for_port(self, container, port):
"""
get the value of the SERVICE_NAME environment variable for the specified `port`.
"""
return self.get_environment_value_for_port(
container, 'SERVICE', 'NAME', port)
def get_kong_api_for_port(self, container, port):
"""
get the value of the KONG_API environment variable for the specified `port`.
"""
return self.get_environment_value_for_port(
container, 'KONG', 'API', port)
def get_api_definitions(self, container):
"""
gets the Kong API definitions for the container.
the API definition is specified through the Port environment variable
KONG_API.
to avoid duplicate definitions, the fields `name` and `upstream_url` may
be omitted if a SERVICE_NAME environment variable has been specified for
the port. In that case, the name of the API will be the SERVICE_NAME,
while the upstream_url will be set to http://<service_name><self.dns_name>'.
"""
result = {}
ports = self.get_all_exposed_tcp_ports(container)
for port in ports:
api_definition = self.get_kong_api_for_port(container, port)
if api_definition is None:
continue
service_name = self.get_service_name_for_port(container, port)
upstream = 'http://%s%s' % (service_name,
self.dns_name) if service_name is not None else None
try:
api_definition = json.loads(api_definition)
if 'upstream_url' not in api_definition and upstream is not None:
api_definition['upstream_url'] = upstream
if 'name' not in api_definition and service_name is not None:
api_definition['name'] = service_name
except ValueError as e:
log.error(
'invalid KONG API definition for port %s of container %s, %s',
port, container.name,
e.message)
continue
if 'name' not in api_definition:
log.error(
'name field missing missing in API definition for port %s of container %s',
port, container.name)
continue
name = api_definition['name']
if name not in result:
result[name] = api_definition
else:
log.error(
'ignoring duplicate API definition for port %s of container %s',
port, container.name)
return result
def get_upstream_targets(self, container):
"""
get Kong upstream targets definition for the container.
for each exposed port which has a SERVICE_NAME specified a
entry will be added to the returned dictionary.
"<SERVICE_NAME>.`self.dns_name`" : [ "`self.hostname`:<exposed-port>" ]
duplicate service names are not allowed.
"""
result = {}
ports = self.get_all_exposed_tcp_ports(container)
for port in ports:
service_name = self.get_service_name_for_port(container, port)
if service_name is None:
continue
hostPort = ports[port][0]['HostPort']
target = '%s:%s' % (self.hostname, hostPort)
upstream = '%s%s' % (service_name, self.dns_name)
if upstream not in result:
result[upstream] = target
elif upstream is not None:
log.warn(
'ignoring duplicate service name for port %s of container %s',
port, container.name)
return result
def container_died(self):
"""
remove all invalid upstream targets. requires a full synchronization as we cannot link the
targets to the container id.
"""
self.sync()
def container_started(self, container_id):
"""
create upstream targets for all exposed services of the specified container.
"""
try:
container = self.dockr.containers.get(container_id)
state = container.attrs['State']['Health']['Status'] if 'Health' in container.attrs['State'] else None
if state is None or state == 'healthy':
targets = self.get_upstream_targets(container)
if len(targets) > 0:
for upstream in targets:
self.add_target(upstream, targets[upstream])
apis = self.get_api_definitions(container)
self.sync_apis(apis)
else:
log.info('container %s is not healthy.', container.name)
return
except docker.errors.NotFound:
log.error('container %s does not exist.', container_id)
def sync(self):
"""
ensure that the upstream targets are
actually reflecting docker instances running on this host.
"""
targets = {upstream: [] for upstream in self.targets}
apis = {}
containers = self.dockr.containers.list()
for container in containers:
state = container.attrs['State']['Health']['Status'] if 'Health' in container.attrs['State'] else None
if state is None or state == 'healthy':
container_targets = self.get_upstream_targets(container)
for upstream in container_targets:
if upstream not in targets:
targets[upstream] = []
targets[upstream].append(container_targets[upstream])
container_apis = self.get_api_definitions(container)
apis.update(container_apis)
else:
log.info('container %s is not healthy.', container.name)
for upstream in targets:
self.sync_upstream(upstream, targets[upstream])
self.sync_apis(apis)
def remove_all(self):
"""
remove all targets pointing to this host.
"""
for upstream in self.targets:
for target in self.targets[upstream]:
self.remove_target(upstream, target['target'])
def process_events(self):
"""
Process docker container start and die events.
"""
for e in self.dockr.events():
lines = filter(lambda l: len(l) > 0, e.split('\n'))
for line in lines:
event = json.loads(line)
if event['Type'] == 'container':
if event['status'] == 'start' or event['status'] == 'health_status: healthy':
self.container_started(event['id'])
if event['status'] == 'die' or event['status'] == 'health_status: unhealthy':
self.container_died()
else:
log.debug('skipping event "%s"', event['status'])
else:
pass # boring...
@click.group()
@click.option(
'--dns-name', required=False, default='.docker.internal',
help='to append to the service name')
@click.option(
'--hostname', required=False, default=socket.getfqdn(),
help='to use in target records.')
@click.option(
'--admin-url', required=False, default='http://localhost:8001',
help='of the Kong Admin API')
@click.option(
'--verify-ssl/--no-verify-ssl', required=False, default=True,
help='verify ssl connection to Kong Admin API')
@click.pass_context
def cli(ctx, dns_name, hostname, admin_url, verify_ssl):
e = KongServiceRegistrator(admin_url, dns_name, hostname, verify_ssl)
ctx.obj['registrator'] = e
@cli.command()
@click.pass_context
def daemon(ctx):
"""
process docker container 'start' and 'die' events to add and delete upstream targets accordingly.
"""
e = ctx.obj['registrator']
e.sync()
e.process_events()
@cli.command()
@click.pass_context
def remove_all(ctx):
"""
remove all upstream targets associated with this host.
"""
e = ctx.obj['registrator']
e.remove_all()
@cli.command()
@click.pass_context
def sync(ctx):
"""
Synchronize the upstream targets with the current docker containers.
"""
e = ctx.obj['registrator']
e.sync()
if __name__ == '__main__':
cli(obj={})