Fix envFrom.prefix not being applied in container's env view (#5434)

This commit is contained in:
Sebastian Malton authored and GitHub committed 2022-09-02 13:49:53 -04:00
1 parent 6ef284560a
commit bab1b13317
38 files changed
+891 -248

No files matched your search

@@ -47,8 +47,8 @@ const dummyDeployment = new Deployment({
},
},
terminationMessagePath: "dummy",
terminationMessagePolicy: "dummy",
imagePullPolicy: "dummy",
terminationMessagePolicy: "File",
imagePullPolicy: "Always",
}],
restartPolicy: "dummy",
terminationGracePeriodSeconds: 10,
@@ -7,26 +7,39 @@ import "./pod-container-env.scss";
import React, { useEffect, useState } from "react";
import { observer } from "mobx-react";
import type { PodContainer, Secret } from "../../../common/k8s-api/endpoints";
import type { Container, EnvVarKeySelector, Secret } from "../../../common/k8s-api/endpoints";
import { DrawerItem } from "../drawer";
import { autorun } from "mobx";
import { secretStore } from "../+config-secrets/legacy-store";
import { configMapStore } from "../+config-maps/legacy-store";
import { Icon } from "../icon";
import { base64, cssNames, iter } from "../../utils";
import { base64, cssNames, object } from "../../utils";
import _ from "lodash";
import { withInjectables } from "@ogre-tools/injectable-react";
import type { ConfigMapStore } from "../+config-maps/store";
import type { SecretStore } from "../+config-secrets/store";
import configMapStoreInjectable from "../+config-maps/store.injectable";
import secretStoreInjectable from "../+config-secrets/store.injectable";
export interface ContainerEnvironmentProps {
container: PodContainer;
container: Container;
namespace: string;
}
export const ContainerEnvironment = observer((props: ContainerEnvironmentProps) => {
const { container: { env, envFrom = [] }, namespace } = props;
interface Dependencies {
configMapStore: ConfigMapStore;
secretStore: SecretStore;
}
const NonInjectedContainerEnvironment = observer((props: Dependencies & ContainerEnvironmentProps) => {
const {
container: { env, envFrom = [] },
namespace,
configMapStore,
secretStore,
} = props;
useEffect( () => autorun(() => {
for (const { valueFrom } of env ?? []) {
if (valueFrom?.configMapKeyRef) {
if (valueFrom?.configMapKeyRef?.name) {
configMapStore.load({ name: valueFrom.configMapKeyRef.name, namespace });
}
}
@@ -51,27 +64,22 @@ export const ContainerEnvironment = observer((props: ContainerEnvironmentProps)
if (value) {
secretValue = value;
}
if (valueFrom) {
} else if (valueFrom) {
const { fieldRef, secretKeyRef, configMapKeyRef } = valueFrom;
if (fieldRef) {
const { apiVersion, fieldPath } = fieldRef;
secretValue = `fieldRef(${apiVersion}:${fieldPath})`;
}
if (secretKeyRef) {
} else if (secretKeyRef?.name) {
secretValue = (
<SecretKey
reference={secretKeyRef}
namespace={namespace}
secretStore={secretStore}
/>
);
}
if (configMapKeyRef) {
} else if (configMapKeyRef?.name) {
const { name, key } = configMapKeyRef;
const configMap = configMapStore.getByName(name, namespace);
@@ -91,60 +99,61 @@ export const ContainerEnvironment = observer((props: ContainerEnvironmentProps)
});
};
const renderEnvFrom = () => {
return Array.from(iter.filterFlatMap(envFrom, vars => {
if (vars.configMapRef?.name) {
return renderEnvFromConfigMap(vars.configMapRef.name);
}
const renderEnvFrom = () => (
envFrom
.flatMap(({ configMapRef, secretRef, prefix }) => {
if (configMapRef?.name) {
return renderEnvFromConfigMap(configMapRef.name, prefix);
}
if (vars.secretRef?.name) {
return renderEnvFromSecret(vars.secretRef.name);
}
if (secretRef?.name) {
return renderEnvFromSecret(secretRef.name, prefix);
}
return null;
}));
};
return null;
})
);
const renderEnvFromConfigMap = (configMapName: string) => {
const renderEnvFromConfigMap = (configMapName: string, prefix: string | undefined) => {
const configMap = configMapStore.getByName(configMapName, namespace);
if (!configMap) return null;
return Object.entries(configMap.data).map(([name, value]) => (
<div className="variable" key={name}>
<span className="var-name">{name}</span>
:
{value}
</div>
));
return object.entries(configMap.data)
.map(([name, value]) => (
<div className="variable" key={name}>
<span className="var-name">
{prefix}
{name}
</span>
:
{value}
</div>
));
};
const renderEnvFromSecret = (secretName: string) => {
const renderEnvFromSecret = (secretName: string, prefix: string | undefined) => {
const secret = secretStore.getByName(secretName, namespace);
if (!secret) return null;
return Object.keys(secret.data).map(key => {
const secretKeyRef = {
name: secret.getName(),
key,
};
const value = (
<SecretKey
reference={secretKeyRef}
namespace={namespace}
/>
);
return (
return Object.keys(secret.data)
.map(key => (
<div className="variable" key={key}>
<span className="var-name">{key}</span>
<span className="var-name">
{prefix}
{key}
</span>
:
{value}
<SecretKey
reference={{
name: secret.getName(),
key,
}}
namespace={namespace}
secretStore={secretStore} />
</div>
);
});
));
};
return (
@@ -155,19 +164,34 @@ export const ContainerEnvironment = observer((props: ContainerEnvironmentProps)
);
});
export interface SecretKeyProps {
reference: {
name: string;
key: string;
};
export const ContainerEnvironment = withInjectables<Dependencies, ContainerEnvironmentProps>(NonInjectedContainerEnvironment, {
getProps: (di, props) => ({
...props,
configMapStore: di.inject(configMapStoreInjectable),
secretStore: di.inject(secretStoreInjectable),
}),
});
interface SecretKeyProps {
reference: EnvVarKeySelector;
namespace: string;
secretStore: SecretStore;
}
const SecretKey = (props: SecretKeyProps) => {
const { reference: { name, key }, namespace } = props;
const {
reference: { name, key },
namespace,
secretStore,
} = props;
const [loading, setLoading] = useState(false);
const [secret, setSecret] = useState<Secret>();
if (!name) {
return null;
}
const showKey = async (evt: React.MouseEvent) => {
evt.preventDefault();
setLoading(true);
@@ -6,7 +6,7 @@
import "./pod-details-container.scss";
import React from "react";
import type { PodContainer, PodContainerStatus, Pod } from "../../../common/k8s-api/endpoints";
import type { Container, PodContainerStatus, Pod } from "../../../common/k8s-api/endpoints";
import { DrawerItem } from "../drawer";
import { cssNames, isDefined } from "../../utils";
import { StatusBrick } from "../status-brick";
@@ -27,7 +27,7 @@ import getActiveClusterEntityInjectable from "../../api/catalog/entity/get-activ
export interface PodDetailsContainerProps {
pod: Pod;
container: PodContainer;
container: Container;
metrics?: Partial<Record<string, MetricData>>;
}
@@ -43,12 +43,12 @@ const dummyReplicaSet = new ReplicaSet({
containers: [{
name: "dummy",
image: "dummy",
imagePullPolicy: "dummy",
imagePullPolicy: "Always",
}],
initContainers: [{
name: "dummy",
image: "dummy",
imagePullPolicy: "dummy",
imagePullPolicy: "Always",
}],
priority: 1,
serviceAccountName: "dummy",
@@ -23,8 +23,8 @@ const spec: PodSpec = {
},
},
terminationMessagePath: "test",
terminationMessagePolicy: "test",
imagePullPolicy: "test",
terminationMessagePolicy: "File",
imagePullPolicy: "Always",
}],
restartPolicy: "restart",
terminationGracePeriodSeconds: 1200,
@@ -22,7 +22,7 @@ export const dockerPod = new Pod({
{
name: "docker-exporter",
image: "docker.io/prom/node-exporter:v1.0.0-rc.0",
imagePullPolicy: "pull",
imagePullPolicy: "IfNotPresent",
},
],
serviceAccountName: "dummy",
@@ -66,24 +66,24 @@ export const deploymentPod1 = new Pod({
{
name: "init-node-exporter",
image: "docker.io/prom/node-exporter:v1.0.0-rc.0",
imagePullPolicy: "pull",
imagePullPolicy: "IfNotPresent",
},
{
name: "init-node-exporter-1",
image: "docker.io/prom/node-exporter:v1.0.0-rc.0",
imagePullPolicy: "pull",
imagePullPolicy: "IfNotPresent",
},
],
containers: [
{
name: "node-exporter",
image: "docker.io/prom/node-exporter:v1.0.0-rc.0",
imagePullPolicy: "pull",
imagePullPolicy: "IfNotPresent",
},
{
name: "node-exporter-1",
image: "docker.io/prom/node-exporter:v1.0.0-rc.0",
imagePullPolicy: "pull",
imagePullPolicy: "IfNotPresent",
},
],
serviceAccountName: "dummy",
@@ -127,24 +127,24 @@ export const deploymentPod2 = new Pod({
{
name: "init-node-exporter",
image: "docker.io/prom/node-exporter:v1.0.0-rc.0",
imagePullPolicy: "pull",
imagePullPolicy: "IfNotPresent",
},
{
name: "init-node-exporter-1",
image: "docker.io/prom/node-exporter:v1.0.0-rc.0",
imagePullPolicy: "pull",
imagePullPolicy: "IfNotPresent",
},
],
containers: [
{
name: "node-exporter",
image: "docker.io/prom/node-exporter:v1.0.0-rc.0",
imagePullPolicy: "pull",
imagePullPolicy: "IfNotPresent",
},
{
name: "node-exporter-1",
image: "docker.io/prom/node-exporter:v1.0.0-rc.0",
imagePullPolicy: "pull",
imagePullPolicy: "IfNotPresent",
},
],
serviceAccountName: "dummy",
@@ -188,12 +188,12 @@ export const deploymentPod3 = new Pod({
{
name: "node-exporter",
image: "docker.io/prom/node-exporter:v1.0.0-rc.0",
imagePullPolicy: "pull",
imagePullPolicy: "IfNotPresent",
},
{
name: "node-exporter-1",
image: "docker.io/prom/node-exporter:v1.0.0-rc.0",
imagePullPolicy: "pull",
imagePullPolicy: "IfNotPresent",
},
],
serviceAccountName: "dummy",
@@ -3,13 +3,13 @@
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectable } from "@ogre-tools/injectable";
import type { PodContainer, Pod } from "../../../../common/k8s-api/endpoints";
import type { Container, Pod } from "../../../../common/k8s-api/endpoints";
import type { TabId } from "../dock/store";
import createLogsTabInjectable from "./create-logs-tab.injectable";
export interface PodLogsTabData {
selectedPod: Pod;
selectedContainer: PodContainer;
selectedContainer: Container;
}
const createPodLogsTabInjectable = getInjectable({
@@ -12,7 +12,7 @@ import { Badge } from "../../badge";
import type { SelectOption } from "../../select";
import { Select } from "../../select";
import type { LogTabViewModel } from "./logs-view-model";
import type { PodContainer, Pod } from "../../../../common/k8s-api/endpoints";
import type { Container, Pod } from "../../../../common/k8s-api/endpoints";
import type { SingleValue } from "react-select";
export interface LogResourceSelectorProps {
@@ -40,7 +40,7 @@ export const LogResourceSelector = observer(({ model }: LogResourceSelectorProps
}));
const allContainers = pod.getAllContainers();
const container = allContainers.find(container => container.name === selectedContainer) ?? null;
const onContainerChange = (option: SingleValue<SelectOption<PodContainer>>) => {
const onContainerChange = (option: SingleValue<SelectOption<Container>>) => {
if (!option) {
return;
}
@@ -105,7 +105,7 @@ export const LogResourceSelector = observer(({ model }: LogResourceSelectorProps
menuClass="pod-selector-menu"
/>
<span>Container</span>
<Select<PodContainer, SelectOption<PodContainer>, false>
<Select<Container, SelectOption<Container>, false>
id="container-selector-input"
options={containerSelectOptions}
value={container}