-
Notifications
You must be signed in to change notification settings - Fork 2
/
slack.py
54 lines (48 loc) · 1.76 KB
/
slack.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
#!/usr/bin/env python
from slackclient import SlackClient
import os
import json
import jsonpickle
# map some colors to pod event types
color = { 'DELETED': '#ff0000', 'MODIFIED': 'f0ff00', 'ADDED': '56ff00' }
def slack_message(event, context):
try:
slack_token = os.environ.get('SLACK_TOKEN', None)
slack_channel = os.environ.get('SLACK_CHANNEL', None)
if None in (slack_token, slack_channel):
msg = 'Please specify a slack token and slack channel'
print(msg)
return msg
else:
sc = SlackClient(slack_token)
sc.api_call(
"chat.postMessage",
channel=slack_channel,
attachments=[
{
"title": event['data']['type']+" Pod event",
"color": color[event['data']['type']],
"fields": [
{
"title": "Name",
"value": event['data']['object']['metadata']['name']
},
{
"title": "Namespace",
"value": event['data']['object']['metadata']['namespace']
},
{
"title": "Node",
"value": event['data']['object']['spec']['nodeName']
},
]
}
]
)
print(jsonpickle.encode(event))
except Exception as inst:
print(type(inst))
print(inst)
print(event.keys())
print(type(event))
print(str(event))