-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathnode_network_plugin.py
executable file
·775 lines (638 loc) · 25 KB
/
node_network_plugin.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
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
#!/usr/bin/env python2
# KuberDock - is a platform that allows users to run applications using Docker
# container images and create SaaS / PaaS based on these applications.
# Copyright (C) 2017 Cloud Linux INC
#
# This file is part of KuberDock.
#
# KuberDock is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# KuberDock is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with KuberDock; if not, see <http://www.gnu.org/licenses/>.
from __future__ import print_function
import errno
import json
import os
import shutil
import socket
import subprocess
import sys
import tarfile
import time
import urlparse
import zipfile
from collections import OrderedDict
from ConfigParser import ConfigParser
from contextlib import contextmanager
from StringIO import StringIO
from tempfile import NamedTemporaryFile
from urllib import urlopen
import ipaddress
import requests
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
KD_SCRIPT_DIR = '/var/lib/kuberdock/scripts'
FSLIMIT_PATH = os.path.join(KD_SCRIPT_DIR, 'fslimit.py')
STORAGE_MANAGE_DIR = 'node_storage_manage'
PLUGIN_PATH = '/usr/libexec/kubernetes/kubelet-plugins/net/exec/kuberdock/'
KD_CONF_PATH = PLUGIN_PATH + 'kuberdock.json'
PUBLIC_IP_RULE = 'iptables -w -{0} KUBERDOCK-PUBLIC-IP -t nat -d {1} ' \
'-p {2} --dport {3} -j DNAT --to-destination {4}:{5}'
PUBLIC_IP_MANGLE_RULE = \
'iptables -w -{0} KUBERDOCK-PUBLIC-IP -t mangle -d {1} ' \
'-p {2} --dport {3} -j MARK --set-mark 2'
PUBLIC_IP_POSTROUTING_RULE = 'iptables -w -{0} KUBERDOCK-PUBLIC-IP-SNAT ' \
'-t nat -s {1} -j SNAT --to-source {2}'
# MARKS:
# 1 - traffic to reject/drop
# 2 - traffic for public ip (will be added and used later)
# Possibly to stderr if in daemon mode or to our log-file
LOG_TO = sys.stderr
def glog(*args):
print(*args, file=LOG_TO)
class PluginException(Exception):
pass
class ETCD(object):
ignore = ('createdIndex', 'dir', 'key', 'modifiedIndex')
extended_statuses = 'extended_statuses'
users_path = 'users'
def __init__(self, path=None):
config = read_config_ini('/etc/sysconfig/flanneld')
self.server = config['flannel_etcd']
self.cert = config['etcd_certfile']
self.key = config['etcd_keyfile']
if path is None:
path = '/'.join([config['flannel_etcd_key'].strip('/'), 'plugin'])
self.path = path
def _url(self, *args):
url = '/'.join([self.server, 'v2/keys', self.path] + map(str, args))
return url
def _args(self, **kwargs):
args = dict(cert=(self.cert, self.key), verify=False)
args.update(kwargs)
return args
def _get(self, *args):
r = requests.get(self._url(*args), **self._args())
try:
items = r.json()['node']['nodes']
except Exception:
return {}
res_dict = {}
for item in items:
item_key = item['key'].rsplit('/')[-1]
item_dict = dict(
[(k, v) for k, v in item.items() if k not in self.ignore]
)
res_dict[item_key] = item_dict
return res_dict
def pods(self, user):
pods_list = []
for k, v in self._get(self.users_path, user).items():
try:
value = json.loads(v['value'])
except ValueError:
value = {'node': None, 'service': None}
pods_list.append((k, value))
pods = dict(pods_list)
return pods
def users(self):
users = []
for user in self._get(self.users_path):
try:
users.append(int(user))
except ValueError as e:
glog('Error while try to convert user_id to int: {}'.format(e))
return users
def delete_user(self, user, pod):
url = self._url(self.users_path, user, pod)
self.delete(url)
def delete(self, url):
requests.delete(url, **self._args())
def registered_hosts(self):
registered_hosts = list(self._get('registered_hosts'))
return registered_hosts
def delete_ex_status(self, namespace, k8s_pod):
url = self._url(self.extended_statuses, namespace, k8s_pod)
self.delete(url)
def put_ex_status(self, namespace, k8s_pod, message):
url = self._url(self.extended_statuses, namespace, k8s_pod)
self.put(url, message)
def put(self, url, value):
requests.put(url, data={'value': value}, **self._args())
def wait(self):
requests.get(
self._url(),
**self._args(params=dict(wait=True, recursive=True))
)
def read_config_ini(filename):
section = '__section__'
with open(filename) as config_fp_orig:
config_raw_orig = config_fp_orig.read()
config_raw_new = '[{0}]\n'.format(section)
config_raw_new += config_raw_orig
config_fp_new = StringIO(config_raw_new)
config = ConfigParser()
config.readfp(config_fp_new)
config_fp_new.close()
config_dict = dict([(k, v.strip('"')) for k, v in config.items(section)])
return config_dict
def read_config_json(filename):
with open(filename) as f:
return json.loads(f.read())
def set_config(filename, data):
with open(filename) as f:
lines = f.read().splitlines()
d = OrderedDict([l.split('=', 1) for l in lines])
d.update(data)
with open(filename, 'w') as f:
f.writelines(['{0}={1}\n'.format(k, v) for k, v in d.items()])
def _update_ipset(set_name, ip_list, set_type='hash:ip'):
set_temp = '{0}_temp'.format(set_name)
subprocess.call(['ipset', '-exist', 'create', set_name, set_type])
subprocess.call(['ipset', '-exist', 'create', set_temp, set_type])
subprocess.call(['ipset', 'flush', set_temp])
for ip in ip_list:
subprocess.call(['ipset', 'add', set_temp, ip])
subprocess.call(['ipset', 'swap', set_temp, set_name])
subprocess.call(['ipset', 'destroy', set_temp])
def update_ipset():
shared_ips = ['10.254.0.1', '10.254.0.10']
nodes_ips = set()
etcd = ETCD()
for user in etcd.users():
set_name = 'kuberdock_user_{0}'.format(user)
user_ip_list = set(shared_ips)
for pod_ip, value in etcd.pods(user).items():
user_ip_list.add(pod_ip)
if value['service']:
user_ip_list.add(value['service'])
if value['node']:
nodes_ips.add(value['node'])
_update_ipset(set_name, user_ip_list)
nodes_ips.update(etcd.registered_hosts())
_update_ipset('kuberdock_nodes', nodes_ips)
def modify_ip(cmd, ip, iface):
if subprocess.call(['ip', 'addr', cmd, ip + '/32', 'dev', iface]):
raise PluginException(
'Error {0} ip: {1} on iface: {2}'.format(cmd, ip, iface))
if cmd == 'add':
subprocess.call(
['arping', '-I', iface, '-A', ip, '-c', '10', '-w', '1'])
return 0
def get_pod_spec(pod_spec_file):
if not os.path.exists(pod_spec_file):
raise PluginException('Pod spec file is not exists. Skip call')
with open(pod_spec_file) as f:
return json.load(f)
def get_public_ip(pod):
config = read_config_json(KD_CONF_PATH)
master = config['master']
node = config['node']
token = config['token']
try:
r = requests.get(
'https://{0}/api/ippool/get-public-ip/{1}/{2}?token={3}'.format(
master, node, pod, token
),
verify=False).json()
if r['status'] == 'OK':
ip = r['data']
glog('Requested Public IP {0} for Pod {1}'.format(ip, pod))
return ip
except:
pass
def handle_public_ip(
action, public_ip, pod_ip, iface, namespace, k8s_pod_id, pod_data,
pod_spec_file):
with send_feedback_context(namespace, k8s_pod_id):
fixed_ip_pools = (public_ip == 'true')
if fixed_ip_pools:
public_ip = get_public_ip(namespace)
if public_ip is None:
raise PluginException('Cannot get Public IP for {0}'.format(
namespace)
)
set_config(pod_data, {'POD_PUBLIC_IP': public_ip})
if action not in ('add', 'del',):
raise PluginException('Unknown action for public ip. Skip call.')
spec = get_pod_spec(pod_spec_file)
try:
ports_s = spec['metadata']['annotations']['kuberdock-pod-ports']
ports = json.loads(ports_s)
except (TypeError, ValueError, KeyError) as e:
raise PluginException(
'Error loading ports from spec "{0}" Skip call.'.format(e))
if not ports:
return 4
for container in ports:
for port_spec in container:
is_public = port_spec.get('isPublic', False)
if not is_public:
continue
container_port = port_spec.get('containerPort')
if not container_port:
raise PluginException(
'Something went wrong. Bad pod spec received. Skip')
proto = port_spec.get('protocol', 'tcp')
host_port = port_spec.get('hostPort', None) or container_port
if action == 'add':
add_ip(container_port, host_port, pod_ip, proto, public_ip)
elif action == 'del':
delete_ip(container_port, host_port, pod_ip, proto,
public_ip)
# Temporarily disable check. Maybe will be removed completely
# if not (fixed_ip_pools or is_fixed_ip_pools_mode_enabled()):
# modify_ip(action, public_ip, iface)
modify_ip(action, public_ip, iface)
return 0
return 1
def is_fixed_ip_pools_mode_enabled():
enabled_options = ('1', 'on', 't', 'true', 'y', 'yes')
config = read_config_json(KD_CONF_PATH)
return config['fixed_ip_pools'].lower() in enabled_options
def delete_ip(container_port, host_port, pod_ip, proto, public_ip):
subprocess.call(
PUBLIC_IP_RULE.format('D', public_ip, proto, host_port,
pod_ip, container_port).split(
' '))
subprocess.call(
PUBLIC_IP_MANGLE_RULE.format('D', public_ip, proto,
host_port).split(' '))
subprocess.call(
PUBLIC_IP_POSTROUTING_RULE.format('D', pod_ip, public_ip).split(' '))
def add_ip(container_port, host_port, pod_ip, proto, public_ip):
if subprocess.call(
PUBLIC_IP_RULE.format('C', public_ip, proto,
host_port, pod_ip,
container_port).split(' ')):
subprocess.call(
PUBLIC_IP_RULE.format('I', public_ip, proto,
host_port, pod_ip,
container_port).split(' '))
if subprocess.call(
PUBLIC_IP_MANGLE_RULE.format('C', public_ip, proto,
host_port).split(
' ')):
subprocess.call(
PUBLIC_IP_MANGLE_RULE.format('I', public_ip, proto,
host_port).split(' '))
if subprocess.call(
PUBLIC_IP_POSTROUTING_RULE.format('C', pod_ip,
public_ip).split(' ')):
subprocess.call(
PUBLIC_IP_POSTROUTING_RULE.format('I', pod_ip,
public_ip).split(' '))
def init():
config = read_config_ini('/run/flannel/subnet.env')
config_network = config['flannel_network']
config_subnet = config['flannel_subnet']
_update_ipset('kuberdock_flannel', [config_network], set_type='hash:net')
_update_ipset('kuberdock_nodes', [])
_update_ipset('kuberdock_cluster',
['kuberdock_flannel', 'kuberdock_nodes'],
set_type='list:set')
network = ipaddress.ip_network(unicode(config_network))
subnet = ipaddress.ip_network(unicode(config_subnet), strict=False)
etcd = ETCD()
for user in etcd.users():
for pod in etcd.pods(user):
pod_ip = ipaddress.ip_address(pod)
if pod_ip not in network or pod_ip in subnet:
etcd.delete_user(user, pod)
update_ipset()
def watch(callback, args=None, path=None):
if args is None:
args = ()
etcd = ETCD(path)
while True:
try:
callback(*args)
etcd.wait()
except KeyboardInterrupt:
break
except requests.RequestException as e:
glog("Error while request etcd: {}".format(e))
time.sleep(5)
def send_feedback(namespace, k8s_pod, message):
glog(message)
etcd = ETCD()
etcd.put_ex_status(namespace, k8s_pod, message)
@contextmanager
def send_feedback_context(namespace, k8s_pod):
try:
yield
except Exception as e:
status = "{}: {}".format(type(e).__name__, e.message)
send_feedback(namespace, k8s_pod, status)
# Will output exception to the caller and return 1 exit code
raise
def remove_feedback(namespace, k8s_pod):
etcd = ETCD()
etcd.delete_ex_status(namespace, k8s_pod)
def handle_ex_status(action, namespace=None, k8s_pod=None, status=None):
if action == 'add':
send_feedback(namespace, k8s_pod, status)
elif action == 'delete':
remove_feedback(namespace, k8s_pod)
class VolumeRestoreException(Exception):
pass
class VolumeSpec(object):
def __init__(self, path, size, name, backup_url=None):
"""
:param path: Local path.
:param size: Size.
:param name: Name.
:param backup_url: Optional. URL which contains backup archives.
Expected archive in format .tar.gz or .zip.
"""
self.path = path
self.size = size
self.name = name
self.backup_url = backup_url
class FileAdapter(requests.adapters.BaseAdapter):
def send(self, req, **kwargs):
resp = requests.Response()
try:
resp.raw = urlopen(req.url)
content_length = resp.raw.headers.get('content-length')
if content_length is not None:
resp.headers['Content-Length'] = content_length
except IOError as e:
if e.errno == errno.EACCES:
resp.status_code = requests.codes.forbidden
elif e.errno == errno.ENOENT:
resp.status_code = requests.codes.not_found
else:
resp.status_code = requests.codes.bad_request
except OSError:
resp.status_code = requests.codes.bad_request
else:
resp.status_code = requests.codes.ok
return resp
def close(self):
pass
def _run_storage_manage_command(args):
"""Executes node storage manager command with specified arguments.
:param args: list of arguments
:return: tuple of success flag, error message
"""
env = os.environ.copy()
env['PYTHONPATH'] = KD_SCRIPT_DIR
try:
res = subprocess.check_output(
['python2', '-m', '{}.{}'.format(STORAGE_MANAGE_DIR, 'manage')]
+ args,
env=env
)
except subprocess.CalledProcessError as err:
return False, str(err)
try:
response = json.loads(res)
if response['status'] != 'OK':
return False, response['data']['message']
except (ValueError, TypeError, KeyError):
return False, 'Invalid result format from storage script'
return True, None
class VolumeManager(object):
"""Class that creates, restores from backup and deletes volumes."""
def restore_if_needed(self, volume_spec):
"""If backup url specified, it downloads the backup archive from
backup url and extracts it into a volume path.
"""
if not volume_spec.backup_url:
return
url_path = urlparse.urlparse(volume_spec.backup_url).path
extractor = self._ArchiveExtractor()
try:
extractor.detect_type_by_extension(url_path)
except extractor.UnknownType:
raise VolumeRestoreException(
'Unknown type of archive got from {url}. '
'At the moment only {supported_formats} formats '
'are supported'.format(
url=volume_spec.backup_url,
supported_formats=', '.join(
x.strip('.') for x in extractor.supported_formats)
))
requests_session = requests.session()
requests_session.mount('file://', FileAdapter())
requests_session.mount('ftp://', FileAdapter())
try:
r = requests_session.get(volume_spec.backup_url, stream=True,
verify=False)
r.raise_for_status()
with NamedTemporaryFile('w+b') as f:
for chunk in r.iter_content(chunk_size=1024):
if chunk:
f.write(chunk)
f.seek(0)
extractor.extract(f, volume_spec.path)
except (requests.exceptions.RequestException, socket.timeout):
raise VolumeRestoreException(
'Connection failure while downloading backup from {}'
.format(volume_spec.backup_url))
except extractor.BadArchive:
raise VolumeRestoreException(
'An error occurred while extracting archive got from {}'
.format(volume_spec.backup_url))
def create(self, volume_spec):
"""
Creates a folder for the volume and sets correct SELinux type on it
"""
err_message = u'Failed to create local storage: {}'
ok, err = _run_storage_manage_command([
'create-volume',
'--path', volume_spec.path,
'--quota', volume_spec.size.replace('g', '')
])
if not ok:
raise PluginException(err_message.format(err))
try:
subprocess.call(
['chcon', '-Rt', 'svirt_sandbox_file_t', volume_spec.path])
except os.error:
raise PluginException(err_message.format(volume_spec.path))
@staticmethod
def remove(volume_spec):
"""
Physically removes directory
"""
ok, err = _run_storage_manage_command([
'remove-volume',
'--path', volume_spec.path
])
if not ok:
raise PluginException(u'Failed to remove volume: {}'.format(err))
class _ArchiveExtractor(object):
supported_formats = ['.tar.gz', '.zip']
def __init__(self, archive_type=None):
self.archive_type = archive_type
def detect_type_by_extension(self, path):
for ext in self.supported_formats:
if path.endswith(ext):
self.archive_type = ext
return self
raise self.UnknownType
def extract(self, file_obj, path):
assert self.archive_type
extractors = {
'.tar.gz': self._extract_tar,
'.zip': self._extract_zip
}
try:
extract = extractors[self.archive_type]
except KeyError:
raise self.UnknownType
else:
return extract(file_obj, path)
def _extract_tar(self, file_obj, path):
try:
with tarfile.open(fileobj=file_obj, mode='r:gz') as archive:
archive.extractall(path)
except tarfile.TarError:
raise self.BadArchive
def _extract_zip(self, file_obj, path):
try:
with zipfile.ZipFile(file_obj) as archive:
archive.extractall(path)
except zipfile.BadZipfile:
raise self.BadArchive
class UnknownType(Exception):
pass
class BadArchive(Exception):
pass
class LocalStorage(object):
volume_manager = VolumeManager()
@classmethod
def init(cls, pod_spec_file):
annotations, metadata, vol_annotations = cls._parse_pod_spec(
pod_spec_file)
volumes = [cls.extract_volume_spec(a)
for a in vol_annotations if cls.check_annotation(a)]
cls.create_volumes(volumes)
try:
cls.restore_backups(volumes)
except VolumeRestoreException:
cls.remove_volumes(volumes)
raise
@classmethod
def extract_volume_spec(cls, annotation):
"""
Extracts volumes specifications from annotation.
:param annotation: POD's volume annotation.
:return: Instance of :class:`VolumeSpec`.
"""
ls = annotation['localStorage']
backup_url = annotation.get('backupUrl')
path, size, name = ls['path'], ls.get('size', 1), ls['name']
volume_spec = VolumeSpec(path=path, size='{}g'.format(size), name=name,
backup_url=backup_url)
return volume_spec
@classmethod
def create_volumes(cls, volume_specs):
for v in volume_specs:
cls.volume_manager.create(v)
@classmethod
def restore_backups(cls, volume_specs):
for v in volume_specs:
cls.volume_manager.restore_if_needed(v)
@classmethod
def remove_volumes(cls, volume_specs):
for v in volume_specs:
cls.volume_manager.remove(v)
@staticmethod
def check_annotation(annotation):
"""
Verifies if POD's annotation has all necessary information for local
storage creation and if storage directory was already created
:param annotation: dictionary containing POD's annotations
:return: True if annotations are good and False otherwise
"""
try:
if os.path.exists(annotation['localStorage']['path']):
return False
except (KeyError, TypeError):
return False
return True
@classmethod
def _parse_pod_spec(cls, pod_spec_file):
try:
metadata = get_pod_spec(pod_spec_file)['metadata']
annotations = metadata['annotations']
vol_annotations = json.loads(
annotations.get('kuberdock-volume-annotations', '[]'))
except (TypeError, ValueError, KeyError) as e:
raise PluginException(
'Error loading volume annotations from spec "{0}" '
'Skip call.'.format(e)
)
return annotations, metadata, vol_annotations
def get_k8s_info():
server = token = ''
with open('/etc/kubernetes/configfile') as cf:
for l in cf:
l = l.strip()
if l.startswith('server:'):
server = l.split('server:')[-1].strip()
if l.startswith('token:'):
token = l.split('token:')[-1].strip()
if server and token:
break
return server, token
def existing_pods():
server, token = get_k8s_info()
r = requests.get('{0}/api/v1/pods'.format(server),
headers={'Authorization': 'Bearer {0}'.format(token)},
verify=False)
if r.status_code != 200:
return
pods = set()
for pod in r.json()['items']:
pod_metadata = pod['metadata']
pod_namespace = pod_metadata['namespace']
pod_name = pod_metadata['name']
pods.add((pod_namespace, pod_name))
return pods
def node_known_pods():
data_dir = os.path.join(PLUGIN_PATH, 'data')
pods = set()
namespaces = [d for d in os.listdir(data_dir)
if os.path.isdir(os.path.join(data_dir, d))]
for namespace in namespaces:
name = os.listdir(os.path.join(data_dir, namespace))[0]
pods.add((namespace, name))
return pods
def teardown_unexisting():
pods_to_teardown = node_known_pods() - existing_pods()
for pod_namespace, pod_name in pods_to_teardown:
subprocess.call([os.path.join(PLUGIN_PATH, 'kuberdock'),
'teardown', pod_namespace, pod_name])
def main(action, *args):
if action == 'init':
# TODO must be called after each restart service and flush/restore
# correct iptables rules and chains (incl. public ip chains)
init()
elif action == 'setup':
handle_public_ip('add', *args)
elif action == 'initlocalstorage':
LocalStorage.init(*args)
elif action == 'teardown':
handle_public_ip('del', *args)
elif action == 'update':
update_ipset()
elif action == 'watch':
watch(update_ipset)
elif action == 'ex_status':
handle_ex_status(*args)
elif action == 'teardown_unexisting':
teardown_unexisting()
if __name__ == '__main__':
main(*sys.argv[1:])