Skip to content

Commit

Permalink
fix: improved handling of file updates and automatic termination of k…
Browse files Browse the repository at this point in the history
…ustomize dry-runs
  • Loading branch information
olensmar committed Dec 7, 2023
1 parent 8c216e9 commit e04ff9c
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ const DryRunsPane: React.FC = () => {
return false;
});

rowVirtualizer.scrollToIndex(index);
if (index >= 0) {
rowVirtualizer.scrollToIndex(index);
}
}, [preview, list, rowVirtualizer]);

if (isLoading) {
Expand Down
2 changes: 1 addition & 1 deletion src/redux/selectors/dryRunsSelectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ export const dryRunLabelSelector = createSelector(

if (preview.type === 'kustomize') {
const resource = localResourceMetaMap[preview.kustomizationId];
return basename(resource.name);
return resource ? basename(resource.name) : 'Kustomize Overlay';
}

if (preview.type === 'helm') {
Expand Down
3 changes: 3 additions & 0 deletions src/redux/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import {Middleware, combineReducers, configureStore, createAction} from '@reduxj

import {createLogger} from 'redux-logger';

import {shouldStopKustomizeDryRunListener} from '@redux/thunks/listeners/shouldStopKustomizeDryRunListener';

import {editorSlice} from '@editor/editor.slice';
import {editorListeners} from '@editor/listeners';

Expand Down Expand Up @@ -55,6 +57,7 @@ combineListeners([
...appConfigListeners,
...clusterListeners,
imageListParserListener,
shouldStopKustomizeDryRunListener,
]);

const appReducer = combineReducers({
Expand Down
25 changes: 25 additions & 0 deletions src/redux/thunks/listeners/shouldStopKustomizeDryRunListener.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import {isAnyOf} from '@reduxjs/toolkit';

import {AppListenerFn} from '@redux/listeners/base';
import {multiplePathsChanged} from '@redux/thunks/multiplePathsChanged';
import {stopPreview} from '@redux/thunks/preview';
import {updateFileEntry} from '@redux/thunks/updateFileEntry';

export const shouldStopKustomizeDryRunListener: AppListenerFn = listen => {
listen({
matcher: isAnyOf(updateFileEntry.fulfilled, multiplePathsChanged.fulfilled),
effect: (action, listenerApi) => {
listenerApi.cancelActiveListeners();

const mainState = listenerApi.getState().main;

// check if the update resulted in the removal of the currently dry-run kustomize overlay
if (
mainState.preview?.type === 'kustomize' &&
!mainState.resourceMetaMapByStorage.local[mainState.preview.kustomizationId]
) {
listenerApi.dispatch(stopPreview());
}
},
});
};
24 changes: 8 additions & 16 deletions src/redux/thunks/updateFileEntry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {HELM_CHART_ENTRY_FILE} from '@constants/constants';
import {UpdateFileEntryPayload} from '@redux/reducers/main';
import {getLocalResourceMetasForPath} from '@redux/services/fileEntry';
import {reprocessHelm} from '@redux/services/helm';
import {isKustomizationFile} from '@redux/services/kustomize';
import {deleteResource, extractK8sResources, splitK8sResource} from '@redux/services/resource';

import {getFileStats, getFileTimestamp} from '@utils/files';
Expand Down Expand Up @@ -89,8 +88,14 @@ export const updateFileEntry = createAsyncThunk<
});

const newHighlights: AppSelection[] = [];
Object.values(extractedResources).forEach(r => {
fileSideEffect.affectedResourceIds.push(r.id);
Object.values(extractedResources).forEach((r, ix) => {
// if we're just replacing one resource with another consider this an update
if (extractedResources.length === fileSideEffect.affectedResourceIds.length) {
r.id = fileSideEffect.affectedResourceIds[ix];
} else {
fileSideEffect.affectedResourceIds.push(r.id);
}

const {meta, content} = splitK8sResource(r);
mainState.resourceMetaMapByStorage.local[meta.id] = meta;
mainState.resourceContentMapByStorage.local[content.id] = content;
Expand All @@ -102,19 +107,6 @@ export const updateFileEntry = createAsyncThunk<
},
});
});

// did we just replace a kustomization being dry-run? -> update the kustomizationId to the new one
// and restart the dry-run
if (
isKustomizationFile(fileEntry, mainState.resourceMetaMapByStorage.local) &&
mainState.preview?.type === 'kustomize' &&
mainState.preview.kustomizationId === fileSideEffect.affectedResourceIds[0]
) {
// mainState.preview.kustomizationId = fileSideEffect.affectedResourceIds[1];
// thunkAPI.dispatch(stopPreview());
}

mainState.highlights = newHighlights;
}
}

Expand Down

0 comments on commit e04ff9c

Please sign in to comment.