Skip to content

feat: add single tailnet support to pgcoord #9351

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 24 commits into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
a20d318
feat: add single tailnet support to pgcoord
coadler Aug 25, 2023
efa2071
fixup! feat: add single tailnet support to pgcoord
coadler Aug 25, 2023
2976c00
fixup! feat: add single tailnet support to pgcoord
coadler Aug 25, 2023
a7b39df
separate table + use channels
coadler Aug 31, 2023
618b0e0
fix migrations
coadler Sep 7, 2023
f50f929
fixup! fix migrations
coadler Sep 7, 2023
3a5bb76
fixup! fix migrations
coadler Sep 8, 2023
3ddc783
fixup! fix migrations
coadler Sep 8, 2023
c84626a
Merge branch 'main' into colin/single-pgcoord
coadler Sep 13, 2023
a0cb904
Merge branch 'main' into colin/single-pgcoord
coadler Sep 14, 2023
dcb007d
add subscriber subsystem
coadler Sep 15, 2023
bdd7ef1
fixup! add subscriber subsystem
coadler Sep 15, 2023
751b22f
fixup! add subscriber subsystem
coadler Sep 15, 2023
3af1af1
fixup! add subscriber subsystem
coadler Sep 15, 2023
7762a73
querier <- subscriber
coadler Sep 19, 2023
08501e6
Merge branch 'main' into colin/single-pgcoord
coadler Sep 19, 2023
eb681ff
fixup! Merge branch 'main' into colin/single-pgcoord
coadler Sep 19, 2023
390e837
fixup! Merge branch 'main' into colin/single-pgcoord
coadler Sep 19, 2023
a1c3acf
fixup! Merge branch 'main' into colin/single-pgcoord
coadler Sep 19, 2023
8256670
fixup! Merge branch 'main' into colin/single-pgcoord
coadler Sep 19, 2023
a75f6f1
add extensive multiagent tests
coadler Sep 20, 2023
d143d2d
use dedicated channels for querier subscribe and closing conns
coadler Sep 20, 2023
036094f
fixup! use dedicated channels for querier subscribe and closing conns
coadler Sep 20, 2023
e8a2b01
fixup! use dedicated channels for querier subscribe and closing conns
coadler Sep 21, 2023
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
Next Next commit
feat: add single tailnet support to pgcoord
  • Loading branch information
coadler committed Sep 7, 2023
commit a20d318c7358936d18698e255dab317e4583c3cb
2 changes: 1 addition & 1 deletion coderd/coderd.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ func New(options *Options) *API {
api.DERPMap,
options.DeploymentValues.DERP.Config.ForceWebSockets.Value(),
func(context.Context) (tailnet.MultiAgentConn, error) {
return (*api.TailnetCoordinator.Load()).ServeMultiAgent(uuid.New()), nil
return (*api.TailnetCoordinator.Load()).ServeMultiAgent(uuid.New())
},
wsconncache.New(api._dialWorkspaceAgentTailnet, 0),
api.TracerProvider,
Expand Down
4 changes: 4 additions & 0 deletions coderd/database/dbtestutil/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/coder/coder/v2/coderd/database"
"github.com/coder/coder/v2/coderd/database/dbfake"
"github.com/coder/coder/v2/coderd/database/migrations"
"github.com/coder/coder/v2/coderd/database/postgres"
"github.com/coder/coder/v2/coderd/database/pubsub"
)
Expand Down Expand Up @@ -42,6 +43,9 @@ func NewDB(t testing.TB) (database.Store, pubsub.Pubsub) {
})
db = database.New(sqlDB)

err = migrations.Up(sqlDB)
require.NoError(t, err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if this is required, how were our tests working before?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is only if you specify a database URL to run tests against. I needed to run a standalone postgres to see the logs and previously it wouldn't run the migrations. I can prob remove this, I'm not sure if it has any greater effects.


ps, err = pubsub.New(context.Background(), sqlDB, connectionURL)
require.NoError(t, err)
t.Cleanup(func() {
Expand Down
15 changes: 8 additions & 7 deletions coderd/database/dump.sql

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
BEGIN;

-- ALTER TABLE
-- tailnet_clients
-- ADD COLUMN
-- agent_id uuid;

UPDATE
tailnet_clients
SET
-- grab just the first agent_id, or default to an empty UUID.
agent_id = COALESCE(agent_ids[0], '00000000-0000-0000-0000-000000000000'::uuid);

ALTER TABLE
tailnet_clients
DROP COLUMN
agent_ids;

COMMIT;
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
BEGIN;

ALTER TABLE
tailnet_clients
ADD COLUMN
agent_ids uuid[];

UPDATE
tailnet_clients
SET
agent_ids = ARRAY[agent_id]::uuid[];

ALTER TABLE
tailnet_clients
ALTER COLUMN
agent_ids SET NOT NULL;


CREATE INDEX idx_tailnet_clients_agent_ids ON tailnet_clients USING GIN (agent_ids);

CREATE OR REPLACE FUNCTION tailnet_notify_client_change() RETURNS trigger
LANGUAGE plpgsql
AS $$
BEGIN
-- check new first to get the updated agent ids.
IF (NEW IS NOT NULL) THEN
PERFORM pg_notify('tailnet_client_update', NEW.id || ',' || array_to_string(NEW.agent_ids, ','));
RETURN NULL;
END IF;
IF (OLD IS NOT NULL) THEN
PERFORM pg_notify('tailnet_client_update', OLD.id || ',' || array_to_string(OLD.agent_ids, ','));
RETURN NULL;
END IF;
END;
$$;

ALTER TABLE
tailnet_clients
DROP COLUMN
agent_id;

COMMIT;
2 changes: 1 addition & 1 deletion coderd/database/models.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/querier.go

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

27 changes: 13 additions & 14 deletions coderd/database/queries.sql.go

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

9 changes: 4 additions & 5 deletions coderd/database/queries/tailnet.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ INSERT INTO
tailnet_clients (
id,
coordinator_id,
agent_id,
agent_ids,
node,
updated_at
)
Expand All @@ -13,7 +13,7 @@ ON CONFLICT (id, coordinator_id)
DO UPDATE SET
id = $1,
coordinator_id = $2,
agent_id = $3,
agent_ids = $3,
node = $4,
updated_at = now() at time zone 'utc'
RETURNING *;
Expand Down Expand Up @@ -66,12 +66,11 @@ FROM tailnet_agents;
-- name: GetTailnetClientsForAgent :many
SELECT *
FROM tailnet_clients
WHERE agent_id = $1;
WHERE $1::uuid = ANY(agent_ids);

-- name: GetAllTailnetClients :many
SELECT *
FROM tailnet_clients
ORDER BY agent_id;
FROM tailnet_clients;

-- name: UpsertTailnetCoordinator :one
INSERT INTO
Expand Down
2 changes: 1 addition & 1 deletion coderd/tailnet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ func setupAgent(t *testing.T, agentAddresses []netip.Prefix) (uuid.UUID, agent.A
derpServer,
func() *tailcfg.DERPMap { return manifest.DERPMap },
false,
func(context.Context) (tailnet.MultiAgentConn, error) { return coord.ServeMultiAgent(uuid.New()), nil },
func(context.Context) (tailnet.MultiAgentConn, error) { return coord.ServeMultiAgent(uuid.New()) },
cache,
trace.NewNoopTracerProvider(),
)
Expand Down
2 changes: 1 addition & 1 deletion enterprise/coderd/workspaceproxycoordinate.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (api *API) workspaceProxyCoordinate(rw http.ResponseWriter, r *http.Request
}

id := uuid.New()
sub := (*api.AGPL.TailnetCoordinator.Load()).ServeMultiAgent(id)
sub, err := (*api.AGPL.TailnetCoordinator.Load()).ServeMultiAgent(id)
ctx, nc := websocketNetConn(ctx, conn, websocket.MessageText)
defer nc.Close()

Expand Down
6 changes: 4 additions & 2 deletions enterprise/coderd/workspaceproxycoordinator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ func Test_agentIsLegacy(t *testing.T) {
defer cancel()

nodeID := uuid.New()
ma := coordinator.ServeMultiAgent(nodeID)
ma, err := coordinator.ServeMultiAgent(nodeID)
require.NoError(t, err)
defer ma.Close()
require.NoError(t, ma.UpdateSelf(&agpl.Node{
ID: 55,
Expand Down Expand Up @@ -123,7 +124,8 @@ func Test_agentIsLegacy(t *testing.T) {
defer cancel()

nodeID := uuid.New()
ma := coordinator.ServeMultiAgent(nodeID)
ma, err := coordinator.ServeMultiAgent(nodeID)
require.NoError(t, err)
defer ma.Close()
require.NoError(t, ma.UpdateSelf(&agpl.Node{
ID: 55,
Expand Down
Loading