Skip to content

Commit f61e01c

Browse files
tonistiigicrazy-max
authored andcommitted
llbsolver: on-demand CDI devices with automatic setup
Signed-off-by: Tonis Tiigi <[email protected]>
1 parent 3cb3f68 commit f61e01c

File tree

22 files changed

+268
-48
lines changed

22 files changed

+268
-48
lines changed

Dockerfile

+18-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ ARG GO_VERSION=1.23
2222
ARG ALPINE_VERSION=3.21
2323
ARG XX_VERSION=1.6.1
2424
ARG BUILDKIT_DEBUG
25+
ARG EXPORT_BASE=alpine
2526

2627
# minio for s3 integration tests
2728
FROM minio/minio:${MINIO_VERSION} AS minio
@@ -194,12 +195,28 @@ RUN --mount=from=binaries \
194195
FROM scratch AS release
195196
COPY --link --from=releaser /out/ /
196197

197-
FROM alpine:${ALPINE_VERSION} AS buildkit-export
198+
FROM alpine:${ALPINE_VERSION} AS buildkit-export-alpine
198199
RUN apk add --no-cache fuse3 git openssh pigz xz iptables ip6tables \
199200
&& ln -s fusermount3 /usr/bin/fusermount
200201
COPY --link examples/buildctl-daemonless/buildctl-daemonless.sh /usr/bin/
201202
VOLUME /var/lib/buildkit
202203

204+
FROM ubuntu:24.04 AS buildkit-export-ubuntu
205+
RUN apt-get update \
206+
&& apt-get install -y --no-install-recommends \
207+
fuse3 \
208+
git \
209+
openssh-client \
210+
pigz \
211+
xz-utils \
212+
iptables \
213+
ca-certificates \
214+
&& rm -rf /var/lib/apt/lists/*
215+
COPY --link examples/buildctl-daemonless/buildctl-daemonless.sh /usr/bin/
216+
VOLUME /var/lib/buildkit
217+
218+
FROM buildkit-export-${EXPORT_BASE} AS buildkit-export
219+
203220
FROM gobuild-base AS containerd-build
204221
WORKDIR /go/src/github.com/containerd/containerd
205222
ARG TARGETPLATFORM

api/types/worker.pb.go

+20-10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/types/worker.proto

+1
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,5 @@ message CDIDevice {
3636
string Name = 1;
3737
bool AutoAllow = 2;
3838
map<string, string> Annotations = 3;
39+
bool OnDemand = 4;
3940
}

api/types/worker_vtproto.pb.go

+37
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

client/info.go

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ type CDIDevice struct {
2222
Name string `json:"name"`
2323
AutoAllow bool `json:"autoAllow"`
2424
Annotations map[string]string `json:"annotations"`
25+
OnDemand bool `json:"onDemand"`
2526
}
2627

2728
func (c *Client) Info(ctx context.Context) (*Info, error) {
@@ -52,6 +53,7 @@ func fromAPICDIDevices(in []*apitypes.CDIDevice) []CDIDevice {
5253
Name: d.Name,
5354
AutoAllow: d.AutoAllow,
5455
Annotations: d.Annotations,
56+
OnDemand: d.OnDemand,
5557
})
5658
}
5759
return out

cmd/buildctl/debug/workers.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,12 @@ func printWorkersVerbose(tw *tabwriter.Writer, winfo []*client.WorkerInfo) {
8686
fmt.Fprint(tw, "Devices:\n")
8787
for _, d := range wi.CDIDevices {
8888
fmt.Fprintf(tw, "\tName:\t%s\n", d.Name)
89-
fmt.Fprintf(tw, "\tAutoAllow:\t%v\n", d.AutoAllow)
89+
if d.OnDemand {
90+
fmt.Fprintf(tw, "\tOnDemand:\t%v\n", d.OnDemand)
91+
} else {
92+
fmt.Fprintf(tw, "\tAutoAllow:\t%v\n", d.AutoAllow)
93+
}
94+
9095
for _, k := range sortedKeys(d.Annotations) {
9196
v := d.Annotations[k]
9297
fmt.Fprintf(tw, "\t\t%s:\t%s\n", k, v)

cmd/buildkitd/main_containerd_worker.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
ctd "github.com/containerd/containerd/v2/client"
1313
"github.com/containerd/containerd/v2/defaults"
1414
"github.com/moby/buildkit/cmd/buildkitd/config"
15+
"github.com/moby/buildkit/solver/llbsolver/cdidevices"
1516
"github.com/moby/buildkit/util/bklog"
1617
"github.com/moby/buildkit/util/disk"
1718
"github.com/moby/buildkit/util/network/cniprovider"
@@ -344,7 +345,7 @@ func containerdWorkerInitializer(c *cli.Context, common workerInitializerOpt) ([
344345
ParallelismSem: parallelismSem,
345346
TraceSocket: common.traceSocket,
346347
Runtime: runtime,
347-
CDIManager: cdiManager,
348+
CDIManager: cdidevices.NewManager(cdiManager),
348349
}
349350

350351
opt, err := containerd.NewWorkerOpt(workerOpts, ctd.WithTimeout(60*time.Second))

control/control.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333
"github.com/moby/buildkit/solver"
3434
"github.com/moby/buildkit/solver/bboltcachestorage"
3535
"github.com/moby/buildkit/solver/llbsolver"
36+
"github.com/moby/buildkit/solver/llbsolver/cdidevices"
3637
"github.com/moby/buildkit/solver/llbsolver/proc"
3738
"github.com/moby/buildkit/solver/pb"
3839
"github.com/moby/buildkit/util/bklog"
@@ -54,7 +55,6 @@ import (
5455
"google.golang.org/grpc/metadata"
5556
"google.golang.org/grpc/status"
5657
"google.golang.org/protobuf/types/known/timestamppb"
57-
"tags.cncf.io/container-device-interface/pkg/cdi"
5858
)
5959

6060
type Opt struct {
@@ -686,18 +686,18 @@ func toPBBuildkitVersion(in client.BuildkitVersion) *apitypes.BuildkitVersion {
686686
}
687687
}
688688

689-
func toPBCDIDevices(manager *cdi.Cache) []*apitypes.CDIDevice {
689+
func toPBCDIDevices(manager *cdidevices.Manager) []*apitypes.CDIDevice {
690690
if manager == nil {
691691
return nil
692692
}
693693
devs := manager.ListDevices()
694694
out := make([]*apitypes.CDIDevice, 0, len(devs))
695695
for _, dev := range devs {
696-
spec := manager.GetDevice(dev).GetSpec()
697696
out = append(out, &apitypes.CDIDevice{
698-
Name: dev,
697+
Name: dev.Name,
699698
AutoAllow: true, // TODO
700-
Annotations: spec.Annotations,
699+
Annotations: dev.Annotations,
700+
OnDemand: dev.OnDemand,
701701
})
702702
}
703703
return out

executor/containerdexecutor/executor.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"github.com/moby/buildkit/util/bklog"
1414
"go.opentelemetry.io/otel/attribute"
1515
"go.opentelemetry.io/otel/trace"
16-
"tags.cncf.io/container-device-interface/pkg/cdi"
1716

1817
ctd "github.com/containerd/containerd/v2/client"
1918
"github.com/containerd/containerd/v2/core/mount"
@@ -23,6 +22,7 @@ import (
2322
resourcestypes "github.com/moby/buildkit/executor/resources/types"
2423
gatewayapi "github.com/moby/buildkit/frontend/gateway/pb"
2524
"github.com/moby/buildkit/identity"
25+
"github.com/moby/buildkit/solver/llbsolver/cdidevices"
2626
"github.com/moby/buildkit/solver/pb"
2727
"github.com/moby/buildkit/util/network"
2828
"github.com/pkg/errors"
@@ -41,7 +41,7 @@ type containerdExecutor struct {
4141
traceSocket string
4242
rootless bool
4343
runtime *RuntimeInfo
44-
cdiManager *cdi.Cache
44+
cdiManager *cdidevices.Manager
4545
}
4646

4747
// OnCreateRuntimer provides an alternative to OCI hooks for applying network
@@ -74,7 +74,7 @@ type ExecutorOptions struct {
7474
TraceSocket string
7575
Rootless bool
7676
Runtime *RuntimeInfo
77-
CDIManager *cdi.Cache
77+
CDIManager *cdidevices.Manager
7878
}
7979

8080
// New creates a new executor backed by connection to containerd API

executor/oci/spec.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ import (
1717
"github.com/mitchellh/hashstructure/v2"
1818
"github.com/moby/buildkit/executor"
1919
"github.com/moby/buildkit/snapshot"
20+
"github.com/moby/buildkit/solver/llbsolver/cdidevices"
2021
"github.com/moby/buildkit/util/network"
2122
rootlessmountopts "github.com/moby/buildkit/util/rootless/mountopts"
2223
traceexec "github.com/moby/buildkit/util/tracing/exec"
2324
"github.com/moby/sys/userns"
2425
specs "github.com/opencontainers/runtime-spec/specs-go"
2526
"github.com/opencontainers/selinux/go-selinux"
2627
"github.com/pkg/errors"
27-
"tags.cncf.io/container-device-interface/pkg/cdi"
2828
)
2929

3030
// ProcessMode configures PID namespaces
@@ -60,7 +60,7 @@ func (pm ProcessMode) String() string {
6060

6161
// GenerateSpec generates spec using containerd functionality.
6262
// opts are ignored for s.Process, s.Hostname, and s.Mounts .
63-
func GenerateSpec(ctx context.Context, meta executor.Meta, mounts []executor.Mount, id, resolvConf, hostsFile string, namespace network.Namespace, cgroupParent string, processMode ProcessMode, idmap *idtools.IdentityMapping, apparmorProfile string, selinuxB bool, tracingSocket string, cdiManager *cdi.Cache, opts ...oci.SpecOpts) (*specs.Spec, func(), error) {
63+
func GenerateSpec(ctx context.Context, meta executor.Meta, mounts []executor.Mount, id, resolvConf, hostsFile string, namespace network.Namespace, cgroupParent string, processMode ProcessMode, idmap *idtools.IdentityMapping, apparmorProfile string, selinuxB bool, tracingSocket string, cdiManager *cdidevices.Manager, opts ...oci.SpecOpts) (*specs.Spec, func(), error) {
6464
c := &containers.Container{
6565
ID: id,
6666
}

executor/oci/spec_darwin.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ import (
55
"github.com/containerd/containerd/v2/pkg/oci"
66
"github.com/containerd/continuity/fs"
77
"github.com/docker/docker/pkg/idtools"
8+
"github.com/moby/buildkit/solver/llbsolver/cdidevices"
89
"github.com/moby/buildkit/solver/pb"
910
"github.com/opencontainers/runtime-spec/specs-go"
1011
"github.com/pkg/errors"
11-
"tags.cncf.io/container-device-interface/pkg/cdi"
1212
)
1313

1414
func withProcessArgs(args ...string) oci.SpecOpts {
@@ -64,7 +64,7 @@ func sub(m mount.Mount, subPath string) (mount.Mount, func() error, error) {
6464
return m, func() error { return nil }, nil
6565
}
6666

67-
func generateCDIOpts(_ *cdi.Cache, devices []*pb.CDIDevice) ([]oci.SpecOpts, error) {
67+
func generateCDIOpts(_ *cdidevices.Manager, devices []*pb.CDIDevice) ([]oci.SpecOpts, error) {
6868
if len(devices) == 0 {
6969
return nil, nil
7070
}

executor/oci/spec_freebsd.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ import (
55
"github.com/containerd/containerd/v2/pkg/oci"
66
"github.com/containerd/continuity/fs"
77
"github.com/docker/docker/pkg/idtools"
8+
"github.com/moby/buildkit/solver/llbsolver/cdidevices"
89
"github.com/moby/buildkit/solver/pb"
910
specs "github.com/opencontainers/runtime-spec/specs-go"
1011
"github.com/pkg/errors"
11-
"tags.cncf.io/container-device-interface/pkg/cdi"
1212
)
1313

1414
func withProcessArgs(args ...string) oci.SpecOpts {
@@ -72,7 +72,7 @@ func sub(m mount.Mount, subPath string) (mount.Mount, func() error, error) {
7272
return m, func() error { return nil }, nil
7373
}
7474

75-
func generateCDIOpts(_ *cdi.Cache, devices []*pb.CDIDevice) ([]oci.SpecOpts, error) {
75+
func generateCDIOpts(_ *cdidevices.Manager, devices []*pb.CDIDevice) ([]oci.SpecOpts, error) {
7676
if len(devices) == 0 {
7777
return nil, nil
7878
}

0 commit comments

Comments
 (0)