-
Notifications
You must be signed in to change notification settings - Fork 9
/
lookout.py
226 lines (189 loc) · 8.51 KB
/
lookout.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
import os
from copy import copy
from kubernetes import client, config, watch
import slack
def _generate_progress_bar(position, max_value):
if position is None:
position = 0
filled_squares = (100 / max_value * position) / 5
filled_char = "⬛"
empty_char = "⬜"
return (filled_char * int(filled_squares)) + (
empty_char * (20 - int(filled_squares))) + "\n"
class KubeLookout:
template = [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": ""
}
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": ""
},
"accessory": {
"type": "image",
"image_url": "",
"alt_text": "status image"
}
}
]
def __init__(self, warning_image, progress_image, ok_image,
slack_key, slack_channel, cluster_name):
super().__init__()
self.warning_image = warning_image
self.ok_image = ok_image
self.progress_image = progress_image
self.slack_client = None
self.slack_key = slack_key
self.slack_channel = slack_channel
self.cluster_name = cluster_name
self.rollouts = {}
self.degraded = set()
def _init_client(self):
if "KUBERNETES_PORT" in os.environ:
config.load_incluster_config()
else:
config.load_kube_config()
api_client = client.api_client.ApiClient()
self.core = client.AppsV1Api(api_client)
def _send_slack_block(self, blocks, channel, message_id=None):
if self.slack_client is None:
self.slack_client = slack.WebClient(
self.slack_key)
if message_id is None:
response = self.slack_client.chat_postMessage(channel=channel,
blocks=blocks)
return response.data['ts'], response.data['channel']
response = self.slack_client.chat_update(
channel=channel,
ts=message_id, blocks=blocks)
return response.data['ts'], response.data['channel']
def _handle_deployment_change(self, deployment):
metadata = deployment.metadata
deployment_key = f"{metadata.namespace}/{metadata.name}"
ready_replicas = 0
if deployment.status.ready_replicas is not None:
ready_replicas = deployment.status.ready_replicas
if deployment_key not in self.rollouts and \
deployment.status.updated_replicas is None:
blocks = self._generate_deployment_rollout_block(deployment)
resp = self._send_slack_block(blocks, self.slack_channel)
self.rollouts[deployment_key] = resp
elif deployment_key in self.rollouts:
rollout_complete = (
deployment.status.updated_replicas ==
deployment.status.replicas ==
ready_replicas)
blocks = self._generate_deployment_rollout_block(deployment,
rollout_complete)
self.rollouts[deployment_key] = self._send_slack_block(
channel=self.rollouts[deployment_key][1],
message_id=self.rollouts[deployment_key][0], blocks=blocks)
if rollout_complete:
self.rollouts.pop(deployment_key)
elif ready_replicas < deployment.spec.replicas:
blocks = self._generate_deployment_degraded_block(deployment)
self._send_slack_block(blocks, self.slack_channel)
self.degraded.add(deployment_key)
elif (deployment_key in self.degraded and
ready_replicas >= deployment.spec.replicas):
self.degraded.remove(deployment_key)
blocks = self._generate_deployment_not_degraded_block(deployment)
self._send_slack_block(blocks, self.slack_channel)
def _handle_event(self, deployment):
self._handle_deployment_change(deployment)
def main_loop(self):
while True:
self._init_client()
pods = self.core.list_deployment_for_all_namespaces(watch=False)
resource_version = pods.metadata.resource_version
stream = watch.Watch().stream(
self.core.list_deployment_for_all_namespaces,
resource_version=resource_version
)
print("Waiting for deployment events to come in..")
for event in stream:
deployment = event['object']
self._handle_event(deployment)
def _generate_deployment_rollout_block(self, deployment,
rollout_complete=False):
block = copy(self.template)
header = f"*{self.cluster_name} deployment " \
f"{deployment.metadata.namespace}/{deployment.metadata.name}" \
f" is rolling out an update.*"
message = ''
for container in deployment.spec.template.spec.containers:
message += f"Container {container.name} has image " \
f"_ {container.image} _\n"
message += "\n"
message += f"{deployment.status.updated_replicas} replicas " \
f"updated out of " \
f"{deployment.spec.replicas}, {deployment.status.ready_replicas}" \
f" ready.\n\n"
message += _generate_progress_bar(
deployment.status.updated_replicas, deployment.spec.replicas)
block[0]['text']['text'] = header
block[1]['text']['text'] = message
block[1]['accessory']['image_url'] = self.progress_image
if rollout_complete:
block[1]['accessory'][
'image_url'] = self.ok_image
return block
def _generate_deployment_degraded_block(self, deployment):
block = copy(self.template)
header = f"*{self.cluster_name} deployment " \
f"{deployment.metadata.namespace}/{deployment.metadata.name}" \
f" has become degraded.*"
message = f"Deployment " \
f"{deployment.metadata.namespace}/{deployment.metadata.name}" \
f" has {deployment.status.ready_replicas} ready replicas " \
f"when it should have {deployment.spec.replicas}.\n"
message += _generate_progress_bar(deployment.status.ready_replicas,
deployment.spec.replicas)
block[0]['text']['text'] = header
block[1]['text']['text'] = message
block[1]['accessory'][
'image_url'] = self.warning_image
return block
def _generate_deployment_not_degraded_block(self, deployment):
block = copy(self.template)
header = f"*{self.cluster_name} deployment " \
f"{deployment.metadata.namespace}/{deployment.metadata.name}" \
f" is no longer in a degraded state.*"
message = f"Deployment " \
f"{deployment.metadata.namespace}/{deployment.metadata.name}" \
f" has {deployment.status.ready_replicas} ready " \
f"replicas out of " \
f"{deployment.spec.replicas}.\n"
message += _generate_progress_bar(deployment.status.ready_replicas,
deployment.spec.replicas)
block[0]['text']['text'] = header
block[1]['text']['text'] = message
block[1]['accessory'][
'image_url'] = self.ok_image
return block
if __name__ == "__main__":
env_warning_image = os.environ.get(
"WARNING_IMAGE",
"https://upload.wikimedia.org/wikipedia/"
"commons/thumb/6/6e/Dialog-warning.svg/"
"200px-Dialog-warning.svg.png")
env_progress_image = os.environ.get("PROGRESS_IMAGE",
"https://i.gifer.com/80ZN.gif")
env_ok_image = os.environ.get("OK_IMAGE",
"https://upload.wikimedia.org/wikipedia/"
"commons/thumb/f/fb/Yes_check.svg/"
"200px-Yes_check.svg.png")
env_slack_token = os.environ["SLACK_TOKEN"]
env_slack_channel = os.environ.get("SLACK_CHANNEL", "#general")
env_cluster_name = os.environ.get("CLUSTER_NAME", "Kubernetes Cluster")
kube_deploy_watch = KubeLookout(env_warning_image,
env_progress_image,
env_ok_image, env_slack_token,
env_slack_channel, env_cluster_name)
kube_deploy_watch.main_loop()