Skip to content

Commit 65e6a18

Browse files
committed
feat: support platform override
Support setting `platform` for dependencies and also allow overriding `platform` for a specific package. Signed-off-by: Noel Georgi <[email protected]>
1 parent 6eaf7ab commit 65e6a18

File tree

13 files changed

+130
-41
lines changed

13 files changed

+130
-41
lines changed

Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
# syntax = docker/dockerfile-upstream:1.6.0-labs
1+
# syntax = docker/dockerfile-upstream:1.7.0-labs
22

33
# THIS FILE WAS AUTOMATICALLY GENERATED, PLEASE DO NOT EDIT.
44
#
5-
# Generated on 2024-03-05T05:52:53Z by kres latest.
5+
# Generated on 2024-03-06T10:32:46Z by kres latest.
66

77
ARG TOOLCHAIN
88

Makefile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# THIS FILE WAS AUTOMATICALLY GENERATED, PLEASE DO NOT EDIT.
22
#
3-
# Generated on 2024-03-05T05:52:53Z by kres latest.
3+
# Generated on 2024-03-06T10:32:46Z by kres latest.
44

55
# common variables
66

@@ -14,15 +14,15 @@ WITH_RACE ?= false
1414
REGISTRY ?= ghcr.io
1515
USERNAME ?= siderolabs
1616
REGISTRY_AND_USERNAME ?= $(REGISTRY)/$(USERNAME)
17-
PROTOBUF_GO_VERSION ?= 1.32.0
17+
PROTOBUF_GO_VERSION ?= 1.33.0
1818
GRPC_GO_VERSION ?= 1.3.0
1919
GRPC_GATEWAY_VERSION ?= 2.19.1
2020
VTPROTOBUF_VERSION ?= 0.6.0
2121
DEEPCOPY_VERSION ?= v0.5.6
2222
GOLANGCILINT_VERSION ?= v1.56.2
2323
GOFUMPT_VERSION ?= v0.6.0
24-
GO_VERSION ?= 1.22.0
25-
GOIMPORTS_VERSION ?= v0.18.0
24+
GO_VERSION ?= 1.22.1
25+
GOIMPORTS_VERSION ?= v0.19.0
2626
GO_BUILDFLAGS ?=
2727
GO_LDFLAGS ?=
2828
CGO_ENABLED ?= 0

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,8 @@ Additionally, hermetic text functions from [Sprig](http://masterminds.github.io/
227227
On the root level, following properties are available:
228228

229229
- `name` (*str*, *required*): name of the package, also used to reference this package from other packages as dependency.
230+
- `platform` (*str*, *optional*): platform override for the build.
231+
If not set, defaults to the platform of the build.
230232
- `variant` (*str*, *optional*): variant of the base image of the build.
231233
Two variants are available:
232234
- `alpine`: Alpine Linux 3.16 image with `bash` package pre-installed
@@ -270,6 +272,8 @@ Properties:
270272
Contents of the stage are poured into the build at the location specified with `to:` parameter.
271273
- `image` (*str*, *external dependency*): reference to the registry container image this package depends on.
272274
Contents of the image are poured into the build at the location specified with `to:` parameter.
275+
- `platform` (*str*, *optional*): platform to pull the image for.
276+
If not set, defaults to the platform of the build.
273277
- `runtime` (*bool*, *optional*): if set, marks dependency as runtime.
274278
This means that when this package is pulled in into the build, all the runtime dependencies are pulled in automatically as well.
275279
This also applies to transitive runtime dependencies.

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ require (
66
github.com/Masterminds/semver v1.5.0
77
github.com/Masterminds/sprig/v3 v3.2.3
88
github.com/alessio/shellescape v1.4.2
9-
github.com/containerd/containerd v1.7.11
9+
github.com/containerd/containerd v1.7.13
1010
github.com/emicklei/dot v1.6.1
1111
github.com/google/go-github/v60 v60.0.0
1212
github.com/hashicorp/go-multierror v1.1.1

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ github.com/Microsoft/hcsshim v0.11.4 h1:68vKo2VN8DE9AdN4tnkWnmdhqdbpUFM8OF3Airm7
1212
github.com/Microsoft/hcsshim v0.11.4/go.mod h1:smjE4dvqPX9Zldna+t5FG3rnoHhaB7QYxPRqGcpAD9w=
1313
github.com/alessio/shellescape v1.4.2 h1:MHPfaU+ddJ0/bYWpgIeUnQUqKrlJ1S7BfEYPM4uEoM0=
1414
github.com/alessio/shellescape v1.4.2/go.mod h1:PZAiSCk0LJaZkiCSkPv8qIobYglO3FPpyFjDCtHLS30=
15-
github.com/containerd/containerd v1.7.11 h1:lfGKw3eU35sjV0aG2eYZTiwFEY1pCzxdzicHP3SZILw=
16-
github.com/containerd/containerd v1.7.11/go.mod h1:5UluHxHTX2rdvYuZ5OJTC5m/KJNs0Zs9wVoJm9zf5ZE=
15+
github.com/containerd/containerd v1.7.13 h1:wPYKIeGMN8vaggSKuV1X0wZulpMz4CrgEsZdaCyB6Is=
16+
github.com/containerd/containerd v1.7.13/go.mod h1:zT3up6yTRfEUa6+GsITYIJNgSVL9NQ4x4h1RPzk0Wu4=
1717
github.com/containerd/continuity v0.4.2 h1:v3y/4Yz5jwnvqPKJJ+7Wf93fyWoCB3F5EclWG023MDM=
1818
github.com/containerd/continuity v0.4.2/go.mod h1:F6PTNCKepoxEaXLQp3wDAjygEnImnZ/7o4JzpodfroQ=
1919
github.com/containerd/log v0.1.0 h1:TCJt7ioM2cr/tfR8GPbGf9/VRAX8D2B4PjzCpfX540I=

internal/pkg/convert/graph.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212

1313
"github.com/siderolabs/bldr/internal/pkg/constants"
1414
"github.com/siderolabs/bldr/internal/pkg/environment"
15+
"github.com/siderolabs/bldr/internal/pkg/platform"
1516
"github.com/siderolabs/bldr/internal/pkg/solver"
1617
"github.com/siderolabs/bldr/internal/pkg/types/v1alpha2"
1718
)
@@ -29,19 +30,24 @@ type GraphLLB struct {
2930
LocalContext llb.State
3031

3132
baseImageProcessor llbProcessor
32-
cache map[*solver.PackageNode]llb.State
33+
cache map[cacheKey]llb.State
3334

3435
commonRunOptions []llb.RunOption
3536
}
3637

38+
type cacheKey struct {
39+
*solver.PackageNode
40+
Platform string
41+
}
42+
3743
type llbProcessor func(llb.State) llb.State
3844

3945
// NewGraphLLB creates new GraphLLB and initializes shared images.
4046
func NewGraphLLB(graph *solver.PackageGraph, options *environment.Options) *GraphLLB {
4147
result := &GraphLLB{
4248
PackageGraph: graph,
4349
Options: options,
44-
cache: make(map[*solver.PackageNode]llb.State),
50+
cache: make(map[cacheKey]llb.State),
4551
}
4652

4753
if options.ProxyEnv != nil {
@@ -86,9 +92,12 @@ func (graph *GraphLLB) buildBaseImages() {
8692
return addEnv(addPkg(root))
8793
}
8894

95+
platform, _ := platform.ToV1Platform(graph.Root.Pkg.Platform, graph.Options.TargetPlatform.String()) //nolint:errcheck
96+
8997
graph.BaseImages[v1alpha2.Alpine] = graph.baseImageProcessor(llb.Image(
9098
constants.DefaultBaseImage,
9199
llb.WithCustomName(graph.Options.CommonPrefix+"base"),
100+
llb.Platform(platform),
92101
).Run(
93102
append(graph.commonRunOptions,
94103
llb.Shlex("apk --no-cache --update add bash"),
@@ -105,9 +114,12 @@ func (graph *GraphLLB) buildBaseImages() {
105114
}
106115

107116
func (graph *GraphLLB) buildChecksummer() {
117+
platform, _ := platform.ToV1Platform(graph.Root.Pkg.Platform, graph.Options.TargetPlatform.String()) //nolint:errcheck
118+
108119
graph.Checksummer = llb.Image(
109120
constants.DefaultBaseImage,
110121
llb.WithCustomName(graph.Options.CommonPrefix+"cksum"),
122+
llb.Platform(platform),
111123
).Run(
112124
append(graph.commonRunOptions,
113125
llb.Shlex("apk --no-cache --update add coreutils"),
@@ -132,7 +144,7 @@ func (graph *GraphLLB) buildLocalContext() {
132144

133145
// Build converts package graph to LLB.
134146
func (graph *GraphLLB) Build() (llb.State, error) {
135-
return NewNodeLLB(graph.Root, graph).Build()
147+
return NewNodeLLB(graph.Root, graph, graph.Root.Pkg.Platform).Build()
136148
}
137149

138150
// Marshal returns marshaled LLB.

internal/pkg/convert/node.go

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ import (
1212

1313
"github.com/moby/buildkit/client/llb"
1414
"github.com/opencontainers/go-digest"
15-
v1 "github.com/opencontainers/image-spec/specs-go/v1"
1615
"github.com/siderolabs/gen/xslices"
1716

1817
"github.com/siderolabs/bldr/internal/pkg/constants"
1918
"github.com/siderolabs/bldr/internal/pkg/environment"
19+
"github.com/siderolabs/bldr/internal/pkg/platform"
2020
"github.com/siderolabs/bldr/internal/pkg/solver"
2121
"github.com/siderolabs/bldr/internal/pkg/types/v1alpha2"
2222
)
@@ -55,17 +55,24 @@ func defaultCopyOptions(options *environment.Options, reproducible bool) *llb.Co
5555
type NodeLLB struct {
5656
*solver.PackageNode
5757

58-
Graph *GraphLLB
59-
Prefix string
58+
Graph *GraphLLB
59+
Prefix string
60+
Platform string
6061
}
6162

6263
// NewNodeLLB wraps PackageNode for LLB conversion.
63-
func NewNodeLLB(node *solver.PackageNode, graph *GraphLLB) *NodeLLB {
64+
func NewNodeLLB(node *solver.PackageNode, graph *GraphLLB, platformOverride string) *NodeLLB {
65+
// set default platform if not set
66+
if platformOverride == "" {
67+
platformOverride = graph.Options.TargetPlatform.String()
68+
}
69+
6470
return &NodeLLB{
6571
PackageNode: node,
6672

67-
Graph: graph,
68-
Prefix: graph.Options.CommonPrefix + node.Name + ":",
73+
Graph: graph,
74+
Prefix: graph.Options.CommonPrefix + node.Name + ":",
75+
Platform: platformOverride,
6976
}
7077
}
7178

@@ -96,26 +103,9 @@ func (node *NodeLLB) context(root llb.State) llb.State {
96103
)
97104
}
98105

99-
func (node *NodeLLB) convertPlatform(platform string) (v1.Platform, error) {
100-
switch platform {
101-
case "linux/amd64":
102-
return v1.Platform{
103-
OS: "linux",
104-
Architecture: "amd64",
105-
}, nil
106-
case "linux/arm64":
107-
return v1.Platform{
108-
OS: "linux",
109-
Architecture: "arm64",
110-
}, nil
111-
default:
112-
return v1.Platform{}, fmt.Errorf("unknown platform %q", platform)
113-
}
114-
}
115-
116106
func (node *NodeLLB) convertDependency(dep solver.PackageDependency) (depState llb.State, srcName string, err error) {
117107
if dep.IsInternal() {
118-
depState, err = NewNodeLLB(dep.Node, node.Graph).Build()
108+
depState, err = NewNodeLLB(dep.Node, node.Graph, node.Pkg.Platform).Build()
119109
if err != nil {
120110
return llb.Scratch(), "", err
121111
}
@@ -126,7 +116,7 @@ func (node *NodeLLB) convertDependency(dep solver.PackageDependency) (depState l
126116
srcName = dep.Image
127117

128118
if dep.Platform != "" {
129-
platform, err := node.convertPlatform(dep.Platform)
119+
platform, err := platform.ToV1Platform(dep.Platform, "")
130120
if err != nil {
131121
return llb.Scratch(), "", err
132122
}
@@ -160,11 +150,18 @@ func (node *NodeLLB) dependencies(root llb.State) (llb.State, error) {
160150
stages := []llb.State{root}
161151

162152
for _, dep := range deps {
163-
if _, alreadyProcessed := seen[dep.ID()]; alreadyProcessed {
153+
depID := dep.ID() + node.Platform
154+
155+
// set dep platform to node platform if not set
156+
if dep.Platform == "" {
157+
dep.Platform = node.Platform
158+
}
159+
160+
if _, alreadyProcessed := seen[depID]; alreadyProcessed {
164161
continue
165162
}
166163

167-
seen[dep.ID()] = struct{}{}
164+
seen[depID] = struct{}{}
168165

169166
depState, srcName, err := node.convertDependency(dep)
170167
if err != nil {
@@ -328,7 +325,12 @@ func (node *NodeLLB) finalize(root llb.State) llb.State {
328325

329326
// Build converts PackageNode to buildkit LLB.
330327
func (node *NodeLLB) Build() (llb.State, error) {
331-
if state, ok := node.Graph.cache[node.PackageNode]; ok {
328+
cacheSt := cacheKey{
329+
PackageNode: node.PackageNode,
330+
Platform: node.Platform,
331+
}
332+
333+
if state, ok := node.Graph.cache[cacheSt]; ok {
332334
return state, nil
333335
}
334336

@@ -348,7 +350,7 @@ func (node *NodeLLB) Build() (llb.State, error) {
348350

349351
root = node.finalize(root)
350352

351-
node.Graph.cache[node.PackageNode] = root
353+
node.Graph.cache[cacheSt] = root
352354

353355
return root, nil
354356
}

internal/pkg/integration/testdata/arch-amd64/final/pkg.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
---
22
name: final
3+
dependencies:
4+
# this arm64 variant of the image only contains the u-boot binary,
5+
# so this is a good test to ensure that we can copy the binary from
6+
# the arm64 image to the x86_64 image
7+
- image: ghcr.io/siderolabs/u-boot:v1.7.0-alpha.0-31-gcb39126
8+
platform: linux/arm64
9+
from: /rpi_generic
310
steps:
411
- test:
512
- test "${BUILD:-x}" = "x86_64-linux-musl"
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# syntax = SHEBANG
2+
3+
format: v1alpha2
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
name: final
3+
platform: linux/arm64
4+
steps:
5+
- test:
6+
- test `uname -m` = "aarch64"
7+
finalize:
8+
- from: /
9+
to: /

0 commit comments

Comments
 (0)