From bd5ba7b5d0eb4b7c10015466b63637145044242e Mon Sep 17 00:00:00 2001
From: Prabhat Khera <91852476+prabhat-org@users.noreply.github.com>
Date: Wed, 6 Dec 2023 16:02:26 +1300
Subject: [PATCH] fix(kube): configmaps and secrets from envFrom in the app
detail screen [EE-6282] (#10742)
* fix configmaps and secrets from envFrom
* adress review comments
---
.../ApplicationEnvVarsTable.tsx | 124 +++++++++++-------
.../applications/application.service.ts | 7 +-
.../kubernetes/applications/pod.service.ts | 10 +-
.../ListView/ConfigMapsDatatable/utils.ts | 17 ++-
.../ListView/SecretsDatatable/utils.ts | 17 ++-
5 files changed, 111 insertions(+), 64 deletions(-)
diff --git a/app/react/kubernetes/applications/DetailsView/ApplicationDetailsWidget/ApplicationEnvVarsTable.tsx b/app/react/kubernetes/applications/DetailsView/ApplicationDetailsWidget/ApplicationEnvVarsTable.tsx
index 5c2ac907e..19ff4967f 100644
--- a/app/react/kubernetes/applications/DetailsView/ApplicationDetailsWidget/ApplicationEnvVarsTable.tsx
+++ b/app/react/kubernetes/applications/DetailsView/ApplicationDetailsWidget/ApplicationEnvVarsTable.tsx
@@ -1,4 +1,4 @@
-import { EnvVar, Pod } from 'kubernetes-types/core/v1';
+import { Pod } from 'kubernetes-types/core/v1';
import { Asterisk, File, FileCode, Key, Lock } from 'lucide-react';
import { Icon } from '@@/Icon';
@@ -44,7 +44,7 @@ export function ApplicationEnvVarsTable({ namespace, app }: Props) {
{envVar.isInitContainer && (
- {envVar.valueFrom?.fieldRef?.fieldPath} (
+ {envVar.fieldPath} (
)}
-
{envVar.name} |
+ {envVar.key || '-'} |
{envVar.value && {envVar.value}}
- {envVar.valueFrom?.fieldRef?.fieldPath && (
+ {envVar.fieldPath && (
- {envVar.valueFrom.fieldRef.fieldPath} (
+ {envVar.fieldPath} (
)}
- {envVar.valueFrom?.secretKeyRef?.key && (
+ {envVar.key ? (
- {envVar.valueFrom.secretKeyRef.key}
+ {envVar.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.resourseName && -}
+ {envVar.resourseName && (
-
- {envVar.valueFrom.secretKeyRef.name}
+
+ {envVar.resourseName}
)}
@@ -130,9 +116,14 @@ export function ApplicationEnvVarsTable({ namespace, app }: Props) {
);
}
-interface ContainerEnvVar extends EnvVar {
+interface ContainerEnvVar {
+ key?: string;
+ value?: string;
+ fieldPath?: string;
containerName: string;
isInitContainer: boolean;
+ type: 'configMap' | 'secret';
+ resourseName: string;
}
function getApplicationEnvironmentVariables(
@@ -150,23 +141,60 @@ function getApplicationEnvironmentVariables(
// get all the environment variables for each container
const appContainersEnvVars =
- appContainers?.flatMap(
- (container) =>
+ appContainers?.flatMap((container) => {
+ const containerEnvVars: ContainerEnvVar[] =
container?.env?.map((envVar) => ({
- ...envVar,
+ key: envVar?.name,
+ fieldPath: envVar?.valueFrom?.fieldRef?.fieldPath,
+ containerName: container.name,
+ isInitContainer: false,
+ type: envVar?.valueFrom?.configMapKeyRef ? 'configMap' : 'secret',
+ resourseName:
+ envVar?.valueFrom?.configMapKeyRef?.name ||
+ envVar?.valueFrom?.secretKeyRef?.name ||
+ '',
+ })) || [];
+
+ 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) =>
+ appInitContainers?.flatMap((container) => {
+ const containerEnvVars: ContainerEnvVar[] =
container?.env?.map((envVar) => ({
- ...envVar,
+ key: envVar?.name,
+ fieldPath: envVar?.valueFrom?.fieldRef?.fieldPath,
+ containerName: container.name,
+ isInitContainer: false,
+ type: envVar?.valueFrom?.configMapKeyRef ? 'configMap' : 'secret',
+ resourseName:
+ envVar?.valueFrom?.configMapKeyRef?.name ||
+ envVar?.valueFrom?.secretKeyRef?.name ||
+ '',
+ })) || [];
+
+ const containerEnvFroms: ContainerEnvVar[] =
+ container?.envFrom?.map((envFrom) => ({
+ name: '',
+ resourseName:
+ envFrom?.configMapRef?.name || envFrom?.secretRef?.name || '',
containerName: container.name,
- isInitContainer: true,
- })) || []
- ) || [];
+ isInitContainer: false,
+ type: envFrom?.configMapRef ? 'configMap' : 'secret',
+ })) || [];
+
+ return [...containerEnvVars, ...containerEnvFroms];
+ }) || [];
return [...appContainersEnvVars, ...appInitContainersEnvVars];
}
diff --git a/app/react/kubernetes/applications/application.service.ts b/app/react/kubernetes/applications/application.service.ts
index 8fb2c1909..555c815bb 100644
--- a/app/react/kubernetes/applications/application.service.ts
+++ b/app/react/kubernetes/applications/application.service.ts
@@ -233,7 +233,12 @@ async function getApplicationsByKind(
const { data } = await axios.get(
buildUrl(environmentId, namespace, `${appKind}s`)
);
- return data.items as T['items'];
+ const items = (data.items || []).map((app) => ({
+ ...app,
+ kind: appKind,
+ apiVersion: data.apiVersion,
+ }));
+ return items as T['items'];
} catch (e) {
throw parseKubernetesAxiosError(
e,
diff --git a/app/react/kubernetes/applications/pod.service.ts b/app/react/kubernetes/applications/pod.service.ts
index fde6248b8..5e99665b1 100644
--- a/app/react/kubernetes/applications/pod.service.ts
+++ b/app/react/kubernetes/applications/pod.service.ts
@@ -21,7 +21,15 @@ export async function getNamespacePods(
},
}
);
- return data.items;
+ const items = (data.items || []).map(
+ (pod) =>
+ {
+ ...pod,
+ kind: 'Pod',
+ apiVersion: data.apiVersion,
+ }
+ );
+ return items;
} catch (e) {
throw parseKubernetesAxiosError(
e,
diff --git a/app/react/kubernetes/configs/ListView/ConfigMapsDatatable/utils.ts b/app/react/kubernetes/configs/ListView/ConfigMapsDatatable/utils.ts
index 8d829bbb9..343b89278 100644
--- a/app/react/kubernetes/configs/ListView/ConfigMapsDatatable/utils.ts
+++ b/app/react/kubernetes/configs/ListView/ConfigMapsDatatable/utils.ts
@@ -14,13 +14,16 @@ export function getIsConfigMapInUse(
? app?.spec
: app?.spec?.template?.spec;
- const hasEnvVarReference = appSpec?.containers.some(
- (container) =>
- container.env?.some(
- (envVar) =>
- envVar.valueFrom?.configMapKeyRef?.name === configMap.metadata?.name
- )
- );
+ const hasEnvVarReference = appSpec?.containers.some((container) => {
+ const valueFromEnv = container.env?.some(
+ (envVar) =>
+ envVar.valueFrom?.configMapKeyRef?.name === configMap.metadata?.name
+ );
+ const envFromEnv = container.envFrom?.some(
+ (envVar) => envVar.configMapRef?.name === configMap.metadata?.name
+ );
+ return valueFromEnv || envFromEnv;
+ });
const hasVolumeReference = appSpec?.volumes?.some(
(volume) => volume.configMap?.name === configMap.metadata?.name
);
diff --git a/app/react/kubernetes/configs/ListView/SecretsDatatable/utils.ts b/app/react/kubernetes/configs/ListView/SecretsDatatable/utils.ts
index be8182ba3..b6d1d015d 100644
--- a/app/react/kubernetes/configs/ListView/SecretsDatatable/utils.ts
+++ b/app/react/kubernetes/configs/ListView/SecretsDatatable/utils.ts
@@ -11,13 +11,16 @@ export function getIsSecretInUse(secret: Secret, applications: Application[]) {
? app?.spec
: app?.spec?.template?.spec;
- const hasEnvVarReference = appSpec?.containers.some(
- (container) =>
- container.env?.some(
- (envVar) =>
- envVar.valueFrom?.secretKeyRef?.name === secret.metadata?.name
- )
- );
+ const hasEnvVarReference = appSpec?.containers.some((container) => {
+ const valueFromEnv = container.env?.some(
+ (envVar) =>
+ envVar.valueFrom?.secretKeyRef?.name === secret.metadata?.name
+ );
+ const envFromEnv = container.envFrom?.some(
+ (envVar) => envVar.secretRef?.name === secret.metadata?.name
+ );
+ return valueFromEnv || envFromEnv;
+ });
const hasVolumeReference = appSpec?.volumes?.some(
(volume) => volume.secret?.secretName === secret.metadata?.name
);
|