-
Notifications
You must be signed in to change notification settings - Fork 2
/
controller.py
executable file
·65 lines (53 loc) · 2.42 KB
/
controller.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
#!/usr/bin/env python3
import json
import sys
from kubernetes import client, watch
from functions import get_config, process_event, create_logger, parse_too_old_failure
runtime_config = get_config()
crds = client.CustomObjectsApi()
logger = create_logger(log_level=runtime_config['log_level'])
resource_version = ''
if __name__ == "__main__":
logger.info('newrelic-controller initializing')
while True:
stream = watch.Watch().stream(crds.list_cluster_custom_object, 'newrelic.com', 'v1', 'newrelicsettings', resource_version=resource_version)
try:
for event in stream:
event_type = event["type"]
obj = event["object"]
metadata = obj.get('metadata')
spec = obj.get('spec')
code = obj.get('code')
if code == 410:
logger.debug('Error code 410')
new_version = parse_too_old_failure(obj.get('message'))
if new_version == None:
logger.error('Failed to parse 410 error code')
resource_version = ''
break
else:
resource_version = new_version
logger.debug('Updating resource version to {0} due to "too old" error'.format(new_version))
break
if not metadata or not spec:
logger.error('No metadata or spec in object, skipping: {0}'.format(json.dumps(obj, indent=1)))
break
if metadata['resourceVersion'] is not None:
resource_version = metadata['resourceVersion']
logger.debug('resourceVersion now: {0}'.format(resource_version))
try:
process_event(crds, obj, event_type)
except Exception as e:
logger.error('Failed to process event, skipping {0}: {1}'.format(obj['metadata']['name'], e))
except client.rest.ApiException as e:
if e.status == 404:
logger.error('Custom Resource Definition not created in cluster')
sys.exit(1)
elif e.status == 401:
logger.error('Unauthorized')
sys.exit(1)
else:
raise e
except KeyboardInterrupt:
logger.info('newrelic-controller exiting')
sys.exit(0)