Skip to content

Commit

Permalink
buildx: handle new fields when parsing inspect output
Browse files Browse the repository at this point in the history
Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
  • Loading branch information
crazy-max committed Jun 15, 2023
1 parent 495c96b commit 8e9f684
Show file tree
Hide file tree
Showing 5 changed files with 212 additions and 13 deletions.
77 changes: 76 additions & 1 deletion __tests__/buildx/builder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,83 @@ describe('parseInspect', () => {
}
]
}
]
],
[
'inspect8.txt',
{
"name": "builder-52aa0611-faf0-42ac-a940-461e4e287d68",
"driver": "docker-container",
"lastActivity": new Date("2023-06-13T13:52:31.000Z"),
"nodes": [
{
"buildkit": "v0.11.6",
"buildkitd-flags": "--debug --allow-insecure-entitlement security.insecure --allow-insecure-entitlement network.host",
"driver-opts": [
"image=moby/buildkit:buildx-stable-1",
"network=host",
],
"endpoint": "unix:///var/run/docker.sock",
"name": "builder-52aa0611-faf0-42ac-a940-461e4e287d680",
"platforms": "linux/amd64,linux/amd64/v2,linux/amd64/v3,linux/amd64/v4,linux/arm64,linux/riscv64,linux/ppc64le,linux/s390x,linux/386,linux/mips64le,linux/mips64,linux/arm/v7,linux/arm/v6",
"status": "running",
"labels": {
"org.mobyproject.buildkit.worker.executor": "oci",
"org.mobyproject.buildkit.worker.hostname": "fv-az572-38",
"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",
},
"gcPolicy": [
{
"all": true,
"filter": [
"type==source.local",
"type==exec.cachemount",
"type==source.git.checkout"
],
"keepDuration": "48h0m0s",
"keepBytes": "488.3MiB",
},
{
"all": true,
"keepDuration": "1440h0m0s",
"keepBytes": "8.382GiB",
},
{
"all": true,
"keepBytes": "8.382GiB",
}
]
}
]
}
],
[
'inspect9.txt',
{
"name": "default",
"driver": "docker",
"lastActivity": new Date("2023-06-13T18:13:43.000Z"),
"nodes": [
{
"buildkit": "v0.11.7-0.20230525183624-798ad6b0ce9f",
"endpoint": "default",
"name": "default",
"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",
"status": "running",
"gcPolicy": [
{
"all": true,
"keepBytes": "100GiB",
}
]
}
]
}
],
])('given %p', async (inspectFile, expected) => {
console.log(JSON.stringify(await Builder.parseInspect(fs.readFileSync(path.join(fixturesDir, inspectFile)).toString()), null, 2))
expect(await Builder.parseInspect(fs.readFileSync(path.join(fixturesDir, inspectFile)).toString())).toEqual(expected);
});
});
34 changes: 34 additions & 0 deletions __tests__/fixtures/inspect8.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
Name: builder-52aa0611-faf0-42ac-a940-461e4e287d68
Driver: docker-container
Last Activity: 2023-06-13 13:52:31 +0000 UTC

Nodes:
Name: builder-52aa0611-faf0-42ac-a940-461e4e287d680
Endpoint: unix:///var/run/docker.sock
Driver Options: image="moby/buildkit:buildx-stable-1" network="host"
Status: running
Flags: --debug --allow-insecure-entitlement security.insecure --allow-insecure-entitlement network.host
Buildkit: v0.11.6
Platforms: linux/amd64, linux/amd64/v2, linux/amd64/v3, linux/amd64/v4, linux/arm64, linux/riscv64, linux/ppc64le, linux/s390x, linux/386, linux/mips64le, linux/mips64, linux/arm/v7, linux/arm/v6
Labels:
org.mobyproject.buildkit.worker.executor: oci
org.mobyproject.buildkit.worker.hostname: fv-az572-38
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
GC Policy rule#0:
All: false
Filters: type==source.local,type==exec.cachemount,type==source.git.checkout
Keep Duration: 48h0m0s
Keep Bytes: 488.3MiB
GC Policy rule#1:
All: false
Keep Duration: 1440h0m0s
Keep Bytes: 8.382GiB
GC Policy rule#2:
All: false
Keep Bytes: 8.382GiB
GC Policy rule#3:
All: true
Keep Bytes: 8.382GiB
14 changes: 14 additions & 0 deletions __tests__/fixtures/inspect9.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Name: default
Driver: docker
Last Activity: 2023-06-13 18:13:43 +0000 UTC

Nodes:
Name: default
Endpoint: default
Status: running
Buildkit: v0.11.7-0.20230525183624-798ad6b0ce9f
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
GC Policy rule#0:
All: true
Filters:
Keep Bytes: 100GiB
91 changes: 79 additions & 12 deletions 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, NodeInfo} from '../types/builder';
import {BuilderInfo, GCPolicy, NodeInfo} from '../types/builder';

export interface BuilderOpts {
buildx?: Buildx;
Expand Down Expand Up @@ -72,55 +72,72 @@ export class Builder {
const builder: BuilderInfo = {
nodes: []
};
let typeSet = 'none';
let node: NodeInfo = {};
let gcPolicy: GCPolicy | undefined;
for (const line of data.trim().split(`\n`)) {
// eslint-disable-next-line prefer-const
const [key, ...rest] = line.split(':');
const lkey = key.toLowerCase();
const value = rest.map(v => v.trim()).join(':');
if (key.length == 0 || value.length == 0) {
if (key.length == 0) {
continue;
}
switch (key.toLowerCase()) {
case 'name': {
switch (true) {
case lkey == 'name': {
typeSet = 'none';
if (builder.name == undefined) {
builder.name = value;
} else {
if (Object.keys(node).length > 0) {
builder.nodes.push(node);
node = {};
}
if (gcPolicy && node.gcPolicy) {
node.gcPolicy.push(gcPolicy);
gcPolicy = undefined;
}
node.name = value;
}
break;
}
case 'driver': {
case lkey == 'driver': {
typeSet = 'none';
builder.driver = value;
break;
}
case 'last activity': {
case lkey == 'last activity': {
typeSet = 'none';
builder.lastActivity = new Date(value);
break;
}
case 'endpoint': {
case lkey == 'endpoint': {
typeSet = 'none';
node.endpoint = value;
break;
}
case 'driver options': {
case lkey == 'driver options': {
typeSet = 'none';
node['driver-opts'] = (value.match(/([a-zA-Z0-9_.]+)="([^"]*)"/g) || []).map(v => v.replace(/^(.*)="(.*)"$/g, '$1=$2'));
break;
}
case 'status': {
case lkey == 'status': {
typeSet = 'none';
node.status = value;
break;
}
case 'flags': {
case lkey == 'flags': {
typeSet = 'none';
node['buildkitd-flags'] = value;
break;
}
case 'buildkit': {
case lkey == 'buildkit': {
typeSet = 'none';
node.buildkit = value;
break;
}
case 'platforms': {
case lkey == 'platforms': {
typeSet = 'none';
let platforms: Array<string> = [];
// if a preferred platform is being set then use only these
// https://docs.docker.com/engine/reference/commandline/buildx_inspect/#get-information-about-a-builder-instance
Expand All @@ -137,8 +154,58 @@ export class Builder {
node.platforms = platforms.join(',');
break;
}
case lkey == 'labels': {
typeSet = 'label';
node.labels = {};
break;
}
case lkey.startsWith('gc policy rule#'): {
typeSet = 'gcpolicy';
if (node.gcPolicy && gcPolicy) {
node.gcPolicy.push(gcPolicy);
gcPolicy = undefined;
}
break;
}
default: {
switch (typeSet) {
case 'label': {
node.labels = node.labels || {};
node.labels[key.trim()] = value;
break;
}
case 'gcpolicy': {
node.gcPolicy = node.gcPolicy || [];
gcPolicy = gcPolicy || {};
switch (lkey.trim()) {
case 'all': {
gcPolicy.all = !!value;
break;
}
case 'filters': {
if (value) {
gcPolicy.filter = value.split(',');
}
break;
}
case 'keep duration': {
gcPolicy.keepDuration = value;
break;
}
case 'keep bytes': {
gcPolicy.keepBytes = value;
break;
}
}
break;
}
}
}
}
}
if (gcPolicy && node.gcPolicy?.length == 0) {
node.gcPolicy.push(gcPolicy);
}
if (Object.keys(node).length > 0) {
builder.nodes.push(node);
}
Expand Down
9 changes: 9 additions & 0 deletions src/types/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,13 @@ export interface Node {
export interface NodeInfo extends Node {
status?: string;
buildkit?: string;
labels?: Record<string, string>;
gcPolicy?: Array<GCPolicy>;
}

export interface GCPolicy {
all?: boolean;
filter?: string[];
keepDuration?: string;
keepBytes?: string;
}

0 comments on commit 8e9f684

Please sign in to comment.