Skip to content

Commit

Permalink
chore: account for capability NS and ignored NS
Browse files Browse the repository at this point in the history
Signed-off-by: Case Wylie <[email protected]>
  • Loading branch information
cmwylie19 committed Jan 6, 2025
1 parent 26c8253 commit 51574be
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 6 deletions.
6 changes: 6 additions & 0 deletions src/lib/filter/adjudicators/adjudicators.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,9 @@ describe("uncarryableNamespace", () => {
[[], {}, false],
[[], { metadata: { namespace: "namespace" } }, false],

[["namespace"], { kind: "Namespace", metadata: { name: "namespace" } }, false],
[["namespace"], { kind: "Namespace", metadata: { name: "monitoring" } }, true],

[["namespace"], {}, false],
[["namespace"], { metadata: {} }, false],
[["namespace"], { metadata: { namespace: null } }, false],
Expand Down Expand Up @@ -297,6 +300,9 @@ describe("carriesIgnoredNamespace", () => {
[[], {}, false],
[[], { metadata: { namespace: "whatever" } }, false],

[["ignored"], { kind: "Namespace", metadata: { name: "ignored" } }, true],
[["ignored"], { kind: "Namespace", metadata: { name: "monitoring" } }, false],

[["ignored"], {}, false],
[["ignored"], { metadata: {} }, false],
[["ignored"], { metadata: { namespace: null } }, false],
Expand Down
34 changes: 28 additions & 6 deletions src/lib/filter/adjudicators/adjudicators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,8 @@ export const missingName = complement(carriesName);
export const carriedNamespace = pipe((kubernetesObject: KubernetesObject): string | undefined => {
if (kubernetesObject?.kind === "Namespace") {
return kubernetesObject?.metadata?.name;
} else {
return kubernetesObject?.metadata?.namespace;
}
return kubernetesObject?.metadata?.namespace;
}, defaultTo(""));

export const carriesNamespace = pipe(carriedNamespace, equals(""), not);
Expand Down Expand Up @@ -252,10 +251,21 @@ export const mismatchedLabels = allPass([
pipe((binding, kubernetesObject) => metasMismatch(definedLabels(binding), carriedLabels(kubernetesObject))),
]);

/*
* If the object does not have a namespace, and it is not a namespace,
* then we must return false because it cannot be uncarryable
*/
export const uncarryableNamespace = allPass([
pipe(nthArg(0), length, gt(__, 0)),
pipe(nthArg(1), carriesNamespace),
pipe((namespaceSelector, kubernetesObject) => namespaceSelector.includes(carriedNamespace(kubernetesObject)), not),
pipe((namespaceSelector, kubernetesObject) => {
if (kubernetesObject?.kind === "Namespace") {
return namespaceSelector.includes(kubernetesObject?.metadata?.name);
}
if (carriesNamespace(kubernetesObject)) {
return namespaceSelector.includes(carriedNamespace(kubernetesObject));
}
return true;
}, not),
]);

export const missingCarriableNamespace = allPass([
Expand All @@ -267,10 +277,22 @@ export const missingCarriableNamespace = allPass([
),
]);

/*
* If the object does not have a namespace, and it is not a namespace,
* then we must return false because it cannot be ignored
*/
export const carriesIgnoredNamespace = allPass([
pipe(nthArg(0), length, gt(__, 0)),
pipe(nthArg(1), carriesNamespace),
pipe((namespaceSelector, kubernetesObject) => namespaceSelector.includes(carriedNamespace(kubernetesObject))),
pipe((namespaceSelector, kubernetesObject) => {
if (kubernetesObject?.kind === "Namespace") {
return namespaceSelector.includes(kubernetesObject?.metadata?.name);
}
if (carriesNamespace(kubernetesObject)) {
return namespaceSelector.includes(carriedNamespace(kubernetesObject));
}

return false;
}),
]);

export const unbindableNamespaces = allPass([
Expand Down
4 changes: 4 additions & 0 deletions src/lib/filter/filter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1218,6 +1218,10 @@ describe("adjudicateUnbindableNamespaces", () => {
});

describe("adjudicateUncarryableNamespace", () => {
it("should return uncarryableNamespace reason when the object is a namespace that is not allowed by the capability", () => {
const result = adjudicateUncarryableNamespace(["default"], { kind: "Namespace", metadata: { name: "pepr-demo" } });
expect(result).toBe(`Object carries namespace 'pepr-demo' but namespaces allowed by Capability are '["default"]'.`);
});
it("should return uncarryableNamespace reason when the object carries a namespace that is not allowed by the capability", () => {
const result = adjudicateUncarryableNamespace(["default"], { metadata: { namespace: "kube-system" } });
expect(result).toBe(
Expand Down

0 comments on commit 51574be

Please sign in to comment.