Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
3d45f55
feat(coderd/database): allow filtering provisioner daemons by tags
SasSwart Nov 8, 2024
92b9d25
feat(coderd/database): allow filtering provisioner daemons by tags
SasSwart Nov 8, 2024
e68a8fc
add a type hint for sqlc
SasSwart Nov 8, 2024
93058e6
ensure BC with GetProvisionerDaemonsByOrganization
SasSwart Nov 8, 2024
a2bfae3
add a type hint for sqlc
SasSwart Nov 11, 2024
39ab8a0
fix unit test
SasSwart Nov 11, 2024
8bf79c8
Add tags param to provisioner daemons endpoint
SasSwart Nov 11, 2024
86f7dab
fix provisioner tag filtering
SasSwart Nov 11, 2024
bd6f3ed
handle tag reading error
SasSwart Nov 11, 2024
a2476dc
remove assignment to a nil map
SasSwart Nov 11, 2024
e835326
filter by tags in dbmem
SasSwart Nov 11, 2024
23bd23f
correctly set the default value when no tags are provided
SasSwart Nov 11, 2024
9985ef6
support the zero value tag in sql
SasSwart Nov 11, 2024
a173bfb
update the default value for when no tags are provided
SasSwart Nov 12, 2024
70c7ea9
use a sql domain as a type alias so that we can override it as a Stri…
SasSwart Nov 12, 2024
1c57bd2
complete down migration
SasSwart Nov 12, 2024
ebb716d
complete down migration
SasSwart Nov 12, 2024
96b64f4
update the default value for when no tags are provided
SasSwart Nov 12, 2024
5941dc3
fix job acquisition for untagged provisioners
SasSwart Nov 12, 2024
ff75f5e
make gen
SasSwart Nov 12, 2024
85b1ef4
review notes
SasSwart Nov 12, 2024
071fdc4
fix down migration
SasSwart Nov 12, 2024
78abf67
use the correct name for provisionerdaemons' tag field
SasSwart Nov 12, 2024
d1c7d3e
typo
SasSwart Nov 12, 2024
38d77cf
use the updated tag name
SasSwart Nov 12, 2024
1c64353
fix special case of tagset_contains
SasSwart Nov 12, 2024
8508cd8
Use a stringmap instead of a stringslice for provisioner tags
SasSwart Nov 13, 2024
9dcbb83
update tags param parsing in provisionerDaemons
SasSwart Nov 13, 2024
4c1fc0d
update tags param parsing in provisionerDaemons
SasSwart Nov 13, 2024
fbd70f3
fix special case of tagset_contains
SasSwart Nov 13, 2024
69126f4
rename tagset_contains function
SasSwart Nov 13, 2024
e109caf
attempt to fix the special case for provisioner_tagset_contains
SasSwart Nov 13, 2024
f10561e
attempt to fix the special case for provisioner_tagset_contains
SasSwart Nov 13, 2024
a56b130
attempt to fix the special case for provisioner_tagset_contains
SasSwart Nov 13, 2024
2bce007
fix provisioner_tagset_contains
SasSwart Nov 13, 2024
2860f5d
remove defunct code
SasSwart Nov 14, 2024
d6f26df
Add tag filtering tests for GetProvisionerDaemons
SasSwart Nov 14, 2024
1ce410f
fix foreign key constraint in tests
SasSwart Nov 14, 2024
c065742
Add linting
SasSwart Nov 14, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
use a sql domain as a type alias so that we can override it as a Stri…
…ngMap in sqlc
  • Loading branch information
SasSwart committed Nov 12, 2024
commit 70c7ea90d83743de685c230d505f35b493496581
7 changes: 1 addition & 6 deletions coderd/database/dbmem/dbmem.go
Original file line number Diff line number Diff line change
Expand Up @@ -3635,12 +3635,7 @@ func (q *FakeQuerier) GetProvisionerDaemonsByOrganization(_ context.Context, arg
continue
}
if arg.Tags != nil {
var argTags map[string]string
err := json.Unmarshal(arg.Tags, &argTags)
if err != nil {
continue
}
for k, v := range argTags {
for k, v := range arg.Tags {
if t, found := daemon.Tags[k]; !found || t != v {
continue
}
Expand Down
8 changes: 5 additions & 3 deletions coderd/database/dump.sql

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions coderd/database/migrations/000274_check_tags.up.sql
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
CREATE OR REPLACE FUNCTION tags_compatible(subset_tags jsonb, superset_tags jsonb)
CREATE DOMAIN tags AS jsonb;

CREATE OR REPLACE FUNCTION tags_compatible(subset_tags tags, superset_tags tags)
RETURNS boolean as $$
BEGIN
RETURN CASE
WHEN superset_tags = '{"scope": "organization", "owner": ""}' :: jsonb
WHEN superset_tags :: jsonb = '{"scope": "organization", "owner": ""}' :: jsonb
THEN subset_tags = superset_tags
ELSE subset_tags <@ superset_tags
ELSE subset_tags :: jsonb <@ superset_tags :: jsonb
END;
END;
$$ LANGUAGE plpgsql;
8 changes: 4 additions & 4 deletions coderd/database/queries.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion coderd/database/queries/provisionerdaemons.sql
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ WHERE
organization_id = @organization_id :: uuid
AND
-- adding support for searching by tags:
(@tags :: jsonb = '' :: jsonb OR tags_compatible(@tags :: jsonb, provisioner_daemons.tags :: jsonb));
(@tags :: tags = '{}' :: tags OR tags_compatible(@tags::tags, provisioner_daemons.tags::tags));

-- name: DeleteOldProvisionerDaemons :exec
-- Delete provisioner daemons that have been created at least a week ago
Expand Down
2 changes: 1 addition & 1 deletion coderd/database/queries/provisionerjobs.sql
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ WHERE
AND nested.organization_id = @organization_id
-- Ensure the caller has the correct provisioner.
AND nested.provisioner = ANY(@types :: provisioner_type [ ])
AND tags_compatible(@tags, nested.tags)
AND tags_compatible(@tags :: jsonb, nested.tags)
ORDER BY
nested.created_at
FOR UPDATE
Expand Down
3 changes: 3 additions & 0 deletions coderd/database/sqlc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ sql:
- db_type: "name_organization_pair"
go_type:
type: "NameOrganizationPair"
- db_type: "tags"
go_type:
type: "StringMap"
- column: "custom_roles.site_permissions"
go_type:
type: "CustomRolePermissions"
Expand Down
17 changes: 4 additions & 13 deletions enterprise/coderd/provisionerdaemons.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package coderd
import (
"context"
"database/sql"
"encoding/json"
"fmt"
"io"
"net/http"
Expand Down Expand Up @@ -64,15 +63,7 @@ func (api *API) provisionerDaemons(rw http.ResponseWriter, r *http.Request) {
ctx := r.Context()
org := httpmw.OrganizationParam(r)

tags, err := provisionerTags(r)
if err != nil {
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
Message: "Internal error reading tags.",
Detail: err.Error(),
})
return
}

tags := provisionerTags(r)
daemons, err := api.Database.GetProvisionerDaemonsByOrganization(
ctx,
database.GetProvisionerDaemonsByOrganizationParams{
Expand All @@ -91,10 +82,10 @@ func (api *API) provisionerDaemons(rw http.ResponseWriter, r *http.Request) {
httpapi.Write(ctx, rw, http.StatusOK, db2sdk.List(daemons, db2sdk.ProvisionerDaemon))
}

func provisionerTags(r *http.Request) ([]byte, error) {
func provisionerTags(r *http.Request) map[string]string {
tags := r.URL.Query()["tags"]
if len(tags) == 0 {
return []byte{}, nil
return nil
}

tagMap := map[string]string{}
Expand All @@ -106,7 +97,7 @@ func provisionerTags(r *http.Request) ([]byte, error) {
}
}

return json.Marshal(tagMap)
return tagMap
}

type provisiionerDaemonAuthResponse struct {
Expand Down
Loading