Skip to content

Commit e1fa386

Browse files
authored
Merge pull request #276 from crazy-max/update-builder-inspect
builder: update inspect command parsing
2 parents 3401826 + 70d3d9a commit e1fa386

File tree

4 files changed

+136
-22
lines changed

4 files changed

+136
-22
lines changed

__tests__/buildx/builder.test.ts

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,71 @@ describe('parseInspect', () => {
393393
}
394394
],
395395
}
396-
]
396+
],
397+
[
398+
'inspect11.txt',
399+
{
400+
"name": "builder",
401+
"driver": "docker-container",
402+
"lastActivity": new Date("2024-03-01T14:25:03.000Z"),
403+
"nodes": [
404+
{
405+
"buildkit": "37657a1",
406+
"buildkitd-flags": "--debug --allow-insecure-entitlement security.insecure --allow-insecure-entitlement network.host --allow-insecure-entitlement=network.host",
407+
"driver-opts": [
408+
"env.JAEGER_TRACE=localhost:6831",
409+
"image=moby/buildkit:master",
410+
"network=host",
411+
"env.BUILDKIT_STEP_LOG_MAX_SIZE=10485760",
412+
"env.BUILDKIT_STEP_LOG_MAX_SPEED=10485760",
413+
],
414+
"endpoint": "unix:///var/run/docker.sock",
415+
"name": "builder0",
416+
"platforms": "linux/amd64,linux/amd64/v2,linux/amd64/v3,linux/arm64,linux/riscv64,linux/ppc64le,linux/s390x,linux/386,linux/mips64le,linux/mips64,linux/arm/v7,linux/arm/v6",
417+
"status": "running",
418+
"features": {
419+
"Cache export": true,
420+
"Docker exporter": true,
421+
"Multi-platform build": true,
422+
"OCI exporter": true,
423+
},
424+
"labels": {
425+
"org.mobyproject.buildkit.worker.executor": "oci",
426+
"org.mobyproject.buildkit.worker.hostname": "docker-desktop",
427+
"org.mobyproject.buildkit.worker.network": "host",
428+
"org.mobyproject.buildkit.worker.oci.process-mode": "sandbox",
429+
"org.mobyproject.buildkit.worker.selinux.enabled": "false",
430+
"org.mobyproject.buildkit.worker.snapshotter": "overlayfs",
431+
},
432+
"gcPolicy": [
433+
{
434+
"all": false,
435+
"filter": [
436+
"type==source.local",
437+
"type==exec.cachemount",
438+
"type==source.git.checkout"
439+
],
440+
"keepDuration": "48h0m0s",
441+
"keepBytes": "488.3MiB",
442+
},
443+
{
444+
"all": false,
445+
"keepDuration": "1440h0m0s",
446+
"keepBytes": "94.06GiB",
447+
},
448+
{
449+
"all": false,
450+
"keepBytes": "94.06GiB",
451+
},
452+
{
453+
"all": true,
454+
"keepBytes": "94.06GiB",
455+
}
456+
]
457+
}
458+
]
459+
}
460+
],
397461
])('given %p', async (inspectFile, expected) => {
398462
expect(await Builder.parseInspect(fs.readFileSync(path.join(fixturesDir, inspectFile)).toString())).toEqual(expected);
399463
});

__tests__/fixtures/inspect11.txt

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
Name: builder
2+
Driver: docker-container
3+
Last Activity: 2024-03-01 14:25:03 +0000 UTC
4+
5+
Nodes:
6+
Name: builder0
7+
Endpoint: unix:///var/run/docker.sock
8+
Driver Options: env.JAEGER_TRACE="localhost:6831" image="moby/buildkit:master" network="host" env.BUILDKIT_STEP_LOG_MAX_SIZE="10485760" env.BUILDKIT_STEP_LOG_MAX_SPEED="10485760"
9+
Status: running
10+
BuildKit daemon flags: --debug --allow-insecure-entitlement security.insecure --allow-insecure-entitlement network.host --allow-insecure-entitlement=network.host
11+
BuildKit version: 37657a1
12+
Platforms: linux/amd64, linux/amd64/v2, linux/amd64/v3, linux/arm64, linux/riscv64, linux/ppc64le, linux/s390x, linux/386, linux/mips64le, linux/mips64, 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: docker-desktop
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+
GC Policy rule#0:
26+
All: false
27+
Filters: type==source.local,type==exec.cachemount,type==source.git.checkout
28+
Keep Duration: 48h0m0s
29+
Keep Bytes: 488.3MiB
30+
GC Policy rule#1:
31+
All: false
32+
Keep Duration: 1440h0m0s
33+
Keep Bytes: 94.06GiB
34+
GC Policy rule#2:
35+
All: false
36+
Keep Bytes: 94.06GiB
37+
GC Policy rule#3:
38+
All: true
39+
Keep Bytes: 94.06GiB

src/buildx/builder.ts

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,19 @@ export class Builder {
5656
}
5757

5858
public async inspect(name: string): Promise<BuilderInfo> {
59+
// always enable debug for inspect command, so we can display additional
60+
// fields such as features: https://github.com/docker/buildx/pull/1854
61+
const envs = Object.assign({}, process.env, {
62+
DEBUG: '1'
63+
}) as {
64+
[key: string]: string;
65+
};
66+
5967
const cmd = await this.buildx.getCommand(['inspect', name]);
6068
return await Exec.getExecOutput(cmd.command, cmd.args, {
6169
ignoreReturnCode: true,
62-
silent: true
70+
silent: true,
71+
env: envs
6372
}).then(res => {
6473
if (res.stderr.length > 0 && res.exitCode != 0) {
6574
throw new Error(res.stderr.trim());
@@ -83,7 +92,7 @@ export class Builder {
8392
continue;
8493
}
8594
switch (true) {
86-
case lkey == 'name': {
95+
case lkey == 'name':
8796
parsingType = undefined;
8897
if (builder.name == undefined) {
8998
builder.name = value;
@@ -98,42 +107,36 @@ export class Builder {
98107
currentNode = {name: value};
99108
}
100109
break;
101-
}
102-
case lkey == 'driver': {
110+
case lkey == 'driver':
103111
parsingType = undefined;
104112
builder.driver = value;
105113
break;
106-
}
107-
case lkey == 'last activity': {
114+
case lkey == 'last activity':
108115
parsingType = undefined;
109116
builder.lastActivity = new Date(value);
110117
break;
111-
}
112-
case lkey == 'endpoint': {
118+
case lkey == 'endpoint':
113119
parsingType = undefined;
114120
currentNode.endpoint = value;
115121
break;
116-
}
117-
case lkey == 'driver options': {
122+
case lkey == 'driver options':
118123
parsingType = undefined;
119124
currentNode['driver-opts'] = (value.match(/([a-zA-Z0-9_.]+)="([^"]*)"/g) || []).map(v => v.replace(/^(.*)="(.*)"$/g, '$1=$2'));
120125
break;
121-
}
122-
case lkey == 'status': {
126+
case lkey == 'status':
123127
parsingType = undefined;
124128
currentNode.status = value;
125129
break;
126-
}
127-
case lkey == 'flags': {
130+
case lkey == 'buildkit daemon flags':
131+
case lkey == 'flags': // buildx < v0.13
128132
parsingType = undefined;
129133
currentNode['buildkitd-flags'] = value;
130134
break;
131-
}
132-
case lkey == 'buildkit': {
135+
case lkey == 'buildkit version':
136+
case lkey == 'buildkit': // buildx < v0.13
133137
parsingType = undefined;
134138
currentNode.buildkit = value;
135139
break;
136-
}
137140
case lkey == 'platforms': {
138141
parsingType = undefined;
139142
if (!value) {
@@ -155,21 +158,28 @@ export class Builder {
155158
currentNode.platforms = platforms.join(',');
156159
break;
157160
}
158-
case lkey == 'labels': {
161+
case lkey == 'features':
162+
parsingType = 'features';
163+
currentNode.features = {};
164+
break;
165+
case lkey == 'labels':
159166
parsingType = 'label';
160167
currentNode.labels = {};
161168
break;
162-
}
163-
case lkey.startsWith('gc policy rule#'): {
169+
case lkey.startsWith('gc policy rule#'):
164170
parsingType = 'gcpolicy';
165171
if (currentNode.gcPolicy && currentGCPolicy) {
166172
currentNode.gcPolicy.push(currentGCPolicy);
167173
currentGCPolicy = undefined;
168174
}
169175
break;
170-
}
171176
default: {
172177
switch (parsingType || '') {
178+
case 'features': {
179+
currentNode.features = currentNode.features || {};
180+
currentNode.features[key.trim()] = Boolean(value);
181+
break;
182+
}
173183
case 'label': {
174184
currentNode.labels = currentNode.labels || {};
175185
currentNode.labels[key.trim()] = value;

src/types/builder.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export interface Node {
3232
export interface NodeInfo extends Node {
3333
status?: string;
3434
buildkit?: string;
35+
features?: Record<string, boolean>;
3536
labels?: Record<string, string>;
3637
gcPolicy?: Array<GCPolicy>;
3738
}

0 commit comments

Comments
 (0)