forked from openshift/origin-web-console
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddSecretToApplication.js
180 lines (163 loc) · 6.03 KB
/
addSecretToApplication.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
"use strict";
(function() {
angular.module("openshiftConsole").component('addSecretToApplication', {
controller: [
'$filter',
'$scope',
'APIService',
'DataService',
'Navigate',
'NotificationsService',
'StorageService',
AddSecretToApplication
],
controllerAs: 'ctrl',
bindings: {
project: '<',
secret: '<',
onComplete: '<',
onCancel: '<'
},
templateUrl: 'views/directives/add-secret-to-application.html'
});
function AddSecretToApplication($filter, $scope, APIService, DataService, Navigate, NotificationsService, StorageService) {
var ctrl = this;
var deploymentConfigs;
var deployments;
var replicationControllers;
var replicaSets;
var statefulSets;
var sortApplications = function() {
// Don't waste time sorting on each data load, just sort when we have them all
if (deploymentConfigs && deployments && replicationControllers && replicaSets && statefulSets) {
var apiObjects = deploymentConfigs.concat(deployments)
.concat(replicationControllers)
.concat(replicaSets)
.concat(statefulSets);
ctrl.applications = _.sortBy(apiObjects, ['metadata.name', 'kind']);
ctrl.updating = false;
}
};
var getApplications = function() {
var hasDeploymentFilter = $filter('hasDeployment');
var hasDeploymentConfigFilter = $filter('hasDeploymentConfig');
ctrl.updating = true;
var context = {
namespace: ctrl.project.metadata.name
};
// Load all the "application" types
DataService.list('deploymentconfigs', context).then(function(deploymentConfigData) {
deploymentConfigs = _.toArray(deploymentConfigData.by('metadata.name'));
sortApplications();
});
DataService.list('replicationcontrollers', context).then(function(replicationControllerData) {
replicationControllers = _.reject(replicationControllerData.by('metadata.name'), hasDeploymentConfigFilter);
sortApplications();
});
DataService.list({
group: 'apps',
resource: 'deployments'
}, context).then(function(deploymentData) {
deployments = _.toArray(deploymentData.by('metadata.name'));
sortApplications();
});
DataService.list({
group: 'extensions',
resource: 'replicasets'
}, context).then(function(replicaSetData) {
replicaSets = _.reject(replicaSetData.by('metadata.name'), hasDeploymentFilter);
sortApplications();
});
DataService.list({
group: 'apps',
resource: 'statefulsets'
}, context).then(function(statefulSetData) {
statefulSets = _.toArray(statefulSetData.by('metadata.name'));
sortApplications();
});
};
ctrl.$onInit = function() {
ctrl.addType = 'env';
ctrl.disableInputs = false;
getApplications();
};
ctrl.$postLink = function() {
$scope.$watch(function() {
return ctrl.application;
}, function() {
// Look at the existing mount paths so that we can warn if the new value is not unique.
var podTemplate = _.get(ctrl.application, 'spec.template');
ctrl.existingMountPaths = StorageService.getMountPaths(podTemplate);
});
};
ctrl.addToApplication = function() {
var applicationToUpdate = angular.copy(ctrl.application);
var podTemplate = _.get(applicationToUpdate, 'spec.template');
ctrl.disableInputs = true;
if (ctrl.addType === 'env') {
var newEnvFrom = {
secretRef: {
name: ctrl.secret.metadata.name
}
};
// For each container, add the new volume mount.
_.each(podTemplate.spec.containers, function(container) {
container.envFrom = container.envFrom || [];
container.envFrom.push(newEnvFrom);
});
} else {
var generateName = $filter('generateName');
var name = generateName(ctrl.secret.metadata.name + '-');
var newVolumeMount = {
name: name,
mountPath: ctrl.mountVolume,
readOnly: true
};
// For each selected container, add the new volume mount.
_.each(podTemplate.spec.containers, function(container) {
container.volumeMounts = container.volumeMounts || [];
container.volumeMounts.push(newVolumeMount);
});
var newVolume = {
name: name,
secret: {
secretName: ctrl.secret.metadata.name
}
};
podTemplate.spec.volumes = podTemplate.spec.volumes || [];
podTemplate.spec.volumes.push(newVolume);
}
var humanizeKind = $filter('humanizeKind');
var sourceKind = humanizeKind(ctrl.secret.kind);
var targetKind = humanizeKind(applicationToUpdate.kind);
var context = {
namespace: ctrl.project.metadata.name
};
DataService.update(APIService.kindToResource(applicationToUpdate.kind), applicationToUpdate.metadata.name, applicationToUpdate, context).then(
function() {
NotificationsService.addNotification({
type: "success",
message: "Successfully added " + sourceKind + " " + ctrl.secret.metadata.name + " to " + targetKind + " " + applicationToUpdate.metadata.name + ".",
links: [{
href: Navigate.resourceURL(applicationToUpdate),
label: "View " + humanizeKind(applicationToUpdate.kind, true)
}]
});
if (angular.isFunction(ctrl.onComplete)) {
ctrl.onComplete();
}
},
function(result) {
var getErrorDetails = $filter('getErrorDetails');
NotificationsService.addNotification({
type: "error",
message: "An error occurred adding " + sourceKind + " " + ctrl.secret.metadata.name + " to " + targetKind + " " + applicationToUpdate.metadata.name + ". " +
getErrorDetails(result)
});
}).finally(function() {
ctrl.disableInputs = false;
}
);
};
}
})();