-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.go
274 lines (235 loc) · 7.46 KB
/
controller.go
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
// Copyright 2022 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package schema
// ControllerDDL is used to create the controller database schema at bootstrap.
func ControllerDDL() []string {
schemas := []func() string{
leaseSchema,
changeLogSchema,
cloudSchema,
externalControllerSchema,
}
var deltas []string
for _, fn := range schemas {
deltas = append(deltas, fn())
}
return deltas
}
func leaseSchema() string {
return `
CREATE TABLE lease_type (
id INT PRIMARY KEY,
type TEXT
);
CREATE UNIQUE INDEX idx_lease_type_type
ON lease_type (type);
INSERT INTO lease_type VALUES
(0, 'singular-controller'), -- The controller running singular controller/model workers.
(1, 'application-leadership'); -- The unit that holds leadership for an application.
CREATE TABLE lease (
uuid TEXT PRIMARY KEY,
lease_type_id INT NOT NULL,
model_uuid TEXT,
name TEXT,
holder TEXT,
start TIMESTAMP,
expiry TIMESTAMP,
CONSTRAINT fk_lease_lease_type
FOREIGN KEY (lease_type_id)
REFERENCES lease_type(id)
);
CREATE UNIQUE INDEX idx_lease_model_type_name
ON lease (model_uuid, lease_type_id, name);
CREATE INDEX idx_lease_expiry
ON lease (expiry);
CREATE TABLE lease_pin (
-- The presence of entries in this table for a particular lease_uuid
-- implies that the lease in question is pinned and cannot expire.
uuid TEXT PRIMARY KEY,
lease_uuid TEXT,
entity_id TEXT,
CONSTRAINT fk_lease_pin_lease
FOREIGN KEY (lease_uuid)
REFERENCES lease(uuid)
);
CREATE UNIQUE INDEX idx_lease_pin_lease_entity
ON lease_pin (lease_uuid, entity_id);
CREATE INDEX idx_lease_pin_lease
ON lease_pin (lease_uuid);
`[1:]
}
func changeLogSchema() string {
return `
CREATE TABLE change_log_edit_type (
id INT PRIMARY KEY,
edit_type TEXT
);
CREATE UNIQUE INDEX idx_change_log_edit_type_edit_type
ON change_log_edit_type (edit_type);
-- The change log type values are bitmasks, so that multiple types can be
-- expressed when looking for changes.
INSERT INTO change_log_edit_type VALUES
(1, 'create'),
(2, 'update'),
(4, 'delete');
CREATE TABLE change_log_namespace (
id INT PRIMARY KEY,
namespace TEXT
);
CREATE UNIQUE INDEX idx_change_log_namespace_namespace
ON change_log_namespace (namespace);
INSERT INTO change_log_namespace VALUES
(1, 'external_controller');
CREATE TABLE change_log (
id INTEGER PRIMARY KEY AUTOINCREMENT,
edit_type_id INT NOT NULL,
namespace_id INT NOT NULL,
changed_uuid TEXT NOT NULL,
created_at DATETIME NOT NULL DEFAULT(STRFTIME('%Y-%m-%d %H:%M:%f', 'NOW', 'utc')),
CONSTRAINT fk_change_log_edit_type
FOREIGN KEY (edit_type_id)
REFERENCES change_log_edit_type(id),
CONSTRAINT fk_change_log_namespace
FOREIGN KEY (namespace_id)
REFERENCES change_log_namespace(id)
);`[1:]
}
func cloudSchema() string {
return `
CREATE TABLE cloud_type (
id INT PRIMARY KEY,
type TEXT
);
CREATE UNIQUE INDEX idx_cloud_type_type
ON cloud_type (type);
-- The list of all the cloud types that are supported for this release. This
-- doesn't indicate whether the cloud type is supported for the current
-- controller, but rather the cloud type is supported in general.
INSERT INTO cloud_type VALUES
(0, 'kubernetes'),
(1, 'lxd'),
(2, 'maas'),
(3, 'manual'),
(4, 'azure'),
(5, 'ec2'),
(6, 'equinix'),
(7, 'gce'),
(8, 'oci'),
(9, 'openstack'),
(10, 'vsphere');
CREATE TABLE auth_type (
id INT PRIMARY KEY,
type TEXT
);
CREATE UNIQUE INDEX idx_auth_type_type
ON auth_type (type);
INSERT INTO auth_type VALUES
(0, 'access-key'),
(1, 'instance-role'),
(2, 'userpass'),
(3, 'oauth1'),
(4, 'oauth2'),
(5, 'jsonfile'),
(6, 'clientcertificate'),
(7, 'httpsig'),
(8, 'interactive'),
(9, 'empty'),
(10, 'certificate'),
(11, 'oauth2withcert');
CREATE TABLE cloud_auth_type (
uuid TEXT PRIMARY KEY,
cloud_uuid TEXT NOT NULL,
auth_type_id INT NOT NULL,
CONSTRAINT fk_cloud_auth_type_cloud
FOREIGN KEY (cloud_uuid)
REFERENCES cloud(uuid),
CONSTRAINT fk_cloud_auth_type_auth_type
FOREIGN KEY (auth_type_id)
REFERENCES auth_type(id)
);
CREATE UNIQUE INDEX idx_cloud_auth_type_cloud_uuid_auth_type_id
ON cloud_auth_type (cloud_uuid, auth_type_id);
CREATE TABLE cloud_region (
uuid TEXT PRIMARY KEY,
cloud_uuid TEXT NOT NULL,
name TEXT NOT NULL,
endpoint TEXT,
identity_endpoint TEXT,
storage_endpoint TEXT,
CONSTRAINT fk_cloud_region_cloud
FOREIGN KEY (cloud_uuid)
REFERENCES cloud(uuid)
);
CREATE UNIQUE INDEX idx_cloud_region_cloud_uuid
ON cloud_region (cloud_uuid);
CREATE TABLE ca_cert (
uuid TEXT PRIMARY KEY,
ca_cert TEXT
);
CREATE TABLE cloud_ca_cert (
uuid TEXT PRIMARY KEY,
cloud_uuid TEXT NOT NULL,
ca_cert_uuid TEXT NOT NULL,
CONSTRAINT fk_cloud_ca_cert_cloud
FOREIGN KEY (cloud_uuid)
REFERENCES cloud(uuid),
CONSTRAINT fk_cloud_ca_cert_ca_cert
FOREIGN KEY (ca_cert_uuid)
REFERENCES ca_cert(uuid)
);
CREATE UNIQUE INDEX idx_cloud_ca_cert_cloud_uuid_ca_cert_uuid
ON cloud_ca_cert (cloud_uuid, ca_cert_uuid);
CREATE TABLE cloud (
uuid TEXT PRIMARY KEY,
name TEXT NOT NULL,
cloud_type_id INT NOT NULL,
endpoint TEXT NOT NULL,
identity_endpoint TEXT,
storage_endpoint TEXT,
skip_tls_verify BOOLEAN NOT NULL
);`[1:]
}
func externalControllerSchema() string {
return `
CREATE TABLE external_controller (
uuid TEXT PRIMARY KEY,
alias TEXT,
ca_cert_uuid TEXT NOT NULL
);
CREATE TABLE external_controller_address (
uuid TEXT PRIMARY KEY,
address TEXT,
controller_uuid TEXT NOT NULL,
CONSTRAINT fk_external_controller_address_external_controller_uuid
FOREIGN KEY (controller_uuid)
REFERENCES external_controller(uuid)
);
CREATE UNIQUE INDEX idx_external_controller_address
ON external_controller_address (uuid, address);
CREATE TABLE external_model (
uuid TEXT PRIMARY KEY,
controller_uuid TEXT NOT NULL,
CONSTRAINT fk_external_model_external_controller_uuid
FOREIGN KEY (controller_uuid)
REFERENCES external_controller(uuid)
);
CREATE TRIGGER trg_log_external_controller_insert
AFTER INSERT ON external_controller FOR EACH ROW
BEGIN
INSERT INTO change_log (edit_type_id, namespace_id, changed_uuid, created_at)
VALUES (1, 1, NEW.uuid, DATETIME('now'));
END;
CREATE TRIGGER trg_log_external_controller_update
AFTER UPDATE ON external_controller FOR EACH ROW
BEGIN
INSERT INTO change_log (edit_type_id, namespace_id, changed_uuid, created_at)
VALUES (2, 1, OLD.uuid, DATETIME('now'));
END;
CREATE TRIGGER trg_log_external_controller_delete
AFTER DELETE ON external_controller FOR EACH ROW
BEGIN
INSERT INTO change_log (edit_type_id, namespace_id, changed_uuid, created_at)
VALUES (4, 1, OLD.uuid, DATETIME('now'));
END;
`[1:]
}