-
Notifications
You must be signed in to change notification settings - Fork 593
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adds support tp move sink for eventSource
- Loading branch information
1 parent
87db3e7
commit f37ecef
Showing
8 changed files
with
204 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
frontend/packages/knative-plugin/src/actions/sink-source.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { KebabOption } from '@console/internal/components/utils'; | ||
import { K8sKind, K8sResourceKind } from '@console/internal/module/k8s'; | ||
import { setSinkSourceModal } from '../components/modals'; | ||
|
||
export const setSinkSource = (model: K8sKind, obj: K8sResourceKind): KebabOption => { | ||
return { | ||
label: 'Move Sink', | ||
callback: () => | ||
setSinkSourceModal({ | ||
obj, | ||
}), | ||
accessReview: { | ||
group: model.apiGroup, | ||
resource: model.plural, | ||
name: obj.metadata.name, | ||
namespace: obj.metadata.namespace, | ||
verb: 'update', | ||
}, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
frontend/packages/knative-plugin/src/components/sink-source/SinkSource.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import * as React from 'react'; | ||
import { Formik, FormikValues, FormikHelpers } from 'formik'; | ||
import { K8sResourceKind, k8sUpdate, referenceFor, modelFor } from '@console/internal/module/k8s'; | ||
import SinkSourceModal from './SinkSourceModal'; | ||
|
||
export interface SinkSourceProps { | ||
source: K8sResourceKind; | ||
cancel?: () => void; | ||
close?: () => void; | ||
} | ||
|
||
const SinkSource: React.FC<SinkSourceProps> = ({ source, cancel, close }) => { | ||
const { | ||
metadata: { namespace, name }, | ||
spec: { | ||
sink: { | ||
ref: { name: sinkName }, | ||
}, | ||
}, | ||
} = source; | ||
|
||
const initialValues = { | ||
sink: { | ||
ref: { | ||
apiVersion: '', | ||
kind: '', | ||
name: sinkName || '', | ||
}, | ||
}, | ||
}; | ||
const handleSubmit = (values: FormikValues, action: FormikHelpers<FormikValues>) => { | ||
const updatePayload = { | ||
...source, | ||
spec: { ...source.spec, ...values }, | ||
}; | ||
k8sUpdate(modelFor(referenceFor(source)), updatePayload) | ||
.then(() => { | ||
action.setSubmitting(false); | ||
action.setStatus({ error: '' }); | ||
close(); | ||
}) | ||
.catch((err) => { | ||
action.setStatus({ error: err.message || 'An error occurred. Please try again' }); | ||
}); | ||
}; | ||
|
||
return ( | ||
<Formik | ||
initialValues={initialValues} | ||
onSubmit={handleSubmit} | ||
onReset={cancel} | ||
initialStatus={{ error: '' }} | ||
> | ||
{(props) => ( | ||
<SinkSourceModal {...props} namespace={namespace} resourceName={name} cancel={cancel} /> | ||
)} | ||
</Formik> | ||
); | ||
}; | ||
|
||
export default SinkSource; |
19 changes: 19 additions & 0 deletions
19
frontend/packages/knative-plugin/src/components/sink-source/SinkSourceController.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import * as React from 'react'; | ||
import { K8sResourceKind } from '@console/internal/module/k8s'; | ||
import { createModalLauncher, ModalComponentProps } from '@console/internal/components/factory'; | ||
import SinkSource from './SinkSource'; | ||
|
||
type SinkSourceControllerProps = { | ||
obj: K8sResourceKind; | ||
}; | ||
|
||
const SinkSourceController: React.FC<SinkSourceControllerProps> = (props) => { | ||
const { obj } = props; | ||
return <SinkSource {...props} source={obj} />; | ||
}; | ||
|
||
type Props = SinkSourceControllerProps & ModalComponentProps; | ||
|
||
export const sinkModalLauncher = createModalLauncher<Props>(SinkSourceController); | ||
|
||
export default SinkSourceController; |
89 changes: 89 additions & 0 deletions
89
frontend/packages/knative-plugin/src/components/sink-source/SinkSourceModal.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import * as React from 'react'; | ||
import * as fuzzy from 'fuzzysearch'; | ||
import { FormikProps, FormikValues } from 'formik'; | ||
import { | ||
ModalTitle, | ||
ModalBody, | ||
ModalSubmitFooter, | ||
} from '@console/internal/components/factory/modal'; | ||
import { ResourceDropdownField } from '@console/shared'; | ||
import FormSection from '@console/dev-console/src/components/import/section/FormSection'; | ||
import { knativeServingResourcesServices } from '../../utils/create-knative-utils'; | ||
|
||
export interface SinkSourceModalProps { | ||
namespace: string; | ||
resourceName: string; | ||
cancel?: () => void; | ||
} | ||
|
||
type Props = FormikProps<FormikValues> & SinkSourceModalProps; | ||
|
||
const SinkSourceModal: React.FC<Props> = ({ | ||
namespace, | ||
resourceName, | ||
handleSubmit, | ||
cancel, | ||
isSubmitting, | ||
status, | ||
setFieldValue, | ||
setFieldTouched, | ||
validateForm, | ||
values, | ||
}) => { | ||
const autocompleteFilter = (strText, item): boolean => fuzzy(strText, item?.props?.name); | ||
const onSinkChange = React.useCallback( | ||
(selectedValue, target) => { | ||
const { | ||
props: { model }, | ||
} = target; | ||
if (selectedValue) { | ||
setFieldTouched('sink.ref.name', true); | ||
setFieldValue('sink.ref.name', selectedValue); | ||
if (model) { | ||
const sinkApiversion = `${model?.apiGroup}/${model?.apiVersion}`; | ||
setFieldValue('sink.ref.apiVersion', sinkApiversion); | ||
setFieldTouched('sink.ref.apiVersion', true); | ||
setFieldValue('sink.ref.kind', model?.kind); | ||
setFieldTouched('sink.ref.kind', true); | ||
} | ||
validateForm(); | ||
} | ||
}, | ||
[setFieldValue, setFieldTouched, validateForm], | ||
); | ||
return ( | ||
<form className="modal-content modal-content--no-inner-scroll" onSubmit={handleSubmit}> | ||
<ModalTitle>Move Sink</ModalTitle> | ||
<ModalBody> | ||
<p> | ||
Select a sink to move the event source | ||
<strong>{` ${resourceName} `}</strong> | ||
to | ||
</p> | ||
<FormSection fullWidth> | ||
<ResourceDropdownField | ||
name="sink.ref.name" | ||
resources={knativeServingResourcesServices(namespace)} | ||
dataSelector={['metadata', 'name']} | ||
fullWidth | ||
required | ||
placeholder="Select a sink" | ||
showBadge | ||
autocompleteFilter={autocompleteFilter} | ||
onChange={onSinkChange} | ||
autoSelect | ||
selectedKey={values.sink.ref.name} | ||
/> | ||
</FormSection> | ||
</ModalBody> | ||
<ModalSubmitFooter | ||
inProgress={isSubmitting} | ||
submitText="Save" | ||
cancel={cancel} | ||
errorMessage={status.error} | ||
/> | ||
</form> | ||
); | ||
}; | ||
|
||
export default SinkSourceModal; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters