Skip to content

Commit

Permalink
feat: handle optional matchResources
Browse files Browse the repository at this point in the history
  • Loading branch information
topliceanurazvan committed Sep 28, 2023
1 parent da06582 commit 9a6f172
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions packages/validation/src/validators/admission-policy/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,21 +96,25 @@ export class AdmissionPolicyValidator extends AbstractPlugin {
const namespaceMatchLabels: Record<string, string> =
policyBinding.content?.spec?.matchResources?.namespaceSelector?.matchLabels;

if (!namespaceMatchLabels) continue;
let filteredResources: Resource[] = [];

const namespacesResources = resources.filter(r => r.kind === 'Namespace');
if (!namespaceMatchLabels) {
filteredResources = resources;
} else {
const namespacesResources = resources.filter(r => r.kind === 'Namespace');

const filteredNamespaces = namespacesResources.filter(n => {
for (const key of Object.keys(namespaceMatchLabels)) {
if (n.content?.metadata?.labels?.[key] !== namespaceMatchLabels[key]) {
return false;
const filteredNamespaces = namespacesResources.filter(n => {
for (const key of Object.keys(namespaceMatchLabels)) {
if (n.content?.metadata?.labels?.[key] !== namespaceMatchLabels[key]) {
return false;
}
}
}

return true;
});
return true;
});

const filteredResources = resources.filter(r => filteredNamespaces.find(n => n.name === r.namespace));
filteredResources = resources.filter(r => filteredNamespaces.find(n => n.name === r.namespace));
}

if (!filteredResources.length) continue;

Expand Down

0 comments on commit 9a6f172

Please sign in to comment.