-
Notifications
You must be signed in to change notification settings - Fork 9
/
binary.go
105 lines (82 loc) · 3.12 KB
/
binary.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
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package parsing
import (
"context"
"namespacelabs.dev/foundation/internal/fnerrors"
"namespacelabs.dev/foundation/internal/parsing/invariants"
"namespacelabs.dev/foundation/internal/versions"
"namespacelabs.dev/foundation/schema"
"namespacelabs.dev/foundation/std/pkggraph"
)
const Version_GolangBaseVersionMarker = 68
func transformBinary(ctx context.Context, pl EarlyPackageLoader, loc pkggraph.Location, bin *schema.Binary) error {
if bin.PackageName != "" {
return fnerrors.NewWithLocation(loc, "package_name can not be set")
}
if bin.Name == "" {
return fnerrors.NewWithLocation(loc, "binary name can't be empty")
}
if bin.BuildPlan == nil {
return fnerrors.NewWithLocation(loc, "a build plan is required")
}
bin.PackageName = loc.PackageName.String()
if len(bin.GetConfig().GetCommand()) == 0 {
hasGoLayers := false
for _, layer := range bin.BuildPlan.LayerBuildPlan {
if isImagePlanGo(layer) {
hasGoLayers = true
break
}
}
// For Go, by default, assume the binary is built with the same name as the package name.
// TODO: revisit this heuristic.
if hasGoLayers {
if bin.Config == nil {
bin.Config = &schema.BinaryConfig{}
}
bin.Config.Command = []string{"/" + bin.Name}
}
}
for _, layer := range bin.BuildPlan.GetLayerBuildPlan() {
if err := ensureBaseImageDeps(ctx, pl, loc.PackageName, layer); err != nil {
return err
}
}
return nil
}
func isImagePlanGo(plan *schema.ImageBuildPlan) bool {
return plan.GoBuild != nil || plan.GoPackage != ""
}
func ensureBaseImageDeps(ctx context.Context, pl EarlyPackageLoader, owner schema.PackageName, layer *schema.ImageBuildPlan) error {
switch {
case layer.Binary != nil:
return invariants.EnsurePackageLoaded(ctx, pl, owner, layer.Binary)
case layer.GoBuild != nil, layer.GoPackage != "":
if ok, err := hasGolangBase(ctx, pl); err != nil {
return err
} else if !ok {
return fnerrors.InternalError("the current ns version requires a namespacelabs.dev/foundation dependency with at least version %d", Version_GolangBaseVersionMarker)
}
// XXX dedup
baseImageRef := schema.MakePackageRef("namespacelabs.dev/foundation/library/golang/baseimage", "baseimage")
return invariants.EnsurePackageLoaded(ctx, pl, owner, baseImageRef)
case layer.FilesFrom.GetFrom() != nil:
return ensureBaseImageDeps(ctx, pl, owner, layer.FilesFrom.From)
case layer.MakeFsImage.GetFrom() != nil:
return ensureBaseImageDeps(ctx, pl, owner, layer.MakeFsImage.From)
}
return nil
}
func hasGolangBase(ctx context.Context, pl pkggraph.PackageLoader) (bool, error) {
pkg, err := pl.Resolve(ctx, "namespacelabs.dev/foundation")
if err != nil {
return false, err
}
data, err := versions.LoadAtOrDefaults(pkg.Module.ReadOnlyFS(), "internal/versions/versions.json")
if err != nil {
return false, fnerrors.InternalError("failed to load namespacelabs.dev/foundation version data: %w", err)
}
return data.APIVersion >= Version_GolangBaseVersionMarker, nil
}