import { Pod } from 'kubernetes-types/core/v1';
import { Asterisk, File, FileCode, Key, Lock } from 'lucide-react';
import { Icon } from '@@/Icon';
import { TextTip } from '@@/Tip/TextTip';
import { Link } from '@@/Link';
import { Application } from '../../types';
import { applicationIsKind } from '../../utils';
type Props = {
namespace: string;
app?: Application;
};
export function ApplicationEnvVarsTable({ namespace, app }: Props) {
const appEnvVars = getApplicationEnvironmentVariables(app);
return (
<>
Environment variables, ConfigMaps or Secrets
{appEnvVars.length === 0 && (
This application is not using any environment variable, ConfigMap or
Secret.
)}
{appEnvVars.length > 0 && (
Container |
Environment variable |
Value |
Configuration |
{appEnvVars.map((envVar, index) => (
{envVar.containerName}
{envVar.isInitContainer && (
{envVar.fieldPath} (
init container
)
)}
|
{envVar.key || '-'} |
{envVar.value && {envVar.value}}
{envVar.fieldPath && (
{envVar.fieldPath} (
downward API
)
)}
{envVar.type !== 'env' &&
(envVar.key ? (
{envVar.key}
) : (
'-'
))}
|
{!envVar.resourseName && -}
{envVar.resourseName && (
{envVar.resourseName}
)}
|
))}
)}
>
);
}
type EnvVarType = 'env' | 'configMap' | 'secret';
interface ContainerEnvVar {
key?: string;
value?: string;
fieldPath?: string;
containerName: string;
isInitContainer: boolean;
type: EnvVarType;
resourseName: string;
}
function getApplicationEnvironmentVariables(
app?: Application
): ContainerEnvVar[] {
if (!app) {
return [];
}
const podSpec = applicationIsKind('Pod', app)
? app.spec
: app.spec?.template?.spec;
const appContainers = podSpec?.containers || [];
const appInitContainers = podSpec?.initContainers || [];
// get all the environment variables for each container
const appContainersEnvVars =
appContainers?.flatMap((container) => {
const containerEnvVars: ContainerEnvVar[] =
container?.env?.map((envVar) => {
let envtype: EnvVarType = 'env';
if (envVar?.valueFrom?.configMapKeyRef) {
envtype = 'configMap';
} else if (envVar?.valueFrom?.secretKeyRef) {
envtype = 'secret';
}
return {
key: envVar?.name,
fieldPath: envVar?.valueFrom?.fieldRef?.fieldPath,
containerName: container.name,
isInitContainer: false,
type: envtype,
resourseName:
envVar?.valueFrom?.configMapKeyRef?.name ||
envVar?.valueFrom?.secretKeyRef?.name ||
'',
value: envVar?.value,
};
}) || [];
const containerEnvFroms: ContainerEnvVar[] =
container?.envFrom?.map((envFrom) => ({
name: '',
resourseName:
envFrom?.configMapRef?.name || envFrom?.secretRef?.name || '',
containerName: container.name,
isInitContainer: false,
type: envFrom?.configMapRef ? 'configMap' : 'secret',
})) || [];
return [...containerEnvVars, ...containerEnvFroms];
}) || [];
const appInitContainersEnvVars =
appInitContainers?.flatMap((container) => {
const containerEnvVars: ContainerEnvVar[] =
container?.env?.map((envVar) => {
let envtype: EnvVarType = 'env';
if (envVar?.valueFrom?.configMapKeyRef) {
envtype = 'configMap';
} else if (envVar?.valueFrom?.secretKeyRef) {
envtype = 'secret';
}
return {
key: envVar?.name,
fieldPath: envVar?.valueFrom?.fieldRef?.fieldPath,
containerName: container.name,
isInitContainer: true,
type: envtype,
resourseName:
envVar?.valueFrom?.configMapKeyRef?.name ||
envVar?.valueFrom?.secretKeyRef?.name ||
'',
value: envVar?.value,
};
}) || [];
const containerEnvFroms: ContainerEnvVar[] =
container?.envFrom?.map((envFrom) => ({
name: '',
resourseName:
envFrom?.configMapRef?.name || envFrom?.secretRef?.name || '',
containerName: container.name,
isInitContainer: true,
type: envFrom?.configMapRef ? 'configMap' : 'secret',
})) || [];
return [...containerEnvVars, ...containerEnvFroms];
}) || [];
return [...appContainersEnvVars, ...appInitContainersEnvVars];
}