Skip to content

Commit

Permalink
fix: prevent deletion of active connection secrets when updating note…
Browse files Browse the repository at this point in the history
…book environment variables (#3445)
  • Loading branch information
Gkrumbach07 authored Nov 7, 2024
1 parent 91584ea commit 01d0519
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ const SpawnerFooter: React.FC<SpawnerFooterProps> = ({
envVariables,
dataConnection,
existingNotebookDataConnection,
connections,
dryRun,
);

Expand Down
9 changes: 6 additions & 3 deletions frontend/src/pages/projects/screens/spawner/SpawnerPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,6 @@ const SpawnerPage: React.FC<SpawnerPageProps> = ({ existingNotebook }) => {
defaultStorageClassName,
);

const [envVariables, setEnvVariables, envVariablesLoaded, deletedConfigMaps, deletedSecrets] =
useNotebookEnvVariables(existingNotebook);

const [dataConnectionData, setDataConnectionData] = useNotebookDataConnection(
dataConnections.data,
existingNotebook,
Expand All @@ -103,6 +100,12 @@ const SpawnerPage: React.FC<SpawnerPageProps> = ({ existingNotebook }) => {
: [],
);

const [envVariables, setEnvVariables, envVariablesLoaded, deletedConfigMaps, deletedSecrets] =
useNotebookEnvVariables(existingNotebook, [
...notebookConnections.map((connection) => connection.metadata.name),
dataConnectionData.existing?.secretRef.name || '',
]);

const restartNotebooks = useWillNotebooksRestart([existingNotebook?.metadata.name || '']);

const [data, loaded, loadError] = useNotebookImageData(existingNotebook);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ export const fetchNotebookEnvVariables = (notebook: NotebookKind): Promise<EnvVa

export const useNotebookEnvVariables = (
notebook?: NotebookKind,
excludedResources: string[] = [],
): [
envVariables: EnvVariable[],
setEnvVariables: (envVars: EnvVariable[]) => void,
Expand All @@ -96,6 +97,7 @@ export const useNotebookEnvVariables = (
const { deletedConfigMaps, deletedSecrets } = getDeletedConfigMapOrSecretVariables(
notebook,
existingEnvVariables,
excludedResources,
);
React.useEffect(() => {
setEnvVariables(existingEnvVariables);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export const isStringKeyValuePairObject = (object: unknown): object is Record<st
export const getDeletedConfigMapOrSecretVariables = (
notebook: NotebookKind | undefined,
envVariables: EnvVariable[],
excludedResources: string[] = [],
): {
deletedSecrets: string[];
deletedConfigMaps: string[];
Expand All @@ -32,10 +33,18 @@ export const getDeletedConfigMapOrSecretVariables = (
const deletedSecrets: string[] = [];

(notebook?.spec.template.spec.containers[0].envFrom || []).forEach((env) => {
if (env.configMapRef && !existingEnvVariables.has(env.configMapRef.name)) {
if (
env.configMapRef &&
!existingEnvVariables.has(env.configMapRef.name) &&
!excludedResources.includes(env.configMapRef.name)
) {
deletedConfigMaps.push(env.configMapRef.name);
}
if (env.secretRef && !existingEnvVariables.has(env.secretRef.name)) {
if (
env.secretRef &&
!existingEnvVariables.has(env.secretRef.name) &&
!excludedResources.includes(env.secretRef.name)
) {
deletedSecrets.push(env.secretRef.name);
}
});
Expand Down
6 changes: 6 additions & 0 deletions frontend/src/pages/projects/screens/spawner/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
} from '~/pages/projects/types';
import { ROOT_MOUNT_PATH } from '~/pages/projects/pvc/const';
import { ConfigMapKind, NotebookKind, SecretKind } from '~/k8sTypes';
import { Connection } from '~/concepts/connectionTypes/types';
import { getVolumesByStorageData } from './spawnerUtils';
import { fetchNotebookEnvVariables } from './environmentVariables/useNotebookEnvVariables';
import { getDeletedConfigMapOrSecretVariables } from './environmentVariables/utils';
Expand Down Expand Up @@ -194,12 +195,17 @@ export const updateConfigMapsAndSecretsForNotebook = async (
envVariables: EnvVariable[],
dataConnection?: DataConnectionData,
existingDataConnection?: DataConnection,
connections?: Connection[],
dryRun = false,
): Promise<EnvironmentFromVariable[]> => {
const existingEnvVars = await fetchNotebookEnvVariables(notebook);
const { deletedConfigMaps, deletedSecrets } = getDeletedConfigMapOrSecretVariables(
notebook,
existingEnvVars,
[
existingDataConnection?.data.metadata.name || '',
...(connections || []).map((connection) => connection.metadata.name),
],
);
const newDataConnection =
dataConnection?.enabled && dataConnection.type === 'creating' && dataConnection.creating
Expand Down

0 comments on commit 01d0519

Please sign in to comment.