Skip to content

Commit 62397de

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 62397de

File tree

4 files changed

+211
-2
lines changed

4 files changed

+211
-2
lines changed

__tests__/.fixtures/inspect12.txt

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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: true
28+
Annotations:
29+
foo: bar
30+
org.mobyproject.buildkit.device.autoallow: true
31+
Name: docker.com/gpu=venus
32+
Automatically allowed: false
33+
Annotations:
34+
bar: baz
35+
GC Policy rule#0:
36+
All: false
37+
Filters: type==source.local,type==exec.cachemount,type==source.git.checkout
38+
Keep Duration: 48h0m0s
39+
Max Used Space: 488.3MiB
40+
GC Policy rule#1:
41+
All: false
42+
Keep Duration: 1440h0m0s
43+
Reserved Space: 9.313GiB
44+
Max Used Space: 93.13GiB
45+
Min Free Space: 188.1GiB
46+
GC Policy rule#2:
47+
All: false
48+
Reserved Space: 9.313GiB
49+
Max Used Space: 93.13GiB
50+
Min Free Space: 188.1GiB
51+
GC Policy rule#3:
52+
All: true
53+
Reserved Space: 9.313GiB
54+
Max Used Space: 93.13GiB
55+
Min Free Space: 188.1GiB

__tests__/buildx/builder.test.ts

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,89 @@ 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": true,
507+
"name": "nvidia.com/gpu=all"
508+
},
509+
{
510+
"annotations": {
511+
"bar": "baz"
512+
},
513+
"autoAllow": false,
514+
"name": "docker.com/gpu=venus"
515+
}
516+
],
517+
"gcPolicy": [
518+
{
519+
"all": false,
520+
"filter": [
521+
"type==source.local",
522+
"type==exec.cachemount",
523+
"type==source.git.checkout"
524+
],
525+
"keepDuration": "48h0m0s",
526+
"maxUsedSpace": "488.3MiB",
527+
},
528+
{
529+
"all": false,
530+
"keepDuration": "1440h0m0s",
531+
"maxUsedSpace": "93.13GiB",
532+
"minFreeSpace": "188.1GiB",
533+
"reservedSpace": "9.313GiB",
534+
},
535+
{
536+
"all": false,
537+
"maxUsedSpace": "93.13GiB",
538+
"minFreeSpace": "188.1GiB",
539+
"reservedSpace": "9.313GiB",
540+
},
541+
{
542+
"all": true,
543+
"maxUsedSpace": "93.13GiB",
544+
"minFreeSpace": "188.1GiB",
545+
"reservedSpace": "9.313GiB",
546+
}
547+
]
548+
}
549+
]
550+
}
551+
],
469552
])('given %p', async (inspectFile, expected) => {
470553
expect(await Builder.parseInspect(fs.readFileSync(path.join(fixturesDir, inspectFile)).toString())).toEqual(expected);
471554
});

src/buildx/builder.ts

Lines changed: 61 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,10 @@ export class Builder {
172173
parsingType = 'label';
173174
currentNode.labels = {};
174175
break;
176+
case lkey == 'devices':
177+
parsingType = 'devices';
178+
currentNode.devices = currentNode.devices || [];
179+
break;
175180
case lkey.startsWith('gc policy rule#'):
176181
parsingType = 'gcpolicy';
177182
if (currentNode.gcPolicy && currentGCPolicy) {
@@ -186,6 +191,10 @@ export class Builder {
186191
currentNode.files[currentFile] = '';
187192
break;
188193
default: {
194+
if (parsingType && parsingType !== 'devices' && currentNode.devices && currentDevice) {
195+
currentNode.devices.push(currentDevice);
196+
currentDevice = undefined;
197+
}
189198
switch (parsingType || '') {
190199
case 'features': {
191200
currentNode.features = currentNode.features || {};
@@ -197,6 +206,42 @@ export class Builder {
197206
currentNode.labels[key.trim()] = value;
198207
break;
199208
}
209+
case 'devices': {
210+
switch (lkey.trim()) {
211+
case 'name': {
212+
if (currentNode.devices && currentDevice) {
213+
currentNode.devices.push(currentDevice);
214+
}
215+
currentDevice = {};
216+
currentDevice.name = value;
217+
break;
218+
}
219+
case 'on-demand': {
220+
if (currentDevice && value) {
221+
currentDevice.onDemand = value == 'true';
222+
}
223+
break;
224+
}
225+
case 'automatically allowed': {
226+
if (currentDevice && value) {
227+
currentDevice.autoAllow = value == 'true';
228+
}
229+
break;
230+
}
231+
case 'annotations': {
232+
if (currentDevice) {
233+
currentDevice.annotations = currentDevice.annotations || {};
234+
}
235+
break;
236+
}
237+
default: {
238+
if (currentDevice && currentDevice.annotations) {
239+
currentDevice.annotations[key.trim()] = value;
240+
}
241+
}
242+
}
243+
break;
244+
}
200245
case 'gcpolicy': {
201246
currentNode.gcPolicy = currentNode.gcPolicy || [];
202247
currentGCPolicy = currentGCPolicy || {};
@@ -219,6 +264,18 @@ export class Builder {
219264
currentGCPolicy.keepBytes = value;
220265
break;
221266
}
267+
case 'reserved space': {
268+
currentGCPolicy.reservedSpace = value;
269+
break;
270+
}
271+
case 'max used space': {
272+
currentGCPolicy.maxUsedSpace = value;
273+
break;
274+
}
275+
case 'min free space': {
276+
currentGCPolicy.minFreeSpace = value;
277+
break;
278+
}
222279
}
223280
break;
224281
}
@@ -235,6 +292,9 @@ export class Builder {
235292
}
236293
}
237294
}
295+
if (currentDevice && currentNode.devices) {
296+
currentNode.devices.push(currentDevice);
297+
}
238298
if (currentGCPolicy && currentNode.gcPolicy) {
239299
currentNode.gcPolicy.push(currentGCPolicy);
240300
}

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)