forked from openshift/origin-web-console
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeploymentConfig.js
338 lines (313 loc) · 14.5 KB
/
deploymentConfig.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
'use strict';
/**
* @ngdoc function
* @name openshiftConsole.controller:EditDeploymentConfigController
* @description
* Controller of the openshiftConsole
*/
angular.module('openshiftConsole')
.controller('EditDeploymentConfigController',
function($scope,
$filter,
$location,
$routeParams,
$uibModal,
AlertMessageService,
AuthorizationService,
BreadcrumbsService,
DataService,
Navigate,
ProjectsService,
SecretsService,
keyValueEditorUtils) {
$scope.projectName = $routeParams.project;
$scope.deploymentConfig = null;
$scope.alerts = {};
$scope.view = {
advancedStrategyOptions: false,
advancedImageOptions: false
};
$scope.triggers = {};
$scope.breadcrumbs = BreadcrumbsService.getBreadcrumbs({
name: $routeParams.name,
kind: $routeParams.kind,
namespace: $routeParams.project,
subpage: 'Edit Deployment Config',
includeProject: true
});
$scope.deploymentConfigStrategyTypes = [
"Recreate",
"Rolling",
"Custom"
];
AlertMessageService.getAlerts().forEach(function(alert) {
$scope.alerts[alert.name] = alert.data;
});
AlertMessageService.clearAlerts();
var watches = [];
var getParamsPropertyName = function(strategyType) {
switch (strategyType) {
case "Recreate":
return "recreateParams";
case "Rolling":
return "rollingParams";
case "Custom":
return "customParams";
default:
Logger.error('Unknown deployment strategy type: ' + strategyType);
return;
}
};
ProjectsService
.get($routeParams.project)
.then(_.spread(function(project, context) {
$scope.project = project;
$scope.context = context;
if (!AuthorizationService.canI('deploymentconfigs', 'update', $routeParams.project)) {
Navigate.toErrorPage('You do not have authority to update deployment config ' +
$routeParams.deploymentconfig + '.', 'access_denied');
return;
}
DataService.get("deploymentconfigs", $routeParams.deploymentconfig, context).then(
// success
function(deploymentConfig) {
$scope.deploymentConfig = deploymentConfig;
$scope.breadcrumbs = BreadcrumbsService.getBreadcrumbs({
object: deploymentConfig,
project: project,
subpage: 'Edit',
includeProject: true
});
// Create map which will associate concatiner name to container's data(envVar, trigger and image which will be used on manual deployment)
var mapContainerConfigByName = function(containers, triggers) {
var containerConfigByName = {};
var imageChangeTriggers = _.filter(triggers, {type: 'ImageChange'});
_.each(containers, function(container) {
var imageChangeTriggerForContainer = _.find(imageChangeTriggers, function(trigger) {
return _.includes(trigger.imageChangeParams.containerNames, container.name);
});
var triggerData = {};
containerConfigByName[container.name] = {
env: container.env || [],
image: container.image,
hasDeploymentTrigger: !_.isEmpty(imageChangeTriggerForContainer)
};
if (imageChangeTriggerForContainer) {
var triggerFromData = imageChangeTriggerForContainer.imageChangeParams.from;
var triggerImageNameParts = triggerFromData.name.split(':');
triggerData = {
data: imageChangeTriggerForContainer,
istag: {namespace: triggerFromData.namespace || $scope.projectName, imageStream: triggerImageNameParts[0], tagObject: {tag: triggerImageNameParts[1]}},
automatic: _.get(imageChangeTriggerForContainer, 'imageChangeParams.automatic', false)
};
} else {
triggerData = {
istag: {namespace: "", imageStream: ""},
// Default to true when setting up a new image change trigger.
automatic: true
};
}
_.set(containerConfigByName, [container.name, 'triggerData'], triggerData);
});
return containerConfigByName;
};
$scope.updatedDeploymentConfig = angular.copy($scope.deploymentConfig);
$scope.containerNames = _.map($scope.deploymentConfig.spec.template.spec.containers, 'name');
$scope.containerConfigByName = mapContainerConfigByName($scope.updatedDeploymentConfig.spec.template.spec.containers, $scope.updatedDeploymentConfig.spec.triggers);
$scope.secrets = {
pullSecrets: angular.copy($scope.deploymentConfig.spec.template.spec.imagePullSecrets) || [{name: ''}]
};
$scope.volumeNames = _.map($scope.deploymentConfig.spec.template.spec.volumes, 'name');
$scope.strategyData = angular.copy($scope.deploymentConfig.spec.strategy);
$scope.originalStrategy = $scope.strategyData.type;
$scope.strategyParamsPropertyName = getParamsPropertyName($scope.strategyData.type);
$scope.triggers.hasConfigTrigger = _.some($scope.updatedDeploymentConfig.spec.triggers, {type: 'ConfigChange'});
// If strategy is 'Custom' and no environment variables are present, initiliaze them.
if ($scope.strategyData.type === 'Custom' && !_.has($scope.strategyData, 'customParams.environment')) {
$scope.strategyData.customParams.environment = [];
}
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.secretsByType = _.each(secretNamesByType, function(secretsArray) {
secretsArray.unshift("");
});
});
// If we found the item successfully, watch for changes on it
watches.push(DataService.watchObject("deploymentconfigs", $routeParams.deploymentconfig, context, function(deploymentConfig, action) {
if (action === 'MODIFIED') {
$scope.alerts["updated/deleted"] = {
type: "warning",
message: "This deployment 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 deployment configuration has been deleted."
};
$scope.disableInputs = true;
}
$scope.deploymentConfig = deploymentConfig;
}));
$scope.loaded = true;
},
// failure
function(e) {
$scope.loaded = true;
$scope.alerts["load"] = {
type: "error",
message: "The deployment configuration details could not be loaded.",
details: $filter('getErrorDetails')(e)
};
}
);
})
);
// helper for detemining if strategy switch was done between Rolling <-> Recreate strategy
var isRollingRecreateSwitch = function() {
return ($scope.strategyData.type !== 'Custom' && $scope.originalStrategy !== 'Custom' && $scope.strategyData.type !== $scope.originalStrategy);
};
var promptToMoveParams = function(pickedStrategyParams) {
if (_.has($scope.strategyData, pickedStrategyParams)) {
return;
}
var modalInstance = $uibModal.open({
animation: true,
templateUrl: 'views/modals/confirm.html',
controller: 'ConfirmModalController',
resolve: {
modalConfig: function() {
return {
alerts: $scope.alerts,
message: "Some of your existing " + $scope.originalStrategy.toLowerCase() + " strategy parameters can be used for the " + $scope.strategyData.type.toLowerCase() + " strategy. Keep parameters?",
details: "The timeout parameter and any pre or post lifecycle hooks will be copied from " + $scope.originalStrategy.toLowerCase() + " strategy to " + $scope.strategyData.type.toLowerCase() + " strategy. After saving the changes, " + $scope.originalStrategy.toLowerCase() + " strategy parameters will be removed.",
okButtonText: "Yes",
okButtonClass: "btn-primary",
cancelButtonText: "No"
};
}
}
});
modalInstance.result.then(function () {
// Move parameters that belong to the origial strategy to the picked one.
$scope.strategyData[pickedStrategyParams] = angular.copy($scope.strategyData[getParamsPropertyName($scope.originalStrategy)]);
}, function() {
// Create empty parameters for the newly picked strategy
$scope.strategyData[pickedStrategyParams] = {};
});
};
$scope.strategyChanged = function() {
var pickedStrategyParams = getParamsPropertyName($scope.strategyData.type);
if (isRollingRecreateSwitch()) {
promptToMoveParams(pickedStrategyParams);
} else {
if (!_.has($scope.strategyData, pickedStrategyParams)) {
if ($scope.strategyData.type !== 'Custom') {
$scope.strategyData[pickedStrategyParams] = {};
} else {
$scope.strategyData[pickedStrategyParams] = {
image: "",
command: [],
environment: []
};
}
}
}
$scope.strategyParamsPropertyName = pickedStrategyParams;
};
var assembleImageChangeTrigger = function(containerName, ist, trigger, automatic) {
var istagObject = {
kind: "ImageStreamTag",
namespace: ist.namespace,
name: ist.imageStream + ':' + ist.tagObject.tag
};
if (trigger) {
trigger.imageChangeParams.from = istagObject;
trigger.imageChangeParams.automatic = automatic;
} else {
trigger = {
type: "ImageChange",
imageChangeParams: {
automatic: automatic,
containerNames: [containerName],
from: istagObject
}
};
}
return trigger;
};
var updateTriggers = function() {
// Preserve any triggers we don't handle in the editor.
var updatedTriggers = _.reject($scope.updatedDeploymentConfig.spec.triggers, function(trigger) {
return trigger.type === 'ImageChange' || trigger.type === 'ConfigChange';
});
_.each($scope.containerConfigByName, function(containerData, containerName) {
if (containerData.hasDeploymentTrigger) {
updatedTriggers.push(assembleImageChangeTrigger(containerName,
containerData.triggerData.istag,
containerData.triggerData.data,
containerData.triggerData.automatic));
} else {
var imageSpec = _.find($scope.updatedDeploymentConfig.spec.template.spec.containers, { name: containerName });
imageSpec.image = containerData.image;
}
});
if ($scope.triggers.hasConfigTrigger) {
updatedTriggers.push({
type: "ConfigChange"
});
}
return updatedTriggers;
};
$scope.save = function() {
$scope.disableInputs = true;
// Update env for each container
_.each($scope.containerConfigByName, function(containerData, containerName) {
var matchingContainer = _.find($scope.updatedDeploymentConfig.spec.template.spec.containers, { name: containerName });
matchingContainer.env = keyValueEditorUtils.compactEntries(containerData.env);
});
// Remove parameters of previously set strategy, if user moved
if (isRollingRecreateSwitch()) {
delete $scope.strategyData[getParamsPropertyName($scope.originalStrategy)];
}
if ($scope.strategyData.type !== 'Custom') {
_.each(['pre', 'mid', 'post'], function(hookType) {
if (_.has($scope.strategyData, [$scope.strategyParamsPropertyName, hookType, 'execNewPod', 'env'])) {
$scope.strategyData[$scope.strategyParamsPropertyName][hookType].execNewPod.env = keyValueEditorUtils.compactEntries($scope.strategyData[$scope.strategyParamsPropertyName][hookType].execNewPod.env);
}
});
} else if (_.has($scope, 'strategyData.customParams.environment')) {
$scope.strategyData.customParams.environment = keyValueEditorUtils.compactEntries($scope.strategyData.customParams.environment);
}
// Update image pull secrets
$scope.updatedDeploymentConfig.spec.template.spec.imagePullSecrets = _.filter($scope.secrets.pullSecrets, 'name');
$scope.updatedDeploymentConfig.spec.strategy = $scope.strategyData;
$scope.updatedDeploymentConfig.spec.triggers = updateTriggers();
DataService.update("deploymentconfigs", $scope.updatedDeploymentConfig.metadata.name, $scope.updatedDeploymentConfig, $scope.context).then(
function() {
AlertMessageService.addAlert({
name: $scope.updatedDeploymentConfig.metadata.name,
data: {
type: "success",
message: "Deployment config " + $scope.updatedDeploymentConfig.metadata.name + " was successfully updated."
}
});
var returnURL = Navigate.resourceURL($scope.updatedDeploymentConfig);
$location.url(returnURL);
},
function(result) {
$scope.disableInputs = false;
$scope.alerts["save"] = {
type: "error",
message: "An error occurred updating deployment config " + $scope.updatedDeploymentConfig.metadata.name + ".",
details: $filter('getErrorDetails')(result)
};
}
);
};
$scope.$on('$destroy', function(){
DataService.unwatchAll(watches);
});
});