forked from openshift/origin-web-console
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimages.js
246 lines (231 loc) · 7.08 KB
/
images.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
'use strict';
angular.module("openshiftConsole")
.factory("ImagesService", function($filter, ApplicationGenerator, DataService) {
var findImage = function(name, projectContext) {
var importImage = {
kind: "ImageStreamImport",
apiVersion: "v1",
metadata: {
name: "newapp",
namespace: projectContext.namespace
},
spec: {
import: false,
images: [{
from: {
kind: "DockerImage",
name: name
}
}]
},
status: {}
};
return DataService.create('imagestreamimports',
null,
importImage,
projectContext).then(function(response) {
return {
name: _.get(response, 'spec.images[0].from.name'),
image: _.get(response, 'status.images[0].image'),
tag: _.get(response, 'status.images[0].tag'),
// Call it "result" to avoid awkward "status.status" properties.
result: _.get(response, 'status.images[0].status')
};
});
};
var runsAsRoot = function(image) {
var user = _.get(image, 'dockerImageMetadata.Config.User');
return !user ||
user === '0' ||
user === 'root';
};
var getVolumes = function(image) {
return _.get(image, 'dockerImageMetadata.Config.Volumes');
};
// Generates resources for this Docker image, including
//
// - A deployment config
// - A service
// - An image stream (if not using an existing image stream tag)
//
// Creates emptyDir volumes for volumes declared in Docker image metadata.
//
// This is the same behavior as `oc new-app <image-name>`.
//
// Parameters:
//
// config, an object with the following required properties:
// name: the "app" name
// image: the image name (e.g., "mysql")
// tag: the image tag (e.g., "latest")
// namspace: the image stream namespace (if using an existing image stream tag)
// ports: the image ports as specified in the Docker image metadata
// volumes: the image volumes as specified in the Docker image metadata
// env: environment vars
// labels: labels for each resource
//
// Returns an array of objects that can be passed to `DataService.createList`
var getResources = function(config) {
var resources = [];
var annotations = {
"openshift.io/generated-by": "OpenShiftWebConsole"
};
// environment variables
var env = [];
_.forEach(config.env, function(value, key) {
env.push({name: key, value: value});
});
// volumes and volume mounts
var volumes = [], volumeMounts = [], volumeNumber = 0;
_.forEach(config.volumes, function(value, path) {
volumeNumber++;
var volumeName = config.name + '-' + volumeNumber;
volumes.push({
name: volumeName,
emptyDir: {}
});
volumeMounts.push({
name: volumeName,
mountPath: path
});
});
if (!config.namespace) {
var imageStream = {
kind: "ImageStream",
apiVersion: "v1",
metadata: {
name: config.name,
labels: config.labels
},
spec: {
tags: [{
name: config.tag,
annotations: _.assign({
"openshift.io/imported-from": config.image
}, annotations),
from: {
kind: "DockerImage",
name: config.image
},
importPolicy: {}
}]
}
};
resources.push(imageStream);
}
var deploymentConfig = {
kind: "DeploymentConfig",
apiVersion: "v1",
metadata: {
name: config.name,
labels: config.labels,
annotations: annotations
},
spec: {
strategy: {
resources: {}
},
triggers: [{
type: "ConfigChange"
}, {
type: "ImageChange",
imageChangeParams: {
automatic: true,
containerNames: [
config.name
],
from: {
kind: "ImageStreamTag",
// If we created the ImageStream, use our name. Otherwise, use
// the image name when referring to an ImageStreamTag from
// another namespace.
name: (config.namespace ? config.image : config.name) + ":" + config.tag,
namespace: config.namespace
}
}
}],
replicas: 1,
test: false,
selector: {
app: config.name,
deploymentconfig: config.name
},
template: {
metadata: {
labels: _.assign({
deploymentconfig: config.name
}, config.labels),
annotations: annotations
},
spec: {
volumes: volumes,
containers: [{
name: config.name,
image: config.image,
ports: config.ports,
env: env,
volumeMounts: volumeMounts
}],
resources: {}
}
}
},
status: {}
};
if(_.first(config.pullSecrets).name){
deploymentConfig.spec.template.spec.imagePullSecrets = config.pullSecrets;
}
resources.push(deploymentConfig);
var service;
if (config.ports.length) {
service = {
kind: "Service",
apiVersion: "v1",
metadata: {
name: config.name,
labels: config.labels,
annotations: annotations
},
spec: {
selector: {
deploymentconfig: config.name
},
ports: _.map(config.ports, function(port) {
return ApplicationGenerator.getServicePort(port);
})
}
};
resources.push(service);
}
return resources;
};
var getEnvironment = function(imageStreamImage) {
return _.map(_.get(imageStreamImage, 'image.dockerImageMetadata.Config.Env'),
function(entry) {
var ind = entry.indexOf('=');
var key = "";
var value = "";
if (ind > 0) {
key = entry.substring(0, ind);
if (ind + 1 < entry.length) {
value = entry.substring(ind + 1);
}
}
else {
key = entry;
}
return {
name: key,
value: value
};
}
);
};
return {
findImage: findImage,
getVolumes: getVolumes,
runsAsRoot: runsAsRoot,
getResources: getResources,
getEnvironment: getEnvironment
};
});