Skip to content

Commit

Permalink
fix: added filter logic based on namespaceref
Browse files Browse the repository at this point in the history
  • Loading branch information
olensmar committed Oct 13, 2021
1 parent 595c774 commit 3475f11
Showing 1 changed file with 49 additions and 3 deletions.
52 changes: 49 additions & 3 deletions src/redux/services/resourceRefs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {ResourceMapType, ResourceRefsProcessingOptions} from '@models/appstate';
import {K8sResource, RefNode, RefPosition, ResourceRef, ResourceRefType} from '@models/k8sresource';
import {REF_PATH_SEPARATOR} from '@constants/constants';
import {getIncomingRefMappers, getResourceKindHandler} from '@src/kindhandlers';
import {RefMapper} from '@models/resourcekindhandler';
import {NamespaceRefEnum, RefMapper} from '@models/resourcekindhandler';
import {isKustomizationResource} from '@redux/services/kustomize';
import {traverseDocument} from './manifest-utils';
import {createResourceRef, getParsedDoc, linkResources, NodeWrapper} from './resource';
Expand Down Expand Up @@ -108,6 +108,14 @@ export function processResourceRefNodes(resource: K8sResource) {
addRefNodeAtPath(refNode, optionalSiblingPath, resource.refNodesByPath);
}
}

if (refMapper.source.namespaceRef === NamespaceRefEnum.Explicit) {
const namespacePropertyName = refMapper.source.namespaceProperty || 'namespace';
const namespaceSiblingPath = joinPathParts([...refMapper.source.pathParts.slice(0, -1), namespacePropertyName]);
if (keyPath.endsWith(namespaceSiblingPath)) {
addRefNodeAtPath(refNode, namespaceSiblingPath, resource.refNodesByPath);
}
}
});
});
}
Expand Down Expand Up @@ -310,6 +318,44 @@ function clearResourceRefs(resource: K8sResource, resourceMap: ResourceMapType)
});
}

function resourceMatchesRefMapper(
targetResource: K8sResource,
outgoingRefMapper: RefMapper,
sourceResource: K8sResource
) {
if (targetResource.kind === outgoingRefMapper.target.kind) {
if (!outgoingRefMapper.source.namespaceRef) {
return true;
}

switch (outgoingRefMapper.source.namespaceRef) {
case NamespaceRefEnum.Implicit:
return sourceResource.namespace === targetResource.namespace;
case NamespaceRefEnum.Explicit:
case NamespaceRefEnum.OptionalImplicit: {
const namespacePropertyName = outgoingRefMapper.source.namespaceProperty || 'namespace';
const namespaceSiblingPath = joinPathParts([
...outgoingRefMapper.source.pathParts.slice(0, -1),
namespacePropertyName,
]);
const namespaceSiblingRefNodes = sourceResource.refNodesByPath
? sourceResource.refNodesByPath[namespaceSiblingPath]
: undefined;

return namespaceSiblingRefNodes
? namespaceSiblingRefNodes.some(refNode => refNode.scalar.value === targetResource.namespace)
: outgoingRefMapper.source.namespaceRef === NamespaceRefEnum.OptionalImplicit &&
sourceResource.namespace === targetResource.namespace;
}

default:
return true;
}
}

return false;
}

export function processRefs(
resourceMap: ResourceMapType,
processingOptions: ResourceRefsProcessingOptions,
Expand Down Expand Up @@ -337,8 +383,8 @@ export function processRefs(
return;
}
resourceKindHandler.outgoingRefMappers.forEach(outgoingRefMapper => {
const targetResources = Object.values(resourceMap).filter(
targetResource => targetResource.kind === outgoingRefMapper.target.kind
const targetResources = Object.values(resourceMap).filter(targetResource =>
resourceMatchesRefMapper(targetResource, outgoingRefMapper, sourceResource)
);

if (outgoingRefMapper.matchPairs) {
Expand Down

0 comments on commit 3475f11

Please sign in to comment.