Skip to content

Commit

Permalink
adds rbac for eventSource, kameletBinding and removes camelSource unw…
Browse files Browse the repository at this point in the history
…anted changes
  • Loading branch information
invincibleJai committed Dec 21, 2020
1 parent 36558f2 commit f1aaf93
Show file tree
Hide file tree
Showing 20 changed files with 309 additions and 570 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@
"Cannot be longer than 253 characters.": "Cannot be longer than 253 characters.",
"No model registered for {{referenceForKnEventSource}}": "No model registered for {{referenceForKnEventSource}}",
"Event source cannot be created": "Event source cannot be created",
"Creation of Event sources are not currently supported on this Cluster.": "Creation of Event sources are not currently supported on this Cluster.",
"Event source is not found on this Cluster.": "Event source is not found on this Cluster.",
"You do not have create access for Event Source in this project.": "You do not have create access for Event Source in this project.",
"Note: Some fields may not be represented in this form view. Please select \"YAML view\" for full control of object creation.": "Note: Some fields may not be represented in this form view. Please select \"YAML view\" for full control of object creation.",
"Provided by {{provider}}": "Provided by {{provider}}",
"Create an Event source to register interest in a class of events from a particular system. Configure using the YAML and form views.": "Create an Event source to register interest in a class of events from a particular system. Configure using the YAML and form views.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { useTranslation } from 'react-i18next';
import { TFunction } from 'i18next';
import { CatalogItem, CatalogExtensionHook } from '@console/plugin-sdk';
import { K8sKind, referenceForModel } from '@console/internal/module/k8s';
import { useEventSourceModels } from '../utils/fetch-dynamic-eventsources-utils';
import { getEventSourceIcon } from '../utils/get-knative-icon';
import { getEventSourceCatalogProviderData } from './event-source-data';
import { useEventSourceModelsWithAccess } from '../hooks';

const normalizeEventSources = (
eventSources: K8sKind[],
Expand Down Expand Up @@ -33,7 +33,9 @@ const useEventSourceProvider: CatalogExtensionHook<CatalogItem[]> = ({
namespace,
}): [CatalogItem[], boolean, any] => {
const { t } = useTranslation();
const { loaded, eventSourceModels } = useEventSourceModels();
const { loaded, eventSourceModelsList: eventSourceModels } = useEventSourceModelsWithAccess(
namespace,
);
const normalizedSources = React.useMemo(
() => (loaded ? normalizeEventSources(eventSourceModels, namespace, t) : []),

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ import {
useK8sWatchResource,
WatchK8sResource,
} from '@console/internal/components/utils/k8s-watch-hook';
import { useAccessReview } from '@console/internal/components/utils/rbac';
import { CamelKameletBindingModel, CamelKameletModel } from '../models';
import { getEventSourceIcon } from '../utils/get-knative-icon';
import { CAMEL_APIGROUP } from '../const';

const CAMEL_K_PROVIDER_ANNOTATION = `${CAMEL_APIGROUP}/provider`;
import { CAMEL_K_PROVIDER_ANNOTATION } from '../const';

const normalizeKamelets = (
kamelets: K8sResourceKind[],
Expand Down Expand Up @@ -45,6 +44,12 @@ const normalizeKamelets = (

const useKameletsProvider = ({ namespace }): [CatalogItem[], boolean, any] => {
const { t } = useTranslation();
const canCreateKameletBinding = useAccessReview({
group: CamelKameletBindingModel.apiGroup,
resource: CamelKameletBindingModel.plural,
verb: 'create',
namespace,
});
const resource: WatchK8sResource = React.useMemo(
() => ({ kind: referenceForModel(CamelKameletModel), isList: true, namespace, optional: true }),
[namespace],
Expand All @@ -53,8 +58,9 @@ const useKameletsProvider = ({ namespace }): [CatalogItem[], boolean, any] => {
resource,
);
const normalizedSource = React.useMemo(
() => (kameletsLoaded ? normalizeKamelets(kamelets, namespace, t) : []),
[kameletsLoaded, kamelets, namespace, t],
() =>
kameletsLoaded && canCreateKameletBinding ? normalizeKamelets(kamelets, namespace, t) : [],
[kameletsLoaded, kamelets, namespace, canCreateKameletBinding, t],
);
return [normalizedSource, kameletsLoaded, kameletsLoadError];
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,16 @@ import { getEventSourceModels } from '../../utils/fetch-dynamic-eventsources-uti
import { KNATIVE_EVENT_SOURCE_APIGROUP } from '../../const';
import {
EventSourceSyncFormData,
EventSourceListData,
SinkType,
EVENT_SOURCES_APP,
EventSourceMetaData,
} from './import-types';
import { CamelKameletBindingModel } from '../../models';
import EventSourceMetaDescription from './EventSourceMetadataDescription';

interface EventSourceProps {
namespace: string;
eventSourceStatus: EventSourceListData;
normalizedSource: EventSourceMetaData;
contextSource?: string;
selectedApplication?: string;
sourceKind?: string;
Expand All @@ -54,7 +54,7 @@ type Props = EventSourceProps & StateProps;

export const EventSource: React.FC<Props> = ({
namespace,
eventSourceStatus,
normalizedSource,
activeApplication,
contextSource,
sourceKind = '',
Expand All @@ -66,6 +66,7 @@ export const EventSource: React.FC<Props> = ({
let sourceData = {};
let selApiVersion = '';
let selSourceName = '';
let kameletSourceName = '';
if (sourceKind) {
const selDataModel = _.find(getEventSourceModels(), { kind: sourceKind });
selApiVersion = selDataModel
Expand All @@ -78,9 +79,8 @@ export const EventSource: React.FC<Props> = ({
? { [sourceKind]: getKameletSourceData(kameletSource) }
: { [sourceKind]: getEventSourceData(sourceKind) }
: {};
selSourceName = kameletSource
? `kamelet-${kameletSource.metadata.name}`
: _.kebabCase(sourceKind);
kameletSourceName = kameletSource && kameletSource.metadata.name;
selSourceName = kameletSourceName ? `kamelet-${kameletSourceName}` : _.kebabCase(sourceKind);
}
const [sinkGroupVersionKind = '', sinkName = ''] = contextSource?.split('/') ?? [];
const [sinkGroup = '', sinkVersion = '', sinkKind = ''] =
Expand All @@ -89,7 +89,7 @@ export const EventSource: React.FC<Props> = ({
const sinkApiVersion = sinkGroup ? `${sinkGroup}/${sinkVersion}` : '';

const eventSourceMetaDescription = (
<EventSourceMetaDescription eventSourceStatus={eventSourceStatus} sourceKind={sourceKind} />
<EventSourceMetaDescription normalizedSource={normalizedSource} />
);
const catalogInitialValues: EventSourceSyncFormData = {
editorType: EditorType.Form,
Expand Down Expand Up @@ -166,7 +166,6 @@ export const EventSource: React.FC<Props> = ({
<EventSourceForm
{...formikProps}
namespace={namespace}
eventSourceStatus={eventSourceStatus}
eventSourceMetaDescription={eventSourceMetaDescription}
kameletSource={kameletSource}
/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,35 +1,32 @@
import * as React from 'react';
import * as _ from 'lodash';
import { useTranslation } from 'react-i18next';
import { Alert } from '@patternfly/react-core';
import { EventSourceListData } from './import-types';

interface EventSourceAlertProps {
eventSourceStatus: EventSourceListData;
showSourceKindAlert?: boolean;
isValidSource: boolean;
createSourceAccessLoading: boolean;
createSourceAccess: boolean;
}

const EventSourceAlert: React.FC<EventSourceAlertProps> = ({
eventSourceStatus,
showSourceKindAlert,
isValidSource,
createSourceAccessLoading,
createSourceAccess,
}) => {
const { t } = useTranslation();
const noEventSources = eventSourceStatus.eventSourceList === null;
const noEventSourceAccess =
!noEventSources && eventSourceStatus.loaded && _.isEmpty(eventSourceStatus.eventSourceList);
const showAlert =
noEventSources || noEventSourceAccess || (eventSourceStatus.loaded && showSourceKindAlert);
const showAlert = !isValidSource || (!createSourceAccessLoading && !createSourceAccess);

return showAlert ? (
<Alert
variant={showSourceKindAlert ? 'danger' : 'default'}
variant={!isValidSource ? 'danger' : 'default'}
title={t('knative-plugin~Event source cannot be created')}
isInline
>
{noEventSourceAccess && t('knative-plugin~You do not have write access in this project.')}
{noEventSources &&
t('knative-plugin~Creation of Event sources are not currently supported on this Cluster.')}
{showSourceKindAlert && t('knative-plugin~Event source is not found on this Cluster.')}
{!isValidSource && t('knative-plugin~Event source is not found on this Cluster.')}
{!createSourceAccessLoading &&
!createSourceAccess &&
isValidSource &&
t('knative-plugin~You do not have create access for Event Source in this project.')}
</Alert>
) : null;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,14 @@ import { FormFooter, SyncedEditorField, YAMLEditorField, FlexForm } from '@conso
import { EditorType } from '@console/shared/src/components/synced-editor/editor-toggle';
import { safeJSToYAML } from '@console/shared/src/utils/yaml';
import EventSourceSection from './event-sources/EventSourceSection';
import { EventSourceListData, EventSourceSyncFormData } from './import-types';
import { EventSourceSyncFormData } from './import-types';
import {
getCatalogEventSourceResource,
sanitizeSourceToForm,
} from '../../utils/create-eventsources-utils';
import { isDynamicEventSourceKind } from '../../utils/fetch-dynamic-eventsources-utils';

interface OwnProps {
namespace: string;
eventSourceStatus: EventSourceListData | null;
eventSourceMetaDescription: React.ReactNode;
kameletSource?: K8sResourceKind;
}
Expand All @@ -32,7 +30,6 @@ const EventSourceForm: React.FC<FormikProps<FormikValues> & OwnProps> = ({
isSubmitting,
dirty,
namespace,
eventSourceStatus,
eventSourceMetaDescription,
kameletSource,
}) => {
Expand All @@ -46,47 +43,41 @@ const EventSourceForm: React.FC<FormikProps<FormikValues> & OwnProps> = ({
});

const formEditor = (
<>
{eventSourceStatus && !_.isEmpty(eventSourceStatus.eventSourceList) && (
<div className="row">
<div className="col-sm-12 col-md-4 col-md-push-8 col-lg-5 col-lg-push-7">
{eventSourceMetaDescription}
</div>
<div className="col-sm-12 col-md-8 col-md-pull-4 col-lg-7 col-lg-pull-5">
{values.showCanUseYAMLMessage && (
<Alert
actionClose={
<AlertActionCloseButton
onClose={() => setFieldValue('showCanUseYAMLMessage', false)}
/>
}
isInline
title={t(
'knative-plugin~Note: Some fields may not be represented in this form view. Please select "YAML view" for full control of object creation.',
)}
variant="info"
<div className="row">
<div className="col-sm-12 col-md-4 col-md-push-8 col-lg-5 col-lg-push-7">
{eventSourceMetaDescription}
</div>
<div className="col-sm-12 col-md-8 col-md-pull-4 col-lg-7 col-lg-pull-5">
{values.showCanUseYAMLMessage && (
<Alert
actionClose={
<AlertActionCloseButton
onClose={() => setFieldValue('showCanUseYAMLMessage', false)}
/>
}
isInline
title={t(
'knative-plugin~Note: Some fields may not be represented in this form view. Please select "YAML view" for full control of object creation.',
)}
<EventSourceSection namespace={namespace} kameletSource={kameletSource} fullWidth />{' '}
</div>
</div>
)}
</>
variant="info"
/>
)}
<EventSourceSection namespace={namespace} kameletSource={kameletSource} fullWidth />{' '}
</div>
</div>
);
return (
<FlexForm onSubmit={handleSubmit}>
{(isDynamicEventSourceKind(values.formData.type) || kameletSource) && (
<SyncedEditorField
name="editorType"
formContext={{
name: 'formData',
editor: formEditor,
sanitizeTo: (newFormData: K8sResourceKind) =>
sanitizeSourceToForm(newFormData, values.formData, kameletSource),
}}
yamlContext={{ name: 'yamlData', editor: yamlEditor, sanitizeTo: sanitizeToYaml }}
/>
)}
<SyncedEditorField
name="editorType"
formContext={{
name: 'formData',
editor: formEditor,
sanitizeTo: (newFormData: K8sResourceKind) =>
sanitizeSourceToForm(newFormData, values.formData, kameletSource),
}}
yamlContext={{ name: 'yamlData', editor: yamlEditor, sanitizeTo: sanitizeToYaml }}
/>
<FormFooter
handleReset={handleReset}
errorMessage={status && status.submitError}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,42 +1,34 @@
import * as React from 'react';
import * as _ from 'lodash';
import { useTranslation } from 'react-i18next';
import { EventSourceListData } from './import-types';
import { EventSourceMetaData } from './import-types';

import './EventSourceMetaDescription.scss';

type EventSourceMetaDescriptionProps = {
eventSourceStatus: EventSourceListData | null;
sourceKind: string;
normalizedSource: EventSourceMetaData;
};

const EventSourceMetaDescription: React.FC<EventSourceMetaDescriptionProps> = ({
eventSourceStatus,
sourceKind,
normalizedSource,
}) => {
const { t } = useTranslation();
const sourceMetadata = eventSourceStatus.eventSourceList[sourceKind];
if (!sourceMetadata || _.isEmpty(sourceMetadata)) {
if (_.isEmpty(normalizedSource)) {
return null;
}
const { displayName } = sourceMetadata;
const imgSrc = sourceMetadata.iconUrl;
const { provider } = sourceMetadata;
const description = sourceMetadata.description || '';
const { name, provider, iconUrl, description } = normalizedSource;
return (
<div className="kn-event-source-metadata-description__container">
<div className="co-clusterserviceversion-logo">
<div className="co-clusterserviceversion-logo__icon">
<img
className="co-catalog-item-icon__img co-catalog-item-icon__img--large"
src={imgSrc}
src={iconUrl}
alt=""
/>
</div>
<div className="co-clusterserviceversion-logo__name">
<h1 className="co-clusterserviceversion-logo__name__clusterserviceversion">
{displayName}
</h1>
<h1 className="co-clusterserviceversion-logo__name__clusterserviceversion">{name}</h1>
{provider && (
<span className="co-clusterserviceversion-logo__name__provider text-muted">
{t('knative-plugin~Provided by {{provider}}', {
Expand Down
Loading

0 comments on commit f1aaf93

Please sign in to comment.