forked from openshift/console
/
catalog-utils.tsx
180 lines (167 loc) · 6 KB
/
catalog-utils.tsx
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
import * as React from 'react';
import { Tooltip } from '@patternfly/react-core';
import { TFunction } from 'i18next';
import * as _ from 'lodash';
import { CatalogItem } from '@console/dynamic-plugin-sdk';
import { getImageForIconClass } from '@console/internal/components/catalog/catalog-item-icon';
import { ExternalLink } from '@console/internal/components/utils';
import { K8sResourceKind } from '@console/internal/module/k8s';
import { toTitleCase } from '@console/shared';
import * as certifiedIcon from '../../../icons/certified.svg';
import { HelmChartEntries, HelmChartMetaData } from '../../types/helm-types';
import { getChartRepositoryTitle } from '../../utils/helm-utils';
import HelmReadmeLoader from '../components/HelmReadmeLoader';
import {
CHART_NAME_ANNOTATION,
PROVIDER_TYPE,
PROVIDER_TYPE_ANNOTATION,
PROVIDER_TYPE_KEYS,
PROVIDER_NAME_ANNOTATION,
SUPPORT_URL_ANNOTATION,
} from './const';
export const normalizeHelmCharts = (
chartEntries: HelmChartEntries,
chartRepositories: K8sResourceKind[],
activeNamespace: string = '',
t: TFunction,
): CatalogItem[] => {
return _.reduce(
chartEntries,
(normalizedCharts, charts, key) => {
const chartRepoName = key.split('--').pop();
const chartRepositoryTitle = getChartRepositoryTitle(chartRepositories, chartRepoName);
charts.forEach((chart: HelmChartMetaData) => {
const { name, created, version, appVersion, description, keywords, annotations } = chart;
const annotatedName = annotations?.[CHART_NAME_ANNOTATION] ?? '';
const providerType = annotations?.[PROVIDER_TYPE_ANNOTATION] ?? '';
const providerName = annotations?.[PROVIDER_NAME_ANNOTATION] ?? '';
const supportUrl = annotations?.[SUPPORT_URL_ANNOTATION] ?? '';
const displayName = annotatedName || `${toTitleCase(name)}`;
const imgUrl = chart.icon || getImageForIconClass('icon-helm');
const chartURL = chart.urls[0];
const encodedChartURL = encodeURIComponent(chartURL);
const href = `/catalog/helm-install?chartName=${name}&chartRepoName=${chartRepoName}&chartURL=${encodedChartURL}&preselected-ns=${activeNamespace}`;
const translatedProviderType = PROVIDER_TYPE_KEYS[providerType]
? t(PROVIDER_TYPE_KEYS[providerType])
: _.capitalize(providerType);
const title =
providerType === PROVIDER_TYPE.partner ? (
<>
<span style={{ verticalAlign: 'middle' }}>{displayName}</span>{' '}
<Tooltip
content={t(
'helm-plugin~This Helm Chart is provided by a trusted partner and has been verified for ease of integration.',
)}
>
<img src={certifiedIcon} alt={t('helm-plugin~Certified')} />
</Tooltip>
</>
) : null;
const maintainers = chart.maintainers?.length > 0 && (
<>
{chart.maintainers?.map((maintainer, index) => (
<React.Fragment key={index}>
{maintainer.name}
<br />
<a href={`mailto:${maintainer.email}`}>{maintainer.email}</a>
<br />
</React.Fragment>
))}
</>
);
const homePage = chart.home && (
<ExternalLink href={chart.home} additionalClassName="co-break-all" text={chart.home} />
);
const detailsProperties = [
{
label: t('helm-plugin~Latest Chart version'),
value: version,
},
{
label: t('helm-plugin~Product version'),
value: appVersion,
},
{
label: t('helm-plugin~Source'),
value: translatedProviderType,
},
{
label: t('helm-plugin~Provider'),
value: providerName,
},
{
label: t('helm-plugin~Home page'),
value: homePage,
},
{
label: t('helm-plugin~Repository'),
value: chartRepositoryTitle,
},
{
label: t('helm-plugin~Maintainers'),
value: maintainers,
},
];
const detailsDescriptions = [
{
value: <p>{description}</p>,
},
{
value: <HelmReadmeLoader chartURL={chartURL} />,
},
];
const helmChart = {
uid: `${chartRepoName}--${chartURL}`,
type: 'HelmChart',
name: displayName,
title,
description,
provider: providerName,
tags: keywords,
creationTimestamp: created,
attributes: {
name,
chartRepositoryTitle,
version,
providerType: translatedProviderType,
},
icon: {
class: null,
url: imgUrl,
},
cta: {
label: t('helm-plugin~Install Helm Chart'),
href,
},
details: {
properties: detailsProperties,
descriptions: detailsDescriptions,
},
supportUrl,
};
// group Helm chart with same name and different version together
const existingChartIndex = normalizedCharts.findIndex((currentChart) => {
return (
currentChart.attributes?.name === name &&
currentChart.attributes?.chartRepositoryTitle === chartRepositoryTitle
);
});
if (existingChartIndex > -1) {
const existingChart = normalizedCharts[existingChartIndex];
const versionCompare = helmChart.attributes?.version?.localeCompare(
existingChart?.attributes?.version,
undefined,
{ numeric: true, sensitivity: 'base' },
);
if (versionCompare === 1) {
normalizedCharts[existingChartIndex] = helmChart;
}
} else {
normalizedCharts.push(helmChart);
}
});
return normalizedCharts;
},
[] as CatalogItem[],
);
};