import { EnvVar, 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 (
<>
Configuration
{appEnvVars.length === 0 && (
This application is not using any environment variable or
configuration.
)}
{appEnvVars.length > 0 && (
Container |
Environment variable |
Value |
Configuration |
{appEnvVars.map((envVar, index) => (
{envVar.containerName}
{envVar.isInitContainer && (
{envVar.valueFrom?.fieldRef?.fieldPath} (
init container
)
)}
|
{envVar.name} |
{envVar.value && {envVar.value}}
{envVar.valueFrom?.fieldRef?.fieldPath && (
{envVar.valueFrom.fieldRef.fieldPath} (
downward API
)
)}
{envVar.valueFrom?.secretKeyRef?.key && (
{envVar.valueFrom.secretKeyRef.key}
)}
{envVar.valueFrom?.configMapKeyRef?.key && (
{envVar.valueFrom.configMapKeyRef.key}
)}
{!envVar.value && !envVar.valueFrom && -}
|
{!envVar.valueFrom?.configMapKeyRef?.name &&
!envVar.valueFrom?.secretKeyRef?.name && -}
{envVar.valueFrom?.configMapKeyRef && (
{envVar.valueFrom.configMapKeyRef.name}
)}
{envVar.valueFrom?.secretKeyRef && (
{envVar.valueFrom.secretKeyRef.name}
)}
|
))}
)}
>
);
}
interface ContainerEnvVar extends EnvVar {
containerName: string;
isInitContainer: boolean;
}
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) =>
container?.env?.map((envVar) => ({
...envVar,
containerName: container.name,
isInitContainer: false,
})) || []
) || [];
const appInitContainersEnvVars =
appInitContainers?.flatMap(
(container) =>
container?.env?.map((envVar) => ({
...envVar,
containerName: container.name,
isInitContainer: true,
})) || []
) || [];
return [...appContainersEnvVars, ...appInitContainersEnvVars];
}