forked from openshift-assisted/assisted-installer-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClusterDeploymentHostsDiscoveryStep.tsx
187 lines (172 loc) · 5.63 KB
/
ClusterDeploymentHostsDiscoveryStep.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import React from 'react';
import uniq from 'lodash-es/uniq.js';
import {
Grid,
GridItem,
Alert,
AlertVariant,
List,
ListItem,
useWizardFooter,
WizardFooter,
useWizardContext,
} from '@patternfly/react-core';
import { Alerts, ClusterWizardStepHeader } from '../../../common';
import { ClusterDeploymentHostsDiscoveryStepProps } from './types';
import ClusterDeploymentHostsDiscovery from './ClusterDeploymentHostsDiscovery';
import { getAgentsHostsNames, isAgentOfInfraEnv } from './helpers';
import { getIsSNOCluster, getWizardStepAgentStatus } from '../helpers';
import { canNextFromHostDiscoveryStep } from './wizardTransition';
import { useTranslation } from '../../../common/hooks/use-translation-wrapper';
import { ValidationSection } from './components/ValidationSection';
import { ClusterDeploymentWizardContext } from './ClusterDeploymentWizardContext';
const ClusterDeploymentHostsDiscoveryStep = ({
agentClusterInstall,
agents: allAgents,
infraEnv,
onSaveHostsDiscovery,
...rest
}: ClusterDeploymentHostsDiscoveryStepProps) => {
const { t } = useTranslation();
const { syncError } = React.useContext(ClusterDeploymentWizardContext);
const { activeStep, goToNextStep, goToPrevStep, close } = useWizardContext();
const [showClusterErrors, setShowClusterErrors] = React.useState(false);
const [nextRequested, setNextRequested] = React.useState(false);
const [showFormErrors, setShowFormErrors] = React.useState(false);
const infraEnvAgents = React.useMemo(
() => allAgents.filter((a) => isAgentOfInfraEnv(infraEnv, a)),
[allAgents, infraEnv],
);
const usedHostnames = React.useMemo(() => getAgentsHostsNames(infraEnvAgents), [infraEnvAgents]);
const errors = [];
if (getIsSNOCluster(agentClusterInstall)) {
infraEnvAgents.length > 1 &&
errors.push(t('ai:Single node cluster cannot contain more than 1 host.'));
} else {
infraEnvAgents.length < 3 && errors.push(t('ai:Cluster must have at least 3 hosts.'));
}
if (infraEnvAgents.some((a) => !a.spec.approved)) {
errors.push(t('ai:All hosts must be approved.'));
}
if (usedHostnames.some((h) => h === 'localhost')) {
errors.push(t('ai:Hostname localhost is forbidden.'));
}
if (uniq(usedHostnames).length !== usedHostnames.length) {
errors.push(t('ai:Hostnames must be unique.'));
}
const agentsNotHealthy = infraEnvAgents
.map((agent) => getWizardStepAgentStatus(agent, 'hosts-discovery', t).status.key)
.some((status) =>
['disconnected', 'disabled', 'error', 'insufficient', 'cancelled'].includes(status),
);
if (agentsNotHealthy) {
errors.push(t('ai:Some hosts are not ready.'));
}
const onNext = React.useCallback(async () => {
if (!showFormErrors) {
setShowFormErrors(true);
if (errors.length) {
return;
}
}
await onSaveHostsDiscovery();
setNextRequested(true);
}, [errors.length, onSaveHostsDiscovery, showFormErrors]);
React.useEffect(() => {
setNextRequested(false);
setShowClusterErrors(false);
}, [infraEnvAgents.length]);
React.useEffect(() => {
if (nextRequested) {
if (agentsNotHealthy) {
setNextRequested(false);
} else {
setShowClusterErrors(true);
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
if (canNextFromHostDiscoveryStep(agentClusterInstall, infraEnvAgents)) {
void goToNextStep();
}
}
}
}, [nextRequested, infraEnvAgents, agentClusterInstall, agentsNotHealthy, goToNextStep]);
const submittingText = React.useMemo(
() => (nextRequested && !showClusterErrors ? t('ai:Saving changes...') : undefined),
[nextRequested, showClusterErrors, t],
);
React.useEffect(() => {
if (syncError) {
setNextRequested(false);
}
}, [syncError]);
const footer = React.useMemo(
() => (
<WizardFooter
activeStep={activeStep}
onNext={onNext}
isNextDisabled={nextRequested || !!syncError || (showFormErrors ? !!errors.length : false)}
nextButtonText={submittingText || t('ai:Next')}
nextButtonProps={{ isLoading: !!submittingText }}
onBack={goToPrevStep}
onClose={close}
/>
),
[
activeStep,
close,
errors.length,
goToPrevStep,
nextRequested,
onNext,
showFormErrors,
submittingText,
syncError,
t,
],
);
useWizardFooter(footer);
return (
<Grid hasGutter>
<GridItem>
<ClusterWizardStepHeader>{t('ai:Cluster hosts')}</ClusterWizardStepHeader>
</GridItem>
<GridItem>
<ClusterDeploymentHostsDiscovery
agentClusterInstall={agentClusterInstall}
agents={infraEnvAgents}
infraEnv={infraEnv}
usedHostnames={usedHostnames}
{...rest}
/>
</GridItem>
{showClusterErrors && !!errors.length && (
<GridItem>
<Alerts />
</GridItem>
)}
{showFormErrors && !!errors.length && (
<Alert
variant={AlertVariant.danger}
title={t('ai:Provided cluster configuration is not valid')}
isInline
>
<List>
{errors.map((error) => (
<ListItem key={error}>{error}</ListItem>
))}
</List>
</Alert>
)}
{!!syncError && (
<GridItem>
<ValidationSection currentStepId={'hosts-selection'} hosts={[]}>
<Alert variant={AlertVariant.danger} title={t('ai:An error occured')} isInline>
{syncError}
</Alert>
</ValidationSection>
</GridItem>
)}
</Grid>
);
};
export default ClusterDeploymentHostsDiscoveryStep;