|
| 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 { Icon } from '@iconify/react'; |
| 18 | +import Box from '@mui/material/Box'; |
| 19 | +import Button from '@mui/material/Button'; |
| 20 | +import Typography, { TypographyProps } from '@mui/material/Typography'; |
| 21 | +import React from 'react'; |
| 22 | +import { useTranslation } from 'react-i18next'; |
| 23 | +import { KubeContainer } from '../../../lib/k8s/cluster'; |
| 24 | +import Link from '../Link'; |
| 25 | + |
| 26 | +interface EnvVarGridProps { |
| 27 | + envVars: NonNullable<KubeContainer['env']>; |
| 28 | + namespace: string; |
| 29 | + cluster: string; |
| 30 | +} |
| 31 | + |
| 32 | +export function EnvVarGrid(props: EnvVarGridProps) { |
| 33 | + const { envVars = [], namespace, cluster } = props; |
| 34 | + const { t } = useTranslation(); |
| 35 | + const [expanded, setExpanded] = React.useState(false); |
| 36 | + const defaultNumShown = 20; |
| 37 | + |
| 38 | + const validEnvVars = envVars.filter( |
| 39 | + env => !!env && typeof env.name === 'string' && env.name.trim() !== '' |
| 40 | + ); |
| 41 | + |
| 42 | + const EnvEntry = React.forwardRef((props: TypographyProps, ref: React.Ref<HTMLElement>) => { |
| 43 | + return ( |
| 44 | + <Typography |
| 45 | + {...props} |
| 46 | + sx={theme => ({ |
| 47 | + color: theme.palette.text.primary, |
| 48 | + borderRadius: theme.shape.borderRadius + 'px', |
| 49 | + backgroundColor: theme.palette.background.muted, |
| 50 | + border: '1px solid', |
| 51 | + borderColor: theme.palette.divider, |
| 52 | + fontSize: theme.typography.pxToRem(14), |
| 53 | + padding: '4px 8px', |
| 54 | + marginRight: theme.spacing(1), |
| 55 | + whiteSpace: 'nowrap', |
| 56 | + display: 'inline-block', |
| 57 | + maxWidth: '400px', |
| 58 | + overflow: 'hidden', |
| 59 | + textOverflow: 'ellipsis', |
| 60 | + })} |
| 61 | + ref={ref} |
| 62 | + /> |
| 63 | + ); |
| 64 | + }); |
| 65 | + EnvEntry.displayName = 'EnvEntry'; |
| 66 | + |
| 67 | + const renderEnvVar = (envVar: NonNullable<KubeContainer['env']>[number]) => { |
| 68 | + // Fallback: |
| 69 | + if (!envVar.name) { |
| 70 | + return null; |
| 71 | + } |
| 72 | + // Secret Key: |
| 73 | + if (envVar.valueFrom?.secretKeyRef) { |
| 74 | + const { name: secretName, key: secretKey } = envVar.valueFrom.secretKeyRef; |
| 75 | + |
| 76 | + if (!secretName) { |
| 77 | + return ( |
| 78 | + <EnvEntry component="span" key={envVar.name}> |
| 79 | + {envVar.name}: {t('translation|Invalid Secret Reference')} |
| 80 | + </EnvEntry> |
| 81 | + ); |
| 82 | + } |
| 83 | + |
| 84 | + const secretUrl = `/c/${encodeURIComponent(cluster)}/secrets/${encodeURIComponent( |
| 85 | + namespace |
| 86 | + )}/${encodeURIComponent(secretName)}`; |
| 87 | + |
| 88 | + return ( |
| 89 | + <EnvEntry component="span" key={envVar.name}> |
| 90 | + {envVar.name}:{' '} |
| 91 | + <Link to={secretUrl} style={{ textDecoration: 'underline', fontWeight: 'bold' }}> |
| 92 | + Secret: {secretName} {secretKey ? `(Key: ${secretKey})` : ''} |
| 93 | + </Link> |
| 94 | + </EnvEntry> |
| 95 | + ); |
| 96 | + } |
| 97 | + |
| 98 | + // Config Map: |
| 99 | + if (envVar.valueFrom?.configMapKeyRef) { |
| 100 | + const { name: cmName, key: cmKey } = envVar.valueFrom.configMapKeyRef; |
| 101 | + |
| 102 | + if (!cmName) { |
| 103 | + return ( |
| 104 | + <EnvEntry component="span" key={envVar.name}> |
| 105 | + {envVar.name}: {t('translation|Invalid Config Map Reference')} |
| 106 | + </EnvEntry> |
| 107 | + ); |
| 108 | + } |
| 109 | + |
| 110 | + const configMapUrl = `/c/${encodeURIComponent(cluster)}/configmaps/${encodeURIComponent( |
| 111 | + namespace |
| 112 | + )}/${encodeURIComponent(cmName)}`; |
| 113 | + return ( |
| 114 | + <EnvEntry component="span" key={envVar.name}> |
| 115 | + {envVar.name}:{' '} |
| 116 | + <Link to={configMapUrl} style={{ textDecoration: 'underline', fontWeight: 'bold' }}> |
| 117 | + ConfigMap: {cmName} {cmKey ? `(Key: ${cmKey})` : ''} |
| 118 | + </Link> |
| 119 | + </EnvEntry> |
| 120 | + ); |
| 121 | + } |
| 122 | + |
| 123 | + // FieldRef: |
| 124 | + if (envVar.valueFrom?.fieldRef) { |
| 125 | + const { fieldPath } = envVar.valueFrom.fieldRef; |
| 126 | + return ( |
| 127 | + <EnvEntry component="span" key={envVar.name}> |
| 128 | + {envVar.name}: FieldRef {fieldPath ? `(${fieldPath})` : t('translation|Invalid fieldRef')} |
| 129 | + </EnvEntry> |
| 130 | + ); |
| 131 | + } |
| 132 | + |
| 133 | + // ResourceFieldRef: |
| 134 | + if (envVar.valueFrom?.resourceFieldRef) { |
| 135 | + const { resource } = envVar.valueFrom.resourceFieldRef; |
| 136 | + return ( |
| 137 | + <EnvEntry component="span" key={envVar.name}> |
| 138 | + {envVar.name}: ResourceField{' '} |
| 139 | + {resource ? `(${resource})` : t('translation|Invalid resourceFieldRef')} |
| 140 | + </EnvEntry> |
| 141 | + ); |
| 142 | + } |
| 143 | + |
| 144 | + // Plaintext |
| 145 | + return ( |
| 146 | + <EnvEntry component="span" key={envVar.name}> |
| 147 | + {envVar.name}: {(envVar.value || '').trim()} |
| 148 | + </EnvEntry> |
| 149 | + ); |
| 150 | + }; |
| 151 | + |
| 152 | + return ( |
| 153 | + <Box> |
| 154 | + <Box sx={{ display: 'flex', flexWrap: 'wrap', gap: 0.5 }}> |
| 155 | + {validEnvVars |
| 156 | + .slice(0, expanded ? validEnvVars.length : defaultNumShown) |
| 157 | + .map(env => renderEnvVar(env))} |
| 158 | + </Box> |
| 159 | + {validEnvVars.length > defaultNumShown && ( |
| 160 | + <Button |
| 161 | + onClick={() => setExpanded(!expanded)} |
| 162 | + size="small" |
| 163 | + aria-expanded={expanded} |
| 164 | + aria-label={ |
| 165 | + expanded ? t('translation|Show fewer') : t('translation|Show all environment variables') |
| 166 | + } |
| 167 | + startIcon={<Icon icon={expanded ? 'mdi:menu-up' : 'mdi:menu-down'} />} |
| 168 | + sx={{ mt: 1, mb: 1 }} |
| 169 | + > |
| 170 | + {!expanded |
| 171 | + ? t('translation|Show all environment variables (+{{count}} more)', { |
| 172 | + count: validEnvVars.length - defaultNumShown, |
| 173 | + }) |
| 174 | + : t('translation|Show fewer')} |
| 175 | + </Button> |
| 176 | + )} |
| 177 | + </Box> |
| 178 | + ); |
| 179 | +} |
0 commit comments