-
Notifications
You must be signed in to change notification settings - Fork 64
/
manager.go
501 lines (412 loc) · 13.1 KB
/
manager.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
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
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2022, Unikraft GmbH and The KraftKit Authors.
// Licensed under the BSD-3-Clause License (the "License").
// You may not use this file except in compliance with the License.
package manifest
import (
"context"
"errors"
"fmt"
"os"
"path/filepath"
"sort"
"strconv"
"time"
"unicode"
"github.com/gobwas/glob"
"github.com/sirupsen/logrus"
"kraftkit.sh/config"
"kraftkit.sh/log"
"kraftkit.sh/pack"
"kraftkit.sh/packmanager"
"kraftkit.sh/unikraft"
"kraftkit.sh/unikraft/component"
)
type manifestManager struct {
manifests []string
indexCache *ManifestIndex
}
// NewManifestManager returns a `packmanager.PackageManager` which manipulates
// Unikraft manifests.
func NewManifestManager(ctx context.Context, opts ...any) (packmanager.PackageManager, error) {
manager := manifestManager{}
for _, mopt := range opts {
opt, ok := mopt.(ManifestManagerOption)
if !ok {
return nil, fmt.Errorf("cannot cast ManifestManager option")
}
if err := opt(ctx, &manager); err != nil {
return nil, err
}
}
// Populate the internal list of manifests with locally saved manifests
for _, manifest := range config.G[config.KraftKit](ctx).Unikraft.Manifests {
// Use implicit knowledge about the fact that by default the official
// manifest is a well-known manifest and does not need the `IsCompatible`
// check, speeding up the general initialization of this method.
if manifest == config.DefaultManifestIndex {
manager.manifests = append(manager.manifests, manifest)
continue
}
if _, compatible, _ := manager.IsCompatible(ctx, manifest,
// During initialization, do not perform a remote check to determine
// whether the provided source is compatible.
packmanager.WithUpdate(false),
); compatible {
manager.manifests = append(manager.manifests, manifest)
}
}
return &manager, nil
}
// update retrieves and returns a cache of the upstream manifest registry
func (m *manifestManager) update(ctx context.Context) (*ManifestIndex, error) {
if len(m.manifests) == 0 {
// In this scenario, re-attempt all manifests provided within the config
// space which were not remotely probed during initialization.
for _, manifest := range config.G[config.KraftKit](ctx).Unikraft.Manifests {
if _, compatible, _ := m.IsCompatible(ctx, manifest,
// During initialization, do not perform a remote check to determine
// whether the provided source is compatible.
packmanager.WithUpdate(true),
); compatible {
m.manifests = append(m.manifests, manifest)
}
}
// If the list of manifests is still zero, then there are really no configs.
if len(m.manifests) == 0 {
return nil, fmt.Errorf("no manifests specified in config")
}
}
index := &ManifestIndex{
LastUpdated: time.Now(),
}
mopts := []ManifestOption{
WithAuthConfig(config.G[config.KraftKit](ctx).Auth),
WithCacheDir(config.G[config.KraftKit](ctx).Paths.Sources),
WithUpdate(true),
}
for _, manipath := range m.manifests {
// If the path of the manipath is the same as the current manifest or it
// resides in the same directory as KraftKit's configured path for manifests
// then we can skip this since we don't want to update ourselves.
// if manipath == m.LocalManifestIndex() || filepath.Dir(manipath) == m.LocalManifestsDir() {
// m.opts.Log.Debugf("skipping: %s", manipath)
// continue
// }
log.G(ctx).WithFields(logrus.Fields{
"manifest": manipath,
}).Debug("fetching")
manifests, err := FindManifestsFromSource(ctx, manipath, mopts...)
if err != nil {
log.G(ctx).Warnf("%s", err)
}
index.Origin = manipath
index.Manifests = append(index.Manifests, manifests...)
}
return index, nil
}
func (m *manifestManager) Update(ctx context.Context) error {
index, err := m.update(ctx)
if err != nil {
return err
}
return m.saveIndex(ctx, index)
}
func (m *manifestManager) saveIndex(ctx context.Context, index *ManifestIndex) error {
m.indexCache = new(ManifestIndex)
*m.indexCache = *index
manifests := make([]*Manifest, len(index.Manifests))
// Create parent directories if not present
if err := os.MkdirAll(filepath.Dir(m.LocalManifestIndex(ctx)), 0o771); err != nil {
return err
}
// TODO: Partition directories when there is a large number of manifests
// TODO: Merge manifests of same name and type?
// Create a file for each manifest
for i, manifest := range index.Manifests {
filename := manifest.Name + ".yaml"
if manifest.Type != unikraft.ComponentTypeCore {
filename = manifest.Type.Plural() + string(filepath.Separator) + filename
}
fileloc := filepath.Join(m.LocalManifestsDir(ctx), filename)
if err := os.MkdirAll(filepath.Dir(fileloc), 0o771); err != nil {
return err
}
log.G(ctx).WithFields(logrus.Fields{
"path": fileloc,
}).Tracef("saving manifest")
if err := manifest.WriteToFile(fileloc); err != nil {
log.G(ctx).Errorf("could not save manifest: %s", err)
}
// Replace manifest with relative path
manifests[i] = &Manifest{
Name: manifest.Name,
Type: manifest.Type,
Manifest: "./" + filename,
}
}
index.Manifests = manifests
// Sort the names of packages alphabetically.
sort.Slice(index.Manifests, func(i, j int) bool {
// Check if we have numbers, sort them accordingly
if z, err := strconv.Atoi(index.Manifests[i].Name); err == nil {
if y, err := strconv.Atoi(index.Manifests[j].Name); err == nil {
return y < z
}
// If we get only one number, alway say its greater than letter
return true
}
// Compare letters normally
return index.Manifests[j].Name > index.Manifests[i].Name
})
return index.WriteToFile(m.LocalManifestIndex(ctx))
}
func (m *manifestManager) SetSources(_ context.Context, sources ...string) error {
m.manifests = sources
return nil
}
func (m *manifestManager) AddSource(ctx context.Context, source string) error {
if m.manifests == nil {
m.manifests = make([]string, 0)
}
m.manifests = append(m.manifests, source)
return nil
}
// Delete implements packmanager.PackageManager.
func (m *manifestManager) Delete(ctx context.Context, qopts ...packmanager.QueryOption) error {
packs, err := m.Catalog(ctx, qopts...)
if err != nil {
return err
}
var errs []error
for _, pack := range packs {
if err := pack.Delete(ctx); err != nil {
errs = append(errs, err)
}
}
// Since the package has been deleted by the underlying package provider (i.e.
// manifest), update the cache index and save this to disk.
m.indexCache, err = NewManifestIndexFromFile(m.LocalManifestIndex(ctx))
if err == nil {
query := packmanager.NewQuery(qopts...)
manifests, err := FindManifestsFromSource(ctx,
m.indexCache.Origin,
WithAuthConfig(query.Auths()),
WithCacheDir(config.G[config.KraftKit](ctx).Paths.Sources),
WithUpdate(query.Update()),
)
if err != nil {
return err
}
m.indexCache.Manifests = manifests
}
if err := m.saveIndex(ctx, m.indexCache); err != nil {
errs = append(errs, err)
}
return errors.Join(errs...)
}
func (m *manifestManager) RemoveSource(ctx context.Context, source string) error {
for i, needle := range m.manifests {
if needle == source {
ret := make([]string, 0)
ret = append(ret, m.manifests[:i]...)
m.manifests = append(ret, m.manifests[i+1:]...)
break
}
}
return nil
}
func (m *manifestManager) Pack(ctx context.Context, c component.Component, opts ...packmanager.PackOption) ([]pack.Package, error) {
return nil, fmt.Errorf("not implemented manifest.manager.Pack")
}
func (m *manifestManager) Unpack(ctx context.Context, p pack.Package, opts ...packmanager.UnpackOption) ([]component.Component, error) {
return nil, fmt.Errorf("not implemented manifest.manager.Unpack")
}
func (m *manifestManager) From(sub pack.PackageFormat) (packmanager.PackageManager, error) {
return nil, fmt.Errorf("method not applicable to manifest manager")
}
func (m *manifestManager) Catalog(ctx context.Context, qopts ...packmanager.QueryOption) ([]pack.Package, error) {
var err error
var manifests []*Manifest
query := packmanager.NewQuery(qopts...)
mopts := []ManifestOption{
WithAuthConfig(query.Auths()),
WithCacheDir(config.G[config.KraftKit](ctx).Paths.Sources),
WithUpdate(query.Update()),
}
log.G(ctx).WithFields(query.Fields()).Debug("querying manifest catalog")
if len(query.Source()) > 0 {
provider, err := NewProvider(ctx, query.Source(), mopts...)
if err != nil {
return nil, err
}
manifests, err = provider.Manifests()
if err != nil {
return nil, err
}
} else if query.Update() {
// If Catalog is executed in multiple successive calls, which occurs when
// searching for multiple packages sequentially, check if the cacheIndex has
// been set. Even if UseCache set has been set, it means that at least once
// call to Catalog has properly updated the index.
if m.indexCache == nil {
indexCache, err := m.update(ctx)
if err != nil {
return nil, err
}
m.indexCache = &ManifestIndex{}
*m.indexCache = *indexCache
}
manifests = m.indexCache.Manifests
} else {
m.indexCache, err = NewManifestIndexFromFile(m.LocalManifestIndex(ctx))
if err == nil {
manifests, err = FindManifestsFromSource(ctx, m.indexCache.Origin, mopts...)
if err != nil {
return nil, err
}
}
}
var packages []pack.Package
var g glob.Glob
types := query.Types()
name := query.Name()
version := query.Version()
if len(name) > 0 {
t, n, v, err := unikraft.GuessTypeNameVersion(name)
// Overwrite additional attributes if pattern-matchable
if err == nil {
name = n
if t != unikraft.ComponentTypeUnknown {
types = append(types, t)
}
if len(v) > 0 {
version = v
}
}
}
g = glob.MustCompile(name)
for _, manifest := range manifests {
if len(types) > 0 {
found := false
for _, t := range types {
if manifest.Type == t {
found = true
break
}
}
if !found {
continue
}
}
if len(query.Source()) > 0 && manifest.Origin != query.Source() {
continue
}
if len(name) > 0 && !g.Match(manifest.Name) {
continue
}
var versions []string
if len(version) > 0 {
if len(manifest.Versions) == 1 && len(manifest.Versions[0].Version) == 0 {
log.G(ctx).Warn("manifest does not supply version")
}
for _, v := range manifest.Versions {
if v.Version == version {
versions = append(versions, v.Version)
break
}
if v.Unikraft == version {
versions = append(versions, v.Unikraft)
break
}
}
if len(versions) == 0 {
for _, channel := range manifest.Channels {
if channel.Name == version {
versions = append(versions, channel.Name)
break
}
}
}
if len(versions) == 0 {
break
}
}
if len(versions) > 0 {
for _, version := range versions {
p, err := NewPackageFromManifestWithVersion(manifest, version, mopts...)
if err != nil {
log.G(ctx).Warn(err)
continue
// TODO: Config option for fast-fail?
// return nil, err
}
packages = append(packages, p)
}
} else {
more, err := NewPackageFromManifest(manifest, mopts...)
if err != nil {
log.G(ctx).Trace(err)
continue
// TODO: Config option for fast-fail?
// return nil, err
}
packages = append(packages, more)
}
}
// Sort packages by name before returning
sort.SliceStable(packages, func(i, j int) bool {
iRunes := []rune(packages[i].Name())
jRunes := []rune(packages[j].Name())
max := len(iRunes)
if max > len(jRunes) {
max = len(jRunes)
}
for idx := 0; idx < max; idx++ {
ir := iRunes[idx]
jr := jRunes[idx]
lir := unicode.ToLower(ir)
ljr := unicode.ToLower(jr)
if lir != ljr {
return lir < ljr
}
// the lowercase runes are the same, so compare the original
if ir != jr {
return ir < jr
}
}
// If the strings are the same up to the length of the shortest string,
// the shorter string comes first
return len(iRunes) < len(jRunes)
})
log.G(ctx).Tracef("found %d/%d matching manifests in catalog", len(packages), len(manifests))
return packages, nil
}
func (m *manifestManager) IsCompatible(ctx context.Context, source string, qopts ...packmanager.QueryOption) (packmanager.PackageManager, bool, error) {
log.G(ctx).WithFields(logrus.Fields{
"source": source,
}).Trace("checking if source is compatible with the manifest manager")
if t, _, _, err := unikraft.GuessTypeNameVersion(source); err == nil && t != unikraft.ComponentTypeUnknown {
return m, true, nil
}
if _, err := NewProvider(ctx, source,
WithUpdate(packmanager.NewQuery(qopts...).Update()),
); err != nil {
return nil, false, fmt.Errorf("incompatible source: %w", err)
}
return m, true, nil
}
// LocalManifestDir returns the user configured path to all the manifests
func (m *manifestManager) LocalManifestsDir(ctx context.Context) string {
if len(config.G[config.KraftKit](ctx).Paths.Manifests) > 0 {
return config.G[config.KraftKit](ctx).Paths.Manifests
}
return filepath.Join(config.DataDir(), "manifests")
}
// LocalManifestIndex returns the user configured path to the manifest index
func (m *manifestManager) LocalManifestIndex(ctx context.Context) string {
return filepath.Join(m.LocalManifestsDir(ctx), "index.yaml")
}
func (m *manifestManager) Format() pack.PackageFormat {
return ManifestFormat
}