Skip to content

Commit

Permalink
feat: support platform override.
Browse files Browse the repository at this point in the history
WIP.

Signed-off-by: Noel Georgi <[email protected]>
  • Loading branch information
frezbo committed Mar 5, 2024
1 parent 6db0478 commit 37679bb
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 13 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ 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.11
github.com/containerd/containerd v1.7.13
github.com/emicklei/dot v1.6.1
github.com/google/go-github/v60 v60.0.0
github.com/hashicorp/go-multierror v1.1.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ github.com/Microsoft/hcsshim v0.11.4 h1:68vKo2VN8DE9AdN4tnkWnmdhqdbpUFM8OF3Airm7
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.11 h1:lfGKw3eU35sjV0aG2eYZTiwFEY1pCzxdzicHP3SZILw=
github.com/containerd/containerd v1.7.11/go.mod h1:5UluHxHTX2rdvYuZ5OJTC5m/KJNs0Zs9wVoJm9zf5ZE=
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=
github.com/containerd/continuity v0.4.2/go.mod h1:F6PTNCKepoxEaXLQp3wDAjygEnImnZ/7o4JzpodfroQ=
github.com/containerd/log v0.1.0 h1:TCJt7ioM2cr/tfR8GPbGf9/VRAX8D2B4PjzCpfX540I=
Expand Down
47 changes: 42 additions & 5 deletions internal/pkg/convert/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"sort"

"github.com/moby/buildkit/client/llb"
v1 "github.com/opencontainers/image-spec/specs-go/v1"

"github.com/siderolabs/bldr/internal/pkg/constants"
"github.com/siderolabs/bldr/internal/pkg/environment"
Expand Down Expand Up @@ -55,6 +56,23 @@ func NewGraphLLB(graph *solver.PackageGraph, options *environment.Options) *Grap
return result
}

func (graph *GraphLLB) convertPlatform(platform string) *v1.Platform {
switch platform {
case "linux/amd64":
return &v1.Platform{
OS: "linux",
Architecture: "amd64",
}
case "linux/arm64":
return &v1.Platform{
OS: "linux",
Architecture: "arm64",
}
default:
return nil
}
}

func (graph *GraphLLB) buildBaseImages() {
graph.BaseImages = make(map[v1alpha2.Variant]llb.State)

Expand Down Expand Up @@ -86,10 +104,20 @@ func (graph *GraphLLB) buildBaseImages() {
return addEnv(addPkg(root))
}

graph.BaseImages[v1alpha2.Alpine] = graph.baseImageProcessor(llb.Image(
alpineBase := llb.Image(
constants.DefaultBaseImage,
llb.WithCustomName(graph.Options.CommonPrefix+"base"),
).Run(
)

if platform := graph.convertPlatform(graph.Root.Pkg.Platform); platform != nil {
alpineBase = llb.Image(
constants.DefaultBaseImage,
llb.WithCustomName(graph.Options.CommonPrefix+"base"),
llb.Platform(*platform),
)
}

graph.BaseImages[v1alpha2.Alpine] = graph.baseImageProcessor(alpineBase).Run(
append(graph.commonRunOptions,
llb.Shlex("apk --no-cache --update add bash"),
llb.WithCustomName(graph.Options.CommonPrefix+"base-apkinstall"),
Expand All @@ -99,16 +127,25 @@ func (graph *GraphLLB) buildBaseImages() {
llb.Args([]string{"ln", "-svf", "/bin/bash", "/bin/sh"}),
llb.WithCustomName(graph.Options.CommonPrefix+"base-symlink"),
)...,
).Root())
).Root()

graph.BaseImages[v1alpha2.Scratch] = graph.baseImageProcessor(llb.Scratch())
}

func (graph *GraphLLB) buildChecksummer() {
graph.Checksummer = llb.Image(
alpineBase := llb.Image(
constants.DefaultBaseImage,
llb.WithCustomName(graph.Options.CommonPrefix+"cksum"),
).Run(
)

if platform := graph.convertPlatform(graph.Root.Pkg.Platform); platform != nil {
alpineBase = llb.Image(
constants.DefaultBaseImage,
llb.WithCustomName(graph.Options.CommonPrefix+"cksum"),
llb.Platform(*platform))
}

graph.Checksummer = alpineBase.Run(
append(graph.commonRunOptions,
llb.Shlex("apk --no-cache --update add coreutils"),
llb.WithCustomName(graph.Options.CommonPrefix+"cksum-apkinstall"),
Expand Down
7 changes: 6 additions & 1 deletion internal/pkg/convert/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,12 @@ func (node *NodeLLB) convertDependency(dep solver.PackageDependency) (depState l
depState = llb.Image(dep.Image)
srcName = dep.Image

if dep.Platform != "" {
dep.Platform = "linux/arm64"
if dep.Platform != "" || node.Pkg.Platform != "" {
if dep.Platform == "" {
dep.Platform = node.Pkg.Platform
}

platform, err := node.convertPlatform(dep.Platform)
if err != nil {
return llb.Scratch(), "", err
Expand Down
5 changes: 3 additions & 2 deletions internal/pkg/solver/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ func (dep PackageDependency) ID() string {

// PackageNode is a Pkg with associated dependencies.
type PackageNode struct {
Pkg *v1alpha2.Pkg
Name string
Pkg *v1alpha2.Pkg
Name string
// Platform string
Dependencies []PackageDependency
}

Expand Down
6 changes: 4 additions & 2 deletions internal/pkg/solver/packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ func (pkgs *Packages) resolve(name string, path []string, cache map[string]*Pack
node := &PackageNode{
Pkg: pkg,
Name: name,
// Platform: pkg.Platform,
}

for _, dep := range pkg.Dependencies {
Expand Down Expand Up @@ -105,8 +106,9 @@ func (pkgs *Packages) ToSet() (set PackageSet) {
}

set = append(set, &PackageNode{
Name: name,
Pkg: pkg,
Name: name,
Pkg: pkg,
// Platform: pkg.Platform,
Dependencies: dependencies,
})
}
Expand Down
1 change: 1 addition & 0 deletions internal/pkg/types/v1alpha2/pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type Pkg struct {
Steps Steps `yaml:"steps,omitempty"`
Finalize []Finalize `yaml:"finalize,omitempty"`
Variant Variant `yaml:"variant,omitempty"`
Platform string `yaml:"platform,omitempty"`
}

// NewPkg loads Pkg structure from file.
Expand Down

0 comments on commit 37679bb

Please sign in to comment.