-
Notifications
You must be signed in to change notification settings - Fork 13
/
main.py
187 lines (178 loc) · 7.45 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
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# =============================================================================
# Created By : github.com/echoboomer
# =============================================================================
"""
This script processes GKE cluster upgrade-related notifications as part of a
GCP Cloud Function.
"""
# =============================================================================
# Imports
# =============================================================================
import base64
import json
import os
import requests
import sys
def process_event(slack_data, webhook_url):
byte_length = str(sys.getsizeof(slack_data))
headers = {
"Content-Type": "application/json",
"Content-Length": byte_length,
}
response = requests.post(webhook_url, data=json.dumps(slack_data), headers=headers)
if response.status_code != 200:
raise Exception(response.status_code, response.text)
def notify_slack(event, context):
"""Background Cloud Function to be triggered by Pub/Sub.
Args:
event (dict): The dictionary with data specific to this type of
event. The `data` field contains the PubsubMessage message. The
`attributes` field will contain custom attributes if there are any.
context (google.cloud.functions.Context): The Cloud Functions event
metadata. The `event_id` field contains the Pub/Sub message ID. The
`timestamp` field contains the publish time.
"""
print(
"""This Function was triggered by messageId {} published at {}
""".format(
context.event_id, context.timestamp
)
)
if "data" in event:
# Print the event at the beginning for easier debug.
print("Event was passed into function and will be processed.")
print(event)
# Shared Variables
cluster = event["attributes"]["cluster_name"]
cluster_resource = json.loads(event["attributes"]["payload"])["resourceType"]
location = event["attributes"]["cluster_location"]
message = base64.b64decode(event["data"]).decode("utf-8")
project = event["attributes"]["project_id"]
webhook_url = os.getenv("SLACK_WEBHOOK_URL")
# UpgradeEvent
if "UpgradeEvent" in event["attributes"]["type_url"]:
# UpgradeEvent Variables
current_version = json.loads(event["attributes"]["payload"])[
"currentVersion"
]
start_time = json.loads(event["attributes"]["payload"])[
"operationStartTime"
]
target_version = json.loads(event["attributes"]["payload"])["targetVersion"]
title = f"GKE Cluster Upgrade Notification :zap:"
slack_data = {
"username": "Platform Notifications",
"icon_emoji": ":satellite:",
"attachments": [
{
"color": "#9733EE",
"fields": [
{"title": title},
{
"title": "Project",
"value": project,
"short": "false",
},
{
"title": "Cluster",
"value": cluster,
"short": "false",
},
{
"title": "Location",
"value": location,
"short": "false",
},
{
"title": "Update Type",
"value": cluster_resource,
"short": "false",
},
{
"title": "Current Version",
"value": current_version,
"short": "false",
},
{
"title": "Target Version",
"value": target_version,
"short": "false",
},
{
"title": "Start Time",
"value": start_time,
"short": "false",
},
{
"title": "Details",
"value": message,
"short": "false",
},
],
}
],
}
process_event(slack_data, webhook_url)
# UpgradeAvailableEvent
elif "UpgradeAvailableEvent" in event["attributes"]["type_url"]:
if os.Getenv("SEND_UPGRADE_AVAILABLE_NOTIFICATIONS") == "enabled":
# UpgradeAvailableEvent Variables
available_version = json.loads(event["attributes"]["payload"])[
"version"
]
title = f"GKE Cluster Upgrade Available Notification :zap:"
slack_data = {
"username": "Platform Notifications",
"icon_emoji": ":satellite:",
"attachments": [
{
"color": "#9733EE",
"fields": [
{"title": title},
{
"title": "Project",
"value": project,
"short": "false",
},
{
"title": "Cluster",
"value": cluster,
"short": "false",
},
{
"title": "Location",
"value": location,
"short": "false",
},
{
"title": "Eligible Resource",
"value": cluster_resource,
"short": "false",
},
{
"title": "Eligible Version",
"value": available_version,
"short": "false",
},
{
"title": "Details",
"value": message,
"short": "false",
},
],
}
],
}
process_event(slack_data, webhook_url)
else:
pass
else:
print(
"Event was neither UpgradeEvent or UpgradeAvailableEvent, so it will be skipped."
)
exit(0)
else:
print("No event was passed into the function. Exiting.")
exit(0)