Skip to content

Commit

Permalink
fixes add page notification if dynamic channel/sources or broker exists
Browse files Browse the repository at this point in the history
  • Loading branch information
invincibleJai committed Sep 22, 2020
1 parent ef71265 commit 1921e3c
Show file tree
Hide file tree
Showing 6 changed files with 165 additions and 13 deletions.
38 changes: 30 additions & 8 deletions frontend/packages/dev-console/src/components/AddPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,15 @@ import { Helmet } from 'react-helmet';
import { match as RMatch } from 'react-router';
import { Firehose, FirehoseResource, HintBlock } from '@console/internal/components/utils';
import { K8sResourceKind, referenceForModel } from '@console/internal/module/k8s';
import { ServiceModel } from '@console/knative-plugin';
import { ServiceModel, EventingBrokerModel } from '@console/knative-plugin/src/models';
import {
getDynamicChannelResourceList,
getDynamicEventSourcesResourceList,
getDynamicChannelModelRefs,
getDynamicEventSourcesModelRefs,
} from '@console/knative-plugin/src/utils/fetch-dynamic-eventsources-utils';
import { getKnativeDynamicResources } from '@console/knative-plugin/src/topology/knative-topology-utils';
import { knativeEventingResourcesBroker } from '@console/knative-plugin/src/utils/get-knative-resources';
import ODCEmptyState from './EmptyState';
import NamespacedPage from './NamespacedPage';
import ProjectsExistWrapper from './ProjectsExistWrapper';
Expand All @@ -17,11 +25,7 @@ export interface AddPageProps {
}

interface ResourcesType {
deploymentConfigs?: K8sResourceKind;
deployments?: K8sResourceKind;
daemonSets?: K8sResourceKind;
statefulSets?: K8sResourceKind;
knativeService?: K8sResourceKind;
[x: string]: K8sResourceKind & { data?: K8sResourceKind[] };
}
interface EmptyStateLoaderProps {
resources?: ResourcesType;
Expand All @@ -36,6 +40,11 @@ const EmptyStateLoader: React.FC<EmptyStateLoaderProps> = ({ resources, loaded,
const deployments = resources?.deployments?.data;
const statefulSets = resources?.statefulSets?.data;
const knativeService = resources?.knativeService?.data;
const knDynamicResources: K8sResourceKind[] = getKnativeDynamicResources(resources, [
...getDynamicChannelModelRefs(),
...getDynamicEventSourcesModelRefs(),
EventingBrokerModel.plural,
]);

React.useEffect(() => {
if (loaded) {
Expand All @@ -44,12 +53,22 @@ const EmptyStateLoader: React.FC<EmptyStateLoaderProps> = ({ resources, loaded,
_.isEmpty(deploymentConfigs) &&
_.isEmpty(deployments) &&
_.isEmpty(statefulSets) &&
_.isEmpty(knativeService),
_.isEmpty(knativeService) &&
_.isEmpty(knDynamicResources),
);
} else if (loadError) {
setNoWorkloads(false);
}
}, [loaded, loadError, daemonSets, deploymentConfigs, deployments, statefulSets, knativeService]);
}, [
loaded,
loadError,
daemonSets,
deploymentConfigs,
deployments,
statefulSets,
knativeService,
knDynamicResources,
]);
return noWorkloads ? (
<ODCEmptyState
title="Add"
Expand Down Expand Up @@ -105,6 +124,9 @@ const RenderEmptyState = ({ namespace }) => {
optional: true,
limit: 1,
},
...knativeEventingResourcesBroker(namespace, 1),
...getDynamicChannelResourceList(namespace, 1),
...getDynamicEventSourcesResourceList(namespace, 1),
];
return (
<Firehose resources={resources}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,9 @@ export const getTriggerFilters = (resource: K8sResourceKind) => {
};

export const getKnativeDynamicResources = (
resources: TopologyDataResources,
resources:
| TopologyDataResources
| { [key: string]: K8sResourceKind & { data?: K8sResourceKind[] } },
dynamicProps: string[],
): K8sResourceKind[] => {
return dynamicProps.reduce((acc, currProp) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
export const mockChannelCRDData = {
kind: 'CustomResourceDefinitionList',
apiVersion: 'apiextensions.k8s.io/v1',
metadata: {
selfLink: '/apis/apiextensions.k8s.io/v1/customresourcedefinitions',
resourceVersion: '157889',
},
items: [
{
metadata: {
name: 'channels.messaging.knative.dev',
labels: {
'duck.knative.dev/addressable': 'true',
'eventing.knative.dev/release': 'v0.14.2',
'knative.dev/crd-install': 'true',
'messaging.knative.dev/subscribable': 'true',
},
},
spec: {
group: 'messaging.knative.dev',
names: {
plural: 'channels',
singular: 'channel',
shortNames: ['ch'],
kind: 'Channel',
listKind: 'ChannelList',
categories: ['all', 'knative', 'messaging', 'channel'],
},
versions: [
{ name: 'v1alpha1', served: true, storage: true },
{ name: 'v1beta1', served: true, storage: false },
],
},
},
{
metadata: {
name: 'inmemorychannels.messaging.knative.dev',
labels: {
'duck.knative.dev/addressable': 'true',
'eventing.knative.dev/release': 'v0.14.2',
'knative.dev/crd-install': 'true',
'messaging.knative.dev/subscribable': 'true',
},
},
spec: {
group: 'messaging.knative.dev',
names: {
plural: 'inmemorychannels',
singular: 'inmemorychannel',
shortNames: ['imc'],
kind: 'InMemoryChannel',
listKind: 'InMemoryChannelList',
categories: ['all', 'knative', 'messaging', 'channel'],
},
versions: [
{ name: 'v1alpha1', served: true, storage: true },
{ name: 'v1beta1', served: true, storage: false },
],
},
},
],
};
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,24 @@ import {
EventSourceContainerModel,
EventSourcePingModel,
ServiceModel,
EventingIMCModel,
} from '../../models';
import {
getEventSourceModels,
fetchEventSourcesCrd,
fetchChannelsCrd,
isDynamicEventResourceKind,
isEventingChannelResourceKind,
getDynamicEventSourcesModelRefs,
getDynamicChannelModelRefs,
getDynamicEventSourceModel,
getDynamicChannelResourceList,
getDynamicEventSourcesResourceList,
} from '../fetch-dynamic-eventsources-utils';
import { mockEventSourcCRDData } from '../__mocks__/dynamic-event-source-crd-mock';
import { mockChannelCRDData } from '../__mocks__/dynamic-channels-crd-mock';

describe('fetch-dynamic-eventsources: ', () => {
describe('fetch-dynamic-eventsources: EventSources', () => {
beforeEach(() => {
jest.spyOn(coFetch, 'coFetch').mockImplementation(() =>
Promise.resolve({
Expand Down Expand Up @@ -73,4 +80,57 @@ describe('fetch-dynamic-eventsources: ', () => {
const resultModel = getDynamicEventSourceModel(referenceForModel(ServiceModel));
expect(resultModel).toBe(undefined);
});

it('should return limit if passed to getDynamicEventSourcesResourceList', async () => {
await fetchEventSourcesCrd();
const resultModel = getDynamicEventSourcesResourceList('sample-app', 1);
expect(resultModel[0].limit).toBe(1);
});

it('should not return limit if not passed to getDynamicEventSourcesResourceList', async () => {
await fetchEventSourcesCrd();
const resultModel = getDynamicEventSourcesResourceList('sample-app');
expect(resultModel[0].limit).toBeUndefined();
});
});

describe('fetch-dynamic-eventsources: Channels', () => {
beforeEach(() => {
jest.spyOn(coFetch, 'coFetch').mockImplementation(() =>
Promise.resolve({
json: () => ({ ...mockChannelCRDData }),
}),
);
});

it('should return true for IMC channel model', async () => {
await fetchChannelsCrd();
expect(isEventingChannelResourceKind(referenceForModel(EventingIMCModel))).toBe(true);
});

it('should return false for ksvc model', async () => {
await fetchChannelsCrd();
expect(isEventingChannelResourceKind(referenceForModel(ServiceModel))).toBe(false);
});

it('should return refs for all channel models', async () => {
await fetchChannelsCrd();
const expectedRefs = [referenceForModel(EventingIMCModel)];
const modelRefs = getDynamicChannelModelRefs();
expectedRefs.forEach((ref) => {
expect(modelRefs.includes(ref)).toBe(true);
});
});

it('should return limit if passed to getDynamicChannelResourceList', async () => {
await fetchChannelsCrd();
const resultModel = getDynamicChannelResourceList('sample-app', 1);
expect(resultModel[0].limit).toBe(1);
});

it('should not return limit if not passed to getDynamicChannelResourceList', async () => {
await fetchChannelsCrd();
const resultModel = getDynamicChannelResourceList('sample-app');
expect(resultModel[0].limit).toBeUndefined();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,15 @@ export const useEventSourceModels = (): EventSourcetData => {

export const getEventSourceModels = (): K8sKind[] => eventSourceData.eventSourceModels;

export const getDynamicEventSourcesResourceList = (namespace: string) => {
export const getDynamicEventSourcesResourceList = (namespace: string, limit?: number) => {
return eventSourceData.eventSourceModels.map((model) => {
return {
isList: true,
kind: referenceForModel(model),
namespace,
prop: referenceForModel(model),
optional: true,
...(limit && { limit }),
};
});
};
Expand Down Expand Up @@ -188,14 +189,15 @@ export const fetchChannelsCrd = async () => {
return eventSourceData.eventSourceChannels;
};

export const getDynamicChannelResourceList = (namespace: string) => {
export const getDynamicChannelResourceList = (namespace: string, limit?: number) => {
return eventSourceData.eventSourceChannels.map((model) => {
return {
isList: true,
kind: referenceForModel(model),
namespace,
prop: referenceForModel(model),
optional: true,
...(limit && { limit }),
};
});
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,14 +143,18 @@ export const knativeEventingResourcesSubscription = (namespace: string): Firehos
return knativeResource;
};

export const knativeEventingResourcesBroker = (namespace: string): FirehoseResource[] => {
export const knativeEventingResourcesBroker = (
namespace: string,
limit?: number,
): FirehoseResource[] => {
const knativeResource = [
{
isList: true,
kind: referenceForModel(EventingBrokerModel),
namespace,
prop: EventingBrokerModel.plural,
optional: true,
...(limit && { limit }),
},
];
return knativeResource;
Expand Down

0 comments on commit 1921e3c

Please sign in to comment.