-
Notifications
You must be signed in to change notification settings - Fork 902
/
cluster.service.js
342 lines (297 loc) · 13.9 KB
/
cluster.service.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
'use strict';
/* eslint consistent-return:0 */
import _ from 'lodash';
import {API_SERVICE} from '../api/api.service';
import {NAMING_SERVICE} from 'core/naming/naming.service';
let angular = require('angular');
module.exports = angular.module('spinnaker.core.cluster.service', [
NAMING_SERVICE,
API_SERVICE,
require('../serverGroup/serverGroup.transformer.js'),
])
.factory('clusterService', function ($q, API, serverGroupTransformer, namingService) {
function loadServerGroups(application) {
var serverGroupLoader = $q.all({
serverGroups: API.one('applications').one(application.name).all('serverGroups').getList().then(g => g, () => []),
});
return serverGroupLoader.then(function(results) {
results.serverGroups = results.serverGroups || [];
results.serverGroups.forEach(addHealthStatusCheck);
results.serverGroups.forEach(parseName);
results.serverGroups.forEach((serverGroup) =>
serverGroup.category = 'serverGroup'
);
return $q.all(results.serverGroups.map(serverGroup => serverGroupTransformer.normalizeServerGroup(serverGroup, application)));
});
}
function parseName(serverGroup) {
var nameParts = namingService.parseServerGroupName(serverGroup.name);
serverGroup.app = nameParts.application;
serverGroup.stack = nameParts.stack;
serverGroup.detail = nameParts.freeFormDetails;
serverGroup.cluster = nameParts.cluster;
}
function addHealthStatusCheck(serverGroup) {
serverGroup.instances.forEach(function (instance) {
var healthList = instance.health;
var activeHealth = _.filter(healthList, function(health) {
return health.state !== 'Unknown';
});
instance.hasHealthStatus = Boolean(activeHealth.length);
});
}
function addHealthCountsToCluster(cluster) {
cluster.instanceCounts = {
up: 0,
down: 0,
unknown: 0,
starting: 0,
outOfService: 0,
succeeded: 0,
failed: 0,
total: 0,
};
var operand = cluster.serverGroups || [];
operand.forEach(function(serverGroup) {
if (!serverGroup.instanceCounts) {
return;
}
cluster.instanceCounts.total += serverGroup.instanceCounts.total || 0;
cluster.instanceCounts.up += serverGroup.instanceCounts.up || 0;
cluster.instanceCounts.down += serverGroup.instanceCounts.down || 0;
cluster.instanceCounts.unknown += serverGroup.instanceCounts.unknown || 0;
cluster.instanceCounts.starting += serverGroup.instanceCounts.starting || 0;
cluster.instanceCounts.outOfService += serverGroup.instanceCounts.outOfService || 0;
cluster.instanceCounts.succeeded += serverGroup.instanceCounts.succeeded || 0;
cluster.instanceCounts.failed += serverGroup.instanceCounts.failed || 0;
});
}
function baseTaskMatcher(task, serverGroup) {
var taskRegion = task.getValueFor('regions') ? task.getValueFor('regions')[0] : task.getValueFor('region') || null;
return serverGroup.account === task.getValueFor('credentials') &&
serverGroup.region === taskRegion &&
serverGroup.name === task.getValueFor('asgName');
}
function instanceIdsTaskMatcher(task, serverGroup) {
if (task.getValueFor('region') === serverGroup.region && task.getValueFor('credentials') === serverGroup.account) {
if (task.getValueFor('knownInstanceIds')) {
return _.intersection(_.map(serverGroup.instances, 'id'), task.getValueFor('knownInstanceIds')).length > 0;
} else {
return _.intersection(_.map(serverGroup.instances, 'id'), task.getValueFor('instanceIds')).length > 0;
}
}
return false;
}
var taskMatchers = {
'createcopylastasg': function(task, serverGroup) {
var source = task.getValueFor('source');
var targetAccount = task.getValueFor('deploy.account.name');
var targetRegion = task.getValueFor('availabilityZones') ? Object.keys(task.getValueFor('availabilityZones'))[0] : null;
var dsgs = task.getValueFor('deploy.server.groups');
var targetServerGroup = targetRegion && dsgs && dsgs[targetRegion] ? dsgs[targetRegion][0] : null;
var sourceServerGroup = source.asgName;
var sourceAccount = source.account;
var sourceRegion = source.region;
if (serverGroup.account === targetAccount && serverGroup.region === targetRegion && serverGroup.name === targetServerGroup) {
return true;
}
if (serverGroup.account === sourceAccount && serverGroup.region === sourceRegion && serverGroup.name === sourceServerGroup) {
return true;
}
return false;
},
'createdeploy': function(task, serverGroup) {
var account = task.getValueFor('deploy.account.name'),
region = task.getValueFor('deploy.server.groups') ? Object.keys(task.getValueFor('deploy.server.groups'))[0] : null,
serverGroupName = (serverGroup && region) ? task.getValueFor('deploy.server.groups')[region][0] : null;
if (account && serverGroup && region) {
return serverGroup.account === account && serverGroup.region === region && serverGroup.name === serverGroupName;
}
return false;
},
'enableinstancesindiscovery': instanceIdsTaskMatcher,
'disableinstancesindiscovery': instanceIdsTaskMatcher,
'disableinstances': instanceIdsTaskMatcher,
'registerinstanceswithloadbalancer': instanceIdsTaskMatcher,
'deregisterinstancesfromloadbalancer': instanceIdsTaskMatcher,
'terminateinstances': instanceIdsTaskMatcher,
'rebootinstances': instanceIdsTaskMatcher,
'resizeasg': baseTaskMatcher,
'resizeservergroup': baseTaskMatcher,
'disableasg': baseTaskMatcher,
'disableservergroup': baseTaskMatcher,
'destroyasg': baseTaskMatcher,
'destroyservergroup': baseTaskMatcher,
'enableasg': baseTaskMatcher,
'enableservergroup': baseTaskMatcher,
'enablegoogleservergroup': baseTaskMatcher,
'disablegoogleservergroup': baseTaskMatcher,
'resumeasgprocessesdescription': baseTaskMatcher, // fun fact, this is how an AWS resize starts
'rollbackServerGroup': function(task, serverGroup) {
var account = task.getValueFor('credentials'),
region = task.getValueFor('regions') ? task.getValueFor('regions')[0] : null;
if (account && serverGroup.account === account && region && serverGroup.region === region) {
return serverGroup.name === task.getValueFor('targetop.asg.disableServerGroup.name') ||
serverGroup.name === task.getValueFor('targetop.asg.enableServerGroup.name');
}
return false;
},
};
function taskMatches(task, serverGroup) {
let notificationType = _.has(task, 'execution.stages') ?
task.execution.stages[0].context['notification.type'] ?
task.execution.stages[0].context['notification.type'] :
task.execution.stages[0].type : // TODO: good grief
task.getValueFor('notification.type');
var matcher = taskMatchers[notificationType];
return matcher ? matcher(task, serverGroup) : false;
}
function addTasksToServerGroups(application) {
let runningTasks = _.get(application, 'runningTasks.data', []);
if (!application.serverGroups.data) {
return; // still run if there are no running tasks, since they may have all finished and we need to clear them.
}
application.serverGroups.data.forEach(function(serverGroup) {
if (!serverGroup.runningTasks) {
serverGroup.runningTasks = [];
}
serverGroup.runningTasks.length = 0;
runningTasks.forEach(function(task) {
if (taskMatches(task, serverGroup)) {
serverGroup.runningTasks.push(task);
}
});
});
}
function findStagesWithServerGroupInfo(stages) {
var stagesWithServerGroups = _.filter(stages, function (stage) {
return ( _.includes(['deploy', 'destroyAsg', 'resizeAsg'], stage.type) && _.has(stage.context, 'deploy.server.groups') ) ||
( stage.type === 'disableAsg' && _.has(stage.context, 'targetop.asg.disableAsg.name') );
});
return stagesWithServerGroups;
}
function extractServerGroupNameFromContext(context) {
return _.head(_.values(context['deploy.server.groups'])) || context['targetop.asg.disableAsg.name'] || undefined;
}
function extractRegionFromContext(context) {
return _.head(_.keys(context['deploy.server.groups'])) || _.head(context['targetop.asg.disableAsg.regions']) || undefined;
}
function addExecutionsToServerGroups(application) {
let executions = _.get(application, 'runningExecutions.data', []);
if (!application.serverGroups.data) {
return; // still run if there are no running tasks, since they may have all finished and we need to clear them.
}
application.serverGroups.data.forEach(function(serverGroup) {
serverGroup.runningExecutions = [];
executions.forEach(function (execution) {
var stages = findStagesWithServerGroupInfo(execution.stages);
_.forEach(stages, function(stage) {
var stageServerGroup = stage ? extractServerGroupNameFromContext(stage.context) : undefined;
var stageAccount = stage && stage.context ? stage.context.account || stage.context.credentials : undefined;
var stageRegion = stage ? extractRegionFromContext(stage.context) : undefined;
if(_.includes(stageServerGroup, serverGroup.name) &&
stageAccount === serverGroup.account &&
stageRegion === serverGroup.region) {
serverGroup.runningExecutions.push(execution);
}
});
});
});
return application;
}
function addProvidersAndServerGroupsToInstances(serverGroups) {
serverGroups.forEach(function(serverGroup) {
serverGroup.instances.forEach(function(instance) {
instance.provider = serverGroup.type || serverGroup.provider;
instance.serverGroup = instance.serverGroup || serverGroup.name;
instance.vpcId = serverGroup.vpcId;
});
});
}
function collateServerGroupsIntoClusters(serverGroups) {
var clusters = [];
var groupedByAccount = _.groupBy(serverGroups, 'account');
_.forOwn(groupedByAccount, function(accountServerGroups, account) {
var groupedByCategory = _.groupBy(accountServerGroups, 'category');
_.forOwn(groupedByCategory, function(categoryServerGroups, category) {
var groupedByCluster = _.groupBy(categoryServerGroups, 'cluster');
_.forOwn(groupedByCluster, function(clusterServerGroups, clusterName) {
var cluster = {account: account, category: category, name: clusterName, serverGroups: clusterServerGroups};
addHealthCountsToCluster(cluster);
clusters.push(cluster);
});
});
});
addProvidersAndServerGroupsToInstances(serverGroups);
return clusters;
}
function updateLoadBalancers(application) {
application.serverGroups.data.forEach(function(serverGroup) {
serverGroup.loadBalancers = application.loadBalancers.data.filter(function(loadBalancer) {
return loadBalancer.serverGroups.some(function(loadBalancerGroup) {
return loadBalancerGroup.name === serverGroup.name &&
loadBalancer.region === serverGroup.region &&
loadBalancer.account === serverGroup.account;
});
}).map(function(loadBalancer) { return loadBalancer.name; });
});
}
function normalizeServerGroupsWithLoadBalancers(application) {
updateLoadBalancers(application);
}
function getCluster(application, account, cluster) {
return API.one('applications').one(application).one('clusters', account).one(cluster).get();
}
function getClusters(application) {
return API.one('applications').one(application).one('clusters').get();
}
function addServerGroupsToApplication(application, serverGroups = []) {
if (application.serverGroups.data) {
let data = application.serverGroups.data;
// remove any that have dropped off, update any that have changed
let toRemove = [];
data.forEach((serverGroup, idx) => {
let matches = serverGroups.filter((test) =>
test.name === serverGroup.name &&
test.account === serverGroup.account &&
test.region === serverGroup.region &&
test.category === serverGroup.category
);
if (!matches.length) {
toRemove.push(idx);
} else {
if (serverGroup.stringVal && matches[0].stringVal && serverGroup.stringVal !== matches[0].stringVal) {
data[idx] = matches[0];
}
}
});
toRemove.forEach((idx) => data.splice(idx, 1));
// add any new ones
serverGroups.forEach((serverGroup) => {
if (!application.serverGroups.data.filter((test) =>
test.name === serverGroup.name &&
test.account === serverGroup.account &&
test.region === serverGroup.region &&
test.category === serverGroup.category
).length) {
data.push(serverGroup);
}
});
return data;
} else {
return serverGroups;
}
}
return {
loadServerGroups: loadServerGroups,
createServerGroupClusters: collateServerGroupsIntoClusters,
normalizeServerGroupsWithLoadBalancers: normalizeServerGroupsWithLoadBalancers,
addTasksToServerGroups: addTasksToServerGroups,
addExecutionsToServerGroups: addExecutionsToServerGroups,
addServerGroupsToApplication: addServerGroupsToApplication,
//for testing purposes only
extractRegionFromContext: extractRegionFromContext,
getCluster: getCluster,
getClusters: getClusters,
};
});