-
Notifications
You must be signed in to change notification settings - Fork 40
/
dbinit.sql
1246 lines (1065 loc) · 33.9 KB
/
dbinit.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* dbinit.sql: raw SQL to initialize a database for use by Omicron
*
* It's not clear what the long-term story for managing the database schema will
* be. For now, this file can be used by the test suite and by developers (via
* the "omicron-dev" program) to set up a local database with which to run the
* system.
*/
/*
* Important CockroachDB notes:
*
* The syntax STRING(63) means a Unicode string with at most 63 code points,
* not 63 bytes. In many cases, Nexus itself will validate a string's
* byte count or code points, so it's still reasonable to limit ourselves to
* powers of two (or powers-of-two-minus-one) to improve storage utilization.
*
* For timestamps, CockroachDB's docs recommend TIMESTAMPTZ rather than
* TIMESTAMP. This does not change what is stored with each datum, but
* rather how it's interpreted when clients use it. It should make no
* difference to us, so we stick with the recommendation.
*
* We avoid explicit foreign keys due to this warning from the docs: "Foreign
* key dependencies can significantly impact query performance, as queries
* involving tables with foreign keys, or tables referenced by foreign keys,
* require CockroachDB to check two separate tables. We recommend using them
* sparingly."
*/
/*
* We assume the database and user do not already exist so that we don't
* inadvertently clobber what's there. If they might exist, the user has to
* clear this first.
*
* NOTE: the database and user names MUST be kept in sync with the
* initialization code and dbwipe.sql.
*/
CREATE DATABASE omicron;
CREATE USER omicron;
GRANT INSERT, SELECT, UPDATE, DELETE ON DATABASE omicron to omicron;
/*
* Racks
*/
CREATE TABLE omicron.public.rack (
/* Identity metadata (asset) */
id UUID PRIMARY KEY,
time_created TIMESTAMPTZ NOT NULL,
time_modified TIMESTAMPTZ NOT NULL,
/*
* Identifies if rack management has been transferred from RSS -> Nexus.
* If "false", RSS is still managing sleds, services, and DNS records.
*
* This value is set to "true" when RSS calls the
* "rack_initialization_complete" endpoint on Nexus' internal interface.
*
* See RFD 278 for more detail.
*/
initialized BOOL NOT NULL,
/* Used to configure the updates service URL */
tuf_base_url STRING(512)
);
/*
* Sleds
*/
CREATE TABLE omicron.public.sled (
/* Identity metadata (asset) */
id UUID PRIMARY KEY,
time_created TIMESTAMPTZ NOT NULL,
time_modified TIMESTAMPTZ NOT NULL,
time_deleted TIMESTAMPTZ,
rcgen INT NOT NULL,
/* The IP address and bound port of the sled agent server. */
ip INET NOT NULL,
port INT4 CHECK (port BETWEEN 0 AND 65535) NOT NULL,
/* The last address allocated to an Oxide service on this sled. */
last_used_address INET NOT NULL
);
/*
* Services
*/
CREATE TYPE omicron.public.service_kind AS ENUM (
'internal_dns',
'nexus',
'oximeter'
);
CREATE TABLE omicron.public.service (
/* Identity metadata (asset) */
id UUID PRIMARY KEY,
time_created TIMESTAMPTZ NOT NULL,
time_modified TIMESTAMPTZ NOT NULL,
/* FK into the Sled table */
sled_id UUID NOT NULL,
/* The IP address of the service. */
ip INET NOT NULL,
/* Indicates the type of service. */
kind omicron.public.service_kind NOT NULL
);
/* Add an index which lets us look up the services on a sled */
CREATE INDEX ON omicron.public.service (
sled_id
);
/*
* ZPools of Storage, attached to Sleds.
* Typically these are backed by a single physical disk.
*/
CREATE TABLE omicron.public.Zpool (
/* Identity metadata (asset) */
id UUID PRIMARY KEY,
time_created TIMESTAMPTZ NOT NULL,
time_modified TIMESTAMPTZ NOT NULL,
time_deleted TIMESTAMPTZ,
rcgen INT NOT NULL,
/* FK into the Sled table */
sled_id UUID NOT NULL,
/* TODO: Could also store physical disk FK here */
total_size INT NOT NULL
);
CREATE TYPE omicron.public.dataset_kind AS ENUM (
'crucible',
'cockroach',
'clickhouse'
);
/*
* A dataset of allocated space within a zpool.
*/
CREATE TABLE omicron.public.Dataset (
/* Identity metadata (asset) */
id UUID PRIMARY KEY,
time_created TIMESTAMPTZ NOT NULL,
time_modified TIMESTAMPTZ NOT NULL,
time_deleted TIMESTAMPTZ,
rcgen INT NOT NULL,
/* FK into the Pool table */
pool_id UUID NOT NULL,
/* Contact information for the dataset */
ip INET NOT NULL,
port INT4 CHECK (port BETWEEN 0 AND 65535) NOT NULL,
kind omicron.public.dataset_kind NOT NULL,
/* An upper bound on the amount of space that might be in-use */
size_used INT
);
/* Create an index on the size usage for Crucible's allocation */
CREATE INDEX on omicron.public.Dataset (
size_used
) WHERE size_used IS NOT NULL AND time_deleted IS NULL AND kind = 'crucible';
/*
* A region of space allocated to Crucible Downstairs, within a dataset.
*/
CREATE TABLE omicron.public.Region (
/* Identity metadata (asset) */
id UUID PRIMARY KEY,
time_created TIMESTAMPTZ NOT NULL,
time_modified TIMESTAMPTZ NOT NULL,
/* FK into the Dataset table */
dataset_id UUID NOT NULL,
/* FK into the volume table */
volume_id UUID NOT NULL,
/* Metadata describing the region */
block_size INT NOT NULL,
blocks_per_extent INT NOT NULL,
extent_count INT NOT NULL
);
/*
* Allow all regions belonging to a disk to be accessed quickly.
*/
CREATE INDEX on omicron.public.Region (
volume_id
);
/*
* A volume within Crucible
*/
CREATE TABLE omicron.public.volume (
id UUID PRIMARY KEY,
time_created TIMESTAMPTZ NOT NULL,
time_modified TIMESTAMPTZ NOT NULL,
time_deleted TIMESTAMPTZ,
/* child resource generation number, per RFD 192 */
rcgen INT NOT NULL,
/*
* A JSON document describing the construction of the volume, including all
* sub volumes. This is what will be POSTed to propolis, and eventually
* consumed by some Upstairs code to perform the volume creation. The Rust
* type of this column should be Crucible::VolumeConstructionRequest.
*/
data TEXT NOT NULL
);
/*
* Silos
*/
CREATE TABLE omicron.public.silo (
/* Identity metadata */
id UUID PRIMARY KEY,
name STRING(128) NOT NULL,
description STRING(512) NOT NULL,
time_created TIMESTAMPTZ NOT NULL,
time_modified TIMESTAMPTZ NOT NULL,
time_deleted TIMESTAMPTZ,
discoverable BOOL NOT NULL,
/* child resource generation number, per RFD 192 */
rcgen INT NOT NULL
);
CREATE UNIQUE INDEX ON omicron.public.silo (
name
) WHERE
time_deleted IS NULL;
/*
* Silo users
*/
CREATE TABLE omicron.public.silo_user (
id UUID PRIMARY KEY,
silo_id UUID NOT NULL,
time_created TIMESTAMPTZ NOT NULL,
time_modified TIMESTAMPTZ NOT NULL,
time_deleted TIMESTAMPTZ
);
/* This index lets us quickly find users for a given silo. */
CREATE INDEX ON omicron.public.silo_user (
silo_id,
id
) WHERE
time_deleted IS NULL;
CREATE TYPE omicron.public.provider_type AS ENUM (
'saml'
);
/*
* Silo identity provider list
*/
CREATE TABLE omicron.public.identity_provider (
/* Identity metadata */
id UUID PRIMARY KEY,
name STRING(128) NOT NULL,
description STRING(512) NOT NULL,
time_created TIMESTAMPTZ NOT NULL,
time_modified TIMESTAMPTZ NOT NULL,
time_deleted TIMESTAMPTZ,
silo_id UUID NOT NULL,
provider_type omicron.public.provider_type NOT NULL
);
CREATE INDEX ON omicron.public.identity_provider (
id,
silo_id
) WHERE
time_deleted IS NULL;
CREATE INDEX ON omicron.public.identity_provider (
name,
silo_id
) WHERE
time_deleted IS NULL;
/*
* Silo SAML identity provider
*/
CREATE TABLE omicron.public.saml_identity_provider (
/* Identity metadata */
id UUID PRIMARY KEY,
name STRING(128) NOT NULL,
description STRING(512) NOT NULL,
time_created TIMESTAMPTZ NOT NULL,
time_modified TIMESTAMPTZ NOT NULL,
time_deleted TIMESTAMPTZ,
silo_id UUID NOT NULL,
idp_metadata_document_string TEXT NOT NULL,
idp_entity_id TEXT NOT NULL,
sp_client_id TEXT NOT NULL,
acs_url TEXT NOT NULL,
slo_url TEXT NOT NULL,
technical_contact_email TEXT NOT NULL,
public_cert TEXT,
private_key TEXT
);
CREATE INDEX ON omicron.public.saml_identity_provider (
id,
silo_id
) WHERE
time_deleted IS NULL;
/*
* Users' public SSH keys, per RFD 44
*/
CREATE TABLE omicron.public.ssh_key (
id UUID PRIMARY KEY,
name STRING(63) NOT NULL,
description STRING(512) NOT NULL,
time_created TIMESTAMPTZ NOT NULL,
time_modified TIMESTAMPTZ NOT NULL,
time_deleted TIMESTAMPTZ,
/* FK into silo_user table */
silo_user_id UUID NOT NULL,
/*
* A 4096 bit RSA key without comment encodes to 726 ASCII characters.
* A (256 bit) Ed25519 key w/o comment encodes to 82 ASCII characters.
*/
public_key STRING(1023) NOT NULL
);
CREATE UNIQUE INDEX ON omicron.public.ssh_key (
silo_user_id,
name
) WHERE
time_deleted IS NULL;
/*
* Organizations
*/
CREATE TABLE omicron.public.organization (
/* Identity metadata */
id UUID PRIMARY KEY,
/* FK into Silo table */
silo_id UUID NOT NULL,
name STRING(63) NOT NULL,
description STRING(512) NOT NULL,
time_created TIMESTAMPTZ NOT NULL,
time_modified TIMESTAMPTZ NOT NULL,
/* Indicates that the object has been deleted */
time_deleted TIMESTAMPTZ,
/* child resource generation number, per RFD 192 */
rcgen INT NOT NULL
);
CREATE UNIQUE INDEX ON omicron.public.organization (
silo_id,
name
) WHERE
time_deleted IS NULL;
/*
* Projects
*/
CREATE TABLE omicron.public.project (
/* Identity metadata (resource) */
id UUID PRIMARY KEY,
name STRING(63) NOT NULL,
description STRING(512) NOT NULL,
time_created TIMESTAMPTZ NOT NULL,
time_modified TIMESTAMPTZ NOT NULL,
/* Indicates that the object has been deleted */
time_deleted TIMESTAMPTZ,
/* Which organization this project belongs to */
organization_id UUID NOT NULL /* foreign key into "Organization" table */
);
CREATE UNIQUE INDEX ON omicron.public.project (
organization_id,
name
) WHERE
time_deleted IS NULL;
/*
* Instances
*/
CREATE TYPE omicron.public.instance_state AS ENUM (
'creating',
'starting',
'running',
'stopping',
'stopped',
'rebooting',
'migrating',
'repairing',
'failed',
'destroyed'
);
/*
* TODO consider how we want to manage multiple sagas operating on the same
* Instance -- e.g., reboot concurrent with destroy or concurrent reboots or the
* like. Or changing # of CPUs or memory size.
*/
CREATE TABLE omicron.public.instance (
/* Identity metadata (resource) */
id UUID PRIMARY KEY,
name STRING(63) NOT NULL,
description STRING(512) NOT NULL,
time_created TIMESTAMPTZ NOT NULL,
time_modified TIMESTAMPTZ NOT NULL,
/* Indicates that the object has been deleted */
/* This is redundant for Instances, but we keep it here for consistency. */
time_deleted TIMESTAMPTZ,
/* Every Instance is in exactly one Project at a time. */
project_id UUID NOT NULL,
/* user data for instance initialization systems (e.g. cloud-init) */
user_data BYTES NOT NULL,
/*
* TODO Would it make sense for the runtime state to live in a separate
* table?
*/
/* Runtime state */
state omicron.public.instance_state NOT NULL,
time_state_updated TIMESTAMPTZ NOT NULL,
state_generation INT NOT NULL,
/*
* Server where the VM is currently running, if any. Note that when we
* support live migration, there may be multiple servers associated with
* this Instance, but only one will be truly active. Still, consumers of
* this information should consider whether they also want to know the other
* servers involved in the migration.
*/
active_server_id UUID,
/* Identifies the underlying propolis-server backing the instance. */
active_propolis_id UUID NOT NULL,
active_propolis_ip INET,
/* Identifies the target propolis-server during a migration of the instance. */
target_propolis_id UUID,
/*
* Identifies an ongoing migration for this instance.
*/
migration_id UUID,
/* Instance configuration */
ncpus INT NOT NULL,
memory INT NOT NULL,
hostname STRING(63) NOT NULL
);
CREATE UNIQUE INDEX ON omicron.public.instance (
project_id,
name
) WHERE
time_deleted IS NULL;
/*
* Guest-Visible, Virtual Disks
*/
/*
* TODO The Rust enum to which this type is converted
* carries data in some of its variants, such as the UUID
* of the instance to which a disk is attached.
*
* This makes the conversion to/from this enum type here much
* more difficult, since we need a way to manage that data
* coherently.
*
* See <https://github.com/oxidecomputer/omicron/issues/312>.
*/
-- CREATE TYPE omicron.public.DiskState AS ENUM (
-- 'creating',
-- 'detached',
-- 'attaching',
-- 'attached',
-- 'detaching',
-- 'destroyed',
-- 'faulted'
-- );
CREATE TYPE omicron.public.block_size AS ENUM (
'512',
'2048',
'4096'
);
CREATE TABLE omicron.public.disk (
/* Identity metadata (resource) */
id UUID PRIMARY KEY,
name STRING(63) NOT NULL,
description STRING(512) NOT NULL,
time_created TIMESTAMPTZ NOT NULL,
time_modified TIMESTAMPTZ NOT NULL,
/* Indicates that the object has been deleted */
/* This is redundant for Disks, but we keep it here for consistency. */
time_deleted TIMESTAMPTZ,
rcgen INT NOT NULL,
/* Every Disk is in exactly one Project at a time. */
project_id UUID NOT NULL,
/* Every disk consists of a root volume */
volume_id UUID NOT NULL,
/*
* TODO Would it make sense for the runtime state to live in a separate
* table?
*/
/* Runtime state */
-- disk_state omicron.public.DiskState NOT NULL, /* TODO see above */
disk_state STRING(15) NOT NULL,
/*
* Every Disk may be attaching to, attached to, or detaching from at most
* one Instance at a time.
*/
attach_instance_id UUID,
state_generation INT NOT NULL,
time_state_updated TIMESTAMPTZ NOT NULL,
/* Disk configuration */
size_bytes INT NOT NULL,
block_size omicron.public.block_size NOT NULL,
origin_snapshot UUID,
origin_image UUID
);
CREATE UNIQUE INDEX ON omicron.public.disk (
project_id,
name
) WHERE
time_deleted IS NULL;
CREATE INDEX ON omicron.public.disk (
attach_instance_id
) WHERE
time_deleted IS NULL AND attach_instance_id IS NOT NULL;
CREATE TABLE omicron.public.image (
/* Identity metadata (resource) */
id UUID PRIMARY KEY,
name STRING(63) NOT NULL,
description STRING(512) NOT NULL,
time_created TIMESTAMPTZ NOT NULL,
time_modified TIMESTAMPTZ NOT NULL,
/* Indicates that the object has been deleted */
time_deleted TIMESTAMPTZ,
project_id UUID NOT NULL,
volume_id UUID NOT NULL,
url STRING(8192),
version STRING(64),
digest TEXT,
block_size omicron.public.block_size NOT NULL,
size_bytes INT NOT NULL
);
CREATE UNIQUE INDEX on omicron.public.image (
project_id,
name
) WHERE
time_deleted is NULL;
CREATE TABLE omicron.public.global_image (
/* Identity metadata (resource) */
id UUID PRIMARY KEY,
name STRING(63) NOT NULL,
description STRING(512) NOT NULL,
time_created TIMESTAMPTZ NOT NULL,
time_modified TIMESTAMPTZ NOT NULL,
/* Indicates that the object has been deleted */
time_deleted TIMESTAMPTZ,
volume_id UUID NOT NULL,
url STRING(8192),
distribution STRING(64) NOT NULL,
version STRING(64) NOT NULL,
digest TEXT,
block_size omicron.public.block_size NOT NULL,
size_bytes INT NOT NULL
);
CREATE UNIQUE INDEX on omicron.public.global_image (
name
) WHERE
time_deleted is NULL;
CREATE TABLE omicron.public.snapshot (
/* Identity metadata (resource) */
id UUID PRIMARY KEY,
name STRING(63) NOT NULL,
description STRING(512) NOT NULL,
time_created TIMESTAMPTZ NOT NULL,
time_modified TIMESTAMPTZ NOT NULL,
/* Indicates that the object has been deleted */
time_deleted TIMESTAMPTZ,
/* Every Snapshot is in exactly one Project at a time. */
project_id UUID NOT NULL,
/* Every Snapshot originated from a single disk */
disk_id UUID NOT NULL,
/* Every Snapshot consists of a root volume */
volume_id UUID NOT NULL,
/* Disk configuration (from the time the snapshot was taken) */
size_bytes INT NOT NULL
);
CREATE UNIQUE INDEX ON omicron.public.snapshot (
project_id,
name
) WHERE
time_deleted IS NULL;
/*
* Oximeter collector servers.
*/
CREATE TABLE omicron.public.oximeter (
id UUID PRIMARY KEY,
time_created TIMESTAMPTZ NOT NULL,
time_modified TIMESTAMPTZ NOT NULL,
ip INET NOT NULL,
port INT4 CHECK (port BETWEEN 0 AND 65535) NOT NULL
);
/*
* Information about registered metric producers.
*/
CREATE TABLE omicron.public.metric_producer (
id UUID PRIMARY KEY,
time_created TIMESTAMPTZ NOT NULL,
time_modified TIMESTAMPTZ NOT NULL,
ip INET NOT NULL,
port INT4 CHECK (port BETWEEN 0 AND 65535) NOT NULL,
interval FLOAT NOT NULL,
/* TODO: Is this length appropriate? */
base_route STRING(512) NOT NULL,
/* Oximeter collector instance to which this metric producer is assigned. */
oximeter_id UUID NOT NULL
);
CREATE INDEX ON omicron.public.metric_producer (
oximeter_id,
id
);
/*
* VPCs and networking primitives
*/
CREATE TABLE omicron.public.vpc (
/* Identity metadata (resource) */
id UUID PRIMARY KEY,
name STRING(63) NOT NULL,
description STRING(512) NOT NULL,
time_created TIMESTAMPTZ NOT NULL,
time_modified TIMESTAMPTZ NOT NULL,
/* Indicates that the object has been deleted */
time_deleted TIMESTAMPTZ,
project_id UUID NOT NULL,
system_router_id UUID NOT NULL,
dns_name STRING(63) NOT NULL,
/*
* The Geneve Virtual Network Identifier for this VPC. Note that this is a
* 24-bit unsigned value, properties which are checked in the application,
* not the database.
*/
vni INT4 NOT NULL,
/* The IPv6 prefix allocated to subnets. */
ipv6_prefix INET NOT NULL,
/* Used to ensure that two requests do not concurrently modify the
VPC's firewall */
firewall_gen INT NOT NULL
);
CREATE UNIQUE INDEX ON omicron.public.vpc (
project_id,
name
) WHERE
time_deleted IS NULL;
CREATE UNIQUE INDEX ON omicron.public.vpc (
vni
) WHERE
time_deleted IS NULL;
CREATE TABLE omicron.public.vpc_subnet (
/* Identity metadata (resource) */
id UUID PRIMARY KEY,
name STRING(63) NOT NULL,
description STRING(512) NOT NULL,
time_created TIMESTAMPTZ NOT NULL,
time_modified TIMESTAMPTZ NOT NULL,
/* Indicates that the object has been deleted */
time_deleted TIMESTAMPTZ,
vpc_id UUID NOT NULL,
ipv4_block INET NOT NULL,
ipv6_block INET NOT NULL
);
/* Subnet and network interface names are unique per VPC, not project */
CREATE UNIQUE INDEX ON omicron.public.vpc_subnet (
vpc_id,
name
) WHERE
time_deleted IS NULL;
CREATE TABLE omicron.public.network_interface (
/* Identity metadata (resource) */
id UUID PRIMARY KEY,
name STRING(63) NOT NULL,
description STRING(512) NOT NULL,
time_created TIMESTAMPTZ NOT NULL,
time_modified TIMESTAMPTZ NOT NULL,
/* Indicates that the object has been deleted */
time_deleted TIMESTAMPTZ,
/* FK into Instance table.
* Note that interfaces are always attached to a particular instance.
* IP addresses may be reserved, but this is a different resource.
*/
instance_id UUID NOT NULL,
/* FK into VPC table */
vpc_id UUID NOT NULL,
/* FK into VPCSubnet table. */
subnet_id UUID NOT NULL,
/*
* The EUI-48 MAC address of the guest interface.
*
* Note that we use the bytes of a 64-bit integer, in big-endian byte order
* to represent the MAC.
*/
mac INT8 NOT NULL,
ip INET NOT NULL,
/*
* Limited to 8 NICs per instance. This value must be kept in sync with
* `crate::nexus::MAX_NICS_PER_INSTANCE`.
*/
slot INT2 NOT NULL CHECK (slot >= 0 AND slot < 8),
/* True if this interface is the primary interface for the instance.
*
* The primary interface appears in DNS and its address is used for external
* connectivity for the instance.
*/
is_primary BOOL NOT NULL
);
/* TODO-completeness
* We currently have a NetworkInterface table with the IP and MAC addresses inline.
* Eventually, we'll probably want to move these to their own tables, and
* refer to them here, most notably to support multiple IPs per NIC, as well
* as moving IPs between NICs on different instances, etc.
*/
/* Ensure we do not assign the same address twice within a subnet */
CREATE UNIQUE INDEX ON omicron.public.network_interface (
subnet_id,
ip
) WHERE
time_deleted IS NULL;
/* Ensure we do not assign the same MAC twice within a VPC
* See RFD174's discussion on the scope of virtual MACs
*/
CREATE UNIQUE INDEX ON omicron.public.network_interface (
vpc_id,
mac
) WHERE
time_deleted IS NULL;
/*
* Index used to verify that an Instance's networking is contained
* within a single VPC, and that all interfaces are in unique VPC
* Subnets.
*
* This is also used to quickly find the primary interface for an
* instance, since we store the `is_primary` column. Such queries are
* mostly used when setting a new primary interface for an instance.
*/
CREATE UNIQUE INDEX ON omicron.public.network_interface (
instance_id,
name
)
STORING (vpc_id, subnet_id, is_primary)
WHERE
time_deleted IS NULL;
CREATE TYPE omicron.public.vpc_router_kind AS ENUM (
'system',
'custom'
);
CREATE TABLE omicron.public.vpc_router (
/* Identity metadata (resource) */
id UUID PRIMARY KEY,
name STRING(63) NOT NULL,
description STRING(512) NOT NULL,
time_created TIMESTAMPTZ NOT NULL,
time_modified TIMESTAMPTZ NOT NULL,
/* Indicates that the object has been deleted */
time_deleted TIMESTAMPTZ,
kind omicron.public.vpc_router_kind NOT NULL,
vpc_id UUID NOT NULL,
rcgen INT NOT NULL
);
CREATE UNIQUE INDEX ON omicron.public.vpc_router (
vpc_id,
name
) WHERE
time_deleted IS NULL;
CREATE TYPE omicron.public.vpc_firewall_rule_status AS ENUM (
'disabled',
'enabled'
);
CREATE TYPE omicron.public.vpc_firewall_rule_direction AS ENUM (
'inbound',
'outbound'
);
CREATE TYPE omicron.public.vpc_firewall_rule_action AS ENUM (
'allow',
'deny'
);
CREATE TYPE omicron.public.vpc_firewall_rule_protocol AS ENUM (
'TCP',
'UDP',
'ICMP'
);
CREATE TABLE omicron.public.vpc_firewall_rule (
/* Identity metadata (resource) */
id UUID PRIMARY KEY,
name STRING(63) NOT NULL,
description STRING(512) NOT NULL,
time_created TIMESTAMPTZ NOT NULL,
time_modified TIMESTAMPTZ NOT NULL,
/* Indicates that the object has been deleted */
time_deleted TIMESTAMPTZ,
vpc_id UUID NOT NULL,
status omicron.public.vpc_firewall_rule_status NOT NULL,
direction omicron.public.vpc_firewall_rule_direction NOT NULL,
/* Array of targets. 128 was picked to include plenty of space for
a tag, colon, and resource identifier. */
targets STRING(128)[] NOT NULL,
/* Also an array of targets */
filter_hosts STRING(128)[],
filter_ports STRING(11)[],
filter_protocols omicron.public.vpc_firewall_rule_protocol[],
action omicron.public.vpc_firewall_rule_action NOT NULL,
priority INT4 CHECK (priority BETWEEN 0 AND 65535) NOT NULL
);
CREATE UNIQUE INDEX ON omicron.public.vpc_firewall_rule (
vpc_id,
name
) WHERE
time_deleted IS NULL;
CREATE TYPE omicron.public.router_route_kind AS ENUM (
'default',
'vpc_subnet',
'vpc_peering',
'custom'
);
CREATE TABLE omicron.public.router_route (
/* Identity metadata (resource) */
id UUID PRIMARY KEY,
name STRING(63) NOT NULL,
description STRING(512) NOT NULL,
time_created TIMESTAMPTZ NOT NULL,
time_modified TIMESTAMPTZ NOT NULL,
/* Indicates that the object has been deleted */
time_deleted TIMESTAMPTZ,
vpc_router_id UUID NOT NULL,
kind omicron.public.router_route_kind NOT NULL,
target STRING(128) NOT NULL,
destination STRING(128) NOT NULL
);
CREATE UNIQUE INDEX ON omicron.public.router_route (
vpc_router_id,
name
) WHERE
time_deleted IS NULL;
/*******************************************************************/
/*
* Sagas
*/
/*
* TODO This may eventually have 'paused', 'needs-operator', and 'needs-support'
*/
CREATE TYPE omicron.public.saga_state AS ENUM (
'running',
'unwinding',
'done'
);
CREATE TABLE omicron.public.saga (
/* immutable fields */
/* unique identifier for this execution */
id UUID PRIMARY KEY,
/* unique id of the creator */
creator UUID NOT NULL,
/* name of the saga template name being run */
template_name STRING(127) NOT NULL,
/* time the saga was started */
time_created TIMESTAMPTZ NOT NULL,
/* saga parameters */
saga_params JSONB NOT NULL,
/*
* TODO:
* - id for current SEC (maybe NULL?)
* - time of last adoption
* - previous SEC? previous adoption time?
* - number of adoptions?
*/
saga_state omicron.public.saga_state NOT NULL,
current_sec UUID,
adopt_generation INT NOT NULL,
adopt_time TIMESTAMPTZ NOT NULL
);
/*
* For recovery (and probably takeover), we need to be able to list running
* sagas by SEC. We need to paginate this list by the id.
*/
CREATE UNIQUE INDEX ON omicron.public.saga (
current_sec, id
) WHERE saga_state != 'done';
/*
* TODO more indexes for Saga?
* - Debugging and/or reporting: saga_template_name? creator?
*/
/*
* TODO: This is a data-carrying enum, see note on disk_state.
*
* See <https://github.com/oxidecomputer/omicron/issues/312>.
*/
-- CREATE TYPE omicron.public.saga_node_event_type AS ENUM (
-- 'started',
-- 'succeeded',
-- 'failed'
-- 'undo_started'