Skip to content

Commit 022a48a

Browse files
authored
feat: Community modules details (#3943)
1 parent 8512402 commit 022a48a

File tree

3 files changed

+99
-53
lines changed

3 files changed

+99
-53
lines changed

src/components/KymaModules/components/CommunityModulesList.tsx

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import { useSetRecoilState } from 'recoil';
44
import { Button } from '@ui5/webcomponents-react';
55
import pluralize from 'pluralize';
66
import {
7+
createModulePartialPath,
8+
findCrd,
9+
findExtension,
710
findModuleTemplate,
811
ModuleTemplateListType,
912
ModuleTemplateType,
@@ -16,12 +19,13 @@ import {
1619
ShowCreate,
1720
} from 'state/columnLayoutAtom';
1821
import { isFormOpenState } from 'state/formOpenAtom';
22+
import { useGet, useGetList } from 'shared/hooks/BackendAPI/useGet';
1923
import { GenericList } from 'shared/components/GenericList/GenericList';
2024
import { useNavigate } from 'react-router';
2125
import { useFetchModuleData } from '../hooks';
2226
import { ModulesListRows } from './ModulesListRows';
2327

24-
type ModulesListProps = {
28+
type CommunityModulesListProps = {
2529
moduleTemplates: ModuleTemplateListType;
2630
selectedModules: any[];
2731
modulesLoading: boolean;
@@ -43,8 +47,23 @@ export const CommunityModulesList = ({
4347
handleResourceDelete,
4448
customSelectedEntry,
4549
setSelectedEntry,
46-
}: ModulesListProps) => {
50+
}: CommunityModulesListProps) => {
4751
const { t } = useTranslation();
52+
53+
const { data: communityExtentions } = useGetList(
54+
(ext: { metadata: { labels: Record<string, string> } }) =>
55+
ext.metadata.labels['app.kubernetes.io/part-of'] !== 'Kyma',
56+
)('/api/v1/configmaps?labelSelector=busola.io/extension=resource', {
57+
pollingInterval: 5000,
58+
} as any);
59+
60+
const { data: crds } = useGet(
61+
`/apis/apiextensions.k8s.io/v1/customresourcedefinitions`,
62+
{
63+
pollingInterval: 5000,
64+
} as any,
65+
);
66+
4867
const navigate = useNavigate();
4968
const { clusterUrl, namespaceUrl } = useUrl();
5069
const setLayoutColumn = useSetRecoilState(columnLayoutState);
@@ -175,40 +194,43 @@ export const CommunityModulesList = ({
175194
};
176195
}
177196

197+
const hasExtension = !!findExtension(
198+
moduleStatus?.resource?.kind,
199+
communityExtentions,
200+
);
201+
const moduleCrd = findCrd(moduleStatus?.resource?.kind, crds);
178202
const skipRedirect = !hasDetailsLink(moduleStatus);
179203

180204
if (skipRedirect) {
181205
return;
182206
}
183207

184-
const pathName = `${pluralize(
185-
moduleStatus?.resource?.kind || '',
186-
).toLowerCase()}/${moduleStatus?.resource?.metadata?.name}`;
187-
188-
const partialPath = moduleStatus?.resource?.metadata?.namespace
189-
? `kymamodules/namespaces/${moduleStatus?.resource?.metadata?.namespace}/${pathName}`
190-
: `kymamodules/${pathName}`;
191-
208+
const partialPath = createModulePartialPath(
209+
hasExtension,
210+
moduleStatus.resource,
211+
moduleCrd,
212+
);
192213
const path = namespaced
193214
? namespaceUrl(partialPath)
194215
: clusterUrl(partialPath);
195216

196217
const { group, version } = extractApiGroupVersion(
197218
moduleStatus?.resource?.apiVersion,
198219
);
220+
199221
setLayoutColumn({
200222
startColumn: {
201-
resourceType: pluralize(
202-
moduleStatus?.resource?.kind || '',
203-
).toLowerCase(),
223+
resourceType: hasExtension
224+
? pluralize(moduleStatus?.resource?.kind || '').toLowerCase()
225+
: moduleCrd?.metadata?.name,
204226
namespaceId: moduleStatus?.resource?.metadata.namespace || '',
205227
apiGroup: group,
206228
apiVersion: version,
207229
} as ColumnState,
208230
midColumn: {
209-
resourceType: pluralize(
210-
moduleStatus?.resource?.kind || '',
211-
).toLowerCase(),
231+
resourceType: hasExtension
232+
? pluralize(moduleStatus?.resource?.kind || '').toLowerCase()
233+
: moduleCrd?.metadata?.name,
212234
resourceName: moduleStatus?.resource?.metadata?.name,
213235
namespaceId: moduleStatus?.resource?.metadata.namespace || '',
214236
apiGroup: group,

src/components/KymaModules/components/ModulesList.tsx

Lines changed: 14 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import React from 'react';
22
import { useTranslation } from 'react-i18next';
33
import { useSetRecoilState } from 'recoil';
4-
import jsyaml from 'js-yaml';
54
import { Button } from '@ui5/webcomponents-react';
65
import pluralize from 'pluralize';
76
import {
7+
createModulePartialPath,
8+
findCrd,
9+
findExtension,
810
findModuleStatus,
911
findModuleTemplate,
1012
KymaResourceSpecModuleType,
@@ -26,13 +28,6 @@ import { GenericList } from 'shared/components/GenericList/GenericList';
2628
import { ModulesListRows } from './ModulesListRows';
2729
import { useNavigate } from 'react-router';
2830

29-
type CustomResourceDefinitionsType = {
30-
items: {
31-
metadata?: { name: string };
32-
spec?: { names?: { kind?: string } };
33-
}[];
34-
};
35-
3631
type ModulesListProps = {
3732
resource: KymaResourceType;
3833
moduleTemplates: ModuleTemplateListType;
@@ -63,12 +58,14 @@ export const ModulesList = ({
6358
setSelectedEntry,
6459
}: ModulesListProps) => {
6560
const { t } = useTranslation();
61+
6662
const { data: kymaExt } = useGetList(
6763
(ext: { metadata: { labels: Record<string, string> } }) =>
6864
ext.metadata.labels['app.kubernetes.io/part-of'] === 'Kyma',
6965
)('/api/v1/configmaps?labelSelector=busola.io/extension=resource', {
7066
pollingInterval: 5000,
7167
} as any);
68+
7269
const { data: crds } = useGet(
7370
`/apis/apiextensions.k8s.io/v1/customresourcedefinitions`,
7471
{
@@ -106,20 +103,6 @@ export const ModulesList = ({
106103
setIsFormOpen(state => ({ ...state, formOpen: true }));
107104
};
108105

109-
const findExtension = (resourceKind: string) => {
110-
return (kymaExt as { data: { general: string } }[] | null)?.find(ext => {
111-
const { resource: extensionResource } =
112-
jsyaml.load(ext.data.general, { json: true }) || ({} as any);
113-
return extensionResource.kind === resourceKind;
114-
});
115-
};
116-
117-
const findCrd = (resourceKind: string) => {
118-
return (crds as CustomResourceDefinitionsType | null)?.items?.find(
119-
crd => crd.spec?.names?.kind === resourceKind,
120-
);
121-
};
122-
123106
const headerRenderer = () => [
124107
t('common.headers.name'),
125108
t('kyma-modules.namespaces'),
@@ -144,8 +127,8 @@ export const ModulesList = ({
144127
const isDeletionFailed = moduleStatus?.state === 'Warning';
145128
const isError = moduleStatus?.state === 'Error';
146129

147-
const hasExtension = !!findExtension(resource?.resource?.kind);
148-
const hasCrd = !!findCrd(resource?.resource?.kind);
130+
const hasExtension = !!findExtension(resource?.resource?.kind, kymaExt);
131+
const hasCrd = !!findCrd(resource?.resource?.kind, crds);
149132

150133
let hasModuleTpl = !!findModuleTemplate(
151134
moduleTemplates,
@@ -232,25 +215,19 @@ export const ModulesList = ({
232215
};
233216
}
234217

235-
const hasExtension = !!findExtension(moduleStatus?.resource?.kind);
236-
const moduleCrd = findCrd(moduleStatus?.resource?.kind);
218+
const hasExtension = !!findExtension(moduleStatus?.resource?.kind, kymaExt);
219+
const moduleCrd = findCrd(moduleStatus?.resource?.kind, crds);
237220
const skipRedirect = !hasDetailsLink(moduleStatus);
238221

239222
if (skipRedirect) {
240223
return;
241224
}
242225

243-
const pathName = `${
244-
hasExtension
245-
? `${pluralize(moduleStatus?.resource?.kind || '').toLowerCase()}/${
246-
moduleStatus?.resource?.metadata?.name
247-
}`
248-
: `${moduleCrd?.metadata?.name}/${moduleStatus?.resource?.metadata?.name}`
249-
}`;
250-
251-
const partialPath = moduleStatus?.resource?.metadata?.namespace
252-
? `kymamodules/namespaces/${moduleStatus?.resource?.metadata?.namespace}/${pathName}`
253-
: `kymamodules/${pathName}`;
226+
const partialPath = createModulePartialPath(
227+
hasExtension,
228+
moduleStatus.resource,
229+
moduleCrd,
230+
);
254231

255232
const path = namespaced
256233
? namespaceUrl(partialPath)

src/components/KymaModules/support.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import pluralize from 'pluralize';
2+
import jsyaml from 'js-yaml';
23
import { ColumnLayoutState } from 'state/columnLayoutAtom';
34
import { resolveType } from './components/ModuleStatus';
45

@@ -22,6 +23,13 @@ export type ConditionType = {
2223
type: string;
2324
};
2425

26+
export type CustomResourceDefinitionsType = {
27+
items: {
28+
metadata?: { name: string };
29+
spec?: { names?: { kind?: string } };
30+
}[];
31+
};
32+
2533
export type KymaResourceSpecModuleType = {
2634
name: string;
2735
channel?: string;
@@ -106,6 +114,20 @@ export const getResourcePath = (resource: any) => {
106114
).toLowerCase()}/${resourceName}`;
107115
};
108116

117+
export const findCrd = (resourceKind: string, crds: any) => {
118+
return (crds as CustomResourceDefinitionsType | null)?.items?.find(
119+
crd => crd.spec?.names?.kind === resourceKind,
120+
);
121+
};
122+
123+
export const findExtension = (resourceKind: string, extensions: any) => {
124+
return (extensions as { data: { general: string } }[] | null)?.find(ext => {
125+
const { resource: extensionResource } =
126+
jsyaml.load(ext.data.general, { json: true }) || ({} as any);
127+
return extensionResource.kind === resourceKind;
128+
});
129+
};
130+
109131
export const findModuleStatus = (
110132
kymaResource: KymaResourceType,
111133
moduleName: string,
@@ -195,6 +217,31 @@ export const checkSelectedModule = (
195217
return false;
196218
};
197219

220+
export const createModulePartialPath = (
221+
hasExtension: boolean,
222+
moduleStatusResource: {
223+
kind: string;
224+
apiVersion: string;
225+
metadata: { name: string; namespace: string };
226+
},
227+
moduleCrd?: { metadata?: { name: string } },
228+
) => {
229+
// Taking info for path from extension or crd
230+
const pathName = `${
231+
hasExtension
232+
? `${pluralize(moduleStatusResource?.kind || '').toLowerCase()}/${
233+
moduleStatusResource?.metadata?.name
234+
}`
235+
: `${moduleCrd?.metadata?.name}/${moduleStatusResource?.metadata?.name}`
236+
}`;
237+
238+
const partialPath = moduleStatusResource?.metadata?.namespace
239+
? `kymamodules/namespaces/${moduleStatusResource?.metadata?.namespace}/${pathName}`
240+
: `kymamodules/${pathName}`;
241+
242+
return partialPath;
243+
};
244+
198245
export const resolveInstallationStateName = (
199246
state?: string,
200247
managerExists?: boolean,

0 commit comments

Comments
 (0)