feat(new tool): Docker Labels Pangolin Converter

Fix #311
This commit is contained in:
ShareVB committed 2026-02-22 23:09:54 +01:00
1 parent 98b98a7d68
commit 90e1813003
4 files changed
+531

No files matched your search

@@ -0,0 +1,244 @@
import { describe, expect, it } from 'vitest';
import yaml from 'yaml';
import { blueprintToLabels, extractPangolinLabelsFromCompose, pangolinLabelsToBlueprint } from './docker-pangolin-labels.service';
const composeYml = `
services:
newt:
image: fosrl/newt
container_name: newt
restart: unless-stopped
volumes:
- /var/run/docker.sock:/var/run/docker.sock
environment:
- PANGOLIN_ENDPOINT=https://app.pangolin.net
- NEWT_ID=h1rbsgku89wf9z3
- NEWT_SECRET=z7g54mbcwkglpx1aau9gb8mzcccoof2fdbs97keoakg2pp5z
- DOCKER_SOCKET=/var/run/docker.sock
nginx1:
image: nginxdemos/hello
container_name: nginx1
labels:
# Public Resource Configuration
- pangolin.public-resources.nginx.name=nginx
- pangolin.public-resources.nginx.full-domain=nginx.fosrl.io
- pangolin.public-resources.nginx.protocol=http
- pangolin.public-resources.nginx.headers[0].name=X-Example-Header
- pangolin.public-resources.nginx.headers[0].value=example-value
# Target Configuration - the port and hostname will be auto-detected
- pangolin.public-resources.nginx.targets[0].method=http
- pangolin.public-resources.nginx.targets[0].path=/path
- pangolin.public-resources.nginx.targets[0].path-match=prefix
nginx2:
image: nginxdemos/hello
container_name: nginx2
labels:
# Additional target for the same resource where the port and hostname are explicit
pangolin.public-resources.nginx.targets[1].method: http
pangolin.public-resources.nginx.targets[1].hostname: nginx2
pangolin.public-resources.nginx.targets[1].port: 80
networks:
default:
name: pangolin_default`;
describe('Pangolin Compose Extraction', () => {
it('extracts Pangolin labels from full compose', () => {
const compose = yaml.parse(composeYml);
const labels = extractPangolinLabelsFromCompose(compose);
expect(labels).to.deep.eq({
'pangolin.public-resources.nginx.full-domain': 'nginx.fosrl.io',
'pangolin.public-resources.nginx.headers[0].name': 'X-Example-Header',
'pangolin.public-resources.nginx.headers[0].value': 'example-value',
'pangolin.public-resources.nginx.name': 'nginx',
'pangolin.public-resources.nginx.protocol': 'http',
'pangolin.public-resources.nginx.targets[0].method': 'http',
'pangolin.public-resources.nginx.targets[0].path': '/path',
'pangolin.public-resources.nginx.targets[0].path-match': 'prefix',
'pangolin.public-resources.nginx.targets[1].hostname': 'nginx2',
'pangolin.public-resources.nginx.targets[1].method': 'http',
'pangolin.public-resources.nginx.targets[1].port': '80',
});
});
it('parses extracted labels into blueprint', () => {
const compose = yaml.parse(composeYml);
const labels = extractPangolinLabelsFromCompose(compose);
const blueprint = pangolinLabelsToBlueprint(labels);
expect(blueprint).to.deep.eq({
'public-resources': {
nginx: {
'full-domain': 'nginx.fosrl.io',
'headers': [
{
name: 'X-Example-Header',
value: 'example-value',
},
],
'name': 'nginx',
'protocol': 'http',
'targets': [
{
'method': 'http',
'path': '/path',
'path-match': 'prefix',
},
{
hostname: 'nginx2',
method: 'http',
port: 80,
},
],
},
},
});
});
});
// ---------------------------------------------------------
// Forward Mapping Tests
// ---------------------------------------------------------
const blueprintYml = `
public-resources:
resource-nice-id-uno:
name: this is a http resource
protocol: http
full-domain: uno.example.com
host-header: example.com
tls-server-name: example.com
headers:
- name: X-Example-Header
value: example-value
- name: X-Another-Header
value: another-value
rules:
- action: allow
match: ip
value: 1.1.1.1
priority: 1
- action: deny
match: cidr
value: 2.2.2.2/32
priority: 2
- action: allow
match: asn
value: AS13335
priority: 3
- action: pass
match: path
value: /admin
targets:
- site: lively-yosemite-toad
hostname: localhost
method: http
port: 8000
- site: slim-alpine-chipmunk
hostname: localhost
path: /admin
path-match: exact
method: https
port: 8001
resource-nice-id-dos:
name: this is a raw resource
protocol: tcp
proxy-port: 3000
targets:
- site: lively-yosemite-toad
hostname: localhost
port: 3000`;
describe('Forward Mapping (blueprint → labels)', () => {
it('blueprintToLabels should produce array', () => {
expect(blueprintToLabels(yaml.parse(blueprintYml), 'array')).to.deep.eq([
'pangolin.public-resources.resource-nice-id-uno.name=this is a http resource',
'pangolin.public-resources.resource-nice-id-uno.protocol=http',
'pangolin.public-resources.resource-nice-id-uno.full-domain=uno.example.com',
'pangolin.public-resources.resource-nice-id-uno.host-header=example.com',
'pangolin.public-resources.resource-nice-id-uno.tls-server-name=example.com',
'pangolin.public-resources.resource-nice-id-uno.headers[0].name=X-Example-Header',
'pangolin.public-resources.resource-nice-id-uno.headers[0].value=example-value',
'pangolin.public-resources.resource-nice-id-uno.headers[1].name=X-Another-Header',
'pangolin.public-resources.resource-nice-id-uno.headers[1].value=another-value',
'pangolin.public-resources.resource-nice-id-uno.rules[0].action=allow',
'pangolin.public-resources.resource-nice-id-uno.rules[0].match=ip',
'pangolin.public-resources.resource-nice-id-uno.rules[0].value=1.1.1.1',
'pangolin.public-resources.resource-nice-id-uno.rules[0].priority=1',
'pangolin.public-resources.resource-nice-id-uno.rules[1].action=deny',
'pangolin.public-resources.resource-nice-id-uno.rules[1].match=cidr',
'pangolin.public-resources.resource-nice-id-uno.rules[1].value=2.2.2.2/32',
'pangolin.public-resources.resource-nice-id-uno.rules[1].priority=2',
'pangolin.public-resources.resource-nice-id-uno.rules[2].action=allow',
'pangolin.public-resources.resource-nice-id-uno.rules[2].match=asn',
'pangolin.public-resources.resource-nice-id-uno.rules[2].value=AS13335',
'pangolin.public-resources.resource-nice-id-uno.rules[2].priority=3',
'pangolin.public-resources.resource-nice-id-uno.rules[3].action=pass',
'pangolin.public-resources.resource-nice-id-uno.rules[3].match=path',
'pangolin.public-resources.resource-nice-id-uno.rules[3].value=/admin',
'pangolin.public-resources.resource-nice-id-uno.targets[0].site=lively-yosemite-toad',
'pangolin.public-resources.resource-nice-id-uno.targets[0].hostname=localhost',
'pangolin.public-resources.resource-nice-id-uno.targets[0].method=http',
'pangolin.public-resources.resource-nice-id-uno.targets[0].port=8000',
'pangolin.public-resources.resource-nice-id-uno.targets[1].site=slim-alpine-chipmunk',
'pangolin.public-resources.resource-nice-id-uno.targets[1].hostname=localhost',
'pangolin.public-resources.resource-nice-id-uno.targets[1].path=/admin',
'pangolin.public-resources.resource-nice-id-uno.targets[1].path-match=exact',
'pangolin.public-resources.resource-nice-id-uno.targets[1].method=https',
'pangolin.public-resources.resource-nice-id-uno.targets[1].port=8001',
'pangolin.public-resources.resource-nice-id-dos.name=this is a raw resource',
'pangolin.public-resources.resource-nice-id-dos.protocol=tcp',
'pangolin.public-resources.resource-nice-id-dos.proxy-port=3000',
'pangolin.public-resources.resource-nice-id-dos.targets[0].site=lively-yosemite-toad',
'pangolin.public-resources.resource-nice-id-dos.targets[0].hostname=localhost',
'pangolin.public-resources.resource-nice-id-dos.targets[0].port=3000',
]);
});
it('blueprintToLabels should produce object', () => {
expect(blueprintToLabels(yaml.parse(blueprintYml), 'object')).to.deep.eq({
'pangolin.public-resources.resource-nice-id-dos.name': 'this is a raw resource',
'pangolin.public-resources.resource-nice-id-dos.protocol': 'tcp',
'pangolin.public-resources.resource-nice-id-dos.proxy-port': '3000',
'pangolin.public-resources.resource-nice-id-dos.targets[0].hostname': 'localhost',
'pangolin.public-resources.resource-nice-id-dos.targets[0].port': '3000',
'pangolin.public-resources.resource-nice-id-dos.targets[0].site': 'lively-yosemite-toad',
'pangolin.public-resources.resource-nice-id-uno.full-domain': 'uno.example.com',
'pangolin.public-resources.resource-nice-id-uno.headers[0].name': 'X-Example-Header',
'pangolin.public-resources.resource-nice-id-uno.headers[0].value': 'example-value',
'pangolin.public-resources.resource-nice-id-uno.headers[1].name': 'X-Another-Header',
'pangolin.public-resources.resource-nice-id-uno.headers[1].value': 'another-value',
'pangolin.public-resources.resource-nice-id-uno.host-header': 'example.com',
'pangolin.public-resources.resource-nice-id-uno.name': 'this is a http resource',
'pangolin.public-resources.resource-nice-id-uno.protocol': 'http',
'pangolin.public-resources.resource-nice-id-uno.rules[0].action': 'allow',
'pangolin.public-resources.resource-nice-id-uno.rules[0].match': 'ip',
'pangolin.public-resources.resource-nice-id-uno.rules[0].priority': '1',
'pangolin.public-resources.resource-nice-id-uno.rules[0].value': '1.1.1.1',
'pangolin.public-resources.resource-nice-id-uno.rules[1].action': 'deny',
'pangolin.public-resources.resource-nice-id-uno.rules[1].match': 'cidr',
'pangolin.public-resources.resource-nice-id-uno.rules[1].priority': '2',
'pangolin.public-resources.resource-nice-id-uno.rules[1].value': '2.2.2.2/32',
'pangolin.public-resources.resource-nice-id-uno.rules[2].action': 'allow',
'pangolin.public-resources.resource-nice-id-uno.rules[2].match': 'asn',
'pangolin.public-resources.resource-nice-id-uno.rules[2].priority': '3',
'pangolin.public-resources.resource-nice-id-uno.rules[2].value': 'AS13335',
'pangolin.public-resources.resource-nice-id-uno.rules[3].action': 'pass',
'pangolin.public-resources.resource-nice-id-uno.rules[3].match': 'path',
'pangolin.public-resources.resource-nice-id-uno.rules[3].value': '/admin',
'pangolin.public-resources.resource-nice-id-uno.targets[0].hostname': 'localhost',
'pangolin.public-resources.resource-nice-id-uno.targets[0].method': 'http',
'pangolin.public-resources.resource-nice-id-uno.targets[0].port': '8000',
'pangolin.public-resources.resource-nice-id-uno.targets[0].site': 'lively-yosemite-toad',
'pangolin.public-resources.resource-nice-id-uno.targets[1].hostname': 'localhost',
'pangolin.public-resources.resource-nice-id-uno.targets[1].method': 'https',
'pangolin.public-resources.resource-nice-id-uno.targets[1].path': '/admin',
'pangolin.public-resources.resource-nice-id-uno.targets[1].path-match': 'exact',
'pangolin.public-resources.resource-nice-id-uno.targets[1].port': '8001',
'pangolin.public-resources.resource-nice-id-uno.targets[1].site': 'slim-alpine-chipmunk',
'pangolin.public-resources.resource-nice-id-uno.tls-server-name': 'example.com',
});
});
});
@@ -0,0 +1,144 @@
export function extractPangolinLabelsFromCompose(compose: any) {
const collected: Record<string, string> = {};
for (const [_serviceName, svc] of Object.entries<any>(compose.services ?? {})) {
const labels = svc.labels ?? [];
if (Array.isArray(labels)) {
for (const entry of labels) {
if (typeof entry === 'string') {
const i = entry.indexOf('=');
if (i === -1) {
continue;
}
const key = entry.slice(0, i);
const value = entry.slice(i + 1);
if (key.startsWith('pangolin.')) {
collected[key] = value;
}
}
}
}
else {
for (const [key, value] of Object.entries(labels)) {
if (key.startsWith('pangolin.')) {
collected[key] = String(value);
}
}
}
}
return collected;
}
export function pangolinLabelsToBlueprint(labels: Record<string, string>) {
const resources: Record<string, any> = {};
for (const [key, rawValue] of Object.entries(labels)) {
if (!key.startsWith('pangolin.')) {
continue;
}
const rest = key.slice('pangolin.'.length);
const [resourceName, ...pathParts] = rest.split('.');
const path = pathParts.join('.');
if (!resources[resourceName]) {
resources[resourceName] = {};
}
assignNested(resources[resourceName], path, coerce(rawValue));
}
return resources;
}
function assignNested(target: any, path: string, value: any) {
if (!path) {
return;
}
// Convert "a.b[0].c" → ["a", "b", "0", "c"]
const segments = path
.replace(/\]/g, '')
.split(/\.|\[/)
.filter(Boolean);
let current = target;
for (let i = 0; i < segments.length; i++) {
const key = segments[i];
const isLast = i === segments.length - 1;
const next = segments[i + 1];
if (isLast) {
// Final assignment
current[key] = value;
return;
}
// Determine whether next segment is array index
const shouldBeArray = /^\d+$/.test(next);
// Create container if missing
if (current[key] === undefined) {
current[key] = shouldBeArray ? [] : {};
}
// If wrong type, fix it
if (shouldBeArray && !Array.isArray(current[key])) {
current[key] = [];
}
else if (!shouldBeArray && typeof current[key] !== 'object') {
current[key] = {};
}
current = current[key];
}
}
function coerce(value: string) {
if (value === 'true') {
return true;
}
if (value === 'false') {
return false;
}
if (!Number.isNaN(Number(value))) {
return Number(value);
}
return value;
}
export function blueprintToLabels(resources: Record<string, any>, labelsType: 'array' | 'object') {
const labels: Record<string, string> = {};
for (const [resourceName, config] of Object.entries(resources)) {
flatten(config, `pangolin.${resourceName}`, labels);
}
if (labelsType === 'object') {
return labels;
}
return Object.entries(labels).map(([k, v]) => `${k}=${v}`);
}
function flatten(obj: any, prefix: string, out: Record<string, string>) {
for (const [key, value] of Object.entries(obj)) {
const path = `${prefix}.${key}`;
if (Array.isArray(value)) {
value.forEach((item, i) => {
flatten(item, `${path}[${i}]`, out);
});
}
else if (value && typeof value === 'object') {
flatten(value, path, out);
}
else {
out[path] = String(value);
}
}
}
@@ -0,0 +1,130 @@
<script setup lang="ts">
import yaml from 'yaml';
import { blueprintToLabels, extractPangolinLabelsFromCompose, pangolinLabelsToBlueprint } from './docker-pangolin-labels.service';
const composeInput = ref('');
const blueprintOutput = ref('');
const blueprintInput = ref('');
const labelsOutput = ref('');
const labelsAsArray = ref(true);
const error = ref<string | null>(null);
function convertComposeToBlueprint() {
error.value = null;
blueprintOutput.value = '';
try {
const compose = yaml.parse(composeInput.value) as any;
if (!compose?.services) {
throw new Error('Invalid docker-compose.yml');
}
const labels = extractPangolinLabelsFromCompose(compose);
const blueprint = pangolinLabelsToBlueprint(labels);
blueprintOutput.value = yaml.stringify(blueprint);
}
catch (err: any) {
error.value = err.toString();
}
}
/* -----------------------------
BLUEPRINT → LABELS
------------------------------ */
function convertBlueprintToLabels() {
error.value = null;
labelsOutput.value = '';
try {
const blueprint = yaml.parse(blueprintInput.value) as any;
if (!blueprint || typeof blueprint !== 'object') {
throw new Error('Invalid blueprint YAML');
}
const labels = blueprintToLabels(blueprint, labelsAsArray.value ? 'array' : 'object');
labelsOutput.value = yaml.stringify(labels);
}
catch (err: any) {
error.value = err?.message ?? String(err);
}
}
</script>
<template>
<div>
<n-tabs type="segment">
<!-- COMPOSE → BLUEPRINT -->
<n-tab-pane name="compose-to-blueprint" tab="Compose → Blueprint">
<c-input-text
v-model:value="composeInput"
label="Docker Compose YAML:"
multiline
rows="10"
placeholder="Paste full docker-compose.yml here…"
mb-2
/>
<n-space justify="center" mb-2>
<n-button type="primary" @click="convertComposeToBlueprint">
Extract Pangolin Blueprint
</n-button>
</n-space>
<n-alert v-if="error" type="error" :show-icon="true">
{{ error }}
</n-alert>
<textarea-copyable
v-if="blueprintOutput"
v-model:value="blueprintOutput"
label="Generated Blueprint:"
language="yaml"
readonly
placeholder="Blueprint YAML will appear here…"
/>
</n-tab-pane>
<!-- BLUEPRINT → LABELS -->
<n-tab-pane name="blueprint-to-labels" tab="Blueprint → Labels">
<c-input-text
v-model:value="blueprintInput"
label="Blueprint YAML:"
multiline
rows="5"
placeholder="Paste Pangolin blueprint YAML here…"
mb-2
/>
<n-space justify="center" mb-2>
<n-switch v-model:value="labelsAsArray">
<template #checked>
Array (- key=value)
</template>
<template #unchecked>
Object (key: value)
</template>
</n-switch>
</n-space>
<n-space justify="center" mb-2>
<n-button type="primary" @click="convertBlueprintToLabels">
Convert to Docker Labels
</n-button>
</n-space>
<textarea-copyable
v-if="labelsOutput"
v-model:value="labelsOutput"
label="Generated Docker labels:"
rows="16"
language="yaml"
placeholder="Docker labels will appear here..."
/>
</n-tab-pane>
</n-tabs>
</div>
</template>
+13
View File
@@ -0,0 +1,13 @@
import { NetworkCheckSharp } from '@vicons/material';
import { defineTool } from '../tool';
export const tool = defineTool({
name: 'Docker Pangolin Labels',
path: '/docker-pangolin-labels',
description: 'Convert Docker Compose Pangolin labels to blueprints and vice versa',
keywords: ['docker', 'compose', 'pangolin', 'blueprint', 'labels'],
component: () => import('./docker-pangolin-labels.vue'),
icon: NetworkCheckSharp,
createdAt: new Date('2026-02-08'),
category: 'Docker',
});