Skip to content

Commit

Permalink
Revert "Undo regexp work to revisit on a separate PR"
Browse files Browse the repository at this point in the history
This reverts commit 257c4b8.
  • Loading branch information
samayer12 committed Nov 22, 2024
1 parent 257c4b8 commit 7f1bbec
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 15 deletions.
8 changes: 4 additions & 4 deletions src/lib/capability.ts
Original file line number Diff line number Diff line change
Expand Up @@ -345,9 +345,9 @@ export class Capability implements CapabilityExport {
return { ...commonChain, WithName, WithNameRegex };
}

function InNamespaceRegex(...namespaces: RegExp[]): BindingWithName<T> {
function InNamespaceRegex(...namespaces: string[]): BindingWithName<T> {

Check warning on line 348 in src/lib/capability.ts

View check run for this annotation

Codecov / codecov/patch

src/lib/capability.ts#L348

Added line #L348 was not covered by tests
Log.debug(`Add regex namespaces filter ${namespaces}`, prefix);
binding.filters.regexNamespaces.push(...namespaces.map(regex => regex.source));
binding.filters.regexNamespaces.push(...namespaces);

Check warning on line 350 in src/lib/capability.ts

View check run for this annotation

Codecov / codecov/patch

src/lib/capability.ts#L350

Added line #L350 was not covered by tests
return { ...commonChain, WithName, WithNameRegex };
}

Expand All @@ -357,9 +357,9 @@ export class Capability implements CapabilityExport {
return commonChain;
}

function WithNameRegex(regexName: RegExp): BindingFilter<T> {
function WithNameRegex(regexName: string): BindingFilter<T> {

Check warning on line 360 in src/lib/capability.ts

View check run for this annotation

Codecov / codecov/patch

src/lib/capability.ts#L360

Added line #L360 was not covered by tests
Log.debug(`Add regex name filter ${regexName}`, prefix);
binding.filters.regexName = regexName.source;
binding.filters.regexName = regexName;

Check warning on line 362 in src/lib/capability.ts

View check run for this annotation

Codecov / codecov/patch

src/lib/capability.ts#L362

Added line #L362 was not covered by tests
return commonChain;
}

Expand Down
23 changes: 15 additions & 8 deletions src/lib/filter/adjudicators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,11 @@ export const definesNameRegex = pipe(definedNameRegex, equals(""), not);
export const definedNamespaces = pipe(binding => binding?.filters?.namespaces, defaultTo([]));
export const definesNamespaces = pipe(definedNamespaces, equals([]), not);

export const definedNamespaceRegexes = pipe(binding => binding?.filters?.regexNamespaces, defaultTo([]));
export const definesNamespaceRegexes = pipe(definedNamespaceRegexes, equals([]), not);
export const definedNamespaceRegexes = pipe(
(binding: Binding): string[] => binding.filters.regexNamespaces,
defaultTo<string[]>([]),
);
export const definesNamespaceRegexes = pipe(definedNamespaceRegexes, equals([] as string[]), not);

export const definedAnnotations = pipe((binding: Partial<Binding>) => binding?.filters?.annotations, defaultTo({}));
export const definesAnnotations = pipe(definedAnnotations, equals({}), not);
Expand Down Expand Up @@ -202,12 +205,16 @@ export const mismatchedNamespace = allPass([
export const mismatchedNamespaceRegex = allPass([
// Check if `definesNamespaceRegexes` returns true
pipe(nthArg(0), definesNamespaceRegexes),
pipe((binding, kubernetesObject) =>
pipe(
any((regEx: string) => new RegExp(regEx).test(carriedNamespace(kubernetesObject))),
not,
)(definedNamespaceRegexes(binding)),
),

// Check if no regex matches
(binding: Binding, kubernetesObject: KubernetesObject) => {
// Convert definedNamespaceRegexes(binding) from string[] to RegExp[]
const regexArray = definedNamespaceRegexes(binding).map(regexStr => new RegExp(regexStr));

// Check if no regex matches the namespace of the Kubernetes object
const result = not(any((regEx: RegExp) => regEx.test(carriedNamespace(kubernetesObject)), regexArray));
return result;
},
]);

export const metasMismatch = pipe(
Expand Down
2 changes: 1 addition & 1 deletion src/lib/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ export function generateWatchNamespaceError(
return err.replace(/\.([^ ])/g, ". $1");
}

// namespaceComplianceValidator ensures that capability bindings respect ignored and capability namespaces
// namespaceComplianceValidator ensures that capability binds respect ignored and capability namespaces
export function namespaceComplianceValidator(capability: CapabilityExport, ignoredNamespaces?: string[]) {
const { namespaces: capabilityNamespaces, bindings, name } = capability;
const bindingNamespaces: string[] = bindings.flatMap((binding: Binding) => binding.filters.namespaces);
Expand Down
4 changes: 2 additions & 2 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,14 +143,14 @@ export type BindingWithName<T extends GenericClass> = BindingFilter<T> & {
/** Only apply the action if the resource name matches the specified name. */
WithName: (name: string) => BindingFilter<T>;
/** Only apply the action if the resource name matches the specified regex name. */
WithNameRegex: (name: RegExp) => BindingFilter<T>;
WithNameRegex: (name: string) => BindingFilter<T>;
};

export type BindingAll<T extends GenericClass> = BindingWithName<T> & {
/** Only apply the action if the resource is in one of the specified namespaces.*/
InNamespace: (...namespaces: string[]) => BindingWithName<T>;
/** Only apply the action if the resource is in one of the specified regex namespaces.*/
InNamespaceRegex: (...namespaces: RegExp[]) => BindingWithName<T>;
InNamespaceRegex: (...namespaces: string[]) => BindingWithName<T>;
};

export type CommonActionChain<T extends GenericClass> = MutateActionChain<T> & {
Expand Down

0 comments on commit 7f1bbec

Please sign in to comment.