Skip to content

Commit d0541a9

Browse files
committed
feat: add mattermost notif
1 parent fa761d5 commit d0541a9

7 files changed

Lines changed: 66 additions & 2 deletions

File tree

.env.example

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
MATTERMOST_WEBHOOK_URL=
2+
MATTERMOST_CHANNEL=

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ Dedicated mainly to report CNPG cluster status.
55

66
## Getting Started
77

8+
Copy and edit `.env.example`. Leave empty values for mattermost if you don't want to use it.
9+
810
Run the development daemon:
911

1012
```bash

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"license": "MIT",
1313
"scripts": {
1414
"build": "tsc && tsc-alias",
15-
"dev": "FORCE_COLOR=true tsx watch src/**/*.ts | pino-pretty",
15+
"dev": "FORCE_COLOR=true tsx watch --env-file=.env src/**/*.ts | pino-pretty",
1616
"lint": "eslint src/**/*.ts && prettier --check src/**/*.ts",
1717
"format": "prettier --write src/**/*.ts",
1818
"postinstall": "is-ci || husky install",

src/lib/config.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { logger } from "./logger"
2+
3+
export const MATTERMOST_WEBHOOK_URL: string | undefined =
4+
process.env.MATTERMOST_WEBHOOK_URL
5+
export const MATTERMOST_CHANNEL: string | undefined =
6+
process.env.MATTERMOST_CHANNEL
7+
8+
if (!MATTERMOST_WEBHOOK_URL) {
9+
logger.info("MATTERMOST_WEBHOOK_URL not configured")
10+
}
11+
12+
if (MATTERMOST_WEBHOOK_URL && !MATTERMOST_CHANNEL) {
13+
logger.error(
14+
"MATTERMOST_WEBHOOK_URL is defined so MATTERMOST_CHANNEL is required"
15+
)
16+
process.exit(1)
17+
}

src/lib/kube-watcher.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
getCnpgClusterArchivingStatus,
1111
} from "@/lib/kube/types"
1212
import { logger } from "@/lib/logger"
13+
import { sendNotification } from "@/lib/mattermost"
1314

1415
const allContexts = ["dev", "prod", "ovh-dev", "ovh-prod"]
1516

@@ -176,13 +177,19 @@ function checkCnpgCluster(
176177
const cacheKey = JSON.stringify(status)
177178
const alertInCache = alertCache.has(cacheKey)
178179

180+
const mattermostFields = Object.entries(status).map(([key, value]) => {
181+
return { title: key, value: String(value) }
182+
})
183+
179184
if (statusOk && !alertInCache) {
180185
return
181186
} else if (statusOk && alertInCache) {
182187
logger.info(status, "cnpgCluster alert resolved")
188+
sendNotification(statusOk, "cnpgCluster alert resolved", mattermostFields)
183189
alertCache.delete(cacheKey)
184190
} else if (!statusOk && !alertInCache) {
185191
logger.error(status, "cnpgCluster alert")
192+
sendNotification(statusOk, "cnpgCluster alert", mattermostFields)
186193
alertCache.set(cacheKey, true)
187194
}
188195

src/lib/mattermost.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { MATTERMOST_WEBHOOK_URL, MATTERMOST_CHANNEL } from "@/lib/config"
2+
import { logger } from "@/lib/logger"
3+
4+
export async function sendNotification(
5+
status: boolean,
6+
title: string,
7+
fields: { title: string; value: string }[]
8+
) {
9+
if (!MATTERMOST_WEBHOOK_URL) {
10+
return
11+
}
12+
13+
try {
14+
await fetch(MATTERMOST_WEBHOOK_URL, {
15+
method: "POST",
16+
headers: {
17+
"Content-Type": "application/json",
18+
},
19+
body: JSON.stringify({
20+
channel: MATTERMOST_CHANNEL,
21+
author_name: "kubesight-alerts",
22+
attachments: [
23+
{
24+
color: status ? "#00FF00" : "#FF0000",
25+
title,
26+
fields: fields.map((field) => {
27+
return { ...field, short: true }
28+
}),
29+
},
30+
],
31+
}),
32+
})
33+
} catch (error) {
34+
logger.error({ error }, "Failed to send Mattermost notification")
35+
}
36+
}

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@
1515
"@/*": ["./src/*"],
1616
}
1717
},
18-
"include":["./src/*"],
18+
"include":["./src/*", "src/lib/config.ts"],
1919
"exclude": ["node_modules"]
2020
}

0 commit comments

Comments
 (0)