-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathdeployment.yaml
167 lines (156 loc) · 5.11 KB
/
deployment.yaml
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
apiVersion: services.cloud.sap.com/v1
kind: ServiceInstance
metadata:
name: alert-notif-ser-instance
spec:
serviceOfferingName: alert-notification
servicePlanName: standard
parameters:
configuration:
actions:
- name: email
properties:
destination: {{ .Values.alertnotification.email | b64dec }}
state: ENABLED
type: EMAIL
- name: slack
properties:
destination: {{ .Values.alertnotification.slack.webhook | b64dec }}
state: ENABLED
type: SLACK
conditions:
- name: system_down_condition
predicate: EQUALS
propertyKey: eventType
propertyValue: system_down_condition
subscriptions:
- actions:
- slack
conditions:
- system_down_condition
name: system_down_condition
state: ENABLED
---
apiVersion: serverless.kyma-project.io/v1alpha1
kind: Function
metadata:
name: alert-notif
spec:
env:
- name: oauth_url
valueFrom:
secretKeyRef:
key: oauth_url
name: alert-notif-ser-binding
- name: client_id
valueFrom:
secretKeyRef:
key: client_id
name: alert-notif-ser-binding
- name: client_secret
valueFrom:
secretKeyRef:
key: client_secret
name: alert-notif-ser-binding
- name: CLUSTER_DOMAIN
value: {{ .Values.alertnotification.clusterdomain | b64dec }}
- name: url
value: {{ .Values.alertnotification.url | b64dec }}
deps: "{ \n \"name\": \"alert-notif-test\",\n \"version\": \"1.0.0\",\n \"dependencies\":
{\n \"@sap_oss/alert-notification-client\": \"1.1.0\"\n }\n}"
runtime: nodejs14
source: >-
const { OAuthAuthentication, AlertNotificationClient, BasicAuthentication,
RegionUtils, Severity, Category } = require("@sap_oss/alert-notification-client");
const { Region, Platform } =
require("@sap_oss/alert-notification-client/dist/utils/region");
const oAuthAuthentication = new OAuthAuthentication({
username: process.env.client_id,
password: process.env.client_secret,
oAuthTokenUrl: process.env.oauth_url
});
module.exports = {
main: async function (event, context) {
try{
const client = new AlertNotificationClient({
authentication: oAuthAuthentication,
region: new Region(Platform.CF, process.env.url),
});
const result = await processAlert(client, event.data);
return JSON.stringify(result);
}catch(error){
console.log(error);
return error;
};
}
}
// data should be a JSON object.
// There is the possiblity to set these keys:
// - severity: ["fatal", "info", "notice", "warning", "error"]
// - category: ["alert", "exception", "notification", "warning"]
// - email_header: string
// - details: JSON object
async function processAlert(client, data){
console.log("processAlert() data: " + JSON.stringify(data));
const cur_time = Math.floor(+new Date() / 1000);
var severity_value = Severity.FATAL;
switch(data.severity) {
case "fatal":
severity_value = Severity.FATAL;
break;
case "info":
severity_value = Severity.INFO;
break;
case "notice":
severity_value = Severity.NOTICE;
break;
case "warning":
severity_value = Severity.WARNING;
break;
case "error":
severity_value = Severity.ERROR;
break;
default:
console.log("Unexpected value for severity in processAlert: " + data.severity);
}
var category_value = Category.ALERT;
switch(data.category) {
case "alert":
category_value = Category.ALERT;
break;
case "info":
category_value = Category.EXCEPTION;
break;
case "notice":
category_value = Category.NOTIFICATION;
break;
case "warning":
category_value = Category.WARNING;
break;
default:
console.log("Unexpected value for category in processAlert: " + data.category);
}
var email_header = data.email_header || "Message";
var details = data.details || data;
return await client.sendEvent({
body: 'The chatbot reported a ' + severity_value + ' ' + category_value + '. \nDETAILS: ' + JSON.stringify(details).replace(/[{}]|,/g, "\n"),
subject: 'Chatbot ' + category_value + ': ' + email_header,
eventType: 'system_down_condition',
severity: severity_value,
category: category_value,
resource: {
resourceName: 'update-bot',
resourceType: 'deployment',
resourceInstance: '1',
},
eventTimestamp: cur_time,
priority: 1
});
}
---
apiVersion: services.cloud.sap.com/v1
kind: ServiceBinding
metadata:
name: alert-notif-ser-binding
spec:
serviceInstanceName: alert-notif-ser-instance