forked from openshift/origin-web-console
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuildConfig.js
579 lines (524 loc) · 21.6 KB
/
buildConfig.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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
'use strict';
/**
* @ngdoc function
* @name openshiftConsole.controller:EditBuildConfigController
* @description
* Controller of the openshiftConsole
*/
angular.module('openshiftConsole')
.controller('EditBuildConfigController',
function($scope,
$filter,
$location,
$routeParams,
AlertMessageService,
ApplicationGenerator,
AuthorizationService,
DataService,
Navigate,
ProjectsService,
SOURCE_URL_PATTERN,
SecretsService,
keyValueEditorUtils) {
$scope.projectName = $routeParams.project;
$scope.buildConfig = null;
$scope.alerts = {};
$scope.sourceURLPattern = SOURCE_URL_PATTERN;
$scope.options = {};
$scope.jenkinsfileOptions = {
type: 'path'
};
$scope.selectTypes = {
ImageStreamTag: "Image Stream Tag",
ImageStreamImage: "Image Stream Image",
DockerImage: "Docker Image Repository"
};
$scope.buildFromTypes = [
"ImageStreamTag",
"ImageStreamImage",
"DockerImage"
];
$scope.pushToTypes = [
"ImageStreamTag",
"DockerImage",
"None"
];
$scope.jenkinsfileTypes = [{
"id": "path",
"title": "From Source Repository"
}, {
"id": "inline",
"title": "Inline"
}];
$scope.view = {
advancedOptions: false,
hasHooks: false
};
$scope.breadcrumbs = [
{
title: $routeParams.project,
link: "project/" + $routeParams.project
}
];
if ($routeParams.isPipeline) {
$scope.breadcrumbs.push({
title: "Pipelines",
link: "project/" + $routeParams.project + "/browse/pipelines"
});
$scope.breadcrumbs.push({
title: $routeParams.buildconfig,
link: "project/" + $routeParams.project + "/browse/pipelines/" + $routeParams.buildconfig
});
} else {
$scope.breadcrumbs.push({
title: "Builds",
link: "project/" + $routeParams.project + "/browse/builds"
});
$scope.breadcrumbs.push({
title: $routeParams.buildconfig,
link: "project/" + $routeParams.project + "/browse/builds/" + $routeParams.buildconfig
});
}
$scope.breadcrumbs.push({
title: $routeParams.isPipeline ? "Edit Pipelines" : "Edit Builds"
});
$scope.imageOptions = {
from: {},
to: {},
fromSource: {}
};
$scope.sources = {
"binary": false,
"dockerfile": false,
"git": false,
"images": false,
"contextDir": false,
"none": true
};
$scope.triggers = {
githubWebhooks: [],
genericWebhooks: [],
imageChangeTriggers: [],
builderImageChangeTrigger: {},
configChangeTrigger: {}
};
$scope.runPolicyTypes = [
"Serial",
"Parallel",
"SerialLatestOnly"
];
$scope.buildHookTypes = [{
id: "command",
label: "Command"
}, {
id: "script",
label: "Shell Script"
}, {
id: "args",
label: "Arguments to default image entry point"
}, {
id: "commandArgs",
label: "Command with arguments"
}, {
id: "scriptArgs",
label: "Shell script with arguments"
}];
$scope.buildHookSelection = {
type: {}
};
var getInitialBuildHookSelection = function() {
var hasArgs = !_.isEmpty(_.get($scope, 'buildConfig.spec.postCommit.args'));
var hasCommand = !_.isEmpty(_.get($scope, 'buildConfig.spec.postCommit.command'));
var hasScript = !!_.get($scope, 'buildConfig.spec.postCommit.script');
$scope.view.hasHooks = hasArgs || hasCommand || hasScript;
var id;
if (hasArgs && hasCommand) {
id = 'commandArgs';
} else if (hasArgs && hasScript) {
id = 'scriptArgs';
} else if (hasArgs) {
id = 'args';
} else if (hasScript) {
id = 'script';
} else {
id = 'command';
}
$scope.buildHookSelection.type = _.find($scope.buildHookTypes, { id: id });
};
var clearIncompatibleBuildHookFields = function() {
if (!$scope.view.hasHooks) {
delete $scope.updatedBuildConfig.spec.postCommit.command;
delete $scope.updatedBuildConfig.spec.postCommit.args;
delete $scope.updatedBuildConfig.spec.postCommit.script;
} else {
switch ($scope.buildHookSelection.type.id) {
case "script":
delete $scope.updatedBuildConfig.spec.postCommit.command;
delete $scope.updatedBuildConfig.spec.postCommit.args;
break;
case "command":
delete $scope.updatedBuildConfig.spec.postCommit.script;
delete $scope.updatedBuildConfig.spec.postCommit.args;
break;
case "args":
delete $scope.updatedBuildConfig.spec.postCommit.script;
delete $scope.updatedBuildConfig.spec.postCommit.command;
break;
case "scriptArgs":
delete $scope.updatedBuildConfig.spec.postCommit.command;
break;
case "commandArgs":
delete $scope.updatedBuildConfig.spec.postCommit.script;
break;
}
}
};
AlertMessageService.getAlerts().forEach(function(alert) {
$scope.alerts[alert.name] = alert.data;
});
AlertMessageService.clearAlerts();
$scope.secrets = {};
var watches = [];
var buildStrategy = $filter('buildStrategy');
ProjectsService
.get($routeParams.project)
.then(_.spread(function(project, context) {
$scope.project = project;
$scope.context = context;
// Update project breadcrumb with display name.
$scope.breadcrumbs[0].title = $filter('displayName')(project);
if (!AuthorizationService.canI('buildconfigs', 'update', $routeParams.project)) {
Navigate.toErrorPage('You do not have authority to update build config ' +
$routeParams.buildconfig + '.', 'access_denied');
return;
}
DataService.get("buildconfigs", $routeParams.buildconfig, context).then(
// success
function(buildConfig) {
$scope.buildConfig = buildConfig;
getInitialBuildHookSelection();
$scope.updatedBuildConfig = angular.copy($scope.buildConfig);
$scope.buildStrategy = buildStrategy($scope.updatedBuildConfig);
$scope.strategyType = $scope.buildConfig.spec.strategy.type;
$scope.envVars = $scope.buildStrategy.env || [];
_.each($scope.envVars, function(env) {
$filter('altTextForValueFrom')(env);
});
$scope.triggers = getTriggerMap($scope.triggers, $scope.buildConfig.spec.triggers);
$scope.sources = getSourceMap($scope.sources, $scope.buildConfig.spec.source);
if (_.has(buildConfig, 'spec.strategy.jenkinsPipelineStrategy.jenkinsfile')) {
$scope.jenkinsfileOptions.type = 'inline';
}
DataService.list("secrets", context, function(secrets) {
var secretsByType = SecretsService.groupSecretsByType(secrets);
var secretNamesByType =_.mapValues(secretsByType, function(secrets) {return _.map(secrets, 'metadata.name')});
// Add empty option to the image/source secrets
$scope.secrets.secretsByType = _.each(secretNamesByType, function(secretsArray) {
secretsArray.unshift("");
});
loadBuildConfigSecrets();
});
var setImageOptions = function(imageOptions, imageData) {
imageOptions.type = (imageData && imageData.kind) ? imageData.kind : "None";
var istag = {},
isimage = "",
dockerImage = "";
if (imageOptions.type === "ImageStreamTag") {
istag = {namespace: imageData.namespace || buildConfig.metadata.namespace, imageStream: imageData.name.split(':')[0], tagObject: {tag: imageData.name.split(':')[1]}};
} else {
istag = {namespace: "", imageStream: "", tagObject: {tag: ""}};
}
if (imageOptions.type === "ImageStreamImage") {
isimage = (imageData.namespace || buildConfig.metadata.namespace) + "/" + imageData.name;
} else {
isimage = "";
}
dockerImage = imageOptions.type === "DockerImage" ? imageData.name : "";
imageOptions.imageStreamTag = istag;
imageOptions.imageStreamImage = isimage;
imageOptions.dockerImage = dockerImage;
};
setImageOptions($scope.imageOptions.from, $scope.buildStrategy.from);
setImageOptions($scope.imageOptions.to, $scope.updatedBuildConfig.spec.output.to);
if ($scope.sources.images) {
$scope.sourceImages = $scope.buildConfig.spec.source.images;
// If only one Image Source is present in the buildConfig make it editable, if more then one show them as readonly.
if ($scope.sourceImages.length === 1) {
$scope.imageSourceTypes = angular.copy($scope.buildFromTypes);
setImageOptions($scope.imageOptions.fromSource, $scope.sourceImages[0].from);
$scope.imageSourcePaths = _.map($scope.sourceImages[0].paths, function(path) {
return {
name: path.sourcePath,
value: path.destinationDir
};
});
} else {
$scope.imageSourceFromObjects = [];
$scope.sourceImages.forEach(function(sourceImage) {
$scope.imageSourceFromObjects.push(sourceImage.from);
});
}
}
$scope.options.forcePull = !!$scope.buildStrategy.forcePull;
if ($scope.strategyType === "Docker") {
$scope.options.noCache = !!$scope.buildConfig.spec.strategy.dockerStrategy.noCache;
// Only DockerStrategy can have empty Strategy object and therefore it's from object
$scope.buildFromTypes.push("None");
}
// If we found the item successfully, watch for changes on it
watches.push(DataService.watchObject("buildconfigs", $routeParams.buildconfig, context, function(buildConfig, action) {
if (action === 'MODIFIED') {
$scope.alerts["updated/deleted"] = {
type: "warning",
message: "This build configuration has changed since you started editing it. You'll need to copy any changes you've made and edit again."
};
}
if (action === "DELETED") {
$scope.alerts["updated/deleted"] = {
type: "warning",
message: "This build configuration has been deleted."
};
$scope.disableInputs = true;
}
$scope.buildConfig = buildConfig;
}));
$scope.loaded = true;
},
// failure
function(e) {
$scope.loaded = true;
$scope.alerts["load"] = {
type: "error",
message: "The build configuration details could not be loaded.",
details: "Reason: " + $filter('getErrorDetails')(e)
};
}
);
})
);
var getTriggerMap = function(triggerMap, triggers) {
// Even if `from` is set in the image change trigger check if its not pointing to the builder image
function isBuilder(imageChangeFrom, buildConfigFrom) {
var imageChangeRef = $filter('imageObjectRef')(imageChangeFrom, $scope.projectName);
var builderRef = $filter('imageObjectRef')(buildConfigFrom, $scope.projectName);
return imageChangeRef === builderRef;
}
var buildConfigFrom = buildStrategy($scope.buildConfig).from;
triggers.forEach(function(trigger) {
switch (trigger.type) {
case "Generic":
triggerMap.genericWebhooks.push({disabled: false, data: trigger});
break;
case "GitHub":
triggerMap.githubWebhooks.push({disabled: false, data: trigger});
break;
case "ImageChange":
var imageChangeFrom = trigger.imageChange.from;
if (!imageChangeFrom) {
imageChangeFrom = buildConfigFrom;
}
var triggerRecord = {present: true, data: trigger};
if (isBuilder(imageChangeFrom, buildConfigFrom)) {
triggerMap.builderImageChangeTrigger = triggerRecord;
} else {
triggerMap.imageChangeTriggers.push(triggerRecord);
}
break;
case "ConfigChange":
triggerMap.configChangeTrigger = {present: true, data: trigger};
break;
}
});
// If the builder imageChange trigger is not present, pre-populate the imageChangeTriggers array with it
// and set the builderImageChangeTrigger object with it.
if (_.isEmpty(triggerMap.builderImageChangeTrigger)) {
triggerMap.builderImageChangeTrigger = {present: false, data: {imageChange: {}, type: "ImageChange"}};
}
if (_.isEmpty(triggerMap.configChangeTrigger)) {
triggerMap.configChangeTrigger = {present: false, data: {type: "ConfigChange"}};
}
return triggerMap;
};
$scope.aceLoaded = function(editor) {
var session = editor.getSession();
session.setOption('tabSize', 2);
session.setOption('useSoftTabs', true);
editor.$blockScrolling = Infinity;
};
var updatedImageSourcePath = function(imageSourcePaths) {
return _.map(
keyValueEditorUtils.compactEntries(imageSourcePaths),
function(path) {
return {
sourcePath: path.name,
destinationDir: path.value
};
});
};
var constructImageObject = function(optionsModel) {
var imageObject = {};
switch (optionsModel.type) {
case 'ImageStreamTag':
imageObject = {
kind: optionsModel.type,
name: optionsModel.imageStreamTag.imageStream + ":" + optionsModel.imageStreamTag.tagObject.tag
};
if (optionsModel.imageStreamTag.namespace !== $scope.buildConfig.metadata.namespace) {
imageObject.namespace = optionsModel.imageStreamTag.namespace;
}
break;
case 'DockerImage':
imageObject = {
kind: optionsModel.type,
name: optionsModel.dockerImage
};
break;
case 'ImageStreamImage':
var namespaceAndName = optionsModel.imageStreamImage.split("/");
imageObject = {
kind: optionsModel.type,
name: _.last(namespaceAndName)
};
imageObject.namespace = (namespaceAndName.length !== 1) ? namespaceAndName[0] : $scope.buildConfig.metadata.namespace;
break;
}
return imageObject;
};
var updateTriggers = function() {
var triggers = [].concat($scope.triggers.githubWebhooks,
$scope.triggers.genericWebhooks,
$scope.triggers.imageChangeTriggers,
$scope.triggers.builderImageChangeTrigger,
$scope.triggers.configChangeTrigger);
// Filter webhook triggers that are not disabled or imageChange triggers that are present
triggers = _.filter(triggers, function(trigger) {
// The condition has to check if the value exist and is set to 'false' in case of webhook triggers and 'true' in case of imageChange and configChange triggers.
return (_.has(trigger, 'disabled') && !trigger.disabled) || trigger.present;
});
triggers = _.map(triggers, 'data');
return triggers;
};
var loadBuildConfigSecrets = function() {
$scope.secrets.picked = {
gitSecret: $scope.buildConfig.spec.source.sourceSecret ? [$scope.buildConfig.spec.source.sourceSecret] : [{name: ""}],
pullSecret: buildStrategy($scope.buildConfig).pullSecret ? [buildStrategy($scope.buildConfig).pullSecret] : [{name: ""}],
pushSecret: $scope.buildConfig.spec.output.pushSecret ? [$scope.buildConfig.spec.output.pushSecret] : [{name: ""}]
};
switch ($scope.strategyType) {
case "Source":
case "Docker":
$scope.secrets.picked.sourceSecrets = $scope.buildConfig.spec.source.secrets || [{secret: { name: ""}, destinationDir: ""}];
break;
case "Custom":
$scope.secrets.picked.sourceSecrets = buildStrategy($scope.buildConfig).secrets || [{secretSource: { name: ""}, mountPath: ""}];
break;
}
};
var updateSecrets = function(object, pickedSecret, secretFieldName) {
if (pickedSecret.name) {
object[secretFieldName] = pickedSecret;
} else {
delete object[secretFieldName];
}
};
var updateSourceSecrets = function(object, pickedSecrets) {
var property = $scope.strategyType === "Custom" ? "secretSource" : "secret";
var nonEmptySecrets = _.filter(pickedSecrets, function(secret) {
return secret[property].name;
});
if (!_.isEmpty(nonEmptySecrets)) {
object.secrets = nonEmptySecrets;
} else {
delete object.secrets;
}
};
var getSourceMap = function(sourceMap, sources) {
if (sources.type === "None") {
return sourceMap;
}
sourceMap.none = false;
angular.forEach(sources, function(value, key) {
sourceMap[key] = true;
});
return sourceMap;
};
$scope.save = function() {
$scope.disableInputs = true;
clearIncompatibleBuildHookFields();
// Update Configuration
buildStrategy($scope.updatedBuildConfig).forcePull = $scope.options.forcePull;
switch ($scope.strategyType) {
case 'Docker':
buildStrategy($scope.updatedBuildConfig).noCache = $scope.options.noCache;
break;
case 'JenkinsPipeline':
if ($scope.jenkinsfileOptions.type === 'path') {
delete $scope.updatedBuildConfig.spec.strategy.jenkinsPipelineStrategy.jenkinsfile;
} else {
delete $scope.updatedBuildConfig.spec.strategy.jenkinsPipelineStrategy.jenkinsfilePath;
}
break;
}
// If imageSources are present update each ones From and Paths.
if ($scope.sources.images && !_.isEmpty($scope.sourceImages)) {
$scope.updatedBuildConfig.spec.source.images[0].paths = updatedImageSourcePath($scope.imageSourcePaths);
// Construct updated imageSource builder image object based on it's kind for the only imageSource
$scope.updatedBuildConfig.spec.source.images[0].from = constructImageObject($scope.imageOptions.fromSource);
}
// Construct updated builder image object based on it's kind
if ($scope.imageOptions.from.type === "None") {
delete buildStrategy($scope.updatedBuildConfig).from;
} else {
buildStrategy($scope.updatedBuildConfig).from = constructImageObject($scope.imageOptions.from);
}
// Construct updated output image object based on it's kind. Only Image Stream Tag, Docker Image and None can
// be specified for the output image. Not Image Stream Image since they are immutable.
if ($scope.imageOptions.to.type === "None") {
// If user will change the output reference to 'None' shall the potential PushSecret be deleted as well?
// This case won't delete them.
delete $scope.updatedBuildConfig.spec.output.to;
} else {
$scope.updatedBuildConfig.spec.output.to = constructImageObject($scope.imageOptions.to);
}
// Update envVars
buildStrategy($scope.updatedBuildConfig).env = keyValueEditorUtils.compactEntries($scope.envVars);
// Update secrets
updateSecrets($scope.updatedBuildConfig.spec.source, _.head($scope.secrets.picked.gitSecret), "sourceSecret");
updateSecrets(buildStrategy($scope.updatedBuildConfig), _.head($scope.secrets.picked.pullSecret), "pullSecret");
updateSecrets($scope.updatedBuildConfig.spec.output, _.head($scope.secrets.picked.pushSecret), "pushSecret");
switch ($scope.strategyType) {
case "Source":
case "Docker":
updateSourceSecrets($scope.updatedBuildConfig.spec.source, $scope.secrets.picked.sourceSecrets);
break;
case "Custom":
updateSourceSecrets(buildStrategy($scope.updatedBuildConfig), $scope.secrets.picked.sourceSecrets);
break;
}
// Update triggers
$scope.updatedBuildConfig.spec.triggers = updateTriggers();
DataService.update("buildconfigs", $scope.updatedBuildConfig.metadata.name, $scope.updatedBuildConfig, $scope.context).then(
function() {
AlertMessageService.addAlert({
name: $scope.updatedBuildConfig.metadata.name,
data: {
type: "success",
message: "Build Config " + $scope.updatedBuildConfig.metadata.name + " was successfully updated."
}
});
$location.path(Navigate.resourceURL($scope.updatedBuildConfig, "BuildConfig", $scope.updatedBuildConfig.metadata.namespace));
},
function(result) {
$scope.disableInputs = false;
$scope.alerts["save"] = {
type: "error",
message: "An error occurred updating the build " + $scope.updatedBuildConfig.metadata.name + "Build Config",
details: $filter('getErrorDetails')(result)
};
}
);
};
$scope.$on('$destroy', function(){
DataService.unwatchAll(watches);
});
});