|
| 1 | +import type { FC } from 'react'; |
| 2 | +import { useMemo } from 'react'; |
| 3 | +import { |
| 4 | + DescriptionList, |
| 5 | + DescriptionListDescription, |
| 6 | + DescriptionListGroup, |
| 7 | + DescriptionListTerm, |
| 8 | + Skeleton, |
| 9 | + Title, |
| 10 | +} from '@patternfly/react-core'; |
| 11 | +import { useTranslation } from 'react-i18next'; |
| 12 | +import Status from '@console/dynamic-plugin-sdk/src/app/components/status/Status'; |
| 13 | +import type { MachineHealthCheckKind } from '@console/internal/module/k8s'; |
| 14 | +import { DASH } from '@console/shared/src/constants/ui'; |
| 15 | +import { |
| 16 | + computeRemediationTimeBoundsFromRefs, |
| 17 | + dedupeRemediationTemplateRefs, |
| 18 | + FALLBACK_REMEDIATION_BOUNDS, |
| 19 | + getRemediationTemplateRefsFromHealthChecks, |
| 20 | + useRemediationResourcesForEstimatedRecovery, |
| 21 | +} from '../../utils/estimatedRecoveryRemediation'; |
| 22 | +import type { NodeHealthCheckKind } from '../../utils/HealthCheckUtils'; |
| 23 | +import { formatTimeoutForDisplay, getMaxTimeoutFromConditions } from '../../utils/utils'; |
| 24 | + |
| 25 | +type DetailsProps = { |
| 26 | + matchingMachineHealthChecks: MachineHealthCheckKind[]; |
| 27 | + matchingNodeHealthChecks: NodeHealthCheckKind[]; |
| 28 | + isLoading: boolean; |
| 29 | + loadError?: unknown; |
| 30 | +}; |
| 31 | + |
| 32 | +const NODE_HEARTBEAT_DETECTION_SECONDS = 50; |
| 33 | +const WORKLOAD_RESTART_SECONDS = 15; |
| 34 | + |
| 35 | +const Details: FC<DetailsProps> = ({ |
| 36 | + matchingMachineHealthChecks, |
| 37 | + matchingNodeHealthChecks, |
| 38 | + isLoading, |
| 39 | + loadError, |
| 40 | +}) => { |
| 41 | + const { t } = useTranslation(); |
| 42 | + const { |
| 43 | + snrConfigs, |
| 44 | + farTemplates, |
| 45 | + loaded: remediationResourcesLoaded, |
| 46 | + } = useRemediationResourcesForEstimatedRecovery(); |
| 47 | + |
| 48 | + const isLoadingDetailsData = isLoading || !remediationResourcesLoaded; |
| 49 | + |
| 50 | + const isHighAvailability = useMemo( |
| 51 | + () => |
| 52 | + getRemediationTemplateRefsFromHealthChecks( |
| 53 | + matchingMachineHealthChecks, |
| 54 | + matchingNodeHealthChecks, |
| 55 | + ).length > 0, |
| 56 | + [matchingMachineHealthChecks, matchingNodeHealthChecks], |
| 57 | + ); |
| 58 | + |
| 59 | + const remediationDisplay = useMemo(() => { |
| 60 | + const primaryMHC = matchingMachineHealthChecks[0]; |
| 61 | + const primaryNHC = matchingNodeHealthChecks[0]; |
| 62 | + const source = primaryMHC |
| 63 | + ? ({ prefix: 'MHC', check: primaryMHC } as const) |
| 64 | + : primaryNHC |
| 65 | + ? ({ prefix: 'NHC', check: primaryNHC } as const) |
| 66 | + : undefined; |
| 67 | + if (!source) { |
| 68 | + return DASH; |
| 69 | + } |
| 70 | + |
| 71 | + const reboot = |
| 72 | + source.prefix === 'MHC' && |
| 73 | + source.check.metadata?.annotations?.['machine.openshift.io/remediation-strategy'] === |
| 74 | + 'external-baremetal'; |
| 75 | + const baseRemediation = reboot |
| 76 | + ? t('console-app~auto-reboot') |
| 77 | + : source.prefix === 'MHC' |
| 78 | + ? t('console-app~machine replacement') |
| 79 | + : t('console-app~template remediation'); |
| 80 | + |
| 81 | + const unhealthyConditions = source.check.spec?.unhealthyConditions ?? []; |
| 82 | + const maxTimeoutSeconds = getMaxTimeoutFromConditions(unhealthyConditions); |
| 83 | + |
| 84 | + if (maxTimeoutSeconds) { |
| 85 | + return t('console-app~{{prefix}}: {{remediation}}; Drain: {{timeout}} timeout', { |
| 86 | + prefix: source.prefix, |
| 87 | + remediation: baseRemediation, |
| 88 | + timeout: formatTimeoutForDisplay(maxTimeoutSeconds), |
| 89 | + }); |
| 90 | + } |
| 91 | + |
| 92 | + return t('console-app~{{prefix}}: {{remediation}}', { |
| 93 | + prefix: source.prefix, |
| 94 | + remediation: baseRemediation, |
| 95 | + }); |
| 96 | + }, [matchingMachineHealthChecks, matchingNodeHealthChecks, t]); |
| 97 | + |
| 98 | + const estimatedRecoveryTimeDisplay = useMemo(() => { |
| 99 | + const allConditions = [ |
| 100 | + ...matchingMachineHealthChecks.flatMap((hc) => hc.spec?.unhealthyConditions ?? []), |
| 101 | + ...matchingNodeHealthChecks.flatMap((hc) => hc.spec?.unhealthyConditions ?? []), |
| 102 | + ]; |
| 103 | + const maxTimeoutSeconds = getMaxTimeoutFromConditions(allConditions); |
| 104 | + |
| 105 | + if (maxTimeoutSeconds === undefined) { |
| 106 | + return undefined; |
| 107 | + } |
| 108 | + |
| 109 | + const orderedRefs = dedupeRemediationTemplateRefs( |
| 110 | + getRemediationTemplateRefsFromHealthChecks( |
| 111 | + matchingMachineHealthChecks, |
| 112 | + matchingNodeHealthChecks, |
| 113 | + ), |
| 114 | + ); |
| 115 | + const remediationBounds = |
| 116 | + computeRemediationTimeBoundsFromRefs(orderedRefs, snrConfigs, farTemplates) ?? |
| 117 | + FALLBACK_REMEDIATION_BOUNDS; |
| 118 | + |
| 119 | + // Recovery estimate model from HA guidance: |
| 120 | + // 50s node heartbeat detection + health-check timeout + remediation time + ~15s workload restart. |
| 121 | + const baseSeconds = |
| 122 | + NODE_HEARTBEAT_DETECTION_SECONDS + maxTimeoutSeconds + WORKLOAD_RESTART_SECONDS; |
| 123 | + const minMinutes = Math.max(1, Math.ceil((baseSeconds + remediationBounds.minSeconds) / 60)); |
| 124 | + const maxMinutes = Math.max( |
| 125 | + minMinutes, |
| 126 | + Math.ceil((baseSeconds + remediationBounds.maxSeconds) / 60), |
| 127 | + ); |
| 128 | + return t('console-app~{{minMinutes}}-{{maxMinutes}} min', { minMinutes, maxMinutes }); |
| 129 | + }, [matchingMachineHealthChecks, matchingNodeHealthChecks, snrConfigs, farTemplates, t]); |
| 130 | + |
| 131 | + return ( |
| 132 | + <> |
| 133 | + <Title headingLevel="h3" className="co-section-heading"> |
| 134 | + <span>{t('console-app~Details')}</span> |
| 135 | + </Title> |
| 136 | + {loadError ? ( |
| 137 | + t('console-app~Unable to load high availability details') |
| 138 | + ) : ( |
| 139 | + <DescriptionList |
| 140 | + className="pf-v6-u-ml-lg" |
| 141 | + columnModifier={{ default: '3Col' }} |
| 142 | + isInlineGrid |
| 143 | + > |
| 144 | + <DescriptionListGroup> |
| 145 | + <DescriptionListTerm>{t('console-app~Status')}</DescriptionListTerm> |
| 146 | + <DescriptionListDescription> |
| 147 | + {isLoadingDetailsData ? ( |
| 148 | + <Skeleton width="120px" /> |
| 149 | + ) : ( |
| 150 | + <Status |
| 151 | + status={isHighAvailability ? 'Ready' : 'Unknown'} |
| 152 | + title={isHighAvailability ? t('console-app~Ready') : t('console-app~Unavailable')} |
| 153 | + /> |
| 154 | + )} |
| 155 | + </DescriptionListDescription> |
| 156 | + </DescriptionListGroup> |
| 157 | + <DescriptionListGroup> |
| 158 | + <DescriptionListTerm>{t('console-app~Remediation')}</DescriptionListTerm> |
| 159 | + <DescriptionListDescription> |
| 160 | + {isLoadingDetailsData ? <Skeleton width="220px" /> : remediationDisplay} |
| 161 | + </DescriptionListDescription> |
| 162 | + </DescriptionListGroup> |
| 163 | + <DescriptionListGroup> |
| 164 | + <DescriptionListTerm>{t('console-app~Estimated recovery time')}</DescriptionListTerm> |
| 165 | + <DescriptionListDescription> |
| 166 | + {isLoadingDetailsData ? ( |
| 167 | + <Skeleton width="90px" /> |
| 168 | + ) : ( |
| 169 | + estimatedRecoveryTimeDisplay ?? DASH |
| 170 | + )} |
| 171 | + </DescriptionListDescription> |
| 172 | + </DescriptionListGroup> |
| 173 | + </DescriptionList> |
| 174 | + )} |
| 175 | + </> |
| 176 | + ); |
| 177 | +}; |
| 178 | + |
| 179 | +export default Details; |
0 commit comments