-
Notifications
You must be signed in to change notification settings - Fork 239
/
load.go
384 lines (333 loc) · 9.47 KB
/
load.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
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
package sqlite
import (
"database/sql"
"encoding/json"
"fmt"
"strings"
_ "github.com/mattn/go-sqlite3"
"github.com/operator-framework/operator-registry/pkg/registry"
)
type SQLLoader struct {
db *sql.DB
}
var _ registry.Load = &SQLLoader{}
func NewSQLLiteLoader(outFilename string) (*SQLLoader, error) {
db, err := sql.Open("sqlite3", outFilename) // TODO: ?immutable=true
if err != nil {
return nil, err
}
createTable := `
CREATE TABLE IF NOT EXISTS operatorbundle (
name TEXT PRIMARY KEY,
csv TEXT UNIQUE,
bundle TEXT
);
CREATE TABLE IF NOT EXISTS package (
name TEXT PRIMARY KEY,
default_channel TEXT,
FOREIGN KEY(default_channel) REFERENCES channel(name)
);
CREATE TABLE IF NOT EXISTS channel (
name TEXT,
package_name TEXT,
head_operatorbundle_name TEXT,
PRIMARY KEY(name, package_name),
FOREIGN KEY(package_name) REFERENCES package(name),
FOREIGN KEY(head_operatorbundle_name) REFERENCES operatorbundle(name)
);
CREATE TABLE IF NOT EXISTS channel_entry (
entry_id INTEGER PRIMARY KEY,
channel_name TEXT,
package_name TEXT,
operatorbundle_name TEXT,
replaces INTEGER,
depth INTEGER,
FOREIGN KEY(replaces) REFERENCES channel_entry(entry_id) DEFERRABLE INITIALLY DEFERRED,
FOREIGN KEY(channel_name) REFERENCES channel(name),
FOREIGN KEY(package_name) REFERENCES channel(package_name),
FOREIGN KEY(operatorbundle_name) REFERENCES operatorbundle(name)
);
CREATE TABLE IF NOT EXISTS api (
group_name TEXT,
version TEXT,
kind TEXT,
plural TEXT NOT NULL,
PRIMARY KEY(group_name, version, kind)
);
CREATE TABLE IF NOT EXISTS api_provider (
group_name TEXT,
version TEXT,
kind TEXT,
channel_entry_id INTEGER,
FOREIGN KEY(channel_entry_id) REFERENCES channel_entry(entry_id),
FOREIGN KEY(group_name, version, kind) REFERENCES api(group_name, version, kind)
);
CREATE INDEX IF NOT EXISTS replaces ON operatorbundle(json_extract(csv, '$.spec.replaces'));
`
if _, err = db.Exec(createTable); err != nil {
return nil, err
}
return &SQLLoader{db}, nil
}
func (s *SQLLoader) AddOperatorBundle(bundle *registry.Bundle) error {
tx, err := s.db.Begin()
if err != nil {
return err
}
stmt, err := tx.Prepare("insert into operatorbundle(name, csv, bundle) values(?, ?, ?)")
if err != nil {
return err
}
defer stmt.Close()
csvName, csvBytes, bundleBytes, err := bundle.Serialize()
if err != nil {
return err
}
if _, err := stmt.Exec(csvName, csvBytes, bundleBytes); err != nil {
return err
}
return tx.Commit()
}
func (s *SQLLoader) AddPackageChannels(manifest registry.PackageManifest) error {
tx, err := s.db.Begin()
if err != nil {
return err
}
addPackage, err := tx.Prepare("insert into package(name) values(?)")
if err != nil {
return err
}
defer addPackage.Close()
addDefaultChannel, err := tx.Prepare("update package set default_channel = ? where name = ?")
if err != nil {
return err
}
defer addDefaultChannel.Close()
if _, err := addPackage.Exec(manifest.PackageName); err != nil {
return err
}
addChannel, err := tx.Prepare("insert into channel(name, package_name, head_operatorbundle_name) values(?, ?, ?)")
if err != nil {
return err
}
defer addChannel.Close()
hasDefault := false
for _, c := range manifest.Channels {
if _, err := addChannel.Exec(c.Name, manifest.PackageName, c.CurrentCSVName); err != nil {
return err
}
if c.IsDefaultChannel(manifest) {
hasDefault = true
if _, err := addDefaultChannel.Exec(c.Name, manifest.PackageName); err != nil {
return err
}
}
}
if !hasDefault {
return fmt.Errorf("no default channel specified for %s", manifest.PackageName)
}
addChannelEntry, err := tx.Prepare("insert into channel_entry(channel_name, package_name, operatorbundle_name, depth) values(?, ?, ?, ?)")
if err != nil {
return err
}
defer addChannelEntry.Close()
getReplaces, err := tx.Prepare(`
SELECT DISTINCT json_extract(operatorbundle.csv, '$.spec.replaces')
FROM operatorbundle,json_tree(operatorbundle.csv)
WHERE operatorbundle.name IS ?
`)
if err != nil {
return err
}
defer getReplaces.Close()
getSkips, err := tx.Prepare(`
SELECT DISTINCT value
FROM operatorbundle,json_each(operatorbundle.csv, '$.spec.skips')
WHERE operatorbundle.name IS ?
`)
if err != nil {
return err
}
defer getSkips.Close()
addReplaces, err := tx.Prepare("update channel_entry set replaces = ? where entry_id = ?")
if err != nil {
return err
}
defer addReplaces.Close()
for _, c := range manifest.Channels {
res, err := addChannelEntry.Exec(c.Name, manifest.PackageName, c.CurrentCSVName, 0)
if err != nil {
return err
}
currentID, err := res.LastInsertId()
if err != nil {
return err
}
channelEntryCSVName := c.CurrentCSVName
depth := 1
for {
// create skip entries
skipRows, err := getSkips.Query(channelEntryCSVName)
if err != nil {
return err
}
for skipRows.Next() {
var skips sql.NullString
if err := skipRows.Scan(&skips); err != nil {
return err
}
if !skips.Valid || skips.String == "" {
break
}
// add dummy channel entry for the skipped version
skippedChannelEntry, err := addChannelEntry.Exec(c.Name, manifest.PackageName, skips.String, depth)
if err != nil {
return err
}
skippedID, err := skippedChannelEntry.LastInsertId()
if err != nil {
return err
}
// add another channel entry for the parent, which replaces the skipped
synthesizedChannelEntry, err := addChannelEntry.Exec(c.Name, manifest.PackageName, channelEntryCSVName, depth)
if err != nil {
return err
}
synthesizedID, err := synthesizedChannelEntry.LastInsertId()
if err != nil {
return err
}
_, err = addReplaces.Exec(skippedID, synthesizedID)
if err != nil {
return err
}
depth += 1
}
// create real replacement chain
replaceRows, err := getReplaces.Query(channelEntryCSVName)
if err != nil {
return err
}
if replaceRows.Next() {
var replaced sql.NullString
if err := replaceRows.Scan(&replaced); err != nil {
return err
}
if !replaced.Valid || replaced.String == "" {
break
}
replacedChannelEntry, err := addChannelEntry.Exec(c.Name, manifest.PackageName, replaced.String, depth)
if err != nil {
return err
}
replacedID, err := replacedChannelEntry.LastInsertId()
if err != nil {
return err
}
_, err = addReplaces.Exec(replacedID, currentID)
if err != nil {
return err
}
currentID = replacedID
channelEntryCSVName = replaced.String
depth += 1
} else {
return fmt.Errorf("%s specifies replacement that couldn't be found", c.CurrentCSVName)
}
}
}
return tx.Commit()
}
func (s *SQLLoader) AddProvidedAPIs() error {
tx, err := s.db.Begin()
if err != nil {
return err
}
addAPI, err := tx.Prepare("insert or replace into api(group_name, version, kind, plural) values(?, ?, ?, ?)")
if err != nil {
return err
}
defer addAPI.Close()
addAPIProvider, err := tx.Prepare("insert into api_provider(group_name, version, kind, channel_entry_id) values(?, ?, ?, ?)")
if err != nil {
return err
}
defer addAPIProvider.Close()
// get CRD provided APIs
getChannelEntryProvidedAPIs, err := tx.Prepare(`
SELECT DISTINCT channel_entry.entry_id, json_extract(json_each.value, '$.name', '$.version', '$.kind')
FROM channel_entry INNER JOIN operatorbundle,json_each(operatorbundle.csv, '$.spec.customresourcedefinitions.owned')
ON channel_entry.operatorbundle_name = operatorbundle.name`)
if err != nil {
return err
}
defer getChannelEntryProvidedAPIs.Close()
rows, err := getChannelEntryProvidedAPIs.Query()
if err != nil {
return err
}
for rows.Next() {
var channelId sql.NullInt64
var gvkSQL sql.NullString
if err := rows.Scan(&channelId, &gvkSQL); err != nil {
return err
}
apigvk := []string{}
if err := json.Unmarshal([]byte(gvkSQL.String), &apigvk); err != nil {
return err
}
plural, group, err := SplitCRDName(apigvk[0])
if err != nil {
return err
}
if _, err := addAPI.Exec(group, apigvk[1], apigvk[2], plural); err != nil {
return err
}
if _, err := addAPIProvider.Exec(group, apigvk[1], apigvk[2], channelId.Int64); err != nil {
return err
}
}
getChannelEntryProvidedAPIsAPIService, err := tx.Prepare(`
SELECT DISTINCT channel_entry.entry_id, json_extract(json_each.value, '$.group', '$.version', '$.kind', '$.name')
FROM channel_entry INNER JOIN operatorbundle,json_each(operatorbundle.csv, '$.spec.apiservicedefinitions.owned')
ON channel_entry.operatorbundle_name = operatorbundle.name`)
if err != nil {
return err
}
defer getChannelEntryProvidedAPIsAPIService.Close()
rows, err = getChannelEntryProvidedAPIsAPIService.Query()
if err != nil {
return err
}
for rows.Next() {
var channelId sql.NullInt64
var gvkSQL sql.NullString
if err := rows.Scan(&channelId, &gvkSQL); err != nil {
return err
}
apigvk := []string{}
if err := json.Unmarshal([]byte(gvkSQL.String), &apigvk); err != nil {
return err
}
if _, err := addAPI.Exec(apigvk[0], apigvk[1], apigvk[2], apigvk[3]); err != nil {
return err
}
if _, err := addAPIProvider.Exec(apigvk[0], apigvk[1], apigvk[2], channelId.Int64); err != nil {
return err
}
}
return tx.Commit()
}
func (s *SQLLoader) Close() {
s.db.Close()
}
func SplitCRDName(crdName string) (plural, group string, err error) {
pluralGroup := strings.SplitN(crdName, ".", 2)
if len(pluralGroup) != 2 {
err = fmt.Errorf("can't split bad CRD name %s", crdName)
return
}
plural = pluralGroup[0]
group = pluralGroup[1]
return
}