Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
eeb2e83
commit 101e0c2
Showing
12 changed files
with
371 additions
and
31 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
65 changes: 65 additions & 0 deletions
65
frontend/packages/dev-console/src/components/health-checks/AddHealthCheck.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,65 @@ | ||
import * as React from 'react'; | ||
import * as _ from 'lodash'; | ||
import { FirehoseResult, ContainerDropdown, history } from '@console/internal/components/utils'; | ||
import { K8sResourceKind } from '@console/internal/module/k8s'; | ||
import HealthChecks from './HealthChecks'; | ||
import { FormikProps, FormikValues, useFormikContext } from 'formik'; | ||
import { Form } from '@patternfly/react-core'; | ||
import { FormFooter } from '@console/shared'; | ||
|
||
type AddHelathProps = { | ||
resource?: FirehoseResult<K8sResourceKind>; | ||
currentContainer: string; | ||
}; | ||
|
||
const AddHealthCheck: React.FC<FormikProps<FormikValues> & AddHelathProps> = ({ | ||
resource, | ||
currentContainer, | ||
handleSubmit, | ||
handleReset, | ||
dirty, | ||
errors, | ||
status, | ||
isSubmitting, | ||
}) => { | ||
const { data } = resource; | ||
const [currentKey, setCurrentKey] = React.useState(currentContainer); | ||
const { setFieldValue } = useFormikContext<FormikValues>(); | ||
|
||
const handleSelectContainer = (containerName) => { | ||
setCurrentKey(containerName); | ||
setFieldValue('containerName', containerName); | ||
history.push( | ||
`/k8s/ns/${data.metadata.namespace}/${data.kind || data.kind}/${ | ||
data.metadata.name | ||
}/containers/${containerName}/health-checks`, | ||
); | ||
}; | ||
|
||
const containers = resource.loaded && _.keyBy(data.spec.template.spec.containers, 'name'); | ||
return ( | ||
<div className="co-m-pane__body"> | ||
<Form onSubmit={handleSubmit}> | ||
<div> | ||
Managing health check for:{' '} | ||
<ContainerDropdown | ||
currentKey={currentKey} | ||
containers={containers} | ||
onChange={handleSelectContainer} | ||
/> | ||
</div> | ||
<HealthChecks /> | ||
<FormFooter | ||
handleReset={handleReset} | ||
errorMessage={status && `${status.errors}`} | ||
isSubmitting={isSubmitting} | ||
submitLabel="Save" | ||
disableSubmit={!dirty || !_.isEmpty(errors)} | ||
resetLabel="Cancel" | ||
/> | ||
</Form> | ||
</div> | ||
); | ||
}; | ||
|
||
export default AddHealthCheck; |
151 changes: 151 additions & 0 deletions
151
frontend/packages/dev-console/src/components/health-checks/AddHealthCheckPage.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,151 @@ | ||
import * as React from 'react'; | ||
import * as _ from 'lodash'; | ||
import Helmet from 'react-helmet'; | ||
import { Formik } from 'formik'; | ||
import { RouteComponentProps } from 'react-router-dom'; | ||
import { | ||
PageHeading, | ||
FirehoseResource, | ||
Firehose, | ||
FirehoseResult, | ||
LoadingBox, | ||
StatusBox, | ||
history, | ||
} from '@console/internal/components/utils'; | ||
import { K8sResourceKind, k8sUpdate, referenceFor, modelFor } from '@console/internal/module/k8s'; | ||
import AddHealthCheck from './AddHealthCheck'; | ||
import { gethealthCheckProbeInitialData } from './health-check-probe-utils'; | ||
import { HealthCheckProbeData, RequestType } from './health-checks-types'; | ||
import { healthChecksProbesValidationSchema } from './health-check-probe-validation-utils'; | ||
|
||
type AddHealthCheckProps = RouteComponentProps<{ | ||
ns: string; | ||
kind: string; | ||
name: string; | ||
containerName: string; | ||
}>; | ||
|
||
type HealthCheckWrapperProps = { | ||
resource?: FirehoseResult<K8sResourceKind>; | ||
currentContainer: string; | ||
}; | ||
|
||
const constructProbeData = (data: HealthCheckProbeData) => { | ||
const probeData = { | ||
...(data.failureThreshold && { failureThreshold: data.failureThreshold }), | ||
...(data.successThreshold && { successThreshold: data.successThreshold }), | ||
...(data.requestType === RequestType.ContainerCommand && { | ||
exec: data.exec, | ||
}), | ||
...(data.requestType === RequestType.HTTPGET && { | ||
httpGet: { | ||
path: data.httpGet.path, | ||
...(_.includes(data[data.requestType]?.scheme, 'HTTPS') && { | ||
scheme: 'HTTPS', | ||
}), | ||
port: _.toInteger(data.httpGet.port), | ||
}, | ||
}), | ||
...(data.requestType === RequestType.TCPSocket && { | ||
tcpSocket: { port: _.toInteger(data.tcpSocket.port) }, | ||
}), | ||
...(data.initialDelaySeconds && { | ||
initialDelaySeconds: data.initialDelaySeconds, | ||
}), | ||
...(data.periodSeconds && { periodSeconds: data.periodSeconds }), | ||
...(data.timeoutSeconds && { timeoutSeconds: data.timeoutSeconds }), | ||
}; | ||
return probeData; | ||
}; | ||
|
||
const HealthCheckWrapper: React.FC<HealthCheckWrapperProps> = ({ resource, currentContainer }) => { | ||
if (!resource.loaded && _.isEmpty(resource.loadError)) { | ||
return <LoadingBox />; | ||
} | ||
|
||
if (resource.loadError) { | ||
return <StatusBox loaded={resource.loaded} loadError={resource.loadError} />; | ||
} | ||
|
||
const container = _.filter( | ||
resource.data.spec.template.spec.containers, | ||
(data) => data.name === currentContainer, | ||
); | ||
|
||
if (_.isEmpty(container)) { | ||
return <div className="text-center">Container not found</div>; | ||
} | ||
|
||
const handleSubmit = (values, actions) => { | ||
const { | ||
healthChecks: { readinessProbe, livenessProbe, startupProbe }, | ||
containerName, | ||
} = values; | ||
const updatedResource = _.cloneDeep(resource.data); | ||
const containerIndex = _.findIndex(updatedResource.spec.template.spec.containers, [ | ||
'name', | ||
containerName, | ||
]); | ||
updatedResource.spec.template.spec.containers[containerIndex] = { | ||
...container[containerIndex], | ||
...(readinessProbe.enabled | ||
? { readinessProbe: constructProbeData(readinessProbe.data) } | ||
: {}), | ||
...(livenessProbe.enabled ? { livenessProbe: constructProbeData(livenessProbe.data) } : {}), | ||
...(startupProbe.enabled ? { startupProbe: constructProbeData(startupProbe.data) } : {}), | ||
}; | ||
k8sUpdate(modelFor(referenceFor(resource.data)), updatedResource) | ||
.then(() => { | ||
actions.setSubmitting(false); | ||
actions.setStatus({ error: '' }); | ||
history.push(`/topology/ns/${updatedResource.metadata.namespace}`); | ||
}) | ||
.catch((err) => { | ||
actions.setSubmitting(false); | ||
actions.setStatus({ errors: err }); | ||
}); | ||
}; | ||
const initialValues = gethealthCheckProbeInitialData(resource.data); | ||
initialValues.containerName = container[0].name; | ||
|
||
return ( | ||
<Formik | ||
initialValues={initialValues} | ||
validationSchema={healthChecksProbesValidationSchema} | ||
onSubmit={handleSubmit} | ||
onReset={history.goBack} | ||
> | ||
{(props) => ( | ||
<AddHealthCheck resource={resource} currentContainer={currentContainer} {...props} /> | ||
)} | ||
</Formik> | ||
); | ||
}; | ||
|
||
const AddHealthCheckPage: React.FC<AddHealthCheckProps> = ({ match }) => { | ||
const { ns, kind, name, containerName } = match.params; | ||
const resource: FirehoseResource[] = [ | ||
{ | ||
kind, | ||
namespace: ns, | ||
isList: false, | ||
name, | ||
prop: 'resource', | ||
}, | ||
]; | ||
return ( | ||
<> | ||
<Helmet> | ||
<title>Health Checks</title> | ||
</Helmet> | ||
<div> | ||
<PageHeading title="Health Checks" /> | ||
<Firehose resources={resource}> | ||
<HealthCheckWrapper currentContainer={containerName} /> | ||
</Firehose> | ||
</div> | ||
</> | ||
); | ||
}; | ||
|
||
export default AddHealthCheckPage; |
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
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
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
Oops, something went wrong.