forked from rancher/dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchart.js
More file actions
612 lines (517 loc) · 20.3 KB
/
chart.js
File metadata and controls
612 lines (517 loc) · 20.3 KB
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
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
import { mapGetters } from 'vuex';
import {
REPO_TYPE, REPO, CHART, VERSION, NAMESPACE, NAME, DESCRIPTION as DESCRIPTION_QUERY, DEPRECATED as DEPRECATED_QUERY, HIDDEN, _FLAGGED, _CREATE, _EDIT
} from '@shell/config/query-params';
import { CATALOG as CATALOG_ANNOTATIONS } from '@shell/config/labels-annotations';
import { SHOW_PRE_RELEASE, mapPref } from '@shell/store/prefs';
import { NAME as EXPLORER } from '@shell/config/product/explorer';
import { NAME as MANAGER } from '@shell/config/product/manager';
import { OPA_GATE_KEEPER_ID } from '@shell/pages/c/_cluster/gatekeeper/index.vue';
import { formatSi, parseSi } from '@shell/utils/units';
import { CAPI, CATALOG } from '@shell/config/types';
import { isPrerelease } from '@shell/utils/version';
import { compareChartVersions } from '@shell/utils/chart';
import difference from 'lodash/difference';
import { LINUX, APP_UPGRADE_STATUS, isRancherRepo } from '@shell/store/catalog';
import { clone } from '@shell/utils/object';
import { merge } from 'lodash';
export default {
data() {
return {
version: null,
versionInfo: null,
versionInfoError: null,
existing: null,
ignoreWarning: false,
chart: null,
};
},
computed: {
...mapGetters(['currentCluster', 'isRancher']),
showPreRelease: mapPref(SHOW_PRE_RELEASE),
repo() {
return this.$store.getters['catalog/repo']({
repoType: this.query.repoType,
repoName: this.query.repoName,
});
},
showReadme() {
return !!this.versionInfo?.readme;
},
hasReadme() {
return !!this.versionInfo?.appReadme || !!this.versionInfo?.readme;
},
mappedVersions() {
const versions = (this.chart?.versions || []).slice();
versions.sort((a, b) => {
return compareChartVersions(b.version, a.version);
});
const selectedVersion = this.targetVersion;
const OSs = this.currentCluster?.workerOSs;
const out = [];
versions.forEach((version) => {
const nue = {
label: version.version,
version: version.version,
originalVersion: version.version,
id: version.version,
created: version.created,
disabled: false,
keywords: version.keywords
};
const permittedOs = version?.annotations?.[CATALOG_ANNOTATIONS.PERMITTED_OS];
const isRancher = isRancherRepo(this.repo, this.chart);
const fallbackOs = isRancher ? LINUX : '';
const permittedSystems = (permittedOs || fallbackOs).split(',').filter(Boolean);
if (permittedSystems.length > 0 && difference(OSs, permittedSystems).length > 0) {
nue.disabled = true;
}
// if only one OS is allowed, show '<OS>-only' on hover
if (permittedSystems.length === 1) {
nue.label = this.t(`catalog.install.versions.${ permittedSystems[0] }`, { ver: version.version });
}
if (!this.showPreRelease && isPrerelease(version.version)) {
return;
}
out.push(nue);
});
const selectedMatch = out.find((v) => v.id === selectedVersion);
if (!selectedMatch) {
out.unshift({
label: selectedVersion,
originalVersion: selectedVersion,
id: selectedVersion,
created: null,
disabled: false,
keywords: []
});
}
const currentVersion = out.find((v) => v.originalVersion === this.currentVersion);
if (currentVersion) {
currentVersion.label = this.t('catalog.install.versions.current', { ver: this.currentVersion });
}
return out;
},
// Conditionally filter out prerelease versions of the chart.
filteredVersions() {
return this.showPreRelease ? this.mappedVersions : this.mappedVersions.filter((v) => !v.isPre);
},
query() {
const query = this.$route.query;
return {
repoType: query[REPO_TYPE],
repoName: query[REPO],
chartName: query[CHART],
versionName: query[VERSION],
appNamespace: query[NAMESPACE] || '',
appName: query[NAME] || '',
description: query[DESCRIPTION_QUERY],
hidden: query[HIDDEN],
deprecated: query[DEPRECATED_QUERY]
};
},
showDeprecated() {
return this.query.deprecated === 'true' || this.query.deprecated === _FLAGGED;
},
showHidden() {
return this.query.hidden === _FLAGGED;
},
// If the user is installing the app for the first time,
// warn them about CPU and memory requirements.
warnings() {
const warnings = [];
if ( this.existing ) {
// Ignore the limits on upgrade (or if asked by query) and don't show any warnings
} else {
// The UI will show warnings about CPU and memory if
// these annotations are added to Helm chart:
// - catalog.cattle.io/requests-cpu
// - catalog.cattle.io/requests-memory
const needCpu = parseSi(this.version?.annotations?.[CATALOG_ANNOTATIONS.REQUESTS_CPU] || '0');
const needMemory = parseSi(this.version?.annotations?.[CATALOG_ANNOTATIONS.REQUESTS_MEMORY] || '0');
// Note: These are null if unknown
const availableCpu = this.currentCluster?.availableCpu;
const availableMemory = this.currentCluster?.availableMemory;
if ( availableCpu !== null && availableCpu < needCpu ) {
warnings.push(this.t('catalog.install.error.insufficientCpu', {
need: Math.round(needCpu * 100) / 100,
have: Math.round(availableCpu * 100) / 100,
}));
}
if ( availableMemory !== null && availableMemory < needMemory ) {
warnings.push(this.t('catalog.install.error.insufficientMemory', {
need: formatSi(needMemory, {
increment: 1024, suffix: 'iB', firstSuffix: 'B'
}),
have: formatSi(availableMemory, {
increment: 1024, suffix: 'iB', firstSuffix: 'B'
}),
}));
}
}
if (this.chart?.id === OPA_GATE_KEEPER_ID) {
warnings.unshift(this.t('gatekeeperIndex.deprecated', {}, true));
}
if (this.existing && this.existing.upgradeAvailable === APP_UPGRADE_STATUS.NOT_APPLICABLE) {
const manager = this.existing?.spec?.chart?.metadata?.annotations?.[CATALOG_ANNOTATIONS.MANAGED] || 'Rancher';
warnings.unshift(this.t('catalog.install.warning.managed', {
name: this.existing.name,
version: this.chart ? this.query.versionName : null,
manager: manager === 'true' ? 'Rancher' : manager
}, true));
}
return warnings;
},
requires() {
const requires = [];
const required = (this.version?.annotations?.[CATALOG_ANNOTATIONS.REQUIRES_GVK] || '').split(/\s*,\s*/).filter((x) => !!x).reverse();
if ( required.length ) {
for ( const gvr of required ) {
if ( this.$store.getters['catalog/isInstalled']({ gvr }) ) {
continue;
}
const provider = this.provider(gvr);
if ( provider ) {
const url = this.$router.resolve(this.chartLocation(true, provider)).href;
requires.push(this.t('catalog.install.error.requiresFound', {
url,
name: provider.name
}, true));
} else {
requires.push(this.t('catalog.install.error.requiresMissing', { name: gvr }));
}
}
}
return requires;
},
currentVersion() {
return this.existing?.spec?.chart?.metadata?.version;
},
targetVersion() {
return this.version ? this.version.version : this.query.versionName;
},
action() {
if (!this.existing) {
return {
name: 'install', tKey: 'install', icon: 'icon-plus'
};
}
if (this.currentVersion === this.targetVersion) {
return {
name: 'editVersion', tKey: 'edit', icon: 'icon-edit'
};
}
const diff = compareChartVersions(this.currentVersion, this.targetVersion);
if (diff < 0) {
return {
name: 'upgrade', tKey: 'upgrade', icon: 'icon-upgrade-alt'
};
}
return {
name: 'downgrade', tKey: 'downgrade', icon: 'icon-downgrade-alt'
};
},
isChartTargeted() {
return this.chart?.targetNamespace && this.chart?.targetName;
},
hasQuestions() {
return this.versionInfo && !!this.versionInfo.questions;
},
},
methods: {
/**
* Populate `this.chart`
*
* `chart` used to be a computed property pointing at getter catalog/chart
*
* this however stopped recalculating given changes to the store
*
* (the store would populate a charts collection, which the getter uses to find the chart,
* however this did not kick off the computed property, so this.charts was not populated)
*
* Now we find and cache the chart
*/
fetchStoreChart() {
if (!this.chart && this.repo && this.query.chartName) {
this.chart = this.$store.getters['catalog/chart']({
repoType: this.query.repoType,
repoName: this.query.repoName,
chartName: this.query.chartName,
includeHidden: true,
showDeprecated: this.showDeprecated
});
}
return this.chart;
},
async fetchChart() {
this.versionInfoError = null;
await Promise.all([
this.$store.dispatch('catalog/load'),
this.$store.dispatch('cluster/findAll', { type: CATALOG.APP })
]);
this.fetchStoreChart();
if ( !this.chart ) {
return;
}
// If no version is given in the URL query,
// use the first version provided by the Helm chart
// as the default.
if ( !this.query.versionName && this.chart.versions?.length ) {
if (this.showPreRelease) {
this.query.versionName = this.chart.versions[0].version;
} else {
const firstRelease = this.chart.versions.find((v) => !isPrerelease(v.version));
this.query.versionName = firstRelease?.version || this.chart.versions[0].version;
}
}
if ( !this.query.versionName ) {
return;
}
try {
this.version = this.$store.getters['catalog/version']({
repoType: this.query.repoType,
repoName: this.query.repoName,
chartName: this.query.chartName,
versionName: this.query.versionName,
showDeprecated: this.showDeprecated
});
} catch (e) {
console.error('Unable to fetch Version: ', e); // eslint-disable-line no-console
}
if (!this.version) {
console.warn('No version found: ', this.query.repoType, this.query.repoName, this.query.chartName, this.query.versionName);// eslint-disable-line no-console
}
try {
this.versionInfo = await this.$store.dispatch('catalog/getVersionInfo', {
repoType: this.query.repoType,
repoName: this.query.repoName,
chartName: this.query.chartName,
versionName: this.query.versionName
});
// Here we set us versionInfo. The returned
// object contains everything all info
// about a currently installed app, and it has the
// following keys:
//
// - appReadme: A short overview of what the app does. This
// forms the first few paragraphs of the chart info when
// you install a Helm chart app through Rancher.
// - chart: Metadata about the Helm chart, including the
// name and version.
// - readme: This is more detailed information that appears
// under the heading "Chart Information (Helm README)" when
// you install or upgrade a Helm chart app through Rancher,
// below the app README.
// - values: All Helm chart values for the currently installed
// app.
} catch (e) {
this.versionInfoError = e;
console.error('Unable to fetch VersionInfo: ', e); // eslint-disable-line no-console
}
if ( this.query.appNamespace && this.query.appName ) {
// First check the URL query for an app name and namespace.
// Use those values to check for a catalog app resource.
// If found, set the form to edit mode. If not, set the
// form to create mode.
try {
this.existing = await this.$store.dispatch('cluster/find', {
type: CATALOG.APP,
id: `${ this.query.appNamespace }/${ this.query.appName }`,
});
await this.existing?.fetchValues(true);
this.mode = _EDIT;
} catch (e) {
this.mode = _CREATE;
this.existing = null;
}
} else {
const targetNamespace = this.version?.annotations?.[CATALOG_ANNOTATIONS.NAMESPACE];
const targetName = this.version?.annotations?.[CATALOG_ANNOTATIONS.RELEASE_NAME];
if ( targetNamespace && targetName ) {
// If the app name and namespace values are not provided in the
// query, fall back on target values defined in the Helm chart itself.
// Ask to install a special chart with fixed namespace/name
// or edit it if there's an existing install.
try {
this.existing = await this.$store.dispatch('cluster/find', {
type: CATALOG.APP,
id: `${ targetNamespace }/${ targetName }`,
});
this.mode = _EDIT;
} catch (e) {
this.mode = _CREATE;
this.existing = null;
}
} else if (this.chart) {
const matching = this.chart.matchingInstalledApps;
if (matching.length === 1) {
this.existing = matching[0];
this.mode = _EDIT;
} else {
this.mode = _CREATE;
}
} else {
// Regular create
this.mode = _CREATE;
}
}
}, // End of fetchChart
// Charts have an annotation that specifies any additional charts that should be installed at the same time eg CRDs
async fetchAutoInstallInfo() {
const out = [];
/*
An example value for auto is ["rancher-monitoring-crd=match"].
It is an array of chart names that lets Rancher know of other
charts that should be auto-installed at the same time.
*/
const auto = (this.version?.annotations?.[CATALOG_ANNOTATIONS.AUTO_INSTALL] || '').split(/\s*,\s*/).filter((x) => !!x).reverse();
for ( const constraint of auto ) {
const provider = this.$store.getters['catalog/versionSatisfying']({
constraint,
repoName: this.chart.repoName,
repoType: this.chart.repoType,
chartVersion: this.version.version,
});
/* An example return value for "provider":
[
{
"name": "rancher-monitoring-crd",
"version": "100.1.3+up19.0.3",
"description": "Installs the CRDs for rancher-monitoring.",
"apiVersion": "v1",
"annotations": {
"catalog.cattle.io/certified": "rancher",
"catalog.cattle.io/hidden": "true",
"catalog.cattle.io/namespace": "cattle-monitoring-system",
"catalog.cattle.io/release-name": "rancher-monitoring-crd"
},
"type": "application",
"urls": [
"https://192.168.0.18:8005/k8s/clusters/c-m-hhpg69fv/v1/catalog.cattle.io.clusterrepos/rancher-charts?chartName=rancher-monitoring-crd&link=chart&version=100.1.3%2Bup19.0.3"
],
"created": "2022-04-27T10:04:18.343124-07:00",
"digest": "ecf07ba23a9cdaa7ffbbb14345d94ea1240b7f3b8e0ce9be4640e3e585c484e2",
"key": "cluster/rancher-charts/rancher-monitoring-crd/100.1.3+up19.0.3",
"repoType": "cluster",
"repoName": "rancher-charts"
}
]
*/
if ( provider ) {
try {
const crdVersionInfo = await this.$store.dispatch('catalog/getVersionInfo', {
repoType: provider.repoType,
repoName: provider.repoName,
chartName: provider.name,
versionName: provider.version
});
let existingCRDApp;
// search for an existing crd app to track any non-default values used on the previous install/upgrade
if (this.mode === _EDIT) {
const targetNamespace = crdVersionInfo?.chart?.annotations?.[CATALOG_ANNOTATIONS.NAMESPACE];
const targetName = crdVersionInfo?.chart?.annotations?.[CATALOG_ANNOTATIONS.RELEASE_NAME];
if (targetName && targetNamespace) {
existingCRDApp = await this.$store.dispatch('cluster/find', {
type: CATALOG.APP,
id: `${ targetNamespace }/${ targetName }`,
});
}
}
if (existingCRDApp) {
await existingCRDApp.fetchValues(true);
// spec.values are any non-default values the user configured
// the installation form should show these, as well as any default values from the chart
const existingValues = clone(existingCRDApp.values || {});
const defaultValues = clone(existingCRDApp.chartValues || {});
crdVersionInfo.existingValues = existingValues;
crdVersionInfo.allValues = merge(defaultValues, existingValues);
} else {
// allValues will potentially be updated in the UI - we want to track this separately from values to avoid mutating the chart object in the store
// this is similar to userValues for the main chart
crdVersionInfo.allValues = clone(crdVersionInfo.values);
}
out.push(crdVersionInfo);
} catch (e) {
console.error('Unable to fetch VersionInfo: ', e); // eslint-disable-line no-console
}
} else {
this.errors.push(`This chart requires ${ constraint } but no matching chart was found`);
}
}
this['autoInstallInfo'] = out;
},
selectVersion({ id: version }) {
this.$router.applyQuery({ [VERSION]: version });
},
provider(gvr) {
return this.$store.getters['catalog/versionProviding']({
gvr,
repoName: this.chart.repoName,
repoType: this.chart.repoType
});
},
/**
* Location of chart install or details page for either the current chart or from gvr
*/
chartLocation(install = false, prov) {
const provider = prov || {
repoType: this.chart.repoType,
repoName: this.chart.repoName,
name: this.chart.chartName,
version: this.query.versionName,
};
const query = {
[REPO_TYPE]: provider.repoType,
[REPO]: provider.repoName,
[CHART]: provider.name,
[VERSION]: provider.version,
};
if (this.showDeprecated) {
query[DEPRECATED_QUERY] = this.query.deprecated;
}
return {
name: install ? 'c-cluster-apps-charts-install' : 'c-cluster-apps-charts-chart',
params: {
cluster: this.$route.params.cluster,
product: this.$store.getters['productId'],
},
query
};
},
appLocation() {
return this.existing?.detailLocation || {
name: `c-cluster-product-resource`,
params: {
product: this.$store.getters['productId'],
cluster: this.$store.getters['clusterId'],
resource: CATALOG.APP,
}
};
},
clusterToolsLocation() {
const query = {};
if (this.showDeprecated) {
query[DEPRECATED_QUERY] = this.query.deprecated;
}
return {
name: `c-cluster-explorer-tools`,
params: {
product: EXPLORER,
cluster: this.$store.getters['clusterId'],
resource: CATALOG.APP,
},
query
};
},
clustersLocation() {
return {
name: 'c-cluster-product-resource',
params: {
cluster: this.$route.params.cluster,
product: MANAGER,
resource: CAPI.RANCHER_CLUSTER,
},
};
}
},
};