Skip to content

Commit

Permalink
buildx(builder): inspect devices and new gc policy opts support
Browse files Browse the repository at this point in the history
Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
  • Loading branch information
crazy-max committed Feb 21, 2025
1 parent 4ecc47d commit 910792f
Show file tree
Hide file tree
Showing 4 changed files with 195 additions and 2 deletions.
51 changes: 51 additions & 0 deletions __tests__/.fixtures/inspect12.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
Name: nvidia
Driver: docker-container
Last Activity: 2025-02-14 15:57:45 +0000 UTC

Nodes:
Name: nvidia0
Endpoint: unix:///var/run/docker.sock
Driver Options: image="moby/buildkit:local"
Status: running
BuildKit daemon flags: --allow-insecure-entitlement=network.host
BuildKit version: v0.20.0-rc2-4-gd30d8e22c.m
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
Features:
Cache export: true
Docker exporter: true
Multi-platform build: true
OCI exporter: true
Labels:
org.mobyproject.buildkit.worker.executor: oci
org.mobyproject.buildkit.worker.hostname: 76ac9a510d96
org.mobyproject.buildkit.worker.network: host
org.mobyproject.buildkit.worker.oci.process-mode: sandbox
org.mobyproject.buildkit.worker.selinux.enabled: false
org.mobyproject.buildkit.worker.snapshotter: overlayfs
Devices:
Name: nvidia.com/gpu=all
Automatically allowed: false
Annotations:
foo: bar
org.mobyproject.buildkit.device.autoallow: true
GC Policy rule#0:
All: false
Filters: type==source.local,type==exec.cachemount,type==source.git.checkout
Keep Duration: 48h0m0s
Max Used Space: 488.3MiB
GC Policy rule#1:
All: false
Keep Duration: 1440h0m0s
Reserved Space: 9.313GiB
Max Used Space: 93.13GiB
Min Free Space: 188.1GiB
GC Policy rule#2:
All: false
Reserved Space: 9.313GiB
Max Used Space: 93.13GiB
Min Free Space: 188.1GiB
GC Policy rule#3:
All: true
Reserved Space: 9.313GiB
Max Used Space: 93.13GiB
Min Free Space: 188.1GiB
76 changes: 76 additions & 0 deletions __tests__/buildx/builder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,82 @@ baz = qux
]
}
],
[
'inspect12.txt',
{
"name": "nvidia",
"driver": "docker-container",
"lastActivity": new Date("2025-02-14T15:57:45.000Z"),
"nodes": [
{
"buildkit": "v0.20.0-rc2-4-gd30d8e22c.m",
"buildkitd-flags": "--allow-insecure-entitlement=network.host",
"driver-opts": [
"image=moby/buildkit:local",
],
"endpoint": "unix:///var/run/docker.sock",
"name": "nvidia0",
"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",
"status": "running",
"features": {
"Cache export": true,
"Docker exporter": true,
"Multi-platform build": true,
"OCI exporter": true,
},
"labels": {
"org.mobyproject.buildkit.worker.executor": "oci",
"org.mobyproject.buildkit.worker.hostname": "76ac9a510d96",
"org.mobyproject.buildkit.worker.network": "host",
"org.mobyproject.buildkit.worker.oci.process-mode": "sandbox",
"org.mobyproject.buildkit.worker.selinux.enabled": "false",
"org.mobyproject.buildkit.worker.snapshotter": "overlayfs",
},
"devices": [
{
"annotations": {
"foo": "bar",
"org.mobyproject.buildkit.device.autoallow": "true"
},
"autoAllow": false,
"name": "nvidia.com/gpu=all"
}
],
"gcPolicy": [
{
"all": false,
"filter": [
"type==source.local",
"type==exec.cachemount",
"type==source.git.checkout"
],
"keepDuration": "48h0m0s",
"maxUsedSpace": "488.3MiB",
},
{
"all": false,
"keepDuration": "1440h0m0s",
"maxUsedSpace": "93.13GiB",
"minFreeSpace": "188.1GiB",
"reservedSpace": "9.313GiB",
},
{
"all": false,
"maxUsedSpace": "93.13GiB",
"minFreeSpace": "188.1GiB",
"reservedSpace": "9.313GiB",
},
{
"all": true,
"maxUsedSpace": "93.13GiB",
"minFreeSpace": "188.1GiB",
"reservedSpace": "9.313GiB",
}
]
}
]
}
],
])('given %p', async (inspectFile, expected) => {
expect(await Builder.parseInspect(fs.readFileSync(path.join(fixturesDir, inspectFile)).toString())).toEqual(expected);
});
Expand Down
57 changes: 56 additions & 1 deletion src/buildx/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import * as core from '@actions/core';
import {Buildx} from './buildx';
import {Exec} from '../exec';

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

export interface BuilderOpts {
buildx?: Buildx;
Expand Down Expand Up @@ -89,6 +89,7 @@ export class Builder {
let parsingType: string | undefined;
let currentNode: NodeInfo = {};
let currentGCPolicy: GCPolicy | undefined;
let currentDevice: Device | undefined;
let currentFile: string | undefined;
for (const line of data.trim().split(`\n`)) {
const [key, ...rest] = line.split(':');
Expand Down Expand Up @@ -172,6 +173,13 @@ export class Builder {
parsingType = 'label';
currentNode.labels = {};
break;
case lkey == 'devices':
parsingType = 'devices';
if (currentNode.devices && currentDevice) {
currentNode.devices.push(currentDevice);
currentDevice = undefined;
}
break;
case lkey.startsWith('gc policy rule#'):
parsingType = 'gcpolicy';
if (currentNode.gcPolicy && currentGCPolicy) {
Expand All @@ -197,6 +205,38 @@ export class Builder {
currentNode.labels[key.trim()] = value;
break;
}
case 'devices': {
currentNode.devices = currentNode.devices || [];
currentDevice = currentDevice || {};
switch (lkey.trim()) {
case 'name': {
currentDevice.name = value;
break;
}
case 'on-demand': {
if (value) {
currentDevice.onDemand = value == 'true';
}
break;
}
case 'automatically allowed': {
if (value) {
currentDevice.autoAllow = value == 'true';
}
break;
}
case 'annotations': {
currentDevice.annotations = currentDevice.annotations || {};
break;
}
default: {
if (currentDevice && currentDevice.annotations) {
currentDevice.annotations[key.trim()] = value;
}
}
}
break;
}
case 'gcpolicy': {
currentNode.gcPolicy = currentNode.gcPolicy || [];
currentGCPolicy = currentGCPolicy || {};
Expand All @@ -219,6 +259,18 @@ export class Builder {
currentGCPolicy.keepBytes = value;
break;
}
case 'reserved space': {
currentGCPolicy.reservedSpace = value;
break;
}
case 'max used space': {
currentGCPolicy.maxUsedSpace = value;
break;
}
case 'min free space': {
currentGCPolicy.minFreeSpace = value;
break;
}
}
break;
}
Expand All @@ -235,6 +287,9 @@ export class Builder {
}
}
}
if (currentDevice && currentNode.devices) {
currentNode.devices.push(currentDevice);
}
if (currentGCPolicy && currentNode.gcPolicy) {
currentNode.gcPolicy.push(currentGCPolicy);
}
Expand Down
13 changes: 12 additions & 1 deletion src/types/buildx/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,24 @@ export interface NodeInfo extends Node {
buildkit?: string;
features?: Record<string, boolean>;
labels?: Record<string, string>;
devices?: Array<Device>;
gcPolicy?: Array<GCPolicy>;
files?: Record<string, string>;
}

export interface Device {
name?: string;
annotations?: Record<string, string>;
autoAllow?: boolean;
onDemand?: boolean;
}

export interface GCPolicy {
all?: boolean;
filter?: string[];
keepDuration?: string;
keepBytes?: string;
keepBytes?: string; // deprecated, use reservedSpace instead
reservedSpace?: string;
maxUsedSpace?: string;
minFreeSpace?: string;
}

0 comments on commit 910792f

Please sign in to comment.