|
| 1 | +import { Icon } from '@iconify/react'; |
| 2 | +import { Button, useTheme } from '@mui/material'; |
| 3 | +import Box from '@mui/material/Box'; |
| 4 | +import Typography from '@mui/material/Typography'; |
| 5 | +import { useTranslation } from 'react-i18next'; |
| 6 | +import { generatePath, useHistory } from 'react-router-dom'; |
| 7 | +import { useClustersConf, useClustersVersion } from '../../../lib/k8s'; |
| 8 | +import { ApiError } from '../../../lib/k8s/apiProxy'; |
| 9 | +import { Cluster } from '../../../lib/k8s/cluster'; |
| 10 | +import { getClusterPrefixedPath } from '../../../lib/util'; |
| 11 | +import { Link, Table } from '../../common'; |
| 12 | +import ClusterContextMenu from './ClusterContextMenu'; |
| 13 | +import { MULTI_HOME_ENABLED } from './config'; |
| 14 | +import { getCustomClusterNames } from './customClusterNames'; |
| 15 | + |
| 16 | +/** |
| 17 | + * ClusterStatus component displays the status of a cluster. |
| 18 | + * It shows an icon and a message indicating whether the cluster is active, unknown, or has an error. |
| 19 | + * |
| 20 | + * @param {Object} props - The component props. |
| 21 | + * @param {ApiError|null} [props.error] - The error object if there is an error with the cluster. |
| 22 | + */ |
| 23 | +function ClusterStatus({ error }: { error?: ApiError | null }) { |
| 24 | + const { t } = useTranslation(['translation']); |
| 25 | + const theme = useTheme(); |
| 26 | + |
| 27 | + const stateUnknown = error === undefined; |
| 28 | + const hasReachError = error && error.status !== 401 && error.status !== 403; |
| 29 | + |
| 30 | + return ( |
| 31 | + <Box width="fit-content"> |
| 32 | + <Box display="flex" alignItems="center" justifyContent="center"> |
| 33 | + {hasReachError ? ( |
| 34 | + <Icon icon="mdi:cloud-off" width={16} color={theme.palette.home.status.error} /> |
| 35 | + ) : stateUnknown ? ( |
| 36 | + <Icon icon="mdi:cloud-question" width={16} color={theme.palette.home.status.unknown} /> |
| 37 | + ) : ( |
| 38 | + <Icon |
| 39 | + icon="mdi:cloud-check-variant" |
| 40 | + width={16} |
| 41 | + color={theme.palette.home.status.success} |
| 42 | + /> |
| 43 | + )} |
| 44 | + <Typography |
| 45 | + variant="body2" |
| 46 | + style={{ |
| 47 | + marginLeft: theme.spacing(1), |
| 48 | + color: hasReachError |
| 49 | + ? theme.palette.home.status.error |
| 50 | + : !stateUnknown |
| 51 | + ? theme.palette.home.status.success |
| 52 | + : undefined, |
| 53 | + }} |
| 54 | + > |
| 55 | + {hasReachError ? error.message : stateUnknown ? '⋯' : t('translation|Active')} |
| 56 | + </Typography> |
| 57 | + </Box> |
| 58 | + </Box> |
| 59 | + ); |
| 60 | +} |
| 61 | + |
| 62 | +export interface ClusterTableProps { |
| 63 | + /** Some clusters have custom names. */ |
| 64 | + customNameClusters: ReturnType<typeof getCustomClusterNames>; |
| 65 | + /** Versions for each cluster. */ |
| 66 | + versions: ReturnType<typeof useClustersVersion>[0]; |
| 67 | + /** Errors for each cluster. */ |
| 68 | + errors: ReturnType<typeof useClustersVersion>[1]; |
| 69 | + /** Clusters configuration. */ |
| 70 | + clusters: ReturnType<typeof useClustersConf>; |
| 71 | + /** Warnings for each cluster. */ |
| 72 | + warningLabels: { [cluster: string]: string }; |
| 73 | +} |
| 74 | + |
| 75 | +/** |
| 76 | + * ClusterTable component displays a table of clusters with their status, origin, and version. |
| 77 | + */ |
| 78 | +export default function ClusterTable({ |
| 79 | + customNameClusters, |
| 80 | + versions, |
| 81 | + errors, |
| 82 | + clusters, |
| 83 | + warningLabels, |
| 84 | +}: ClusterTableProps) { |
| 85 | + const history = useHistory(); |
| 86 | + const { t } = useTranslation(['translation']); |
| 87 | + |
| 88 | + /** |
| 89 | + * Gets the origin of a cluster. |
| 90 | + * |
| 91 | + * @param cluster |
| 92 | + * @returns A description of where the cluster is picked up from: dynamic, in-cluster, or from a kubeconfig file. |
| 93 | + */ |
| 94 | + function getOrigin(cluster: Cluster): string { |
| 95 | + if (cluster.meta_data?.source === 'kubeconfig') { |
| 96 | + const kubeconfigPath = process.env.KUBECONFIG ?? '~/.kube/config'; |
| 97 | + return `Kubeconfig: ${kubeconfigPath}`; |
| 98 | + } else if (cluster.meta_data?.source === 'dynamic_cluster') { |
| 99 | + return t('translation|Plugin'); |
| 100 | + } else if (cluster.meta_data?.source === 'in_cluster') { |
| 101 | + return t('translation|In-cluster'); |
| 102 | + } |
| 103 | + return 'Unknown'; |
| 104 | + } |
| 105 | + const viewClusters = t('View Clusters'); |
| 106 | + |
| 107 | + return ( |
| 108 | + <Table |
| 109 | + columns={[ |
| 110 | + { |
| 111 | + id: 'name', |
| 112 | + header: t('Name'), |
| 113 | + accessorKey: 'name', |
| 114 | + Cell: ({ row: { original } }) => ( |
| 115 | + <Link routeName="cluster" params={{ cluster: original.name }}> |
| 116 | + {original.name} |
| 117 | + </Link> |
| 118 | + ), |
| 119 | + }, |
| 120 | + { |
| 121 | + header: t('Origin'), |
| 122 | + accessorFn: cluster => getOrigin(cluster), |
| 123 | + Cell: ({ row: { original } }) => ( |
| 124 | + <Typography variant="body2">{getOrigin((clusters || {})[original.name])}</Typography> |
| 125 | + ), |
| 126 | + }, |
| 127 | + { |
| 128 | + header: t('Status'), |
| 129 | + accessorFn: cluster => |
| 130 | + errors[cluster.name] === null ? 'Active' : errors[cluster.name]?.message, |
| 131 | + Cell: ({ row: { original } }) => <ClusterStatus error={errors[original.name]} />, |
| 132 | + }, |
| 133 | + { header: t('Warnings'), accessorFn: cluster => warningLabels[cluster.name] }, |
| 134 | + { |
| 135 | + header: t('glossary|Kubernetes Version'), |
| 136 | + accessorFn: ({ name }) => versions[name]?.gitVersion || '⋯', |
| 137 | + }, |
| 138 | + { |
| 139 | + header: '', |
| 140 | + muiTableBodyCellProps: { |
| 141 | + align: 'right', |
| 142 | + }, |
| 143 | + accessorFn: cluster => |
| 144 | + errors[cluster.name] === null ? 'Active' : errors[cluster.name]?.message, |
| 145 | + Cell: ({ row: { original: cluster } }) => { |
| 146 | + return <ClusterContextMenu cluster={cluster} />; |
| 147 | + }, |
| 148 | + }, |
| 149 | + ]} |
| 150 | + data={Object.values(customNameClusters)} |
| 151 | + enableRowSelection={ |
| 152 | + MULTI_HOME_ENABLED |
| 153 | + ? row => { |
| 154 | + // Only allow selection if the cluster is working |
| 155 | + return !errors[row.original.name]; |
| 156 | + } |
| 157 | + : false |
| 158 | + } |
| 159 | + initialState={{ |
| 160 | + sorting: [{ id: 'name', desc: false }], |
| 161 | + }} |
| 162 | + muiToolbarAlertBannerProps={{ |
| 163 | + sx: theme => ({ |
| 164 | + background: theme.palette.background.muted, |
| 165 | + }), |
| 166 | + }} |
| 167 | + renderToolbarAlertBannerContent={({ table }) => ( |
| 168 | + <Button |
| 169 | + variant="contained" |
| 170 | + sx={{ |
| 171 | + marginLeft: 1, |
| 172 | + }} |
| 173 | + onClick={() => { |
| 174 | + history.push({ |
| 175 | + pathname: generatePath(getClusterPrefixedPath(), { |
| 176 | + cluster: table |
| 177 | + .getSelectedRowModel() |
| 178 | + .rows.map(it => it.original.name) |
| 179 | + .join('+'), |
| 180 | + }), |
| 181 | + }); |
| 182 | + }} |
| 183 | + > |
| 184 | + {viewClusters} |
| 185 | + </Button> |
| 186 | + )} |
| 187 | + /> |
| 188 | + ); |
| 189 | +} |
0 commit comments