-
Notifications
You must be signed in to change notification settings - Fork 596
/
AddServerlessFunctionForm.tsx
172 lines (164 loc) · 6.3 KB
/
AddServerlessFunctionForm.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import * as React from 'react';
import { Alert, Flex, FlexItem, ValidatedOptions } from '@patternfly/react-core';
import { FormikProps, FormikValues } from 'formik';
import * as _ from 'lodash';
import { useTranslation } from 'react-i18next';
import { WatchK8sResultsObject } from '@console/dynamic-plugin-sdk';
import { k8sListResourceItems } from '@console/dynamic-plugin-sdk/src/utils/k8s';
import { getGitService, GitProvider } from '@console/git-service/src';
import { evaluateFunc } from '@console/git-service/src/utils/serverless-strategy-detector';
import { K8sResourceKind } from '@console/internal/module/k8s';
import { ServerlessBuildStrategyType } from '@console/knative-plugin/src/types';
import PipelineSection from '@console/pipelines-plugin/src/components/import/pipeline/PipelineSection';
import {
CLUSTER_PIPELINE_NS,
FLAG_OPENSHIFT_PIPELINE,
FUNC_PIPELINE_RUNTIME_LABEL,
} from '@console/pipelines-plugin/src/const';
import { PipelineModel } from '@console/pipelines-plugin/src/models';
import { PipelineKind } from '@console/pipelines-plugin/src/types';
import { useFlag } from '@console/shared/src';
import { FlexForm, FormBody, FormFooter } from '@console/shared/src/components/form-utils';
import { NormalizedBuilderImages } from '../../../utils/imagestream-utils';
import { notSupportedRuntime } from '../../../utils/serverless-functions';
import AdvancedSection from '../advanced/AdvancedSection';
import AppSection from '../app/AppSection';
import GitSection from '../git/GitSection';
import ServerlessFunctionStrategySection from './ServerlessFunctionStrategySection';
type AddServerlessFunctionFormProps = {
builderImages?: NormalizedBuilderImages;
projects: WatchK8sResultsObject<K8sResourceKind[]>;
};
enum SupportedRuntime {
node = 'nodejs',
nodejs = 'nodejs',
typescript = 'nodejs',
quarkus = 'java',
}
const AddServerlessFunctionForm: React.FC<
FormikProps<FormikValues> & AddServerlessFunctionFormProps
> = ({
values,
errors,
handleSubmit,
handleReset,
status,
isSubmitting,
dirty,
builderImages,
projects,
setFieldValue,
setStatus,
}) => {
const { t } = useTranslation();
const {
git: { validated, url, type, ref, dir, secretResource },
build: { strategy },
image,
} = values;
const isPipelineEnabled = useFlag(FLAG_OPENSHIFT_PIPELINE);
const [showPipelineSection, setShowPipelineSection] = React.useState<boolean>(false);
const showFullForm =
strategy === ServerlessBuildStrategyType.ServerlessFunction &&
validated !== ValidatedOptions.default &&
type !== GitProvider.INVALID;
React.useEffect(() => {
if (url) {
const gitService = getGitService(url, type, ref, dir, secretResource);
evaluateFunc(gitService)
.then((res) => {
if (res) {
setStatus({});
setFieldValue('build.env', res.values.builderEnvs);
setFieldValue('deployment.env', res.values.runtimeEnvs);
setFieldValue(
'image.selected',
notSupportedRuntime.indexOf(res.values.runtime) === -1
? SupportedRuntime[res.values.runtime]
: res.values.runtime,
);
setFieldValue('import.showEditImportStrategy', true);
setFieldValue(
'image.tag',
builderImages?.[SupportedRuntime[res.values.runtime]]?.recentTag?.name ?? '',
);
if (builderImages[SupportedRuntime[res.values.runtime]] === undefined) {
setStatus({ errors: 'Builder Image is not present.' });
}
} else {
setStatus({ errors: 'Not evaluated' });
}
})
.catch((err) => {
setStatus({ errors: err });
});
}
}, [setFieldValue, url, type, ref, dir, secretResource, builderImages, setStatus]);
React.useEffect(() => {
if (image.selected && isPipelineEnabled) {
const fetchPipelineTemplate = async () => {
const fetchedPipelines = (await k8sListResourceItems({
model: PipelineModel,
queryParams: {
ns: CLUSTER_PIPELINE_NS,
labelSelector: { matchLabels: { [FUNC_PIPELINE_RUNTIME_LABEL]: image?.selected } },
},
})) as PipelineKind[];
if (fetchedPipelines.length > 0) {
setShowPipelineSection(true);
} else {
setShowPipelineSection(false);
}
};
fetchPipelineTemplate();
}
}, [image, isPipelineEnabled]);
return (
<FlexForm onSubmit={handleSubmit} data-test="create-serverless-function-form">
<Flex direction={{ default: 'column', sm: 'row' }}>
<FlexItem flex={{ default: 'flex_1' }} alignSelf={{ default: 'alignSelfFlexStart' }}>
<FormBody flexLayout>
<GitSection builderImages={builderImages} />
{showFullForm && (
<>
{builderImages && (
<ServerlessFunctionStrategySection builderImages={builderImages} />
)}
<AppSection
project={values.project}
noProjectsAvailable={projects.loaded && _.isEmpty(projects.data)}
/>
{showPipelineSection && <PipelineSection builderImages={builderImages} />}
<AdvancedSection values={values} />
</>
)}
{validated !== ValidatedOptions.default &&
strategy !== ServerlessBuildStrategyType.ServerlessFunction && (
<Alert
variant="warning"
isInline
title={t('devconsole~func.yaml is not present and builder strategy is not s2i')}
>
<p>
{t(
'devconsole~func.yaml must be present and builder strategy should be s2i to create a Serverless function',
)}
</p>
</Alert>
)}
</FormBody>
</FlexItem>
</Flex>
<FormFooter
handleReset={handleReset}
errorMessage={status && status.submitError}
isSubmitting={isSubmitting}
submitLabel={t('devconsole~Create')}
disableSubmit={!dirty || !_.isEmpty(status?.errors) || !_.isEmpty(errors) || isSubmitting}
resetLabel={t('devconsole~Cancel')}
sticky
/>
</FlexForm>
);
};
export default AddServerlessFunctionForm;