Skip to content

Commit

Permalink
feat: add mattermost notif
Browse files Browse the repository at this point in the history
  • Loading branch information
achauve committed Jun 14, 2024
1 parent fa761d5 commit d0541a9
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
MATTERMOST_WEBHOOK_URL=
MATTERMOST_CHANNEL=
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ Dedicated mainly to report CNPG cluster status.

## Getting Started

Copy and edit `.env.example`. Leave empty values for mattermost if you don't want to use it.

Run the development daemon:

```bash
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"license": "MIT",
"scripts": {
"build": "tsc && tsc-alias",
"dev": "FORCE_COLOR=true tsx watch src/**/*.ts | pino-pretty",
"dev": "FORCE_COLOR=true tsx watch --env-file=.env src/**/*.ts | pino-pretty",
"lint": "eslint src/**/*.ts && prettier --check src/**/*.ts",
"format": "prettier --write src/**/*.ts",
"postinstall": "is-ci || husky install",
Expand Down
17 changes: 17 additions & 0 deletions src/lib/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { logger } from "./logger"

export const MATTERMOST_WEBHOOK_URL: string | undefined =
process.env.MATTERMOST_WEBHOOK_URL
export const MATTERMOST_CHANNEL: string | undefined =
process.env.MATTERMOST_CHANNEL

if (!MATTERMOST_WEBHOOK_URL) {
logger.info("MATTERMOST_WEBHOOK_URL not configured")
}

if (MATTERMOST_WEBHOOK_URL && !MATTERMOST_CHANNEL) {
logger.error(
"MATTERMOST_WEBHOOK_URL is defined so MATTERMOST_CHANNEL is required"
)
process.exit(1)
}
7 changes: 7 additions & 0 deletions src/lib/kube-watcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
getCnpgClusterArchivingStatus,
} from "@/lib/kube/types"
import { logger } from "@/lib/logger"
import { sendNotification } from "@/lib/mattermost"

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

Expand Down Expand Up @@ -176,13 +177,19 @@ function checkCnpgCluster(
const cacheKey = JSON.stringify(status)
const alertInCache = alertCache.has(cacheKey)

const mattermostFields = Object.entries(status).map(([key, value]) => {
return { title: key, value: String(value) }
})

if (statusOk && !alertInCache) {
return
} else if (statusOk && alertInCache) {
logger.info(status, "cnpgCluster alert resolved")
sendNotification(statusOk, "cnpgCluster alert resolved", mattermostFields)
alertCache.delete(cacheKey)
} else if (!statusOk && !alertInCache) {
logger.error(status, "cnpgCluster alert")
sendNotification(statusOk, "cnpgCluster alert", mattermostFields)
alertCache.set(cacheKey, true)
}

Expand Down
36 changes: 36 additions & 0 deletions src/lib/mattermost.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { MATTERMOST_WEBHOOK_URL, MATTERMOST_CHANNEL } from "@/lib/config"
import { logger } from "@/lib/logger"

export async function sendNotification(
status: boolean,
title: string,
fields: { title: string; value: string }[]
) {
if (!MATTERMOST_WEBHOOK_URL) {
return
}

try {
await fetch(MATTERMOST_WEBHOOK_URL, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
channel: MATTERMOST_CHANNEL,
author_name: "kubesight-alerts",
attachments: [
{
color: status ? "#00FF00" : "#FF0000",
title,
fields: fields.map((field) => {
return { ...field, short: true }
}),
},
],
}),
})
} catch (error) {
logger.error({ error }, "Failed to send Mattermost notification")
}
}
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@
"@/*": ["./src/*"],
}
},
"include":["./src/*"],
"include":["./src/*", "src/lib/config.ts"],
"exclude": ["node_modules"]
}

0 comments on commit d0541a9

Please sign in to comment.