|
| 1 | +/* |
| 2 | + * Copyright 2025 The Kubernetes Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import Box from '@mui/material/Box'; |
| 18 | +import Chip from '@mui/material/Chip'; |
| 19 | +import Table from '@mui/material/Table'; |
| 20 | +import TableBody from '@mui/material/TableBody'; |
| 21 | +import TableCell from '@mui/material/TableCell'; |
| 22 | +import TableContainer from '@mui/material/TableContainer'; |
| 23 | +import TableHead from '@mui/material/TableHead'; |
| 24 | +import TableRow from '@mui/material/TableRow'; |
| 25 | +import Typography from '@mui/material/Typography'; |
| 26 | +import { useEffect, useState } from 'react'; |
| 27 | +import { useTranslation } from 'react-i18next'; |
| 28 | +import { KubeObject } from '../../../lib/k8s/KubeObject'; |
| 29 | +import type { RevisionInfo } from '../../../lib/k8s/rollback'; |
| 30 | +import { DateLabel } from '../Label'; |
| 31 | +import SectionBox from '../SectionBox'; |
| 32 | + |
| 33 | +interface RevisionHistorySectionProps { |
| 34 | + /** The resource to show revision history for */ |
| 35 | + resource: KubeObject; |
| 36 | +} |
| 37 | + |
| 38 | +/** |
| 39 | + * Helper to extract revision history from a resource that supports it. |
| 40 | + */ |
| 41 | +function getRevisionHistoryFn(resource: KubeObject): (() => Promise<RevisionInfo[]>) | undefined { |
| 42 | + if ('getRevisionHistory' in resource && typeof resource.getRevisionHistory === 'function') { |
| 43 | + return () => (resource as any).getRevisionHistory(); |
| 44 | + } |
| 45 | + return undefined; |
| 46 | +} |
| 47 | + |
| 48 | +/** |
| 49 | + * RevisionHistorySection shows a table of revision history for rollbackable resources. |
| 50 | + * Displays revision number, creation date, container images, and current status. |
| 51 | + * |
| 52 | + * This is added as an extraSection on Deployment, DaemonSet, and StatefulSet details pages. |
| 53 | + */ |
| 54 | +export default function RevisionHistorySection(props: RevisionHistorySectionProps) { |
| 55 | + const { resource } = props; |
| 56 | + const { t } = useTranslation(['translation']); |
| 57 | + |
| 58 | + const [revisions, setRevisions] = useState<RevisionInfo[]>([]); |
| 59 | + const [loading, setLoading] = useState(true); |
| 60 | + const [error, setError] = useState<string | null>(null); |
| 61 | + |
| 62 | + const getHistory = getRevisionHistoryFn(resource); |
| 63 | + |
| 64 | + useEffect(() => { |
| 65 | + const historyGetter = getRevisionHistoryFn(resource); |
| 66 | + let isActive = true; |
| 67 | + |
| 68 | + if (!historyGetter) { |
| 69 | + setRevisions([]); |
| 70 | + setError(null); |
| 71 | + setLoading(false); |
| 72 | + return () => { |
| 73 | + isActive = false; |
| 74 | + }; |
| 75 | + } |
| 76 | + |
| 77 | + setLoading(true); |
| 78 | + setError(null); |
| 79 | + historyGetter() |
| 80 | + .then(history => { |
| 81 | + if (!isActive) { |
| 82 | + return; |
| 83 | + } |
| 84 | + setRevisions(history); |
| 85 | + setLoading(false); |
| 86 | + }) |
| 87 | + .catch(err => { |
| 88 | + if (!isActive) { |
| 89 | + return; |
| 90 | + } |
| 91 | + setError(err instanceof Error ? err.message : String(err)); |
| 92 | + setLoading(false); |
| 93 | + }); |
| 94 | + |
| 95 | + return () => { |
| 96 | + isActive = false; |
| 97 | + }; |
| 98 | + }, [resource?.metadata?.uid, resource?.metadata?.resourceVersion]); |
| 99 | + |
| 100 | + if (!getHistory) { |
| 101 | + return null; |
| 102 | + } |
| 103 | + |
| 104 | + return ( |
| 105 | + <SectionBox title={t('translation|Revision History')}> |
| 106 | + {loading && ( |
| 107 | + <Typography variant="body2" color="text.secondary" sx={{ py: 2 }}> |
| 108 | + {t('translation|Loading revision history…')} |
| 109 | + </Typography> |
| 110 | + )} |
| 111 | + {error && ( |
| 112 | + <Typography variant="body2" color="error" sx={{ py: 2 }}> |
| 113 | + {t('translation|Failed to load revision history: {{ error }}', { error })} |
| 114 | + </Typography> |
| 115 | + )} |
| 116 | + {!loading && !error && revisions.length === 0 && ( |
| 117 | + <Typography variant="body2" color="text.secondary" sx={{ py: 2 }}> |
| 118 | + {t('translation|No revision history available.')} |
| 119 | + </Typography> |
| 120 | + )} |
| 121 | + {!loading && !error && revisions.length > 0 && ( |
| 122 | + <TableContainer> |
| 123 | + <Table size="small"> |
| 124 | + <TableHead> |
| 125 | + <TableRow> |
| 126 | + <TableCell>{t('translation|Revision')}</TableCell> |
| 127 | + <TableCell>{t('translation|Created')}</TableCell> |
| 128 | + <TableCell>{t('translation|Images')}</TableCell> |
| 129 | + <TableCell>{t('translation|Status')}</TableCell> |
| 130 | + </TableRow> |
| 131 | + </TableHead> |
| 132 | + <TableBody> |
| 133 | + {revisions.map(rev => ( |
| 134 | + <TableRow key={rev.revision}> |
| 135 | + <TableCell> |
| 136 | + <Typography variant="body2">{rev.revision}</Typography> |
| 137 | + </TableCell> |
| 138 | + <TableCell> |
| 139 | + <DateLabel date={rev.createdAt} /> |
| 140 | + </TableCell> |
| 141 | + <TableCell> |
| 142 | + <Box sx={{ display: 'flex', flexDirection: 'column', gap: 0.5 }}> |
| 143 | + {rev.images.map((img, i) => ( |
| 144 | + <Typography key={i} variant="body2" sx={{ fontFamily: 'monospace' }}> |
| 145 | + {img} |
| 146 | + </Typography> |
| 147 | + ))} |
| 148 | + </Box> |
| 149 | + </TableCell> |
| 150 | + <TableCell> |
| 151 | + {rev.isCurrent ? ( |
| 152 | + <Chip |
| 153 | + label={t('translation|Current')} |
| 154 | + size="small" |
| 155 | + color="primary" |
| 156 | + variant="outlined" |
| 157 | + /> |
| 158 | + ) : ( |
| 159 | + <Typography variant="body2" color="text.secondary"> |
| 160 | + {t('translation|Previous')} |
| 161 | + </Typography> |
| 162 | + )} |
| 163 | + </TableCell> |
| 164 | + </TableRow> |
| 165 | + ))} |
| 166 | + </TableBody> |
| 167 | + </Table> |
| 168 | + </TableContainer> |
| 169 | + )} |
| 170 | + </SectionBox> |
| 171 | + ); |
| 172 | +} |
0 commit comments