Skip to content

Commit 42ea7d9

Browse files
authored
feat: add support for key-only label filters (set-based label selectors) (#40)
1 parent 90e221c commit 42ea7d9

File tree

5 files changed

+30
-4
lines changed

5 files changed

+30
-4
lines changed

__mocks__/browser-or-node.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// mock the browser-or-node module
2+
3+
import { jest } from "@jest/globals";
4+
5+
jest.mock("browser-or-node", () => ({
6+
isBrowser: false,
7+
isNode: true,
8+
}));

src/fluent/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export function K8s<T extends GenericClass, K extends KubernetesObject = Instanc
5454
* @inheritdoc
5555
* @see {@link K8sInit.WithLabel}
5656
*/
57-
function WithLabel(key: string, value: string) {
57+
function WithLabel(key: string, value = "") {
5858
filters.labels = filters.labels || {};
5959
filters.labels[key] = value;
6060
return withFilters;

src/fluent/types.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,16 +128,17 @@ export type K8sWithFilters<K extends KubernetesObject> = K8sFilteredActions<K> &
128128
* K8s(kind.Deployment)
129129
* .WithLabel("foo", "bar")
130130
* .WithLabel("baz", "qux")
131+
* .WithLabel("quux")
131132
* .Delete(...)
132133
* ```
133134
*
134-
* Will only delete the Deployment if it has the`foo=bar` and `baz=qux` labels.
135+
* Will only delete the Deployment if it has the`foo=bar` and `baz=qux` labels and the `quux` label exists.
135136
*
136137
* @param key - the label key
137138
* @param value - the label value
138139
* @returns the fluent API
139140
*/
140-
WithLabel: (key: string, value: string) => K8sWithFilters<K>;
141+
WithLabel: (key: string, value?: string) => K8sWithFilters<K>;
141142
};
142143

143144
export type K8sInit<K extends KubernetesObject> = K8sWithFilters<K> &

src/fluent/utils.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,21 @@ describe("pathBuilder Function", () => {
2121
expect(() => pathBuilder("", model, filters)).toThrow("Kind not specified for Unknown");
2222
});
2323

24+
it("should generate a path with a set-based label selector", () => {
25+
const filters: Filters = {
26+
namespace: "default",
27+
name: "mypod",
28+
labels: { iamalabel: "" },
29+
};
30+
const result = pathBuilder(serverUrl, Pod, filters);
31+
const expected = new URL(
32+
"/api/v1/namespaces/default/pods/mypod?labelSelector=iamalabel",
33+
serverUrl,
34+
);
35+
36+
expect(result.toString()).toEqual(expected.toString());
37+
});
38+
2439
it("should generate a path for core group kinds (with custom filters)", () => {
2540
const filters: Filters = {
2641
namespace: "default",

src/fluent/utils.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@ export function pathBuilder<T extends GenericClass>(
7373
// Add label selectors to the query params
7474
if (filters.labels) {
7575
const labelSelector = Object.entries(filters.labels)
76-
.map(([key, value]) => `${key}=${value}`)
76+
// Exists set-based operators only include the key
77+
// See https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/#set-based-requirement
78+
.map(([key, value]) => (value ? `${key}=${value}` : key))
7779
.join(",");
7880

7981
url.searchParams.set("labelSelector", labelSelector);

0 commit comments

Comments
 (0)