-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.ts
149 lines (137 loc) · 4.05 KB
/
app.ts
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
import { Probot } from 'probot';
import { Router } from 'express';
import { exposeMetrics, useCounter } from '@operate-first/probot-metrics';
import {
APIS,
createTokenSecret,
deleteTokenSecret,
getNamespace,
getTokenSecretName,
updateTokenSecret,
useApi,
} from '@operate-first/probot-kubernetes';
const generateTaskPayload = (name: string, context: any) => ({
apiVersion: 'tekton.dev/v1beta1',
kind: 'TaskRun',
metadata: {
// "{{name}}" to match the prefix in manifests/base/tasks/kustomization.yaml namePrefix
// (not necessary for functionality, just for consistency)
generateName: `{{name}}-${name}-`,
},
spec: {
taskRef: {
// "{{name}}" to match the prefix in manifests/base/tasks/kustomization.yaml namePrefix
// necessary for functionality
name: '{{name}}-' + name,
},
params: [
{
name: 'SECRET_NAME',
value: getTokenSecretName(context),
},
{
name: 'CONTEXT',
value: JSON.stringify(context.payload),
},
],
},
});
export default (
app: Probot,
{
getRouter,
}: { getRouter?: ((path?: string | undefined) => Router) | undefined }
) => {
// Expose additional routes for /healthz and /metrics
if (!getRouter) {
app.log.error('Missing router.');
return;
}
const router = getRouter();
router.get('/healthz', (_, response) => response.status(200).send('OK'));
exposeMetrics(router, '/metrics');
// Register tracked metrics
const numberOfInstallTotal = useCounter({
name: 'num_of_install_total',
help: 'Total number of installs received',
labelNames: [],
});
const numberOfUninstallTotal = useCounter({
name: 'num_of_uninstall_total',
help: 'Total number of uninstalls received',
labelNames: [],
});
const numberOfActionsTotal = useCounter({
name: 'num_of_actions_total',
help: 'Total number of actions received',
labelNames: ['install', 'action'],
});
const operationsTriggered = useCounter({
name: 'operations_triggered',
help: 'Metrics for action triggered by the operator with respect to the kubernetes operations.',
labelNames: ['install', 'operation', 'status', 'method'],
});
// Simple callback wrapper - executes and async operation and based on the result it inc() operationsTriggered counted
const wrapOperationWithMetrics = (callback: Promise<any>, labels: any) => {
const response = callback
.then(() => ({
status: 'Succeeded',
}))
.catch(() => ({
status: 'Failed',
}));
operationsTriggered
.labels({
...labels,
...response,
operation: 'k8s',
})
.inc();
};
app.onAny((context: any) => {
// On any event inc() the counter
numberOfActionsTotal
.labels({
install: context.payload.installation.id,
action: context.payload.action,
})
.inc();
});
app.on('installation.created', async (context: any) => {
numberOfInstallTotal.labels({}).inc();
// Create secret holding the access token
wrapOperationWithMetrics(createTokenSecret(context), {
install: context.payload.installation.id,
method: 'createSecret',
});
});
app.on('push', async (context: any) => {
// Update token in case it expired
wrapOperationWithMetrics(updateTokenSecret(context), {
install: context.payload.installation.id,
method: 'updateSecret',
});
// Trigger example taskrun
wrapOperationWithMetrics(
useApi(APIS.CustomObjectsApi).createNamespacedCustomObject(
'tekton.dev',
'v1beta1',
getNamespace(),
'taskruns',
generateTaskPayload('example', context)
),
{
install: context.payload.installation.id,
method: 'scheduleExampleTaskRun',
}
);
});
app.on('installation.deleted', async (context: any) => {
numberOfUninstallTotal.labels({}).inc();
// Delete secret containing the token
wrapOperationWithMetrics(deleteTokenSecret(context), {
install: context.payload.installation.id,
method: 'deleteSecret',
});
});
};