Skip to content

Commit

Permalink
feat: support per-dependency arch
Browse files Browse the repository at this point in the history
For internal stages.

Signed-off-by: Andrey Smirnov <andrey.smirnov@siderolabs.com>
Signed-off-by: Noel Georgi <git@frezbo.dev>
  • Loading branch information
smira authored and frezbo committed Mar 7, 2024
1 parent 65e6a18 commit 40aea38
Show file tree
Hide file tree
Showing 21 changed files with 186 additions and 475 deletions.
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,6 @@ Additionally, hermetic text functions from [Sprig](http://masterminds.github.io/
On the root level, following properties are available:

- `name` (*str*, *required*): name of the package, also used to reference this package from other packages as dependency.
- `platform` (*str*, *optional*): platform override for the build.
If not set, defaults to the platform of the build.
- `variant` (*str*, *optional*): variant of the base image of the build.
Two variants are available:
- `alpine`: Alpine Linux 3.16 image with `bash` package pre-installed
Expand Down Expand Up @@ -272,7 +270,7 @@ Properties:
Contents of the stage are poured into the build at the location specified with `to:` parameter.
- `image` (*str*, *external dependency*): reference to the registry container image this package depends on.
Contents of the image are poured into the build at the location specified with `to:` parameter.
- `platform` (*str*, *optional*): platform to pull the image for.
- `platform` (*str*, *optional*): platform to override for the `image` or `stage`.
If not set, defaults to the platform of the build.
- `runtime` (*bool*, *optional*): if set, marks dependency as runtime.
This means that when this package is pulled in into the build, all the runtime dependencies are pulled in automatically as well.
Expand Down
99 changes: 0 additions & 99 deletions cmd/bldr/cmd/llb.go

This file was deleted.

1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ go 1.21.0
require (
github.com/Masterminds/semver v1.5.0
github.com/Masterminds/sprig/v3 v3.2.3
github.com/alessio/shellescape v1.4.2
github.com/containerd/containerd v1.7.13
github.com/emicklei/dot v1.6.1
github.com/google/go-github/v60 v60.0.0
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ github.com/Microsoft/go-winio v0.6.1 h1:9/kr64B9VUZrLm5YYwbGtUJnMgqWVOdUAXu6Migc
github.com/Microsoft/go-winio v0.6.1/go.mod h1:LRdKpFKfdobln8UmuiYcKPot9D2v6svN5+sAH+4kjUM=
github.com/Microsoft/hcsshim v0.11.4 h1:68vKo2VN8DE9AdN4tnkWnmdhqdbpUFM8OF3Airm7fz8=
github.com/Microsoft/hcsshim v0.11.4/go.mod h1:smjE4dvqPX9Zldna+t5FG3rnoHhaB7QYxPRqGcpAD9w=
github.com/alessio/shellescape v1.4.2 h1:MHPfaU+ddJ0/bYWpgIeUnQUqKrlJ1S7BfEYPM4uEoM0=
github.com/alessio/shellescape v1.4.2/go.mod h1:PZAiSCk0LJaZkiCSkPv8qIobYglO3FPpyFjDCtHLS30=
github.com/containerd/containerd v1.7.13 h1:wPYKIeGMN8vaggSKuV1X0wZulpMz4CrgEsZdaCyB6Is=
github.com/containerd/containerd v1.7.13/go.mod h1:zT3up6yTRfEUa6+GsITYIJNgSVL9NQ4x4h1RPzk0Wu4=
github.com/containerd/continuity v0.4.2 h1:v3y/4Yz5jwnvqPKJJ+7Wf93fyWoCB3F5EclWG023MDM=
Expand Down
30 changes: 10 additions & 20 deletions internal/pkg/convert/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (

"github.com/siderolabs/bldr/internal/pkg/constants"
"github.com/siderolabs/bldr/internal/pkg/environment"
"github.com/siderolabs/bldr/internal/pkg/platform"
"github.com/siderolabs/bldr/internal/pkg/solver"
"github.com/siderolabs/bldr/internal/pkg/types/v1alpha2"
)
Expand All @@ -22,6 +21,7 @@ import (
// GraphLLB caches common images used in the build.
type GraphLLB struct {
*solver.PackageGraph
solverFn SolverFunc

Options *environment.Options

Expand All @@ -30,24 +30,20 @@ type GraphLLB struct {
LocalContext llb.State

baseImageProcessor llbProcessor
cache map[cacheKey]llb.State
cache map[*solver.PackageNode]llb.State

commonRunOptions []llb.RunOption
}

type cacheKey struct {
*solver.PackageNode
Platform string
}

type llbProcessor func(llb.State) llb.State

// NewGraphLLB creates new GraphLLB and initializes shared images.
func NewGraphLLB(graph *solver.PackageGraph, options *environment.Options) *GraphLLB {
func NewGraphLLB(graph *solver.PackageGraph, solverFn SolverFunc, options *environment.Options) *GraphLLB {
result := &GraphLLB{
PackageGraph: graph,
Options: options,
cache: make(map[cacheKey]llb.State),
solverFn: solverFn,
cache: make(map[*solver.PackageNode]llb.State),
}

if options.ProxyEnv != nil {
Expand Down Expand Up @@ -92,12 +88,9 @@ func (graph *GraphLLB) buildBaseImages() {
return addEnv(addPkg(root))
}

platform, _ := platform.ToV1Platform(graph.Root.Pkg.Platform, graph.Options.TargetPlatform.String()) //nolint:errcheck

graph.BaseImages[v1alpha2.Alpine] = graph.baseImageProcessor(llb.Image(
constants.DefaultBaseImage,
llb.WithCustomName(graph.Options.CommonPrefix+"base"),
llb.Platform(platform),
).Run(
append(graph.commonRunOptions,
llb.Shlex("apk --no-cache --update add bash"),
Expand All @@ -114,12 +107,9 @@ func (graph *GraphLLB) buildBaseImages() {
}

func (graph *GraphLLB) buildChecksummer() {
platform, _ := platform.ToV1Platform(graph.Root.Pkg.Platform, graph.Options.TargetPlatform.String()) //nolint:errcheck

graph.Checksummer = llb.Image(
constants.DefaultBaseImage,
llb.WithCustomName(graph.Options.CommonPrefix+"cksum"),
llb.Platform(platform),
).Run(
append(graph.commonRunOptions,
llb.Shlex("apk --no-cache --update add coreutils"),
Expand All @@ -143,18 +133,18 @@ func (graph *GraphLLB) buildLocalContext() {
}

// Build converts package graph to LLB.
func (graph *GraphLLB) Build() (llb.State, error) {
return NewNodeLLB(graph.Root, graph, graph.Root.Pkg.Platform).Build()
func (graph *GraphLLB) Build(ctx context.Context) (llb.State, error) {
return NewNodeLLB(graph.Root, graph).Build(ctx)
}

// Marshal returns marshaled LLB.
func (graph *GraphLLB) Marshal() (*llb.Definition, error) {
out, err := graph.Build()
func (graph *GraphLLB) Marshal(ctx context.Context) (*llb.Definition, error) {
out, err := graph.Build(ctx)
if err != nil {
return nil, err
}

out = out.SetMarshalDefaults(graph.Options.BuildPlatform.LLBPlatform)

return out.Marshal(context.TODO())
return out.Marshal(ctx)
}
13 changes: 7 additions & 6 deletions internal/pkg/convert/llb.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@
package convert

import (
"context"

"github.com/moby/buildkit/client/llb"
"github.com/moby/buildkit/frontend/gateway/client"

"github.com/siderolabs/bldr/internal/pkg/environment"
"github.com/siderolabs/bldr/internal/pkg/solver"
)

// BuildLLB translates package graph into LLB DAG.
func BuildLLB(graph *solver.PackageGraph, options *environment.Options) (llb.State, error) {
return NewGraphLLB(graph, options).Build()
}
// SolverFunc can be called to solve the package into the llb state via buildkit.
type SolverFunc func(ctx context.Context, platform environment.Platform, target string) (*client.Result, error)

// MarshalLLB translates package graph into LLB DAG and marshals it.
func MarshalLLB(graph *solver.PackageGraph, options *environment.Options) (*llb.Definition, error) {
return NewGraphLLB(graph, options).Marshal()
func MarshalLLB(ctx context.Context, graph *solver.PackageGraph, solver SolverFunc, options *environment.Options) (*llb.Definition, error) {
return NewGraphLLB(graph, solver, options).Marshal(ctx)
}
Loading

0 comments on commit 40aea38

Please sign in to comment.