Skip to content

Commit 3f0d0ec

Browse files
committed
azlinux/mariner: Use provided platform for outputs
Before this change, the mariner/azl targets were ignoring the client provided platform for the produced aritifacts. With this change the platform is set on images so it will rely on binfmt_misc to execute those images correctly. There's probably some optimizations that can be made here to run certain tasks on the native platform, some thoughts: - Use native (host-arch) golang to download go modules - Use native (host-arch) tdnf to download/install non-native packages onto the target build environment. This does *not* add support for cross compilation (run x86 code to generate, e.g., arm64 code). Signed-off-by: Brian Goff <[email protected]>
1 parent 8c1d340 commit 3f0d0ec

File tree

13 files changed

+316
-17
lines changed

13 files changed

+316
-17
lines changed

.github/workflows/ci.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ on:
3131
- go.mod
3232
- go.sum
3333

34+
env:
35+
# Used in tests to determine if certain tests should be skipped.
36+
# Setting this ensures that they are *not* skipped and instead make sure CI
37+
# is setup to be able to properly run all tests.
38+
DALEC_CI: "1"
39+
3440
permissions:
3541
contents: read
3642

@@ -93,6 +99,8 @@ jobs:
9399
uses: docker/setup-buildx-action@4fd812986e6c8c2a69e18311145f9371337f27d4 # v3.4.0
94100
- name: download deps
95101
run: go mod download
102+
- name: Setup QEMU
103+
run: docker run --rm --privileged multiarch/qemu-user-static --reset -p yes
96104
- name: Run integaration tests
97105
run: go test -v -json ./test | go run ./cmd/test2json2gha
98106
- name: dump logs

frontend/azlinux/handle_container.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func handleContainer(w worker) gwclient.BuildFunc {
2525

2626
pg := dalec.ProgressGroup("Building " + targetKey + " container: " + spec.Name)
2727

28-
rpmDir, err := specToRpmLLB(ctx, w, client, spec, sOpt, targetKey, pg)
28+
rpmDir, err := specToRpmLLB(ctx, w, client, spec, sOpt, targetKey, pg, dalec.WithPlatform(platform))
2929
if err != nil {
3030
return nil, nil, fmt.Errorf("error creating rpm: %w", err)
3131
}
@@ -35,7 +35,7 @@ func handleContainer(w worker) gwclient.BuildFunc {
3535
return nil, nil, err
3636
}
3737

38-
st, err := specToContainerLLB(w, spec, targetKey, rpmDir, rpms, sOpt, pg)
38+
st, err := specToContainerLLB(w, spec, targetKey, rpmDir, rpms, sOpt, pg, dalec.WithPlatform(platform))
3939
if err != nil {
4040
return nil, nil, err
4141
}

frontend/azlinux/handle_depsonly.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func handleDepsOnly(w worker) gwclient.BuildFunc {
2121
return nil, nil, err
2222
}
2323

24-
baseImg, err := w.Base(sOpt, pg)
24+
baseImg, err := w.Base(sOpt, pg, dalec.WithPlatform(platform))
2525
if err != nil {
2626
return nil, nil, err
2727
}

frontend/azlinux/handle_rpm.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func handleRPM(w worker) gwclient.BuildFunc {
2626
return nil, nil, err
2727
}
2828

29-
st, err := specToRpmLLB(ctx, w, client, spec, sOpt, targetKey, pg)
29+
st, err := specToRpmLLB(ctx, w, client, spec, sOpt, targetKey, pg, dalec.WithPlatform(platform))
3030
if err != nil {
3131
return nil, nil, err
3232
}

frontend/debug/handler.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,5 @@ func Handle(ctx context.Context, client gwclient.Client) (*gwclient.Result, erro
2525
Name: "gomods",
2626
Description: "Outputs all the gomodule dependencies for the spec",
2727
})
28-
2928
return r.Handle(ctx, client)
3029
}

frontend/gateway.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@ import (
1717
)
1818

1919
const (
20-
requestIDKey = "requestid"
21-
dalecSubrequstForwardBuild = "dalec.forward.build"
20+
// KeyRequestID is a key used in buildkit to performa subrequest
21+
// This is exposed for convenience only.
22+
KeyRequestID = "requestid"
2223

23-
gatewayFrontend = "gateway.v0"
24+
dalecSubrequstForwardBuild = "dalec.forward.build"
25+
gatewayFrontend = "gateway.v0"
2426
)
2527

2628
func getDockerfile(ctx context.Context, client gwclient.Client, build *dalec.SourceBuild, defPb *pb.Definition) ([]byte, error) {

frontend/mux.go

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"strings"
1111

1212
"github.com/Azure/dalec"
13+
"github.com/containerd/containerd/platforms"
1314
"github.com/moby/buildkit/client/llb"
1415
"github.com/moby/buildkit/frontend/dockerui"
1516
gwclient "github.com/moby/buildkit/frontend/gateway/client"
@@ -21,6 +22,11 @@ import (
2122
"golang.org/x/exp/maps"
2223
)
2324

25+
const (
26+
// KeySubrequestHostPlatform is a dalec specific subrequest for determining the platform of the worker.
27+
KeyHostPlatform = "dalec.debug.platform"
28+
)
29+
2430
// BuildMux implements a buildkit BuildFunc via its Handle method. With a
2531
// BuildMux you register routes with mux.Add("someKey", SomeHandler,
2632
// optionalTargetInfo).
@@ -108,7 +114,7 @@ func (m *BuildMux) describe() (*gwclient.Result, error) {
108114
}
109115

110116
func (m *BuildMux) handleSubrequest(ctx context.Context, client gwclient.Client, opts map[string]string) (*gwclient.Result, bool, error) {
111-
switch opts[requestIDKey] {
117+
switch opts[KeyRequestID] {
112118
case "":
113119
return nil, false, nil
114120
case subrequests.RequestSubrequestsDescribe:
@@ -117,13 +123,31 @@ func (m *BuildMux) handleSubrequest(ctx context.Context, client gwclient.Client,
117123
case bktargets.SubrequestsTargetsDefinition.Name:
118124
res, err := m.list(ctx, client, opts[keyTarget])
119125
return res, true, err
126+
case KeyHostPlatform:
127+
res, err := m.hostPlatform()
128+
return res, true, err
120129
case keyTopLevelTarget:
121130
return nil, false, nil
122131
default:
123-
return nil, false, errors.Errorf("unsupported subrequest %q", opts[requestIDKey])
132+
return nil, false, errors.Errorf("unsupported subrequest %q", opts[KeyRequestID])
124133
}
125134
}
126135

136+
func (m *BuildMux) hostPlatform() (*gwclient.Result, error) {
137+
res := gwclient.NewResult()
138+
139+
p := platforms.DefaultSpec()
140+
dt, err := json.Marshal(p)
141+
if err != nil {
142+
return nil, err
143+
}
144+
145+
res.AddMeta("result.json", dt)
146+
res.AddMeta("result.txt", []byte(platforms.Format(p)))
147+
148+
return res, nil
149+
}
150+
127151
func (m *BuildMux) loadSpec(ctx context.Context, client gwclient.Client) (*dalec.Spec, error) {
128152
if m.spec != nil {
129153
return m.spec, nil
@@ -299,7 +323,7 @@ func (m *BuildMux) Handle(ctx context.Context, client gwclient.Client) (_ *gwcli
299323
WithFields(logrus.Fields{
300324
"handlers": maps.Keys(m.handlers),
301325
"target": opts[keyTarget],
302-
"requestid": opts[requestIDKey],
326+
"requestid": opts[KeyRequestID],
303327
"targetKey": GetTargetKey(client),
304328
}))
305329

@@ -333,7 +357,7 @@ func (m *BuildMux) Handle(ctx context.Context, client gwclient.Client) (_ *gwcli
333357

334358
// If this request was a request to list targets, we need to modify the response a bit
335359
// Otherwise we can just return the result as is.
336-
if opts[requestIDKey] == bktargets.SubrequestsTargetsDefinition.Name {
360+
if opts[KeyRequestID] == bktargets.SubrequestsTargetsDefinition.Name {
337361
return m.fixupListResult(matched, res)
338362
}
339363
return res, nil

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ toolchain go1.21.0
66

77
require (
88
github.com/containerd/containerd v1.7.13
9+
github.com/containerd/platforms v0.2.1
910
github.com/goccy/go-yaml v1.11.3
1011
github.com/google/go-cmp v0.6.0
1112
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ github.com/containerd/log v0.1.0 h1:TCJt7ioM2cr/tfR8GPbGf9/VRAX8D2B4PjzCpfX540I=
4848
github.com/containerd/log v0.1.0/go.mod h1:VRRf09a7mHDIRezVKTRCrOq78v577GXq3bSa3EhrzVo=
4949
github.com/containerd/nydus-snapshotter v0.13.7 h1:x7DHvGnzJOu1ZPwPYkeOPk5MjZZYbdddygEjaSDoFTk=
5050
github.com/containerd/nydus-snapshotter v0.13.7/go.mod h1:VPVKQ3jmHFIcUIV2yiQ1kImZuBFS3GXDohKs9mRABVE=
51+
github.com/containerd/platforms v0.2.1 h1:zvwtM3rz2YHPQsF2CHYM8+KtB5dvhISiXh5ZpSBQv6A=
52+
github.com/containerd/platforms v0.2.1/go.mod h1:XHCb+2/hzowdiut9rkudds9bE5yJ7npe7dG/wG+uFPw=
5153
github.com/containerd/stargz-snapshotter v0.15.1 h1:fpsP4kf/Z4n2EYnU0WT8ZCE3eiKDwikDhL6VwxIlgeA=
5254
github.com/containerd/stargz-snapshotter/estargz v0.15.1 h1:eXJjw9RbkLFgioVaTG+G/ZW/0kEe2oEKCdS/ZxIyoCU=
5355
github.com/containerd/stargz-snapshotter/estargz v0.15.1/go.mod h1:gr2RNwukQ/S9Nv33Lt6UC7xEx58C+LHRdoqbEKjz1Kk=

helpers.go

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

1313
"github.com/moby/buildkit/client/llb"
1414
"github.com/moby/buildkit/identity"
15+
ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
1516
)
1617

1718
var disableDiffMerge atomic.Bool
@@ -399,3 +400,12 @@ func SortedMapValues[T any](m map[string]T) []T {
399400

400401
return out
401402
}
403+
404+
// WithPlatform sets the platform in the constraints opts
405+
// This is similar to [llb.Platform] except this takes a pointer so you don't
406+
// need to worry about dereferencing a potentially nil pointer.
407+
func WithPlatform(p *ocispecs.Platform) llb.ConstraintsOpt {
408+
return constraintsOptFunc(func(c *llb.Constraints) {
409+
c.Platform = p
410+
})
411+
}

0 commit comments

Comments
 (0)