Skip to content

Commit 3d45f55

Browse files
committed
feat(coderd/database): allow filtering provisioner daemons by tags
1 parent d7e8627 commit 3d45f55

File tree

4 files changed

+18
-9
lines changed

4 files changed

+18
-9
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DROP FUNCTION IF EXISTS tags_compatible(jsonb, jsonb);
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
CREATE OR REPLACE FUNCTION tags_compatible(provisioner_tags jsonb, required_tags jsonb)
2+
RETURNS boolean as $$
3+
BEGIN
4+
RETURN CASE
5+
WHEN provisioner_tags = '{"scope": "organization", "owner": ""}' :: jsonb
6+
THEN provisioner_tags = required_tags
7+
ELSE required_tags <@ provisioner_tags
8+
END;
9+
END;
10+
$$ LANGUAGE plpgsql;

coderd/database/queries/provisionerdaemons.sql

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ SELECT
1010
FROM
1111
provisioner_daemons
1212
WHERE
13-
organization_id = @organization_id;
13+
-- If organization_id is provided, filter by it; otherwise, allow all.
14+
(@organization_id IS NULL OR organization_id = @organization_id)
15+
AND
16+
-- If tags are provided, check compatibility; otherwise, skip tags check.
17+
(@tags IS NULL OR tags_compatible(@tags :: jsonb, provisioner_daemons.tags :: jsonb));
1418

1519
-- name: DeleteOldProvisionerDaemons :exec
1620
-- Delete provisioner daemons that have been created at least a week ago

coderd/database/queries/provisionerjobs.sql

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,7 @@ WHERE
2222
AND nested.organization_id = @organization_id
2323
-- Ensure the caller has the correct provisioner.
2424
AND nested.provisioner = ANY(@types :: provisioner_type [ ])
25-
AND CASE
26-
-- Special case for untagged provisioners: only match untagged jobs.
27-
WHEN nested.tags :: jsonb = '{"scope": "organization", "owner": ""}' :: jsonb
28-
THEN nested.tags :: jsonb = @tags :: jsonb
29-
-- Ensure the caller satisfies all job tags.
30-
ELSE nested.tags :: jsonb <@ @tags :: jsonb
31-
END
25+
AND tags_compatible(nested.tags, @tags)
3226
ORDER BY
3327
nested.created_at
3428
FOR UPDATE
@@ -160,4 +154,4 @@ RETURNING *;
160154
-- name: GetProvisionerJobTimingsByJobID :many
161155
SELECT * FROM provisioner_job_timings
162156
WHERE job_id = $1
163-
ORDER BY started_at ASC;
157+
ORDER BY started_at ASC;

0 commit comments

Comments
 (0)