forked from openshift/origin-web-console
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcatalog.js
259 lines (222 loc) · 9.03 KB
/
catalog.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
'use strict';
angular.module("openshiftConsole")
.factory("CatalogService", function($filter,
APIService,
Constants,
KeywordService) {
var getTags = $filter('tags');
// Enable service catalog features if the new experience is enabled and the
// servicecatalog.k8s.io resources are available.
var SERVICE_CATALOG_ENABLED =
!Constants.SERVICE_CATALOG_ENABLED &&
APIService.apiInfo({ group: 'servicecatalog.k8s.io', resource: 'serviceclasses' }) &&
APIService.apiInfo({ group: 'servicecatalog.k8s.io', resource: 'serviceinstances' }) &&
APIService.apiInfo({ group: 'servicecatalog.k8s.io', resource: 'serviceinstancecredentials' });
var categoryItemByID = {};
_.each(Constants.CATALOG_CATEGORIES, function(category) {
_.each(category.items, function(categoryItem) {
categoryItemByID[categoryItem.id] = categoryItem;
// Add subcategories as well.
var subcategories = _.get(categoryItem, 'subcategories', []);
_.each(subcategories, function(subcategory) {
_.each(subcategory.items, function(categoryItem) {
categoryItemByID[categoryItem.id] = categoryItem;
});
});
});
});
var getCategoryItem = function(id) {
return categoryItemByID[id];
};
// Check if tag in is in the array of tags. Ignore case.
var hasTag = function(tag, tags) {
tag = tag.toLowerCase();
var i;
for (i = 0; i < tags.length; i++) {
var next = tags[i].toLowerCase();
if (tag === next) {
return true;
}
}
return false;
};
var hasCategory = function(categoryItem, tags) {
var aliases = _.get(categoryItem, 'categoryAliases', []);
var categoryTags = [categoryItem.id].concat(aliases);
return _.some(categoryTags, function(categoryTag) {
return hasTag(categoryTag, tags);
});
};
var categorizeImageStreams = function(imageStreams) {
var imageStreamsByCategory = {};
_.each(imageStreams, function(imageStream) {
if (!imageStream.status) {
return;
}
// Map of spec tags so we can find them efficiently later when looking
// at status tags.
var specTags = {};
if (imageStream.spec && imageStream.spec.tags) {
_.each(imageStream.spec.tags, function(tag) {
var tags = _.get(tag, 'annotations.tags');
if (tags) {
specTags[tag.name] = tags.split(/\s*,\s*/);
}
});
}
var matchFound = false;
_.each(categoryItemByID, function(categoryItem) {
var matchesCategory = function(imageStream) {
return _.some(imageStream.status.tags, function(tag) {
var categoryTags = specTags[tag.tag] || [];
return hasCategory(categoryItem, categoryTags) &&
hasTag('builder', categoryTags) &&
!hasTag('hidden', categoryTags);
});
};
if (matchesCategory(imageStream)) {
imageStreamsByCategory[categoryItem.id] = imageStreamsByCategory[categoryItem.id] || [];
imageStreamsByCategory[categoryItem.id].push(imageStream);
matchFound = true;
}
});
var isBuilder;
if (!matchFound) {
isBuilder = _.some(imageStream.status.tags, function(tag) {
var categoryTags = specTags[tag.tag] || [];
return hasTag('builder', categoryTags) && !hasTag('hidden', categoryTags);
});
if (isBuilder) {
imageStreamsByCategory[''] = imageStreamsByCategory[''] || [];
imageStreamsByCategory[''].push(imageStream);
}
}
});
return imageStreamsByCategory;
};
var categorizeTemplates = function(templates) {
var templatesByCategory = {};
_.each(templates, function(template) {
var tags = getTags(template), matchFound = false;
_.each(categoryItemByID, function(categoryItem) {
if (hasCategory(categoryItem, tags)) {
templatesByCategory[categoryItem.id] = templatesByCategory[categoryItem.id] || [];
templatesByCategory[categoryItem.id].push(template);
matchFound = true;
}
});
if (!matchFound) {
templatesByCategory[''] = templatesByCategory[''] || [];
templatesByCategory[''].push(template);
}
});
return templatesByCategory;
};
var referencesSameImageStream = function(specTag) {
return specTag.from &&
specTag.from.kind === 'ImageStreamTag' &&
specTag.from.name.indexOf(':') === -1 &&
!specTag.from.namespace;
};
// Don't use KeywordService for image stream filtering so we can add
// special handling for image stream tags. Match keywords (array of regex)
// against image streams and image stream tags, returning a copy of the
// image streams with only matching status tags.
var getDisplayName = $filter('displayName');
var filterImageStreams = function(imageStreams, keywords) {
if (!keywords.length) {
return imageStreams;
}
var filteredImageStreams = [];
_.each(imageStreams, function(imageStream) {
var name = _.get(imageStream, 'metadata.name', '');
var displayName = getDisplayName(imageStream, true);
var specTags = [];
var references = {};
var referencedBy = {};
_.each(imageStream.spec.tags, function(tag) {
// If the tag follows another, track the reference.
if (referencesSameImageStream(tag)) {
references[tag.name] = tag.from.name;
referencedBy[tag.from.name] = referencedBy[tag.from.name] || [];
referencedBy[tag.from.name].push(tag.name);
return;
}
// If the tag doesn't follow another, consider it in the search.
specTags.push(tag);
});
var matchingTags = _.keyBy(specTags, 'name');
// Find tags that match every keyword. Search image stream name, image
// stream display name, and tag names, and tag descriptions. If a
// keyword matches the image stream name or display name, it's
// considered to match all tags.
_.each(keywords, function(regex) {
if (regex.test(name)) {
return;
}
if (displayName && regex.test(displayName)) {
return;
}
// Check tag descriptions.
_.each(specTags, function(tag) {
// If this is not a builder or is hidden, don't match the tag.
var tagTags = _.get(tag, 'annotations.tags', '');
if (!/\bbuilder\b/.test(tagTags) || /\bhidden\b/.test(tagTags)) {
delete matchingTags[tag.name];
return;
}
// If the keyword matches the tag name, accept it.
if (regex.test(tag.name)) {
return;
}
// Search any tag names that reference this tag as well. For
// instance, searching for "latest" should match nodejs:4 if
// nodejs:latest references nodejs:4
var matches = function(referenceName) {
return regex.test(referenceName);
};
if (_.some(referencedBy[tag.name], matches)) {
return;
}
var description = _.get(tag, 'annotations.description');
if (!description || !regex.test(description)) {
delete matchingTags[tag.name];
}
});
});
// Make a copy of the image stream with only the matching tags.
var imageStreamCopy;
if (!_.isEmpty(matchingTags)) {
imageStreamCopy = angular.copy(imageStream);
imageStreamCopy.status.tags = _.filter(imageStreamCopy.status.tags, function(tag) {
// If this tag follow another tag, include it if the other tag matches the search.
var fromTag = references[tag.tag];
if (fromTag) {
return matchingTags[fromTag];
}
// Otherwise check if this tag was a match.
return matchingTags[tag.tag];
});
filteredImageStreams.push(imageStreamCopy);
}
});
return filteredImageStreams;
};
var templateFilterFields = [
'metadata.name',
'metadata.annotations["openshift.io/display-name"]',
'metadata.annotations.description'
];
var filterTemplates = function(templates, keywords) {
return KeywordService.filterForKeywords(templates, templateFilterFields, keywords);
};
return {
SERVICE_CATALOG_ENABLED: SERVICE_CATALOG_ENABLED,
getCategoryItem: getCategoryItem,
categorizeImageStreams: categorizeImageStreams,
categorizeTemplates: categorizeTemplates,
referencesSameImageStream: referencesSameImageStream,
filterImageStreams: filterImageStreams,
filterTemplates: filterTemplates
};
});