/
fetcd.py
972 lines (858 loc) · 38.5 KB
/
fetcd.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
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
# -*- coding: utf-8 -*-
# Copyright 2015 Metaswitch Networks
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
felix.fetcd
~~~~~~~~~~~~
Our API to etcd. Contains function to synchronize felix with etcd
as well as reporting our status into etcd.
"""
import functools
import os
import random
import json
import logging
import socket
import subprocess
from etcd import EtcdException, EtcdKeyNotFound
import gevent
import sys
from gevent.event import Event
from calico import common
from calico.common import ValidationFailed, validate_ip_addr, canonicalise_ip
from calico.datamodel_v1 import (
VERSION_DIR, CONFIG_DIR, RULES_KEY_RE, TAGS_KEY_RE,
dir_for_per_host_config, PROFILE_DIR, HOST_DIR, EndpointId, HOST_IP_KEY_RE,
IPAM_V4_CIDR_KEY_RE, key_for_last_status, key_for_status, FELIX_STATUS_DIR,
get_endpoint_id_from_key, dir_for_felix_status, ENDPOINT_STATUS_ERROR,
ENDPOINT_STATUS_DOWN, ENDPOINT_STATUS_UP
)
from calico.etcddriver.protocol import (
MessageReader, MSG_TYPE_INIT, MSG_TYPE_CONFIG, MSG_TYPE_RESYNC,
MSG_KEY_ETCD_URL, MSG_KEY_HOSTNAME, MSG_KEY_LOG_FILE, MSG_KEY_SEV_FILE,
MSG_KEY_SEV_SYSLOG, MSG_KEY_SEV_SCREEN, STATUS_IN_SYNC,
MSG_TYPE_CONFIG_LOADED, MSG_KEY_GLOBAL_CONFIG, MSG_KEY_HOST_CONFIG,
MSG_TYPE_UPDATE, MSG_KEY_KEY, MSG_KEY_VALUE, MessageWriter,
MSG_TYPE_STATUS, MSG_KEY_STATUS
)
from calico.etcdutils import (
EtcdClientOwner, delete_empty_parents, PathDispatcher, EtcdEvent
)
from calico.felix.actor import Actor, actor_message
from calico.felix.futils import (
intern_dict, intern_list, logging_exceptions, iso_utc_timestamp, IPV4,
IPV6, StatCounter
)
from calico.monotonic import monotonic_time
_log = logging.getLogger(__name__)
RETRY_DELAY = 5
# Etcd paths that we care about for use with the PathDispatcher class.
# We use angle-brackets to name parameters that we want to capture.
PER_PROFILE_DIR = PROFILE_DIR + "/<profile_id>"
TAGS_KEY = PER_PROFILE_DIR + "/tags"
RULES_KEY = PER_PROFILE_DIR + "/rules"
PER_HOST_DIR = HOST_DIR + "/<hostname>"
HOST_IP_KEY = PER_HOST_DIR + "/bird_ip"
WORKLOAD_DIR = PER_HOST_DIR + "/workload"
PER_ORCH_DIR = WORKLOAD_DIR + "/<orchestrator>"
PER_WORKLOAD_DIR = PER_ORCH_DIR + "/<workload_id>"
ENDPOINT_DIR = PER_WORKLOAD_DIR + "/endpoint"
PER_ENDPOINT_KEY = ENDPOINT_DIR + "/<endpoint_id>"
CONFIG_PARAM_KEY = CONFIG_DIR + "/<config_param>"
PER_HOST_CONFIG_PARAM_KEY = PER_HOST_DIR + "/config/<config_param>"
IPAM_DIR = VERSION_DIR + "/ipam"
IPAM_V4_DIR = IPAM_DIR + "/v4"
POOL_V4_DIR = IPAM_V4_DIR + "/pool"
CIDR_V4_KEY = POOL_V4_DIR + "/<pool_id>"
# Max number of events from driver process before we yield to another greenlet.
MAX_EVENTS_BEFORE_YIELD = 200
# Global diagnostic counters.
_stats = StatCounter("Etcd counters")
class EtcdAPI(EtcdClientOwner, Actor):
"""
Our API to etcd.
Since the python-etcd API is blocking, we defer API watches to
a worker greenlet and communicate with it via Events.
We use a second worker for writing our status back to etcd. This
avoids sharing the etcd client between reads and writes, which is
problematic because we need to handle EtcdClusterIdChanged for polls
but not for writes.
"""
def __init__(self, config, hosts_ipset):
super(EtcdAPI, self).__init__(config.ETCD_ADDR)
self._config = config
# Timestamp storing when the EtcdAPI started. This info is needed
# in order to report uptime to etcd.
self._start_time = monotonic_time()
# Create an Actor to report per-endpoint status into etcd.
self.status_reporter = EtcdStatusReporter(config)
# Start up the main etcd-watching greenlet. It will wait for an
# event from us before doing anything.
self._watcher = _FelixEtcdWatcher(config,
self,
self.status_reporter,
hosts_ipset)
self._watcher.link(self._on_worker_died)
self._watcher.start()
# Start up a greenlet to trigger periodic resyncs.
self._resync_greenlet = gevent.spawn(self._periodically_resync)
self._resync_greenlet.link_exception(self._on_worker_died)
# Start up greenlet to report felix's liveness into etcd.
self.done_first_status_report = False
self._status_reporting_greenlet = gevent.spawn(
self._periodically_report_status
)
self._status_reporting_greenlet.link_exception(self._on_worker_died)
# Start the status reporter.
self.status_reporter.start()
self.status_reporter.greenlet.link(self._on_worker_died)
@logging_exceptions
def _periodically_resync(self):
"""
Greenlet: if enabled, periodically triggers a resync from etcd.
:return: Does not return, unless periodic resync disabled.
"""
_log.info("Started periodic resync thread, waiting for config.")
self._watcher.configured.wait()
interval = self._config.RESYNC_INTERVAL
_log.info("Config loaded, resync interval %s.", interval)
if interval == 0:
_log.info("Interval is 0, periodic resync disabled.")
return
while True:
# Jitter by 20% of interval.
jitter = random.random() * 0.2 * interval
sleep_time = interval + jitter
_log.debug("After jitter, next periodic resync will be in %.1f "
"seconds.", sleep_time)
gevent.sleep(sleep_time)
_stats.increment("Periodic resync")
self.force_resync(reason="periodic resync", async=True)
@logging_exceptions
def _periodically_report_status(self):
"""
Greenlet: periodically writes Felix's status into etcd.
:return: Does not return, unless reporting disabled.
"""
_log.info("Started status reporting thread. Waiting for config.")
self._watcher.configured.wait()
ttl = self._config.REPORTING_TTL_SECS
interval = self._config.REPORTING_INTERVAL_SECS
_log.debug("Reporting interval: %s, TTL: %s", interval, ttl)
if interval == 0:
_log.info("Interval is 0, status reporting disabled.")
return
while True:
try:
self._update_felix_status(ttl)
except EtcdException as e:
_log.warning("Error when trying to check into etcd (%r), "
"retrying after %s seconds.", e, RETRY_DELAY)
self.reconnect()
gevent.sleep(RETRY_DELAY)
else:
# Jitter by 10% of interval.
jitter = random.random() * 0.1 * interval
sleep_time = interval + jitter
gevent.sleep(sleep_time)
def _update_felix_status(self, ttl):
"""
Writes two keys to etcd:
* uptime in secs
* felix status in JSON - containing current time in ISO 8601 Zulu
format
:param: ttl int: time to live in sec - lifetime of the status report
"""
time_formatted = iso_utc_timestamp()
uptime = monotonic_time() - self._start_time
status = {
"time": time_formatted,
"uptime": uptime,
"first_update": not self.done_first_status_report,
}
status_value = json.dumps(status)
_log.debug("Reporting felix status/uptime (%.1fs) using hostname %s",
uptime, self._config.HOSTNAME)
status_key = key_for_last_status(self._config.HOSTNAME)
self.client.set(status_key, status_value)
status_key = key_for_status(self._config.HOSTNAME)
self.client.set(status_key, status_value, ttl=ttl)
self.done_first_status_report = True
@actor_message()
def load_config(self):
"""
Loads our config from etcd, should only be called once.
:return: an Event which is triggered when the config has been loaded.
"""
self._watcher.load_config.set()
return self._watcher.configured
@actor_message()
def start_watch(self, splitter):
"""
Starts watching etcd for changes. Implicitly loads the config
if it hasn't been loaded yet.
"""
assert self._watcher.load_config.is_set(), (
"load_config() should be called before start_watch()."
)
self._watcher.splitter = splitter
self._watcher.begin_polling.set()
@actor_message()
def force_resync(self, reason="unknown"):
"""
Force a resync with etcd after the current poll completes.
:param str reason: Optional reason to log out.
"""
_log.info("Forcing a resync with etcd. Reason: %s.", reason)
self._watcher.resync_requested = True
if self._config.REPORT_ENDPOINT_STATUS:
_log.info("Endpoint status reporting enabled, marking existing "
"endpoints as dirty so they'll be resynced.")
self.status_reporter.resync(async=True)
def _on_worker_died(self, watch_greenlet):
"""
Greenlet: spawned by the gevent Hub if the etcd watch loop ever
stops, kills the process.
"""
_log.critical("Worker greenlet died: %s; exiting.", watch_greenlet)
sys.exit(1)
class _FelixEtcdWatcher(gevent.Greenlet):
"""
Greenlet that communicates with the etcd driver over a socket.
* Does the initial handshake with the driver, sending it the init
message.
* Receives the pre-loaded config from the driver and uses that
to do Felix's one-off configuration.
* Sends the relevant config back to the driver.
* Processes the event stream from the driver, sending it on to
the splitter.
This class is similar to the EtcdWatcher class in that it uses
a PathDispatcher to fan out updates but it doesn't own an etcd
connection of its own.
"""
def __init__(self, config, etcd_api, status_reporter, hosts_ipset):
super(_FelixEtcdWatcher, self).__init__()
self._config = config
self._etcd_api = etcd_api
self._status_reporter = status_reporter
self.hosts_ipset = hosts_ipset
# Whether we've been in sync with etcd at some point.
self._been_in_sync = False
# Keep track of the config loaded from etcd so we can spot if it
# changes.
self.last_global_config = None
self.last_host_config = None
self.my_config_dir = dir_for_per_host_config(self._config.HOSTNAME)
# Events triggered by the EtcdAPI Actor to tell us to load the config
# and start polling. These are one-way flags.
self.load_config = Event()
self.begin_polling = Event()
# Event that we trigger once the config is loaded.
self.configured = Event()
# Polling state initialized at poll start time.
self.splitter = None
# Next-hop IP addresses of our hosts, if populated in etcd.
self.ipv4_by_hostname = {}
# Forces a resync after the current poll if set. Safe to set from
# another thread. Automatically reset to False after the resync is
# triggered.
self.resync_requested = False
self.dispatcher = PathDispatcher()
# The Popen object for the driver.
self._driver_process = None
# Stats.
self.read_count = 0
self.msgs_processed = 0
self.last_rate_log_time = monotonic_time()
# Register for events when values change.
self._register_paths()
def _register_paths(self):
"""
Program the dispatcher with the paths we care about.
"""
reg = self.dispatcher.register
# Profiles and their contents.
reg(TAGS_KEY, on_set=self.on_tags_set, on_del=self.on_tags_delete)
reg(RULES_KEY, on_set=self.on_rules_set, on_del=self.on_rules_delete)
# Hosts and endpoints.
reg(HOST_IP_KEY,
on_set=self.on_host_ip_set,
on_del=self.on_host_ip_delete)
reg(PER_ENDPOINT_KEY,
on_set=self.on_endpoint_set, on_del=self.on_endpoint_delete)
reg(CIDR_V4_KEY,
on_set=self.on_ipam_v4_pool_set,
on_del=self.on_ipam_v4_pool_delete)
# Configuration keys. If any of these is changed or created, we'll
# restart to pick up the change.
reg(CONFIG_PARAM_KEY,
on_set=self._on_config_updated,
on_del=self._on_config_updated)
reg(PER_HOST_CONFIG_PARAM_KEY,
on_set=self._on_host_config_updated,
on_del=self._on_host_config_updated)
@logging_exceptions
def _run(self):
# Don't do anything until we're told to load the config.
_log.info("Waiting for load_config event...")
self.load_config.wait()
_log.info("...load_config set. Starting driver read %s loop", self)
# Start the driver process and wait for it to connect back to our
# socket.
self._msg_reader, self._msg_writer = self._start_driver()
# Loop reading from the socket and processing messages.
self._loop_reading_from_driver()
def _loop_reading_from_driver(self):
while True:
for msg_type, msg in self._msg_reader.new_messages(timeout=1):
self._dispatch_msg_from_driver(msg_type, msg)
if self.resync_requested:
_log.info("Resync requested, sending resync request to driver")
self.resync_requested = False
self._msg_writer.send_message(MSG_TYPE_RESYNC)
# Check that the driver hasn't died. The recv() call should
# raise an exception when the buffer runs dry but this usually
# gets hit first.
driver_rc = self._driver_process.poll()
if driver_rc is not None:
_log.critical("Driver process died with RC = %s. Felix must "
"exit.", driver_rc)
die_and_restart()
def _dispatch_msg_from_driver(self, msg_type, msg):
# Optimization: put update first in the "switch" block because
# it's on the critical path.
if msg_type == MSG_TYPE_UPDATE:
_stats.increment("Update messages from driver")
self._on_update_from_driver(msg)
elif msg_type == MSG_TYPE_CONFIG_LOADED:
_stats.increment("Config loaded messages from driver")
self._on_config_loaded_from_driver(msg)
elif msg_type == MSG_TYPE_STATUS:
_stats.increment("Status messages from driver")
self._on_status_from_driver(msg)
else:
raise RuntimeError("Unexpected message %s" % msg)
self.msgs_processed += 1
if self.msgs_processed % MAX_EVENTS_BEFORE_YIELD == 0:
# Yield to ensure that other actors make progress.
# Sleep must be non-zero to work around gevent
# issue where we could be immediately rescheduled.
gevent.sleep(0.000001)
def _on_update_from_driver(self, msg):
"""
Called when the driver sends us a key/value pair update.
After the initial handshake, the stream of events consists
entirely of updates unless something happens to change the
state of the driver.
:param dict msg: The message received from the driver.
"""
assert self.configured.is_set(), "Received update before config"
# The driver starts polling immediately, make sure we block until
# everyone else is ready to receive updates.
self.begin_polling.wait()
# Unpack the message.
key = msg[MSG_KEY_KEY]
value = msg[MSG_KEY_VALUE]
_log.debug("Update from driver: %s -> %s", key, value)
# Output some very coarse stats.
self.read_count += 1
if self.read_count % 1000 == 0:
now = monotonic_time()
delta = now - self.last_rate_log_time
_log.info("Processed %s updates from driver "
"%.1f/s", self.read_count, 1000.0 / delta)
self.last_rate_log_time = now
# Wrap the update in an EtcdEvent object so we can dispatch it via the
# PathDispatcher.
n = EtcdEvent("set" if value is not None else "delete", key, value)
self.dispatcher.handle_event(n)
def _on_config_loaded_from_driver(self, msg):
"""
Called when we receive a config loaded message from the driver.
This message is expected once per resync, when the config is
pre-loaded by the driver.
On the first call, responds to the driver synchronously with a
config response.
If the config has changed since a previous call, triggers Felix
to die.
"""
global_config = msg[MSG_KEY_GLOBAL_CONFIG]
host_config = msg[MSG_KEY_HOST_CONFIG]
_log.info("Config loaded by driver:\n"
"Global: %s\nPer-host: %s",
global_config,
host_config)
if self.configured.is_set():
# We've already been configured. We don't yet support
# dynamic config update so instead we check if the config
# has changed and die if it has.
_log.info("Checking configuration for changes...")
if (host_config != self.last_host_config or
global_config != self.last_global_config):
_log.warning("Felix configuration has changed, "
"felix must restart.")
_log.info("Old host config: %s", self.last_host_config)
_log.info("New host config: %s", host_config)
_log.info("Old global config: %s",
self.last_global_config)
_log.info("New global config: %s", global_config)
die_and_restart()
else:
# First time loading the config. Report it to the config
# object. Take copies because report_etcd_config is
# destructive.
self.last_host_config = host_config.copy()
self.last_global_config = global_config.copy()
self._config.report_etcd_config(host_config,
global_config)
# Config now fully resolved, inform the driver.
driver_log_file = self._config.DRIVERLOGFILE
self._msg_writer.send_message(
MSG_TYPE_CONFIG,
{
MSG_KEY_LOG_FILE: driver_log_file,
MSG_KEY_SEV_FILE: self._config.LOGLEVFILE,
MSG_KEY_SEV_SCREEN: self._config.LOGLEVSCR,
MSG_KEY_SEV_SYSLOG: self._config.LOGLEVSYS,
}
)
self.configured.set()
def _on_status_from_driver(self, msg):
"""
Called when we receive a status update from the driver.
The driver sends us status messages whenever its status changes.
It moves through these states:
(1) wait-for-ready (waiting for the global ready flag to become set)
(2) resync (resyncing with etcd, processing a snapshot and any
concurrent events)
(3) in-sync (snapshot processsing complete, now processing only events
from etcd)
If the driver falls out of sync with etcd then it will start again
from (1).
If the status is in-sync, triggers the relevant processing.
"""
status = msg[MSG_KEY_STATUS]
_log.info("etcd driver status changed to %s", status)
if status == STATUS_IN_SYNC and not self._been_in_sync:
# We're now in sync, tell the Actors that need to do start-of-day
# cleanup.
self.begin_polling.wait() # Make sure splitter is set.
self._been_in_sync = True
self.splitter.on_datamodel_in_sync(async=True)
if self._config.REPORT_ENDPOINT_STATUS:
self._status_reporter.clean_up_endpoint_statuses(async=True)
self._update_hosts_ipset()
def _start_driver(self):
"""
Starts the driver subprocess, connects to it over the socket
and sends it the init message.
Stores the Popen object in self._driver_process for future
access.
:return: the connected socket to the driver.
"""
_log.info("Creating server socket.")
try:
os.unlink("/run/felix-driver.sck")
except OSError:
_log.debug("Failed to delete driver socket, assuming it "
"didn't exist.")
update_socket = socket.socket(socket.AF_UNIX,
socket.SOCK_STREAM)
update_socket.bind("/run/felix-driver.sck")
update_socket.listen(1)
self._driver_process = subprocess.Popen([sys.executable,
"-m",
"calico.etcddriver",
"/run/felix-driver.sck"])
_log.info("Started etcd driver with PID %s", self._driver_process.pid)
update_conn, _ = update_socket.accept()
_log.info("Accepted connection on socket")
# No longer need the server socket, remove it.
try:
os.unlink("/run/felix-driver.sck")
except OSError:
# Unexpected but carry on...
_log.exception("Failed to unlink socket")
else:
_log.info("Unlinked server socket")
# Wrap the socket in reader/writer objects that simplify using the
# protocol.
reader = MessageReader(update_conn)
writer = MessageWriter(update_conn)
# Give the driver its config.
writer.send_message(
MSG_TYPE_INIT,
{
MSG_KEY_ETCD_URL: "http://" + self._config.ETCD_ADDR,
MSG_KEY_HOSTNAME: self._config.HOSTNAME,
}
)
return reader, writer
def on_endpoint_set(self, response, hostname, orchestrator,
workload_id, endpoint_id):
"""Handler for endpoint updates, passes the update to the splitter."""
combined_id = EndpointId(hostname, orchestrator, workload_id,
endpoint_id)
_log.debug("Endpoint %s updated", combined_id)
_stats.increment("Endpoint created/updated")
endpoint = parse_endpoint(self._config, combined_id, response.value)
self.splitter.on_endpoint_update(combined_id, endpoint, async=True)
def on_endpoint_delete(self, response, hostname, orchestrator,
workload_id, endpoint_id):
"""Handler for endpoint deleted, passes the update to the splitter."""
combined_id = EndpointId(hostname, orchestrator, workload_id,
endpoint_id)
_log.debug("Endpoint %s deleted", combined_id)
_stats.increment("Endpoint deleted")
self.splitter.on_endpoint_update(combined_id, None, async=True)
def on_rules_set(self, response, profile_id):
"""Handler for rules updates, passes the update to the splitter."""
_log.debug("Rules for %s set", profile_id)
_stats.increment("Rules created/updated")
rules = parse_rules(profile_id, response.value)
profile_id = intern(profile_id.encode("utf8"))
self.splitter.on_rules_update(profile_id, rules, async=True)
def on_rules_delete(self, response, profile_id):
"""Handler for rules deletes, passes the update to the splitter."""
_log.debug("Rules for %s deleted", profile_id)
_stats.increment("Rules deleted")
self.splitter.on_rules_update(profile_id, None, async=True)
def on_tags_set(self, response, profile_id):
"""Handler for tags updates, passes the update to the splitter."""
_log.debug("Tags for %s set", profile_id)
_stats.increment("Tags created/updated")
rules = parse_tags(profile_id, response.value)
profile_id = intern(profile_id.encode("utf8"))
self.splitter.on_tags_update(profile_id, rules, async=True)
def on_tags_delete(self, response, profile_id):
"""Handler for tags deletes, passes the update to the splitter."""
_log.debug("Tags for %s deleted", profile_id)
_stats.increment("Tags deleted")
self.splitter.on_tags_update(profile_id, None, async=True)
def on_host_ip_set(self, response, hostname):
if not self._config.IP_IN_IP_ENABLED:
_log.debug("Ignoring update to %s because IP-in-IP is disabled",
response.key)
return
_stats.increment("Host IP created/updated")
ip = parse_host_ip(hostname, response.value)
if ip:
self.ipv4_by_hostname[hostname] = ip
else:
_log.warning("Invalid IP for hostname %s: %s, treating as "
"deletion", hostname, response.value)
self.ipv4_by_hostname.pop(hostname, None)
self._update_hosts_ipset()
def on_host_ip_delete(self, response, hostname):
if not self._config.IP_IN_IP_ENABLED:
_log.debug("Ignoring update to %s because IP-in-IP is disabled",
response.key)
return
_stats.increment("Host IP deleted")
if self.ipv4_by_hostname.pop(hostname, None):
self._update_hosts_ipset()
def _update_hosts_ipset(self):
if not self._been_in_sync:
_log.debug("Deferring update to hosts ipset until we're in-sync")
return
self.hosts_ipset.replace_members(self.ipv4_by_hostname.values(),
async=True)
def _on_config_updated(self, response, config_param):
new_value = response.value
if self.last_global_config.get(config_param) != new_value:
_log.critical("Global config value %s updated. Felix must be "
"restarted.", config_param)
die_and_restart()
_stats.increment("Global config (non) updates")
def _on_host_config_updated(self, response, hostname, config_param):
if hostname != self._config.HOSTNAME:
_log.debug("Ignoring config update for host %s", hostname)
return
_stats.increment("Per-host config created/updated")
new_value = response.value
if self.last_host_config.get(config_param) != new_value:
_log.critical("Global config value %s updated. Felix must be "
"restarted.", config_param)
die_and_restart()
def on_ipam_v4_pool_set(self, response, pool_id):
_stats.increment("IPAM pool created/updated")
pool = parse_ipam_pool(pool_id, response.value)
self.splitter.on_ipam_pool_update(pool_id, pool, async=True)
def on_ipam_v4_pool_delete(self, response, pool_id):
_stats.increment("IPAM pool deleted")
self.splitter.on_ipam_pool_update(pool_id, None, async=True)
class EtcdStatusReporter(EtcdClientOwner, Actor):
"""
Actor that manages and rate-limits the queue of status reports to
etcd.
"""
def __init__(self, config):
super(EtcdStatusReporter, self).__init__(config.ETCD_ADDR)
self._config = config
self._endpoint_status = {IPV4: {}, IPV6: {}}
# Two sets of dirty endpoints. The "older" set is the set of dirty
# endpoints that the actor is updating. The "newer" set is the set of
# dirty endpoints that should be done afterwards, and is kept
# separate to avoid pathological conditions where the actor never
# finishes the set.
self._newer_dirty_endpoints = set()
self._older_dirty_endpoints = set()
self._cleanup_pending = False
self._timer_scheduled = False
self._reporting_allowed = True
@actor_message()
def on_endpoint_status_changed(self, endpoint_id, ip_type, status):
assert isinstance(endpoint_id, EndpointId)
if status is not None:
_stats.increment("Endpoint status deleted")
self._endpoint_status[ip_type][endpoint_id] = status
else:
_stats.increment("Endpoint status updated")
self._endpoint_status[ip_type].pop(endpoint_id, None)
self._mark_endpoint_dirty(endpoint_id)
@actor_message()
def resync(self):
"""
Triggers a rewrite of all endpoint statuses.
"""
# Loop over IPv4 and IPv6 statuses.
for statuses in self._endpoint_status.itervalues():
for ep_id in statuses.iterkeys():
self._mark_endpoint_dirty(ep_id)
@actor_message()
def _on_timer_pop(self):
_log.debug("Timer popped, uncorking rate limit")
self._timer_scheduled = False
self._reporting_allowed = True
def _mark_endpoint_dirty(self, endpoint_id):
assert isinstance(endpoint_id, EndpointId)
if endpoint_id in self._older_dirty_endpoints:
# Optimization: if the endpoint is already queued up in
# _older_dirty_endpoints then there's no point in queueing it up a
# second time in _newer_dirty_endpoints.
_log.debug("Endpoint %s already marked dirty", endpoint_id)
return
else:
_log.debug("Marking endpoint %s dirty", endpoint_id)
self._newer_dirty_endpoints.add(endpoint_id)
@actor_message()
def clean_up_endpoint_statuses(self):
"""
Note that we need to do cleanup. We'll then try/retry from
_finish_msg_batch().
"""
self._cleanup_pending = True
def _finish_msg_batch(self, batch, results):
if not self._config.REPORT_ENDPOINT_STATUS:
_log.warning("StatusReporter called even though status reporting "
"disabled. Ignoring.")
self._endpoint_status[IPV4].clear()
self._endpoint_status[IPV6].clear()
self._newer_dirty_endpoints.clear()
self._older_dirty_endpoints.clear()
return
if self._cleanup_pending:
try:
self._attempt_cleanup()
except EtcdException as e:
_log.error("Cleanup failed: %r", e)
_stats.increment("Status report cleanup failed")
else:
_stats.increment("Status report cleanup done")
self._cleanup_pending = False
if self._reporting_allowed:
# We're not rate limited, go ahead and do a write to etcd.
_log.debug("Status reporting is allowed by rate limit.")
if not self._older_dirty_endpoints and self._newer_dirty_endpoints:
_log.debug("_older_dirty_endpoints empty, promoting"
"_newer_dirty_endpoints")
self._older_dirty_endpoints = self._newer_dirty_endpoints
self._newer_dirty_endpoints = set()
if self._older_dirty_endpoints:
ep_id = self._older_dirty_endpoints.pop()
status_v4 = self._endpoint_status[IPV4].get(ep_id)
status_v6 = self._endpoint_status[IPV6].get(ep_id)
status = combine_statuses(status_v4, status_v6)
try:
self._write_endpoint_status_to_etcd(ep_id, status)
except EtcdException:
_log.error("Failed to report status for %s, will retry",
ep_id)
# Add it into the next dirty set. Retrying in the next
# batch ensures that we try to update all of the dirty
# endpoints before we do any retries, ensuring fairness.
self._newer_dirty_endpoints.add(ep_id)
# Reset the rate limit flag.
self._reporting_allowed = False
if not self._timer_scheduled and ((not self._reporting_allowed) or
self._cleanup_pending):
# Schedule a timer to stop our rate limiting or retry cleanup.
timeout = self._config.ENDPOINT_REPORT_DELAY
timeout *= (0.9 + (random.random() * 0.2)) # Jitter by +/- 10%.
gevent.spawn_later(timeout,
self._on_timer_pop,
async=True)
self._timer_scheduled = True
def _attempt_cleanup(self):
our_host_dir = "/".join([FELIX_STATUS_DIR, self._config.HOSTNAME,
"workload"])
try:
# Grab all the existing status reports.
response = self.client.read(our_host_dir,
recursive=True)
except EtcdKeyNotFound:
_log.info("No endpoint statuses found, nothing to clean up")
else:
# Mark all statuses we find as dirty. This will result in any
# unknown endpoints being cleaned up.
for node in response.leaves:
combined_id = get_endpoint_id_from_key(node.key)
if combined_id:
_log.debug("Endpoint %s removed by resync, marking "
"status key for cleanup",
combined_id)
self._mark_endpoint_dirty(combined_id)
elif node.dir:
# This leaf is an empty directory, try to clean it up.
# This is safe even if another thread is adding keys back
# into the directory.
_log.debug("Found empty directory %s, cleaning up",
node.key)
delete_empty_parents(self.client, node.key, our_host_dir)
def _write_endpoint_status_to_etcd(self, ep_id, status):
"""
Try to actually write the status dict into etcd or delete the key
if it is no longer needed.
"""
_stats.increment("Per-port status report etcd writes")
status_key = ep_id.path_for_status
if status:
_log.debug("Writing endpoint status %s = %s", ep_id, status)
self.client.set(status_key,
json.dumps(status))
else:
_log.debug("Removing endpoint status %s", ep_id)
try:
self.client.delete(status_key)
except EtcdKeyNotFound:
_log.debug("Tried to delete %s but it was already gone",
status_key)
# Clean up any now-empty parent directories.
delete_empty_parents(
self.client,
status_key.rsplit("/", 1)[0], # Snip off final path segment.
dir_for_felix_status(self._config.HOSTNAME)
)
def combine_statuses(status_a, status_b):
"""
Combines a pair of status reports for the same interface.
If one status is None, the other is returned. Otherwise, the worst
status wins.
"""
if not status_a:
return status_b
if not status_b:
return status_a
a = status_a["status"]
b = status_b["status"]
if a == ENDPOINT_STATUS_ERROR or b == ENDPOINT_STATUS_ERROR:
return {"status": ENDPOINT_STATUS_ERROR}
elif a == ENDPOINT_STATUS_DOWN or b == ENDPOINT_STATUS_DOWN:
return {"status": ENDPOINT_STATUS_DOWN}
else:
return {"status": ENDPOINT_STATUS_UP}
def die_and_restart():
# Sleep so that we can't die more than 5 times in 10s even if someone is
# churning the config. This prevents our upstart/systemd jobs from giving
# up on us.
gevent.sleep(2)
# Use a failure code to tell systemd that we expect to be restarted. We
# use os._exit() because it is bullet-proof.
os._exit(1)
# Intern JSON keys as we load them to reduce occupancy.
FIELDS_TO_INTERN = set([
# Endpoint dicts. It doesn't seem worth interning items like the MAC
# address or TAP name, which are rarely (if ever) shared.
"profile_id",
"profile_ids",
"state",
"ipv4_gateway",
"ipv6_gateway",
# Rules dicts.
"protocol",
"src_tag",
"dst_tag",
"action",
])
json_decoder = json.JSONDecoder(
object_hook=functools.partial(intern_dict,
fields_to_intern=FIELDS_TO_INTERN)
)
def parse_endpoint(config, combined_id, raw_json):
endpoint = safe_decode_json(raw_json,
log_tag="endpoint %s" % combined_id.endpoint)
try:
common.validate_endpoint(config, combined_id, endpoint)
except ValidationFailed as e:
_log.warning("Validation failed for endpoint %s, treating as "
"missing: %s; %r", combined_id, e.message, raw_json)
endpoint = None
else:
_log.debug("Validated endpoint : %s", endpoint)
return endpoint
def parse_rules(profile_id, raw_json):
rules = safe_decode_json(raw_json, log_tag="rules %s" % profile_id)
try:
common.validate_rules(profile_id, rules)
except ValidationFailed as e:
_log.exception("Validation failed for profile %s rules: %s; %r",
profile_id, rules, e)
return None
else:
return rules
def parse_tags(profile_id, raw_json):
tags = safe_decode_json(raw_json, log_tag="tags %s" % profile_id)
try:
common.validate_tags(profile_id, tags)
except ValidationFailed:
_log.exception("Validation failed for profile %s tags : %s",
profile_id, tags)
return None
else:
# The tags aren't in a top-level object so we need to manually
# intern them here.
return intern_list(tags)
def parse_host_ip(hostname, raw_value):
if raw_value is None or validate_ip_addr(raw_value):
return canonicalise_ip(raw_value, None)
else:
_log.debug("%s has invalid IP: %r", hostname, raw_value)
return None
def parse_ipam_pool(pool_id, raw_json):
pool = safe_decode_json(raw_json, log_tag="ipam pool %s" % pool_id)
try:
common.validate_ipam_pool(pool_id, pool, 4)
except ValidationFailed as e:
_log.exception("Validation failed for ipam pool %s: %s; %r",
pool_id, pool, e)
return None
else:
return pool
def safe_decode_json(raw_json, log_tag=None):
try:
return json_decoder.decode(raw_json)
except (TypeError, ValueError):
_log.warning("Failed to decode JSON for %s: %r. Returning None.",
log_tag, raw_json)
return None