Skip to content

Commit 2528a59

Browse files
committed
feat: add single tailnet support to pgcoord
1 parent 31ffb56 commit 2528a59

24 files changed

+749
-258
lines changed

coderd/coderd.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ func New(options *Options) *API {
406406
options.DERPServer,
407407
options.BaseDERPMap,
408408
func(context.Context) (tailnet.MultiAgentConn, error) {
409-
return (*api.TailnetCoordinator.Load()).ServeMultiAgent(uuid.New()), nil
409+
return (*api.TailnetCoordinator.Load()).ServeMultiAgent(uuid.New())
410410
},
411411
wsconncache.New(api._dialWorkspaceAgentTailnet, 0),
412412
api.TracerProvider,

coderd/database/dbauthz/dbauthz.go

+4
Original file line numberDiff line numberDiff line change
@@ -1166,6 +1166,10 @@ func (q *querier) GetTailnetAgents(ctx context.Context, id uuid.UUID) ([]databas
11661166
return q.db.GetTailnetAgents(ctx, id)
11671167
}
11681168

1169+
func (q *querier) GetTailnetAgentsForClient(ctx context.Context, id uuid.UUID) ([]database.TailnetAgent, error) {
1170+
panic("not implemented")
1171+
}
1172+
11691173
func (q *querier) GetTailnetClientsForAgent(ctx context.Context, agentID uuid.UUID) ([]database.TailnetClient, error) {
11701174
if err := q.authorizeContext(ctx, rbac.ActionRead, rbac.ResourceTailnetCoordinator); err != nil {
11711175
return nil, err

coderd/database/dbfake/dbfake.go

+4
Original file line numberDiff line numberDiff line change
@@ -1975,6 +1975,10 @@ func (*FakeQuerier) GetTailnetAgents(context.Context, uuid.UUID) ([]database.Tai
19751975
return nil, ErrUnimplemented
19761976
}
19771977

1978+
func (q *FakeQuerier) GetTailnetAgentsForClient(ctx context.Context, id uuid.UUID) ([]database.TailnetAgent, error) {
1979+
panic("not implemented")
1980+
}
1981+
19781982
func (*FakeQuerier) GetTailnetClientsForAgent(context.Context, uuid.UUID) ([]database.TailnetClient, error) {
19791983
return nil, ErrUnimplemented
19801984
}

coderd/database/dbmetrics/dbmetrics.go

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/dbmock/dbmock.go

+15
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/dbtestutil/db.go

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
"github.com/coder/coder/v2/coderd/database"
1212
"github.com/coder/coder/v2/coderd/database/dbfake"
13+
"github.com/coder/coder/v2/coderd/database/migrations"
1314
"github.com/coder/coder/v2/coderd/database/postgres"
1415
"github.com/coder/coder/v2/coderd/database/pubsub"
1516
)
@@ -42,6 +43,9 @@ func NewDB(t testing.TB) (database.Store, pubsub.Pubsub) {
4243
})
4344
db = database.New(sqlDB)
4445

46+
err = migrations.Up(sqlDB)
47+
require.NoError(t, err)
48+
4549
ps, err = pubsub.New(context.Background(), sqlDB, connectionURL)
4650
require.NoError(t, err)
4751
t.Cleanup(func() {

coderd/database/dump.sql

+8-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
BEGIN;
2+
3+
-- ALTER TABLE
4+
-- tailnet_clients
5+
-- ADD COLUMN
6+
-- agent_id uuid;
7+
8+
UPDATE
9+
tailnet_clients
10+
SET
11+
-- grab just the first agent_id, or default to an empty UUID.
12+
agent_id = COALESCE(agent_ids[0], '00000000-0000-0000-0000-000000000000'::uuid);
13+
14+
ALTER TABLE
15+
tailnet_clients
16+
DROP COLUMN
17+
agent_ids;
18+
19+
COMMIT;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
BEGIN;
2+
3+
ALTER TABLE
4+
tailnet_clients
5+
ADD COLUMN
6+
agent_ids uuid[];
7+
8+
UPDATE
9+
tailnet_clients
10+
SET
11+
agent_ids = ARRAY[agent_id]::uuid[];
12+
13+
ALTER TABLE
14+
tailnet_clients
15+
ALTER COLUMN
16+
agent_ids SET NOT NULL;
17+
18+
19+
CREATE INDEX idx_tailnet_clients_agent_ids ON tailnet_clients USING GIN (agent_ids);
20+
21+
CREATE OR REPLACE FUNCTION tailnet_notify_client_change() RETURNS trigger
22+
LANGUAGE plpgsql
23+
AS $$
24+
BEGIN
25+
-- check new first to get the updated agent ids.
26+
IF (NEW IS NOT NULL) THEN
27+
PERFORM pg_notify('tailnet_client_update', NEW.id || ',' || array_to_string(NEW.agent_ids, ','));
28+
RETURN NULL;
29+
END IF;
30+
IF (OLD IS NOT NULL) THEN
31+
PERFORM pg_notify('tailnet_client_update', OLD.id || ',' || array_to_string(OLD.agent_ids, ','));
32+
RETURN NULL;
33+
END IF;
34+
END;
35+
$$;
36+
37+
ALTER TABLE
38+
tailnet_clients
39+
DROP COLUMN
40+
agent_id;
41+
42+
COMMIT;

coderd/database/models.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/querier.go

+2-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries.sql.go

+51-14
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries/tailnet.sql

+13-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ INSERT INTO
33
tailnet_clients (
44
id,
55
coordinator_id,
6-
agent_id,
6+
agent_ids,
77
node,
88
updated_at
99
)
@@ -13,7 +13,7 @@ ON CONFLICT (id, coordinator_id)
1313
DO UPDATE SET
1414
id = $1,
1515
coordinator_id = $2,
16-
agent_id = $3,
16+
agent_ids = $3,
1717
node = $4,
1818
updated_at = now() at time zone 'utc'
1919
RETURNING *;
@@ -59,19 +59,27 @@ SELECT *
5959
FROM tailnet_agents
6060
WHERE id = $1;
6161

62+
-- name: GetTailnetAgentsForClient :many
63+
SELECT *
64+
FROM tailnet_agents
65+
WHERE agent_ids IN (
66+
SELECT UNNEST(agent_ids)
67+
FROM tailnet_clients
68+
WHERE tailnet_clients.id = $1
69+
);
70+
6271
-- name: GetAllTailnetAgents :many
6372
SELECT *
6473
FROM tailnet_agents;
6574

6675
-- name: GetTailnetClientsForAgent :many
6776
SELECT *
6877
FROM tailnet_clients
69-
WHERE agent_id = $1;
78+
WHERE $1::uuid = ANY(agent_ids);
7079

7180
-- name: GetAllTailnetClients :many
7281
SELECT *
73-
FROM tailnet_clients
74-
ORDER BY agent_id;
82+
FROM tailnet_clients;
7583

7684
-- name: UpsertTailnetCoordinator :one
7785
INSERT INTO

0 commit comments

Comments
 (0)