Skip to content

Commit

Permalink
filter incompatible helm charts from dev catalog
Browse files Browse the repository at this point in the history
  • Loading branch information
debsmita1 committed Jun 18, 2020
1 parent e7398d4 commit 7e7af31
Show file tree
Hide file tree
Showing 3 changed files with 175 additions and 11 deletions.
69 changes: 69 additions & 0 deletions frontend/__tests__/components/catalog-data.ts
@@ -1,4 +1,5 @@
import { Item } from '../../public/components/catalog/catalog-items';
import { HelmChartEntries } from '@console/internal/components/catalog/catalog-page';

export const developerCatalogItems: Item[] = [
{
Expand Down Expand Up @@ -221,3 +222,71 @@ export const groupedByOperator = {
},
],
};

export const helmChartEntries: HelmChartEntries = {
'ibm-cpq-prod': [
{
apiVersion: 'v2',
appVersion: '10.0.0.6',
description: 'description-xyz',
kubeVersion: '>=1.11.0',
name: 'ibm-cpq-prod',
version: '2.0.0',
created: '2020-06-03T15:43:50.175497776+05:30',
urls: [
'https://redhat-developer.github.com/redhat-helm-charts/charts/ibm-cpq-prod-2.0.0.tgz',
],
},
],
'nodejs-ex-k': [
{
apiVersion: 'v1',
appVersion: '10.0.0.5',
description: 'description-abc',
kubeVersion: '>=1.10.0',
name: 'nodejs-ex-k',
version: '0.2.0',
created: '2020-06-03T15:43:50.196081351+05:30',
urls: ['https://redhat-developer.github.com/redhat-helm-charts/charts/nodejs-ex-k-0.2.0.tgz'],
},
{
apiVersion: 'v2',
appVersion: '10.0.0.6',
description: 'description-mno',
kubeVersion: '>=1.25.0',
name: 'nodejs-ex-k',
version: '0.2.1',
created: '2020-06-03T15:43:50.196081351+05:30',
urls: ['https://redhat-developer.github.com/redhat-helm-charts/charts/nodejs-ex-k-0.2.1.tgz'],
},
],
};

export const filteredHelmChartEntries: HelmChartEntries = {
'ibm-cpq-prod': [
{
apiVersion: 'v2',
appVersion: '10.0.0.6',
description: 'description-xyz',
kubeVersion: '>=1.11.0',
name: 'ibm-cpq-prod',
version: '2.0.0',
created: '2020-06-03T15:43:50.175497776+05:30',
urls: [
'https://redhat-developer.github.com/redhat-helm-charts/charts/ibm-cpq-prod-2.0.0.tgz',
],
},
],
'nodejs-ex-k': [
{
apiVersion: 'v1',
appVersion: '10.0.0.5',
description: 'description-abc',
kubeVersion: '>=1.10.0',
name: 'nodejs-ex-k',
version: '0.2.0',
created: '2020-06-03T15:43:50.196081351+05:30',
urls: ['https://redhat-developer.github.com/redhat-helm-charts/charts/nodejs-ex-k-0.2.0.tgz'],
},
],
};
59 changes: 58 additions & 1 deletion frontend/__tests__/components/catalog.spec.tsx
Expand Up @@ -14,6 +14,7 @@ import {
CatalogListPage,
CatalogListPageProps,
CatalogListPageState,
filterIncompatibleHelmCharts,
} from '../../public/components/catalog/catalog-page';
import {
catalogCategories as initCatalogCategories,
Expand All @@ -25,7 +26,12 @@ import {
catalogItems,
catalogCategories,
} from '../../__mocks__/catalogItemsMocks';
import { developerCatalogItems, groupedByOperator } from './catalog-data';
import {
developerCatalogItems,
groupedByOperator,
filteredHelmChartEntries,
helmChartEntries,
} from './catalog-data';
import { categorizeItems } from '../../public/components/utils/tile-view-page';
import { Dropdown } from '../../public/components/utils';

Expand Down Expand Up @@ -149,4 +155,55 @@ describe(CatalogTileViewPage.displayName, () => {
const groupedByTypeResult = groupItems(developerCatalogItems, 'None');
expect(groupedByTypeResult).toEqual(developerCatalogItems);
});

it('should filter incompatible helm charts in the dev catalog', () => {
let kubernetesVersion = 'v1.18.0';
let filteredHelmChartEntriesResult = filterIncompatibleHelmCharts(
helmChartEntries,
kubernetesVersion,
);
expect(filteredHelmChartEntriesResult).toEqual(filteredHelmChartEntries);
kubernetesVersion = '-';
filteredHelmChartEntriesResult = filterIncompatibleHelmCharts(
helmChartEntries,
kubernetesVersion,
);
expect(filteredHelmChartEntriesResult).toEqual(helmChartEntries);
kubernetesVersion = 'unknown';
filteredHelmChartEntriesResult = filterIncompatibleHelmCharts(
helmChartEntries,
kubernetesVersion,
);
expect(filteredHelmChartEntriesResult).toEqual(helmChartEntries);
kubernetesVersion = 'v1.18.3+e1ba7b6';
filteredHelmChartEntriesResult = filterIncompatibleHelmCharts(
helmChartEntries,
kubernetesVersion,
);
expect(filteredHelmChartEntriesResult).toEqual(filteredHelmChartEntries);
kubernetesVersion = 'v1.20.3-alpha.1';
helmChartEntries['nodejs-ex-k'][1].kubeVersion = '>=1.20.3-0';
filteredHelmChartEntriesResult = filterIncompatibleHelmCharts(
helmChartEntries,
kubernetesVersion,
);
expect(filteredHelmChartEntriesResult).toEqual({
'nodejs-ex-k': [helmChartEntries['nodejs-ex-k'][1]],
});
kubernetesVersion = 'v1.20.3-alpha.1';
helmChartEntries['nodejs-ex-k'][1].kubeVersion = '>=1.19.3-0';
filteredHelmChartEntriesResult = filterIncompatibleHelmCharts(
helmChartEntries,
kubernetesVersion,
);
expect(filteredHelmChartEntriesResult).toEqual({});
kubernetesVersion = 'v1.18.3+8uinj';
helmChartEntries['nodejs-ex-k'][0].kubeVersion = '>= 1.13.0 < 1.14.0 || >= 1.14.1 < 1.15.0';
const expectedFilteredHelmChartEntries = _.omit(filteredHelmChartEntries, 'nodejs-ex-k');
filteredHelmChartEntriesResult = filterIncompatibleHelmCharts(
helmChartEntries,
kubernetesVersion,
);
expect(filteredHelmChartEntriesResult).toEqual(expectedFilteredHelmChartEntries);
});
});
58 changes: 48 additions & 10 deletions frontend/public/components/catalog/catalog-page.tsx
Expand Up @@ -2,7 +2,7 @@ import * as React from 'react';
import * as _ from 'lodash-es';
import { Helmet } from 'react-helmet';
import { safeLoad } from 'js-yaml';

import * as semver from 'semver';
import { PropertyItem } from '@patternfly/react-catalog-view-extension';
import { ANNOTATIONS, FLAGS, APIError } from '@console/shared';
import { CatalogTileViewPage, Item } from './catalog-items';
Expand All @@ -15,6 +15,8 @@ import {
PartialObjectMetadata,
TemplateKind,
} from '../../module/k8s';
import { k8sVersion } from '../../module/status';
import { getK8sGitVersion } from '../../module/k8s/cluster-settings';
import { withStartGuide } from '../start-guide';
import { connectToFlags, flagPending, FlagsObject } from '../../reducers/features';
import {
Expand Down Expand Up @@ -344,6 +346,30 @@ export const CatalogListPage = withExtensions<CatalogListPageExtensionProps>({
},
);

export const filterIncompatibleHelmCharts = (
charts: HelmChartEntries,
kubernetesVersion: string,
): HelmChartEntries => {
const filteredHelmchartEntries: HelmChartEntries = _.reduce(
charts,
(acc, helmCharts: HelmChart[], helmChartName: string) => {
const filteredHelmCharts: HelmChart[] = [];
_.forEach(helmCharts, (helmChart: HelmChart) => {
if (helmChart?.kubeVersion && semver.valid(kubernetesVersion)) {
if (semver.satisfies(kubernetesVersion, helmChart.kubeVersion)) {
filteredHelmCharts.push(helmChart);
}
} else {
filteredHelmCharts.push(helmChart);
}
});
return filteredHelmCharts.length > 0 ? { ...acc, [helmChartName]: filteredHelmCharts } : acc;
},
{},
);
return filteredHelmchartEntries;
};

export const Catalog = connectToFlags<CatalogProps>(
FLAGS.OPENSHIFT,
FLAGS.SERVICE_CATALOG,
Expand Down Expand Up @@ -411,7 +437,18 @@ export const Catalog = connectToFlags<CatalogProps>(
coFetch('/api/helm/charts/index.yaml').then(async (res) => {
const yaml = await res.text();
const json = safeLoad(yaml);
setHelmCharts(json.entries);
let kubernetesVersion: string = '';
try {
const response = await k8sVersion();
kubernetesVersion = getK8sGitVersion(response) || '-';
} catch {
kubernetesVersion = 'unknown';
}
const filteredHelmChartEntries = filterIncompatibleHelmCharts(
json.entries,
kubernetesVersion,
);
setHelmCharts(filteredHelmChartEntries);
});
}, []);

Expand Down Expand Up @@ -522,18 +559,19 @@ export type HelmChartEntries = {

export type HelmChart = {
apiVersion: string;
appVersion: string;
appVersion?: string;
created: string;
description: string;
digest: string;
home: string;
icon: string;
keywords: string[];
maintainers: Array<{ email: string; name: string }>;
description?: string;
digest?: string;
home?: string;
icon?: string;
keywords?: string[];
maintainers?: Array<{ name: string; email?: string; url?: string }>;
name: string;
tillerVersion: string;
tillerVersion?: string;
urls: string[];
version: string;
kubeVersion?: string;
};

CatalogPage.displayName = 'CatalogPage';
Expand Down

0 comments on commit 7e7af31

Please sign in to comment.