Skip to content

Commit 910792f

Browse files
committed
buildx(builder): inspect devices and new gc policy opts support
Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
1 parent 4ecc47d commit 910792f

File tree

4 files changed

+195
-2
lines changed

4 files changed

+195
-2
lines changed

__tests__/.fixtures/inspect12.txt

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
Name: nvidia
2+
Driver: docker-container
3+
Last Activity: 2025-02-14 15:57:45 +0000 UTC
4+
5+
Nodes:
6+
Name: nvidia0
7+
Endpoint: unix:///var/run/docker.sock
8+
Driver Options: image="moby/buildkit:local"
9+
Status: running
10+
BuildKit daemon flags: --allow-insecure-entitlement=network.host
11+
BuildKit version: v0.20.0-rc2-4-gd30d8e22c.m
12+
Platforms: linux/amd64, linux/amd64/v2, linux/amd64/v3, linux/arm64, linux/riscv64, linux/ppc64le, linux/s390x, linux/386, linux/arm/v7, linux/arm/v6
13+
Features:
14+
Cache export: true
15+
Docker exporter: true
16+
Multi-platform build: true
17+
OCI exporter: true
18+
Labels:
19+
org.mobyproject.buildkit.worker.executor: oci
20+
org.mobyproject.buildkit.worker.hostname: 76ac9a510d96
21+
org.mobyproject.buildkit.worker.network: host
22+
org.mobyproject.buildkit.worker.oci.process-mode: sandbox
23+
org.mobyproject.buildkit.worker.selinux.enabled: false
24+
org.mobyproject.buildkit.worker.snapshotter: overlayfs
25+
Devices:
26+
Name: nvidia.com/gpu=all
27+
Automatically allowed: false
28+
Annotations:
29+
foo: bar
30+
org.mobyproject.buildkit.device.autoallow: true
31+
GC Policy rule#0:
32+
All: false
33+
Filters: type==source.local,type==exec.cachemount,type==source.git.checkout
34+
Keep Duration: 48h0m0s
35+
Max Used Space: 488.3MiB
36+
GC Policy rule#1:
37+
All: false
38+
Keep Duration: 1440h0m0s
39+
Reserved Space: 9.313GiB
40+
Max Used Space: 93.13GiB
41+
Min Free Space: 188.1GiB
42+
GC Policy rule#2:
43+
All: false
44+
Reserved Space: 9.313GiB
45+
Max Used Space: 93.13GiB
46+
Min Free Space: 188.1GiB
47+
GC Policy rule#3:
48+
All: true
49+
Reserved Space: 9.313GiB
50+
Max Used Space: 93.13GiB
51+
Min Free Space: 188.1GiB

__tests__/buildx/builder.test.ts

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,82 @@ baz = qux
466466
]
467467
}
468468
],
469+
[
470+
'inspect12.txt',
471+
{
472+
"name": "nvidia",
473+
"driver": "docker-container",
474+
"lastActivity": new Date("2025-02-14T15:57:45.000Z"),
475+
"nodes": [
476+
{
477+
"buildkit": "v0.20.0-rc2-4-gd30d8e22c.m",
478+
"buildkitd-flags": "--allow-insecure-entitlement=network.host",
479+
"driver-opts": [
480+
"image=moby/buildkit:local",
481+
],
482+
"endpoint": "unix:///var/run/docker.sock",
483+
"name": "nvidia0",
484+
"platforms": "linux/amd64,linux/amd64/v2,linux/amd64/v3,linux/arm64,linux/riscv64,linux/ppc64le,linux/s390x,linux/386,linux/arm/v7,linux/arm/v6",
485+
"status": "running",
486+
"features": {
487+
"Cache export": true,
488+
"Docker exporter": true,
489+
"Multi-platform build": true,
490+
"OCI exporter": true,
491+
},
492+
"labels": {
493+
"org.mobyproject.buildkit.worker.executor": "oci",
494+
"org.mobyproject.buildkit.worker.hostname": "76ac9a510d96",
495+
"org.mobyproject.buildkit.worker.network": "host",
496+
"org.mobyproject.buildkit.worker.oci.process-mode": "sandbox",
497+
"org.mobyproject.buildkit.worker.selinux.enabled": "false",
498+
"org.mobyproject.buildkit.worker.snapshotter": "overlayfs",
499+
},
500+
"devices": [
501+
{
502+
"annotations": {
503+
"foo": "bar",
504+
"org.mobyproject.buildkit.device.autoallow": "true"
505+
},
506+
"autoAllow": false,
507+
"name": "nvidia.com/gpu=all"
508+
}
509+
],
510+
"gcPolicy": [
511+
{
512+
"all": false,
513+
"filter": [
514+
"type==source.local",
515+
"type==exec.cachemount",
516+
"type==source.git.checkout"
517+
],
518+
"keepDuration": "48h0m0s",
519+
"maxUsedSpace": "488.3MiB",
520+
},
521+
{
522+
"all": false,
523+
"keepDuration": "1440h0m0s",
524+
"maxUsedSpace": "93.13GiB",
525+
"minFreeSpace": "188.1GiB",
526+
"reservedSpace": "9.313GiB",
527+
},
528+
{
529+
"all": false,
530+
"maxUsedSpace": "93.13GiB",
531+
"minFreeSpace": "188.1GiB",
532+
"reservedSpace": "9.313GiB",
533+
},
534+
{
535+
"all": true,
536+
"maxUsedSpace": "93.13GiB",
537+
"minFreeSpace": "188.1GiB",
538+
"reservedSpace": "9.313GiB",
539+
}
540+
]
541+
}
542+
]
543+
}
544+
],
469545
])('given %p', async (inspectFile, expected) => {
470546
expect(await Builder.parseInspect(fs.readFileSync(path.join(fixturesDir, inspectFile)).toString())).toEqual(expected);
471547
});

src/buildx/builder.ts

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import * as core from '@actions/core';
1919
import {Buildx} from './buildx';
2020
import {Exec} from '../exec';
2121

22-
import {BuilderInfo, GCPolicy, NodeInfo} from '../types/buildx/builder';
22+
import {BuilderInfo, Device, GCPolicy, NodeInfo} from '../types/buildx/builder';
2323

2424
export interface BuilderOpts {
2525
buildx?: Buildx;
@@ -89,6 +89,7 @@ export class Builder {
8989
let parsingType: string | undefined;
9090
let currentNode: NodeInfo = {};
9191
let currentGCPolicy: GCPolicy | undefined;
92+
let currentDevice: Device | undefined;
9293
let currentFile: string | undefined;
9394
for (const line of data.trim().split(`\n`)) {
9495
const [key, ...rest] = line.split(':');
@@ -172,6 +173,13 @@ export class Builder {
172173
parsingType = 'label';
173174
currentNode.labels = {};
174175
break;
176+
case lkey == 'devices':
177+
parsingType = 'devices';
178+
if (currentNode.devices && currentDevice) {
179+
currentNode.devices.push(currentDevice);
180+
currentDevice = undefined;
181+
}
182+
break;
175183
case lkey.startsWith('gc policy rule#'):
176184
parsingType = 'gcpolicy';
177185
if (currentNode.gcPolicy && currentGCPolicy) {
@@ -197,6 +205,38 @@ export class Builder {
197205
currentNode.labels[key.trim()] = value;
198206
break;
199207
}
208+
case 'devices': {
209+
currentNode.devices = currentNode.devices || [];
210+
currentDevice = currentDevice || {};
211+
switch (lkey.trim()) {
212+
case 'name': {
213+
currentDevice.name = value;
214+
break;
215+
}
216+
case 'on-demand': {
217+
if (value) {
218+
currentDevice.onDemand = value == 'true';
219+
}
220+
break;
221+
}
222+
case 'automatically allowed': {
223+
if (value) {
224+
currentDevice.autoAllow = value == 'true';
225+
}
226+
break;
227+
}
228+
case 'annotations': {
229+
currentDevice.annotations = currentDevice.annotations || {};
230+
break;
231+
}
232+
default: {
233+
if (currentDevice && currentDevice.annotations) {
234+
currentDevice.annotations[key.trim()] = value;
235+
}
236+
}
237+
}
238+
break;
239+
}
200240
case 'gcpolicy': {
201241
currentNode.gcPolicy = currentNode.gcPolicy || [];
202242
currentGCPolicy = currentGCPolicy || {};
@@ -219,6 +259,18 @@ export class Builder {
219259
currentGCPolicy.keepBytes = value;
220260
break;
221261
}
262+
case 'reserved space': {
263+
currentGCPolicy.reservedSpace = value;
264+
break;
265+
}
266+
case 'max used space': {
267+
currentGCPolicy.maxUsedSpace = value;
268+
break;
269+
}
270+
case 'min free space': {
271+
currentGCPolicy.minFreeSpace = value;
272+
break;
273+
}
222274
}
223275
break;
224276
}
@@ -235,6 +287,9 @@ export class Builder {
235287
}
236288
}
237289
}
290+
if (currentDevice && currentNode.devices) {
291+
currentNode.devices.push(currentDevice);
292+
}
238293
if (currentGCPolicy && currentNode.gcPolicy) {
239294
currentNode.gcPolicy.push(currentGCPolicy);
240295
}

src/types/buildx/builder.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,24 @@ export interface NodeInfo extends Node {
3434
buildkit?: string;
3535
features?: Record<string, boolean>;
3636
labels?: Record<string, string>;
37+
devices?: Array<Device>;
3738
gcPolicy?: Array<GCPolicy>;
3839
files?: Record<string, string>;
3940
}
4041

42+
export interface Device {
43+
name?: string;
44+
annotations?: Record<string, string>;
45+
autoAllow?: boolean;
46+
onDemand?: boolean;
47+
}
48+
4149
export interface GCPolicy {
4250
all?: boolean;
4351
filter?: string[];
4452
keepDuration?: string;
45-
keepBytes?: string;
53+
keepBytes?: string; // deprecated, use reservedSpace instead
54+
reservedSpace?: string;
55+
maxUsedSpace?: string;
56+
minFreeSpace?: string;
4657
}

0 commit comments

Comments
 (0)