-
Notifications
You must be signed in to change notification settings - Fork 45
/
packages.go
495 lines (442 loc) · 14 KB
/
packages.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
package dag
import (
"context"
"fmt"
"io/fs"
"path/filepath"
"sort"
"strconv"
"strings"
"chainguard.dev/melange/pkg/build"
"chainguard.dev/melange/pkg/config"
"github.com/chainguard-dev/clog"
apk "github.com/chainguard-dev/go-apk/pkg/apk"
)
const (
Local = "local"
)
// Configuration represents a configuration along with the file that sourced it.
// It can be for an origin package, a subpackage, or something that is provided by a package.
// The Configuration field is a pointer to the actual configuration as parsed from a file. The Path field is the
// path to the file from which the configuration was parsed. The Name and Version fields are the name and version
// of the package, subpackage, or provided item. In the case of an origin package, the Name field
// is the same as the Configuration.Package.Name field, and the Version field is the same as
// the Configuration.Package.Version field with the epoch added as `-r<epoch>`. In the case of a
// subpackage or provided item, the Name and Version fields may be different.
type Configuration struct {
*config.Configuration
Path string
name string
version string
// the actual package or subpackage name providing this configuration
// this allows us to distinguish between a subpackge that is providing a virtual and providing itself
pkg string
}
func (c Configuration) String() string {
return fmt.Sprintf("%s-%s", c.name, c.version)
}
func (c Configuration) Name() string {
return c.name
}
func (c Configuration) Version() string {
return c.version
}
func (c Configuration) Source() string {
return Local
}
func (c Configuration) FullName() string {
return fmt.Sprintf("%s-%s-r%d", c.name, c.version, c.Package.Epoch)
}
func (c Configuration) Resolved() bool {
return true
}
// Packages represents a set of package configurations, including
// the parent, or origin, package, its subpackages, and whatever else it 'provides'.
// It contains references from each such origin package, subpackage and provides
// to the origin config.
//
// It also maintains a list of the origin packages.
//
// It does not try to determine relationships and dependencies between packages. For that,
// pass a Packages to NewGraph.
type Packages struct {
configs map[string][]*Configuration
packages map[string][]*Configuration
index map[string]*Configuration
}
var ErrMultipleConfigurations = fmt.Errorf("multiple configurations using the same package name")
func (p *Packages) addPackage(name string, configuration *Configuration) error {
if _, exists := p.packages[name]; exists {
return fmt.Errorf("%s: %w", name, ErrMultipleConfigurations)
}
p.packages[name] = append(p.packages[name], configuration)
return nil
}
func (p *Packages) addConfiguration(name string, configuration *Configuration) error {
p.configs[name] = append(p.configs[name], configuration)
p.index[configuration.String()] = configuration
return nil
}
func (p *Packages) addProvides(c *Configuration, provides []string) error {
for _, prov := range provides {
pctx := &build.PipelineBuild{
Build: &build.Build{
Configuration: *c.Configuration,
},
Package: &c.Package,
}
template, err := build.MutateWith(pctx, nil)
if err != nil {
return err
}
for tmpl, val := range template {
prov = strings.ReplaceAll(prov, tmpl, val)
}
name, version := packageNameFromProvides(prov)
if version == "" {
version = c.version
}
providesc := &Configuration{
Configuration: c.Configuration,
Path: c.Path,
name: name,
version: version, // provides can have own version or inherit package's version
pkg: c.pkg,
}
if err := p.addConfiguration(name, providesc); err != nil {
return err
}
}
return nil
}
// NewPackages reads an fs.FS to get all of the Melange configuration yamls in
// the given directory, and then parses them, including their subpackages and
// 'provides' parameters, to create a Packages struct with all of the
// information, as well as the list of original packages, and, for each such
// package, the source path (yaml) from which it came. The result is a Packages
// struct.
//
// The input is any fs.FS filesystem implementation. Given a directory path, you
// can call NewPackages like this:
//
// NewPackages(ctx, os.DirFS("/path/to/dir"), "/path/to/dir", "./pipelines")
//
// The repetition of the path is necessary because of how the upstream parser in
// melange requires the full path to the directory to be passed in.
func NewPackages(ctx context.Context, fsys fs.FS, dirPath, pipelineDir string) (*Packages, error) {
log := clog.FromContext(ctx)
pkgs := &Packages{
configs: make(map[string][]*Configuration),
packages: make(map[string][]*Configuration),
index: make(map[string]*Configuration),
}
err := fs.WalkDir(fsys, ".", func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
// Skip anything in .github/ and .git/
if path == ".github" {
return fs.SkipDir
}
if path == ".git" {
return fs.SkipDir
}
// Skip .yam.yaml and .melange.k8s.yaml
if d.Type().IsRegular() && path == ".yam.yaml" {
return nil
}
if d.Type().IsRegular() && path == ".melange.k8s.yaml" {
return nil
}
// Skip any file that isn't a yaml file
if !d.Type().IsRegular() || !strings.HasSuffix(path, ".yaml") {
return nil
}
if filepath.Dir(path) != "." && !strings.HasSuffix(path, ".melange.yaml") {
log.With("path", path).Debug("skipping non-melange YAML file")
return nil
}
p := filepath.Join(dirPath, path)
buildc, err := config.ParseConfiguration(ctx, p)
if err != nil {
return err
}
c := &Configuration{
Configuration: buildc,
Path: p,
name: buildc.Package.Name,
version: fullVersion(&buildc.Package),
pkg: buildc.Package.Name,
}
name := c.name
if name == "" {
return fmt.Errorf("no package name in %q", path)
}
if err := pkgs.addConfiguration(name, c); err != nil {
return err
}
if err := pkgs.addPackage(name, c); err != nil {
return err
}
if err := pkgs.addProvides(c, c.Package.Dependencies.Provides); err != nil {
return err
}
for i := range c.Subpackages {
subpkg := c.Subpackages[i]
name := subpkg.Name
if name == "" {
return fmt.Errorf("empty subpackage name at index %d for package %q", i, c.Package.Name)
}
c := &Configuration{
Configuration: buildc,
Path: p,
name: name,
version: fullVersion(&buildc.Package), // subpackages have same version as origin
pkg: name,
}
if err := pkgs.addConfiguration(name, c); err != nil {
return err
}
if err := pkgs.addProvides(c, subpkg.Dependencies.Provides); err != nil {
return err
}
// TODO: resolve deps via `uses` for subpackage pipelines.
}
// Resolve all `uses` used by the pipeline. This updates the set of
// .environment.contents.packages so the next block can include those as build deps.
pctx := &build.PipelineBuild{
Build: &build.Build{
PipelineDirs: []string{pipelineDir},
Configuration: *c.Configuration,
},
Package: &c.Package,
}
for i := range c.Pipeline {
s := &build.PipelineContext{Environment: &pctx.Build.Configuration.Environment, PipelineDirs: []string{pipelineDir}, Pipeline: &c.Pipeline[i]}
if err := s.ApplyNeeds(ctx, pctx); err != nil {
return fmt.Errorf("unable to resolve needs for package %s: %w", name, err)
}
c.Environment.Contents.Packages = pctx.Build.Configuration.Environment.Contents.Packages
}
return nil
})
if err != nil {
return nil, err
}
return pkgs, nil
}
// Config returns the Melange configuration for the package, provides or
// subpackage with the given name, if the package is present in the Graph. If
// it's not present, Config returns an empty list.
//
// Pass packageOnly=true to restruct it just to origin package names.
func (p Packages) Config(name string, packageOnly bool) []*Configuration {
if p.configs == nil {
// this would be unexpected
return nil
}
var (
c []*Configuration
ok bool
)
if packageOnly {
c, ok = p.packages[name]
} else {
c, ok = p.configs[name]
}
if !ok {
return nil
}
list := make([]*Configuration, 0, len(c))
list = append(list, c...)
// sort the list by increasing version
// this should be better about this, perhaps we will use the apko version sorting library in a future revision
sort.Slice(list, func(i, j int) bool {
return fullVersion(&list[i].Package) < fullVersion(&list[j].Package)
})
return list
}
func (p Packages) ConfigByKey(key string) *Configuration {
if len(p.index) == 0 {
return nil
}
c, ok := p.index[key]
if !ok {
return nil
}
return c
}
// PkgConfig returns the melange Configuration for a given package name.
func (p Packages) PkgConfig(pkgName string) *Configuration {
for _, cfg := range p.packages[pkgName] {
if pkgName == cfg.Package.Name {
return cfg
}
}
return nil
}
// PkgInfo returns the build.Package struct for a given package name.
// If no such package name is found in the packages, return nil package and nil error.
func (p Packages) PkgInfo(pkgName string) *config.Package {
if cfg := p.PkgConfig(pkgName); cfg != nil {
return &cfg.Package
}
return nil
}
// Packages returns a slice of every package and subpackage available in the Packages struct,
// sorted alphabetically and then by version, with each package converted to a *apk.RepositoryPackage.
func (p Packages) Packages() []*Configuration {
allPackages := make([]*Configuration, 0, len(p.packages))
for _, byVersion := range p.packages {
allPackages = append(allPackages, byVersion...)
}
// sort for deterministic output
sort.Slice(allPackages, func(i, j int) bool {
if allPackages[i].name == allPackages[j].name {
return allPackages[i].version < allPackages[j].version
}
return allPackages[i].name < allPackages[j].name
})
return allPackages
}
// PackageNames returns a slice of the names of all packages, sorted alphabetically.
func (p Packages) PackageNames() []string {
allPackages := make([]string, 0, len(p.packages))
for name := range p.packages {
allPackages = append(allPackages, name)
}
// sort for deterministic output
sort.Strings(allPackages)
return allPackages
}
// Sub returns a new Packages whose members are the named packages or provides that are listed.
// If a listed element is a provides, automatically includes the origin package that provides it.
// If a listed element is a subpackage, automatically includes the origin package that contains it.
// If a listed element does not exist, returns an error.
func (p Packages) Sub(names ...string) (*Packages, error) {
pkgs := &Packages{
configs: make(map[string][]*Configuration),
index: make(map[string]*Configuration),
packages: make(map[string][]*Configuration),
}
for _, name := range names {
if c, ok := p.configs[name]; ok {
for _, config := range c {
if err := pkgs.addConfiguration(name, config); err != nil {
return nil, err
}
if err := pkgs.addPackage(name, config); err != nil {
return nil, err
}
}
} else {
return nil, fmt.Errorf("package %q not found", name)
}
}
return pkgs, nil
}
func wantArch(have string, want []string) bool {
if len(want) == 0 {
return true
}
for _, a := range want {
if a == have {
return true
}
}
return false
}
// WithArch returns a new Packages whose members are valid for the given arch.
func (p Packages) WithArch(arch string) (*Packages, error) {
pkgs := &Packages{
configs: make(map[string][]*Configuration),
index: p.index,
packages: make(map[string][]*Configuration),
}
for name, c := range p.configs {
for _, config := range c {
if !wantArch(arch, config.Package.TargetArchitecture) {
continue
}
if err := pkgs.addConfiguration(name, config); err != nil {
return nil, err
}
}
}
for name, c := range p.packages {
for _, config := range c {
if !wantArch(arch, config.Package.TargetArchitecture) {
continue
}
if err := pkgs.addPackage(name, config); err != nil {
return nil, err
}
}
}
return pkgs, nil
}
// Repository provide the Packages as a apk.RepositoryWithIndex. To be used in other places that require
// using alpine/go structs instead of ours.
func (p Packages) Repository(arch string) apk.NamedIndex {
repo := apk.NewRepositoryFromComponents(Local, "latest", "", arch)
// Precompute the number of packages to avoid growslice.
size := 0
for _, byVersion := range p.packages {
for _, config := range byVersion {
size++ // top-level package
size += len(config.Subpackages)
}
}
packages := make([]*apk.Package, 0, size)
for _, byVersion := range p.packages {
for _, cfg := range byVersion {
cfg := cfg
packages = append(packages, &apk.Package{
Arch: arch,
Name: cfg.Package.Name,
Version: fullVersion(&cfg.Package),
Description: cfg.Package.Description,
License: cfg.Package.LicenseExpression(),
Origin: cfg.Package.Name,
URL: cfg.Package.URL,
Dependencies: cfg.Environment.Contents.Packages,
Provides: cfg.Package.Dependencies.Provides,
RepoCommit: cfg.Package.Commit,
})
for i := range cfg.Subpackages {
sub := cfg.Subpackages[i]
packages = append(packages, &apk.Package{
Arch: arch,
Name: sub.Name,
Version: fullVersion(&cfg.Package),
Description: sub.Description,
License: cfg.Package.LicenseExpression(),
Origin: cfg.Package.Name,
URL: cfg.Package.URL,
Dependencies: cfg.Environment.Contents.Packages,
Provides: sub.Dependencies.Provides,
RepoCommit: sub.Commit,
})
}
}
}
index := &apk.APKIndex{
Description: "local repository",
Packages: packages,
}
return apk.NewNamedRepositoryWithIndex("", repo.WithIndex(index))
}
func packageNameFromProvides(prov string) (name, version string) {
var ok bool
if name, version, ok = strings.Cut(prov, "~="); ok {
return
}
if name, version, ok = strings.Cut(prov, "="); ok {
return
}
name = prov
return
}
func fullVersion(pkg *config.Package) string {
return pkg.Version + "-r" + strconv.FormatUint(pkg.Epoch, 10)
}