This repository has been archived by the owner on Mar 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
79 lines (71 loc) · 2 KB
/
index.js
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
const promisify = require('util').promisify
const badge = promisify((format, cb) => require('gh-badges')(format, (svg, err) => cb(err, svg)))
const express = require('express')
const http = require('http')
const kubernetes = require('kubernetes-client')
const terminus = require('@godaddy/terminus')
const app = express()
let client
function getImageTag (image) {
const name = image.split('/').slice(-1)[0]
const splits = name.split(':')
if (splits.length < 2) return 'latest'
return splits[1]
}
const wrap = fn => (req, res, next) => fn(req, res).catch(next)
app.get('/ns/:namespace/deploy/:deployment', wrap(async (req, res) => {
let apiRes
if (client.apis.apps.v1) {
apiRes = await client.apis
.apps
.v1
.namespaces(req.params.namespace)
.deployments(req.params.deployment)
.get()
} else {
apiRes = await client.apis
.extensions
.v1beta1
.namespaces(req.params.namespace)
.deployments(req.params.deployment)
.get()
}
//
// Describe the Deployment by concatenating the container image paths.
//
const containers = apiRes.body.spec.template.spec.containers
const imageTags = containers.map(container => getImageTag(container.image))
const subject = encodeURIComponent(req.query.subject || 'deploy')
const status = ` ${imageTags.join(' ')} `
const format = {
text: [ subject, status ],
format: 'svg',
colorscheme: 'green',
template: 'flat'
}
const svg = await badge(format)
res.set('Content-Type', 'image/svg+xml')
res.status(200).send(svg).end()
}))
async function main () {
let config
try {
config = kubernetes.config.getInCluster()
} catch (err) {
config = kubernetes.config.fromKubeconfig()
}
client = new kubernetes.Client({ config })
await client.loadSpec()
const server = http.createServer(app)
terminus(server, {
healthChecks: {
'/healthcheck': () => Promise.resolve()
}
})
server.listen(8080)
}
main()
.catch(err => {
console.error(err)
process.exit(1)
})