Skip to content

Commit

Permalink
fix: reprocess all resources for file updates
Browse files Browse the repository at this point in the history
  • Loading branch information
olensmar committed Nov 16, 2021
1 parent ad40119 commit 32fa37b
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/redux/reducers/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import path from 'path';
import {v4 as uuidv4} from 'uuid';
import {parseDocument} from 'yaml';

import {findResourcesToReprocess} from '@redux/services/resourceRefs';
import {resetSelectionHistory} from '@redux/services/selectionHistory';
import {performResourceDiff} from '@redux/thunks/diffResource';
import {loadClusterDiff} from '@redux/thunks/loadClusterDiff';
Expand Down Expand Up @@ -264,11 +265,21 @@ export const mainSlice = createSlice({
action.payload.content,
filePath.substring(rootFolder.length)
);

let resourceIds: string[] = [];

// only recalculate refs for resources that already have refs
Object.values(state.resourceMap)
.filter(r => r.refs)
.forEach(r => resourceIds.push(r.id));

Object.values(extractedResources).forEach(r => {
state.resourceMap[r.id] = r;
r.isHighlighted = true;
resourceIds.push(r.id);
});
reprocessResources([], state.resourceMap, state.fileMap, state.resourceRefsProcessingOptions, {

reprocessResources(resourceIds, state.resourceMap, state.fileMap, state.resourceRefsProcessingOptions, {
resourceKinds: extractedResources.map(r => r.kind),
});
}
Expand Down Expand Up @@ -296,7 +307,10 @@ export const mainSlice = createSlice({
resource.text = action.payload.content;
resource.content = parseDocument(action.payload.content).toJS();
}
reprocessResources([resource.id], state.resourceMap, state.fileMap, state.resourceRefsProcessingOptions);

let resources = findResourcesToReprocess(resource, state.resourceMap);

reprocessResources(resources, state.resourceMap, state.fileMap, state.resourceRefsProcessingOptions);
if (!action.payload.preventSelectionAndHighlightsUpdate) {
resource.isSelected = false;
updateSelectionAndHighlights(state, resource);
Expand Down
28 changes: 28 additions & 0 deletions src/redux/services/resourceRefs.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {isKustomizationPatch, isKustomizationResource} from '@redux/services/kustomize';
import {Draft} from '@reduxjs/toolkit';

import {ResourceMapType, ResourceRefsProcessingOptions} from '@models/appstate';
import {K8sResource, RefNode, RefPosition, ResourceRef, ResourceRefType} from '@models/k8sresource';
Expand Down Expand Up @@ -510,3 +511,30 @@ export function processRefs(

cleanResourceRefs(resources);
}

/**
* Return a list of resource ids to reprocess when the specified resource has been updated
*/

export function findResourcesToReprocess(resource: Draft<K8sResource>, resourceMap: ResourceMapType) {
// the resource itself
let resources = [resource.id];

// all existing resources with incoming refs to the resource (since they might have broken)
resource.refs
?.filter(ref => isIncomingRef(ref.type))
.forEach(ref => {
if (ref.target?.type === 'resource' && ref.target.resourceId) {
resources.push(ref.target.resourceId);
}
});

// all existing resources with broken refs (since they might now work) -> this should be improved to
// only return resources that could actually refer to the updated resource.
Object.values(resourceMap)
.filter(r => resources.indexOf(r.id) === -1)
.filter(r => r.refs?.some(ref => isUnsatisfiedRef(ref.type)))
.forEach(r => resources.push(r.id));

return resources;
}

0 comments on commit 32fa37b

Please sign in to comment.