-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathclusters.js
476 lines (436 loc) · 18.5 KB
/
clusters.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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
/**
* Copyright 2019 IBM Corp. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const crypto = require('crypto');
const { v4: uuid } = require('uuid');
const express = require('express');
const router = express.Router();
const asyncHandler = require('express-async-handler');
const ebl = require('express-bunyan-logger');
const objectHash = require('object-hash');
const _ = require('lodash');
const moment = require('moment');
const request = require('request-promise-native');
var glob = require('glob-promise');
var fs = require('fs');
const mongoSanitize = require('express-mongo-sanitize');
const verifyAdminOrgKey = require('../../utils/orgs.js').verifyAdminOrgKey;
const getBunyanConfig = require('../../utils/bunyan.js').getBunyanConfig;
const getCluster = require('../../utils/cluster.js').getCluster;
const deleteResource = require('../../utils/resources.js').deleteResource;
const buildSearchableDataForResource = require('../../utils/cluster.js').buildSearchableDataForResource;
const buildSearchableDataObjHash = require('../../utils/cluster.js').buildSearchableDataObjHash;
const buildPushObj = require('../../utils/cluster.js').buildPushObj;
const buildHashForResource = require('../../utils/cluster.js').buildHashForResource;
const { CLUSTER_LIMITS, CLUSTER_REG_STATES } = require('../../apollo/models/const');
const { GraphqlPubSub } = require('../../apollo/subscription');
const pubSub = GraphqlPubSub.getInstance();
const conf = require('../../conf.js').conf;
const addUpdateCluster = async (req, res, next) => {
try {
const Clusters = req.db.collection('clusters');
const Stats = req.db.collection('resourceStats');
const cluster = await Clusters.findOne({ org_id: req.org._id, cluster_id: req.params.cluster_id});
const metadata = req.body;
var reg_state = CLUSTER_REG_STATES.REGISTERED;
if (!cluster) {
// new cluster flow requires a cluster to be registered first.
if (process.env.CLUSTER_REGISTRATION_REQUIRED) {
res.status(404).send({error: 'Not found, the api requires you to register the cluster first.'});
return;
}
const total = await Clusters.count({org_id: req.org._id});
if (total > CLUSTER_LIMITS.MAX_TOTAL ) {
res.status(400).send({error: 'Too many clusters are registered under this organization.'});
return;
}
await Clusters.insertOne({ org_id: req.org._id, cluster_id: req.params.cluster_id, reg_state, registration: {}, metadata, created: new Date(), updated: new Date() });
runAddClusterWebhook(req, req.org._id, req.params.cluster_id, metadata.name); // dont await. just put it in the bg
Stats.updateOne({ org_id: req.org._id }, { $inc: { clusterCount: 1 } }, { upsert: true });
res.status(200).send('Welcome to Razee');
}
else {
if (cluster.reg_state == CLUSTER_REG_STATES.REGISTERING){
reg_state = CLUSTER_REG_STATES.REGISTERING;
}
if (cluster.dirty) {
await Clusters.updateOne({ org_id: req.org._id, cluster_id: req.params.cluster_id },
{ $set: { metadata, reg_state, updated: new Date(), dirty: false } });
res.status(205).send('Please resync');
}
else {
await Clusters.updateOne({ org_id: req.org._id, cluster_id: req.params.cluster_id },
{ $set: { metadata, reg_state, updated: new Date() } });
res.status(200).send('Thanks for the update');
}
}
} catch (err) {
req.log.error(err.message);
next(err);
}
};
var getAddClusterWebhookHeaders = async()=>{
// loads the headers specified in the 'razeedash-add-cluster-webhook-headers-secret' secret
// returns the key-value pairs of the secret as a js obj
var filesDir = '/var/run/secrets/razeeio/razeedash-api/add-cluster-webhook-headers';
var fileNames = await glob('**', {
cwd: filesDir,
nodir: true,
});
var headers = {};
_.each(fileNames, (name)=>{
var val = fs.readFileSync(`${filesDir}/${name}`, 'utf8');
headers[encodeURIComponent(name)] = val;
});
return headers;
};
var runAddClusterWebhook = async(req, orgId, clusterId, clusterName)=>{
var postData = {
org_id: orgId,
cluster_id: clusterId,
cluster_name: clusterName,
};
var url = process.env.ADD_CLUSTER_WEBHOOK_URL;
if(!url){
return;
}
req.log.info({ url, postData }, 'posting add cluster webhook');
try{
var headers = await getAddClusterWebhookHeaders();
var result = await request.post({
url,
body: postData,
json: true,
resolveWithFullResponse: true,
headers,
});
req.log.info({ url, postData, statusCode: result.statusCode }, 'posted add cluster webhook');
}catch(err){
req.log.error({ url, postData, err }, 'add cluster webhook failed');
}
};
const pushToS3 = async (req, key, searchableDataHash, dataStr) => {
//if its a new or changed resource, write the data out to an S3 object
const bucket = conf.s3.resourceBucket;
const hash = crypto.createHash('sha256');
const keyHash = hash.update(JSON.stringify(key)).digest('hex');
await req.s3.createBucketAndObject(bucket, `${keyHash}/${searchableDataHash}`, dataStr);
return `https://${req.s3.endpoint}/${bucket}/${keyHash}/${searchableDataHash}`;
};
var deleteOrgClusterResourceSelfLinks = async(req, orgId, clusterId, selfLinks)=>{
const Resources = req.db.collection('resources');
selfLinks = _.filter(selfLinks); // in such a case that a null is passed to us. if you do $in:[null], it returns all items missing the attr, which is not what we want
if(selfLinks.length < 1){
return;
}
if(!orgId || !clusterId){
throw `missing orgId or clusterId: ${JSON.stringify({ orgId, clusterId })}`;
}
var search = {
org_id: orgId,
cluster_id: clusterId,
selfLink: {
$in: selfLinks,
}
};
await Resources.deleteMany(search);
};
const syncClusterResources = async(req, res)=>{
const orgId = req.org._id;
const clusterId = req.params.cluster_id;
const Resources = req.db.collection('resources');
const Stats = req.db.collection('resourceStats');
var result = await Resources.updateMany(
{ org_id: orgId, cluster_id: clusterId, updated: { $lt: new moment().subtract(1, 'hour').toDate() }, deleted: { $ne: true} },
{ $set: { deleted: true }, $currentDate: { updated: true } },
);
req.log.debug({ org_id: orgId, cluster_id: clusterId }, `${result.modifiedCount} resources marked as deleted:true`);
// deletes items >1day old
var objsToDelete = await Resources.find(
{ org_id: orgId, cluster_id: clusterId, deleted: true, updated: { $lt: new moment().subtract(1, 'day').toDate() } },
{ projection: { selfLink: 1, updated: 1, } }
).toArray();
if(objsToDelete.length > 0){
// if we have items that were marked as deleted and havent updated in >=1day, then deletes them
var selfLinksToDelete = _.map(objsToDelete, 'selfLink');
req.log.info({ org_id: orgId, cluster_id: clusterId, resourceObjs: objsToDelete }, `deleting ${selfLinksToDelete.length} resource objs`);
await deleteOrgClusterResourceSelfLinks(req, orgId, clusterId, selfLinksToDelete);
Stats.updateOne({ org_id: orgId }, { $inc: { deploymentCount: -1 * objsToDelete.length } });
}
res.status(200).send('Thanks');
};
const updateClusterResources = async (req, res, next) => {
try {
var clusterId = req.params.cluster_id;
const body = req.body;
if (!body) {
res.status(400).send('Missing resource body');
return;
}
let resources = body;
if (!Array.isArray(resources)) {
resources = [body];
}
const Resources = req.db.collection('resources');
const Stats = req.db.collection('resourceStats');
for (let resource of resources) {
const type = resource['type'] || 'other';
switch (type.toUpperCase()) {
case 'POLLED':
case 'MODIFIED':
case 'ADDED': {
const resourceHash = buildHashForResource(resource.object, req.org);
let dataStr = JSON.stringify(resource.object);
const selfLink = resource.object.metadata.selfLink;
const key = {
org_id: req.org._id,
cluster_id: req.params.cluster_id,
selfLink: selfLink
};
let searchableDataObj = buildSearchableDataForResource(req.org, resource.object);
if (searchableDataObj.kind == 'RemoteResource' && searchableDataObj.children && searchableDataObj.children.length > 0) {
// if children arrives earlier than this RR without subscription_id, update children's subscription_id
const childSearchKey = {
org_id: req.org._id,
cluster_id: req.params.cluster_id,
selfLink: {$in: searchableDataObj.children},
'searchableData.subscription_id': {$exists: false},
deleted: false
};
const childResource = await Resources.findOne(childSearchKey);
if (childResource) {
const subscription_id = searchableDataObj['annotations["deploy_razee_io_clustersubscription"]'];
req.log.debug({key, subscription_id}, `Updating children's subscription_id to ${subscription_id} for parent key.`);
Resources.updateMany( childSearchKey,
{$set: {'searchableData.subscription_id': subscription_id},$currentDate: { updated: true }}, {});
}
}
const rrSearchKey = {
org_id: req.org._id,
'searchableData.kind': 'RemoteResource',
'searchableData.children': selfLink,
deleted: false
};
const remoteResource = await Resources.findOne(rrSearchKey);
if(remoteResource) {
searchableDataObj['subscription_id'] = remoteResource.searchableData['annotations["deploy_razee_io_clustersubscription"]'];
}
const searchableDataHash = buildSearchableDataObjHash(searchableDataObj);
const currentResource = await Resources.findOne(key);
const hasSearchableDataChanges = (currentResource && searchableDataHash != _.get(currentResource, 'searchableDataHash'));
const pushCmd = buildPushObj(searchableDataObj, _.get(currentResource, 'searchableData', null));
if (req.s3 && (!currentResource || resourceHash !== currentResource.hash)) {
dataStr = await pushToS3(req, key, searchableDataHash, dataStr);
}
var changes = null;
var options = {};
if(currentResource){
// if obj already in db
if (resourceHash === currentResource.hash && !hasSearchableDataChanges){
// if obj in db and nothing has changed
changes = {
$set: { deleted: false },
$currentDate: { updated: true }
};
}
else{
// if obj in db and theres changes to save
changes = {
$set: { deleted: false, hash: resourceHash, data: dataStr, searchableData: searchableDataObj, searchableDataHash: searchableDataHash },
$currentDate: { updated: true, lastModified: true },
...pushCmd
};
}
}
else{
// if obj not in db, then adds it
changes = {
$set: { deleted: false, hash: resourceHash, data: dataStr, searchableData: searchableDataObj, searchableDataHash: searchableDataHash },
$currentDate: { created: true, updated: true, lastModified: true },
...pushCmd
};
options = { upsert: true };
Stats.updateOne({ org_id: req.org._id }, { $inc: { deploymentCount: 1 } }, { upsert: true });
// adds the yaml hist item too
await addResourceYamlHistObj(req, req.org._id, clusterId, selfLink, dataStr);
}
const result = await Resources.updateOne(key, changes, options);
// publish notification to graphql
if (result) {
let resourceId = null;
let resourceCreated = Date.now;
if (result.upsertedId) {
resourceId = result.upsertedId._id;
} else if (currentResource) {
resourceId = currentResource._id;
resourceCreated = currentResource.created;
}
if (resourceId) {
pubSub.resourceChangedFunc(
{_id: resourceId, data: dataStr, created: resourceCreated,
deleted: false, org_id: req.org._id, cluster_id: req.params.cluster_id, selfLink: selfLink,
hash: resourceHash, searchableData: searchableDataObj, searchableDataHash: searchableDataHash});
}
}
if(hasSearchableDataChanges){
// if any of the searchable attrs has changes, then save a new yaml history obj (for diffing in the ui)
await addResourceYamlHistObj(req, req.org._id, clusterId, selfLink, dataStr);
}
break;
}
case 'DELETED': {
const selfLink = resource.object.metadata.selfLink;
let dataStr = JSON.stringify(resource.object);
const key = {
org_id: req.org._id,
cluster_id: req.params.cluster_id,
selfLink: selfLink
};
const searchableDataObj = buildSearchableDataForResource(req.org, resource.object);
const searchableDataHash = buildSearchableDataObjHash(searchableDataObj);
const currentResource = await Resources.findOne(key);
const pushCmd = buildPushObj(searchableDataObj, _.get(currentResource, 'searchableData', null));
if (req.s3) {
dataStr = await pushToS3(req, key, searchableDataHash, dataStr);
}
if (currentResource) {
await Resources.updateOne(
key, {
$set: { deleted: true, data: dataStr, searchableData: searchableDataObj, searchableDataHash: searchableDataHash },
$currentDate: { updated: true },
...pushCmd
}
);
await addResourceYamlHistObj(req, req.org._id, clusterId, selfLink, '');
pubSub.resourceChangedFunc({ _id: currentResource._id, created: currentResource.created, deleted: true, org_id: req.org._id, cluster_id: req.params.cluster_id, selfLink: selfLink, searchableData: searchableDataObj, searchableDataHash: searchableDataHash});
}
break;
}
default: {
throw new Error(`Unsupported event ${resource.type}`);
}
}
}
res.status(200).send('Thanks');
} catch (err) {
req.log.error(err.message);
next(err);
}
};
var addResourceYamlHistObj = async(req, orgId, clusterId, resourceSelfLink, yamlStr)=>{
var ResourceYamlHist = req.db.collection('resourceYamlHist');
var id = uuid();
var obj = {
_id: id,
org_id: orgId,
cluster_id: clusterId,
resourceSelfLink,
yamlStr,
updated: new Date(),
};
await ResourceYamlHist.insertOne(obj);
return id;
};
const addClusterMessages = async (req, res, next) => {
const body = req.body;
if (!body) {
res.status(400).send('Missing message body');
return;
}
const clusterId = req.params.cluster_id;
const errorData = JSON.stringify(body.data) || undefined;
const level = body.level;
const message = body.message;
let key = {};
let data = {};
let insertData = {};
const messageType = 'watch-keeper';
try {
var messageHash = objectHash(message);
key = {
cluster_id: clusterId,
org_id: req.org._id,
level: level,
data: errorData,
message_hash: messageHash,
};
data = {
level: level,
message: message,
data: errorData,
updated: new Date(),
};
insertData = {
created: new Date(),
};
const Messages = req.db.collection('messages');
await Messages.updateOne(key, { $set: data, $setOnInsert: insertData }, { upsert: true });
req.log.debug({ messagedata: data }, `${messageType} message data posted`);
res.status(200).send(`${messageType} message received`);
} catch (err) {
req.log.error(err.message);
next(err);
}
};
const getClusters = async (req, res, next) => {
try {
const Clusters = req.db.collection('clusters');
const orgId = req.org._id + '';
const clusters = await Clusters.find({ 'org_id': orgId }).toArray();
return res.status(200).send({clusters});
} catch (err) {
req.log.error(err.message);
next(err);
}
};
const clusterDetails = async (req, res) => {
const cluster = req.cluster; // req.cluster was set in `getCluster`
if(cluster) {
return res.status(200).send({cluster});
} else {
return res.status(404).send('cluster was not found');
}
};
const deleteCluster = async (req, res, next) => {
try {
if(!req.org._id || !req.params.cluster_id){
throw 'missing orgId or clusterId';
}
const Clusters = req.db.collection('clusters');
const cluster_id = req.params.cluster_id;
await Clusters.deleteOne({ org_id: req.org._id, cluster_id: cluster_id });
req.log.info(`cluster ${cluster_id} deleted`);
next();
} catch (error) {
req.log.error(error.message);
return res.status(500).json({ status: 'error', message: error.message });
}
};
router.use(ebl(getBunyanConfig('razeedash-api/clusters')));
// /api/v2/clusters/:cluster_id
router.post('/:cluster_id', mongoSanitize({ replaceWith: '_' }), asyncHandler(addUpdateCluster));
// /api/v2/clusters/:cluster_id/resources
router.post('/:cluster_id/resources', asyncHandler(getCluster), asyncHandler(updateClusterResources));
// /api/v2/clusters/:cluster_id/resources/sync
router.post('/:cluster_id/resources/sync', asyncHandler(getCluster), asyncHandler(syncClusterResources));
// /api/v2/clusters/:cluster_id/messages
router.post('/:cluster_id/messages', asyncHandler(getCluster), asyncHandler(addClusterMessages));
// /api/v2/clusters
router.get('/', asyncHandler(verifyAdminOrgKey), asyncHandler(getClusters));
// /api/v2/clusters/:cluster_id
router.get('/:cluster_id', asyncHandler(verifyAdminOrgKey), asyncHandler(getCluster), asyncHandler(clusterDetails));
// /api/v2/clusters/:cluster_id
router.delete('/:cluster_id', asyncHandler(verifyAdminOrgKey), asyncHandler(getCluster), asyncHandler(deleteCluster), asyncHandler(deleteResource));
module.exports = router;