Skip to content

Commit

Permalink
feat: regex filters for namespace and name (#1156)
Browse files Browse the repository at this point in the history
## Description

Adds regex for name and namespaces. 

Another note: Filter Logic is extremely difficult to grok through now,
we need to
[refactor](#1154)

**TODO**
- [x] e2e test
- [x] Extend Unit tests to add cases for regex
- [x] `namespaceComplianceValidator` to test for regex namespaces

End to End Test:
[PR](defenseunicorns/pepr-excellent-examples#75)
- [x] hello-pepr-regex-name (should fail until #1162 lands)
- [x] hello-pepr-regex-ns-all
- [x] hello-pepr-regex-ns-bounded
- [x] hello-pepr-regex-ns-wrong

## Related Issue

Fixes #582 
<!-- or -->
Relates to #

## Type of change

- [ ] Bug fix (non-breaking change which fixes an issue)
- [x] New feature (non-breaking change which adds functionality)
- [ ] Other (security config, docs update, etc)

## Checklist before merging
- [x] Unit,
[Journey](https://github.com/defenseunicorns/pepr/tree/main/journey),
[E2E Tests](https://github.com/defenseunicorns/pepr-excellent-examples),
[docs](https://github.com/defenseunicorns/pepr/tree/main/docs),
[adr](https://github.com/defenseunicorns/pepr/tree/main/adr) added or
updated as needed
- [x] [Contributor Guide
Steps](https://docs.pepr.dev/main/contribute/#submitting-a-pull-request)
followed

---------

Signed-off-by: Case Wylie <cmwylie19@defenseunicorns.com>
  • Loading branch information
cmwylie19 authored Sep 23, 2024
1 parent 6db4b40 commit 0781707
Show file tree
Hide file tree
Showing 9 changed files with 626 additions and 53 deletions.
2 changes: 2 additions & 0 deletions docs/030_user-guide/130_filters.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ When(a.ConfigMap)
## `Filters`

- `.WithName("name")`: Filters resources by name.
- `.WithNameRegex(/^pepr/)`: Filters resources by name using a regex.
- `.InNamespace("namespace")`: Filters resources by namespace.
- `.InNamespaceRegex(/(.*)-system/)`: Filters resources by namespace using a regex.
- `.WithLabel("key", "value")`: Filters resources by label. (Can be multiple)
- `.WithDeletionTimestamp()`: Filters resources that have a deletion timestamp.

Expand Down
18 changes: 17 additions & 1 deletion src/lib/capability.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,8 @@ export class Capability implements CapabilityExport {
filters: {
name: "",
namespaces: [],
regexNamespaces: [],
regexName: "",
labels: {},
annotations: {},
deletionTimestamp: false,
Expand Down Expand Up @@ -312,7 +314,13 @@ export class Capability implements CapabilityExport {
function InNamespace(...namespaces: string[]): BindingWithName<T> {
Log.debug(`Add namespaces filter ${namespaces}`, prefix);
binding.filters.namespaces.push(...namespaces);
return { ...commonChain, WithName };
return { ...commonChain, WithName, WithNameRegex };
}

function InNamespaceRegex(...namespaces: RegExp[]): BindingWithName<T> {
Log.debug(`Add regex namespaces filter ${namespaces}`, prefix);
binding.filters.regexNamespaces.push(...namespaces.map(regex => regex.source));
return { ...commonChain, WithName, WithNameRegex };
}

function WithDeletionTimestamp(): BindingFilter<T> {
Expand All @@ -321,6 +329,12 @@ export class Capability implements CapabilityExport {
return commonChain;
}

function WithNameRegex(regexName: RegExp): BindingFilter<T> {
Log.debug(`Add regex name filter ${regexName}`, prefix);
binding.filters.regexName = regexName.source;
return commonChain;
}

function WithName(name: string): BindingFilter<T> {
Log.debug(`Add name filter ${name}`, prefix);
binding.filters.name = name;
Expand All @@ -344,7 +358,9 @@ export class Capability implements CapabilityExport {
return {
...commonChain,
InNamespace,
InNamespaceRegex,
WithName,
WithNameRegex,
WithDeletionTimestamp,
};
}
Expand Down
Loading

0 comments on commit 0781707

Please sign in to comment.