Skip to content

Commit

Permalink
Added more strict types and more changes based on review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
rohitkrai03 committed Dec 18, 2019
1 parent c929691 commit 18fe2e2
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 20 deletions.
@@ -1,19 +1,19 @@
import * as _ from 'lodash';
import { sampleClusterServiceVersions } from '@console/dev-console/src/components/topology/__tests__/topology-test-data';
import { K8sResourceKind } from '@console/internal/module/k8s';
import { ClusterServiceVersionKind } from '@console/operator-lifecycle-manager';
import * as operatorLogo from '../../images/operator.svg';
import { getImageForCSVIcon } from '../icon-utils';
import { mockCSVIcon } from '../__mocks__/mock-csv-icon';

describe('Icon Utils', () => {
it('should return icon from csv data', () => {
const mockCSV: K8sResourceKind = _.cloneDeep(sampleClusterServiceVersions.data[0]);
const mockCSV = _.cloneDeep(sampleClusterServiceVersions.data[0]) as ClusterServiceVersionKind;
expect(getImageForCSVIcon(mockCSV)).toBe(mockCSVIcon);
});

it('should return operator icon if csv has no icon data', () => {
const mockCSV: K8sResourceKind = _.cloneDeep(sampleClusterServiceVersions.data[0]);
mockCSV.spec.icon = '';
const mockCSV = _.cloneDeep(sampleClusterServiceVersions.data[0]) as ClusterServiceVersionKind;
mockCSV.spec.icon = undefined;
expect(getImageForCSVIcon(mockCSV)).toBe(operatorLogo);
});
});
8 changes: 6 additions & 2 deletions frontend/packages/console-shared/src/utils/icon-utils.ts
@@ -1,7 +1,11 @@
import * as _ from 'lodash';
import {
ClusterServiceVersionKind,
ClusterServiceVersionIcon,
} from '@console/operator-lifecycle-manager';
import * as operatorLogo from '../images/operator.svg';

export const getImageForCSVIcon = (csv) => {
const icon = _.get(csv, 'spec.icon', []);
export const getImageForCSVIcon = (csv: ClusterServiceVersionKind) => {
const icon: ClusterServiceVersionIcon = _.get(csv, 'spec.icon', []);
return !_.isEmpty(icon) ? `data:${icon[0].mediatype};base64,${icon[0].base64data}` : operatorLogo;
};
Expand Up @@ -6,6 +6,7 @@ import {
getKnativeServingRoutes,
} from '@console/knative-plugin/src/utils/get-knative-resources';
import { getImageForIconClass } from '@console/internal/components/catalog/catalog-item-icon';
import { ClusterServiceVersionKind } from '@console/operator-lifecycle-manager';
import { WorkloadData, TopologyDataResources } from '../topology-types';
import { transformTopologyData, getEditURL } from '../topology-utils';
import { resources, topologyData, MockResources } from './topology-test-data';
Expand Down Expand Up @@ -231,7 +232,8 @@ describe('TopologyUtils ', () => {
});

it('should return csv icon for operator backed service', () => {
const csvIcon = getImageForCSVIcon(MockResources.clusterServiceVersions.data[0]);
const csvIcon = getImageForCSVIcon(MockResources.clusterServiceVersions
.data[0] as ClusterServiceVersionKind);
const { topologyTransformedData, keys } = getTranformedTopologyData(MockResources, [
'deployments',
]);
Expand Down
Expand Up @@ -2,6 +2,7 @@ import { ComponentType } from 'react';
import { FirehoseResult, KebabOption } from '@console/internal/components/utils';
import { ExtPodKind, OverviewItem, PodControllerOverviewItem } from '@console/shared';
import { DeploymentKind, K8sResourceKind, PodKind } from '@console/internal/module/k8s';
import { ClusterServiceVersionKind } from '@console/operator-lifecycle-manager';
import { Pipeline, PipelineRun } from '../../utils/pipeline-augment';

export type Point = [number, number];
Expand Down Expand Up @@ -118,7 +119,7 @@ export interface DonutStatusData {
}

export type OperatorBackedServiceKindMap = {
[name: string]: K8sResourceKind;
[name: string]: ClusterServiceVersionKind;
};

export interface GraphApi {
Expand Down
Expand Up @@ -132,16 +132,14 @@ export const createTopologyNodeData = (
const deploymentsAnnotations = _.get(deploymentConfig, 'metadata.annotations', {});
const nodeResourceKind = _.get(deploymentConfig, 'metadata.ownerReferences[0].kind');
const operatorBackedService = nodeResourceKind in operatorBackedServiceKindMap;
const getNodeIcon = () => {
if (operatorBackedService) {
return getImageForCSVIcon(operatorBackedServiceKindMap[nodeResourceKind]);
}
return (
getImageForIconClass(`icon-${deploymentsLabels['app.openshift.io/runtime']}`) ||
getImageForIconClass(`icon-${deploymentsLabels['app.kubernetes.io/name']}`) ||
getImageForIconClass(`icon-openshift`)
);
};

const csvIcon =
operatorBackedService && getImageForCSVIcon(operatorBackedServiceKindMap[nodeResourceKind]);
const builderImageIcon =
getImageForIconClass(`icon-${deploymentsLabels['app.openshift.io/runtime']}`) ||
getImageForIconClass(`icon-${deploymentsLabels['app.kubernetes.io/name']}`);
const defaultIcon = getImageForIconClass(`icon-openshift`);

return {
id: dcUID,
name:
Expand All @@ -157,7 +155,7 @@ export const createTopologyNodeData = (
deploymentsAnnotations['app.openshift.io/edit-url'] ||
getEditURL(deploymentsAnnotations['app.openshift.io/vcs-uri'], cheURL),
cheEnabled: !!cheURL,
builderImage: getNodeIcon(),
builderImage: csvIcon || builderImageIcon || defaultIcon,
isKnativeResource:
type && (type === 'event-source' || 'knative-revision')
? true
Expand Down Expand Up @@ -322,7 +320,7 @@ export const transformTopologyData = (
const serviceBindingRequests = _.get(resources, 'serviceBindingRequests.data');
if (installedOperators) {
operatorBackedServiceKindMap = installedOperators.reduce((kindMap, csv) => {
_.get(csv, 'spec.customresourcedefinitions.owned').forEach((crd) => {
_.get(csv, 'spec.customresourcedefinitions.owned', []).forEach((crd) => {
if (!(crd.kind in kindMap)) {
kindMap[crd.kind] = csv;
}
Expand Down

0 comments on commit 18fe2e2

Please sign in to comment.