Skip to content

feat: add tables for PGCoordinator v2 #10442

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 1 commit into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
67 changes: 67 additions & 0 deletions coderd/database/dump.sql

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

2 changes: 2 additions & 0 deletions coderd/database/foreign_key_constraint.go

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

14 changes: 14 additions & 0 deletions coderd/database/migrations/000168_pg_coord_tailnet_v2_api.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
BEGIN;

DROP TRIGGER IF EXISTS tailnet_notify_tunnel_change ON tailnet_tunnels;
DROP FUNCTION IF EXISTS tailnet_notify_tunnel_change;
DROP TABLE IF EXISTS tailnet_tunnels;

DROP TRIGGER IF EXISTS tailnet_notify_peer_change ON tailnet_peers;
DROP FUNCTION IF EXISTS tailnet_notify_peer_change;
DROP INDEX IF EXISTS idx_tailnet_peers_coordinator;
DROP TABLE IF EXISTS tailnet_peers;

DROP TYPE IF EXISTS tailnet_status;

COMMIT;
72 changes: 72 additions & 0 deletions coderd/database/migrations/000168_pg_coord_tailnet_v2_api.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
BEGIN;

CREATE TYPE tailnet_status AS ENUM (
'ok',
'lost'
);

CREATE TABLE tailnet_peers (
id uuid NOT NULL,
coordinator_id uuid NOT NULL,
updated_at timestamp with time zone NOT NULL,
node bytea NOT NULL,
status tailnet_status DEFAULT 'ok'::tailnet_status NOT NULL,
PRIMARY KEY (id, coordinator_id),
FOREIGN KEY (coordinator_id) REFERENCES tailnet_coordinators(id) ON DELETE CASCADE
);

-- For shutting down / GC a coordinator
CREATE INDEX idx_tailnet_peers_coordinator ON tailnet_peers (coordinator_id);

-- Any time tailnet_peers table changes, send an update with the affected peer ID.
CREATE FUNCTION tailnet_notify_peer_change() RETURNS trigger
LANGUAGE plpgsql
AS $$
BEGIN
IF (OLD IS NOT NULL) THEN
PERFORM pg_notify('tailnet_peer_update', OLD.id::text);
RETURN NULL;
END IF;
IF (NEW IS NOT NULL) THEN
PERFORM pg_notify('tailnet_peer_update', NEW.id::text);
RETURN NULL;
END IF;
END;
$$;

CREATE TRIGGER tailnet_notify_peer_change
AFTER INSERT OR UPDATE OR DELETE ON tailnet_peers
FOR EACH ROW
EXECUTE PROCEDURE tailnet_notify_peer_change();

CREATE TABLE tailnet_tunnels (
coordinator_id uuid NOT NULL,
-- we don't keep foreign keys for src_id and dst_id because the coordinator doesn't
-- strictly order creating the peers and creating the tunnels
src_id uuid NOT NULL,
dst_id uuid NOT NULL,
updated_at timestamp with time zone NOT NULL,
PRIMARY KEY (coordinator_id, src_id, dst_id),
FOREIGN KEY (coordinator_id) REFERENCES tailnet_coordinators (id) ON DELETE CASCADE
);

CREATE FUNCTION tailnet_notify_tunnel_change() RETURNS trigger
LANGUAGE plpgsql
AS $$
BEGIN
IF (NEW IS NOT NULL) THEN
PERFORM pg_notify('tailnet_tunnel_update', NEW.src_id || ',' || NEW.dst_id);
RETURN NULL;
ELSIF (OLD IS NOT NULL) THEN
PERFORM pg_notify('tailnet_tunnel_update', OLD.src_id || ',' || OLD.dst_id);
RETURN NULL;
END IF;
END;
$$;

CREATE TRIGGER tailnet_notify_tunnel_change
AFTER INSERT OR UPDATE OR DELETE ON tailnet_tunnels
FOR EACH ROW
EXECUTE PROCEDURE tailnet_notify_tunnel_change();

COMMIT;
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
INSERT INTO tailnet_peers
(id, coordinator_id, updated_at, node, status)
VALUES (
'c0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11',
'a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11',
'2023-06-15 10:23:54+00',
'a fake protobuf byte string',
'ok'
);

INSERT INTO tailnet_tunnels
(coordinator_id, src_id, dst_id, updated_at)
VALUES (
'a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11',
'c0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11',
'b0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11',
'2023-06-15 10:23:54+00'
);
73 changes: 73 additions & 0 deletions coderd/database/models.go

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

2 changes: 2 additions & 0 deletions coderd/database/unique_constraint.go

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