This repository has been archived by the owner on Aug 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
113 lines (79 loc) · 3.25 KB
/
main.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
import yaml
import os
import sys
import argparse
from kubernetes import watch, client, config
from receivers.flowdock_receiver import FlowdockReceiver
from utils import ANNOTATION_ENABLED, ANNOTATION_TEAM, ANNOTATION_RECEIVER
#
# Define a specific yaml loader which substitutes environment
# vars.
#
class YamlEnvLoader(yaml.SafeLoader):
def construct_yaml_str(self, node):
return self.construct_scalar(node).format(**os.environ)
YamlEnvLoader.add_constructor("tag:yaml.org,2002:str", YamlEnvLoader.construct_yaml_str)
def main_loop(receivers):
if "KUBERNETES_PORT" in os.environ:
config.load_incluster_config()
else:
config.load_kube_config()
api_client = client.api_client.ApiClient()
core = client.AppsV1Api(api_client)
event_types = ["ADDED", "MODIFIED"]
while True:
pods = core.list_deployment_for_all_namespaces(watch=False)
resource_version = pods.metadata.resource_version
stream = watch.Watch().stream(
core.list_deployment_for_all_namespaces, resource_version=resource_version,
)
for event in stream:
# Event type
# ADDED | MODIFIED | DELETED
event_type = event["type"]
deployment = event["object"]
# We only care about new/updated events (for now)
if event_type not in event_types:
continue
# Parse out annotations
annotations = deployment.metadata.annotations
# Skip if we have none - this generally means the deployment
# does not exist.
if not annotations:
continue
# Skip watching this deployment unless we've enabled it explicity
if annotations.get(ANNOTATION_ENABLED) != "true":
continue
# Get team routing information from annotation
annotation_team = annotations.get(ANNOTATION_TEAM)
annotation_receiver = annotations.get(ANNOTATION_RECEIVER)
for receiver in receivers:
receiver.handle_event(annotation_team, annotation_receiver, deployment)
def format_constructor(loader, node):
return loader.construct_scalar(node).format(**os.environ)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--config", required=True, help="path to configuration file")
args = parser.parse_args()
config_file = args.config
if not os.path.exists(config_file):
print("Config not found: %s" % (config_file))
sys.exit(1)
print("Using config: %s" % (config_file))
with open(config_file, "r") as ymlfile:
yaml_loader = YamlEnvLoader(ymlfile)
try:
yaml_config = yaml_loader.get_single_data()
finally:
yaml_loader.dispose()
cluster_name = yaml_config.get(
"cluster_name", os.environ.get("CLUSTER_NAME", "Kubernetes Cluster")
)
receivers = []
flowdock_settings = yaml_config.get("receivers", {}).get("flowdock", {})
for team, settings in flowdock_settings.items():
receivers.append(FlowdockReceiver(cluster_name, team, settings.get("token")))
if not receivers:
print("No valid receivers defined in config.yml")
sys.exit(1)
main_loop(receivers)