mirror of
https://github.com/portainer/portainer.git
synced 2025-07-28 01:39:39 +02:00
feat(docker/services): show port ranges [EE-4012] (#10657)
Some checks failed
ci / build_images (map[arch:amd64 platform:windows version:ltsc2022]) (push) Has been cancelled
ci / build_images (map[arch:arm platform:linux version:]) (push) Has been cancelled
ci / build_images (map[arch:arm64 platform:linux version:]) (push) Has been cancelled
ci / build_images (map[arch:ppc64le platform:linux version:]) (push) Has been cancelled
ci / build_images (map[arch:s390x platform:linux version:]) (push) Has been cancelled
ci / build_images (map[arch:amd64 platform:linux version:]) (push) Has been cancelled
ci / build_images (map[arch:amd64 platform:windows version:1809]) (push) Has been cancelled
/ triage (push) Has been cancelled
Lint / Run linters (push) Has been cancelled
Test / test-client (push) Has been cancelled
Test / test-server (map[arch:amd64 platform:linux]) (push) Has been cancelled
Test / test-server (map[arch:amd64 platform:windows version:1809]) (push) Has been cancelled
Test / test-server (map[arch:amd64 platform:windows version:ltsc2022]) (push) Has been cancelled
Test / test-server (map[arch:arm64 platform:linux]) (push) Has been cancelled
ci / build_manifests (push) Has been cancelled
Some checks failed
ci / build_images (map[arch:amd64 platform:windows version:ltsc2022]) (push) Has been cancelled
ci / build_images (map[arch:arm platform:linux version:]) (push) Has been cancelled
ci / build_images (map[arch:arm64 platform:linux version:]) (push) Has been cancelled
ci / build_images (map[arch:ppc64le platform:linux version:]) (push) Has been cancelled
ci / build_images (map[arch:s390x platform:linux version:]) (push) Has been cancelled
ci / build_images (map[arch:amd64 platform:linux version:]) (push) Has been cancelled
ci / build_images (map[arch:amd64 platform:windows version:1809]) (push) Has been cancelled
/ triage (push) Has been cancelled
Lint / Run linters (push) Has been cancelled
Test / test-client (push) Has been cancelled
Test / test-server (map[arch:amd64 platform:linux]) (push) Has been cancelled
Test / test-server (map[arch:amd64 platform:windows version:1809]) (push) Has been cancelled
Test / test-server (map[arch:amd64 platform:windows version:ltsc2022]) (push) Has been cancelled
Test / test-server (map[arch:arm64 platform:linux]) (push) Has been cancelled
ci / build_manifests (push) Has been cancelled
This commit is contained in:
parent
4ca6292805
commit
d336a14e50
37 changed files with 1406 additions and 148 deletions
|
@ -0,0 +1,253 @@
|
|||
import { EndpointPortConfig } from 'docker-types/generated/1.41';
|
||||
import _ from 'lodash';
|
||||
|
||||
import { PortBinding, Protocol, Value, isProtocol, isRange } from './types';
|
||||
|
||||
// if container is number then host is number | undefined
|
||||
export function toViewModel(
|
||||
portBindings: Array<EndpointPortConfig> | undefined = []
|
||||
): Array<Value> {
|
||||
const parsedPorts = parsePorts(portBindings);
|
||||
const sortedPorts = sortPorts(parsedPorts);
|
||||
|
||||
return combinePorts(sortedPorts);
|
||||
|
||||
function parsePorts(portBindings: Array<EndpointPortConfig>) {
|
||||
return portBindings.map((binding) => ({
|
||||
hostPort: binding.PublishedPort,
|
||||
protocol: isProtocol(binding.Protocol) ? binding.Protocol : 'tcp',
|
||||
containerPort: binding.TargetPort,
|
||||
publishMode: binding.PublishMode || 'ingress',
|
||||
}));
|
||||
}
|
||||
|
||||
function sortPorts(
|
||||
ports: Array<{
|
||||
hostPort: number | undefined;
|
||||
protocol: Protocol;
|
||||
containerPort: number | undefined;
|
||||
publishMode: 'ingress' | 'host';
|
||||
}>
|
||||
) {
|
||||
return _.sortBy(ports, [
|
||||
'containerPort',
|
||||
'hostPort',
|
||||
'protocol',
|
||||
'publishMode',
|
||||
]);
|
||||
}
|
||||
|
||||
function combinePorts(
|
||||
ports: Array<PortBinding<number | undefined, number | undefined>>
|
||||
): Array<Value> {
|
||||
return ports.reduce((acc, port) => {
|
||||
const lastPort = acc[acc.length - 1];
|
||||
|
||||
if (
|
||||
!lastPort ||
|
||||
lastPort.publishMode !== port.publishMode ||
|
||||
lastPort.protocol !== port.protocol
|
||||
) {
|
||||
return [...acc, port] satisfies Array<Value>;
|
||||
}
|
||||
|
||||
if (
|
||||
typeof lastPort.hostPort === 'undefined' &&
|
||||
typeof port.hostPort === 'undefined'
|
||||
) {
|
||||
if (isRange(lastPort.containerPort)) {
|
||||
if (lastPort.containerPort.end === port.containerPort) {
|
||||
return acc;
|
||||
}
|
||||
|
||||
if (lastPort.containerPort.end + 1 === port.containerPort) {
|
||||
return [
|
||||
...acc.slice(0, acc.length - 1),
|
||||
{
|
||||
...lastPort,
|
||||
hostPort: undefined,
|
||||
containerPort: {
|
||||
...lastPort.containerPort,
|
||||
end: port.containerPort,
|
||||
},
|
||||
} satisfies Value,
|
||||
];
|
||||
}
|
||||
|
||||
return [...acc, port];
|
||||
}
|
||||
|
||||
if (typeof lastPort.containerPort === 'number') {
|
||||
if (lastPort.containerPort === port.containerPort) {
|
||||
return acc;
|
||||
}
|
||||
|
||||
if (lastPort.containerPort + 1 === port.containerPort) {
|
||||
return [
|
||||
...acc.slice(0, acc.length - 1),
|
||||
{
|
||||
...lastPort,
|
||||
hostPort: undefined,
|
||||
containerPort: {
|
||||
start: lastPort.containerPort,
|
||||
end: port.containerPort,
|
||||
},
|
||||
} satisfies Value,
|
||||
];
|
||||
}
|
||||
|
||||
return [...acc, port];
|
||||
}
|
||||
}
|
||||
|
||||
if (
|
||||
typeof lastPort.hostPort === 'number' &&
|
||||
typeof lastPort.containerPort === 'number'
|
||||
) {
|
||||
if (
|
||||
lastPort.hostPort === port.hostPort &&
|
||||
lastPort.containerPort === port.containerPort
|
||||
) {
|
||||
return acc;
|
||||
}
|
||||
|
||||
if (
|
||||
lastPort.hostPort + 1 === port.hostPort &&
|
||||
lastPort.containerPort === port.containerPort
|
||||
) {
|
||||
return [
|
||||
...acc.slice(0, acc.length - 1),
|
||||
{
|
||||
...lastPort,
|
||||
hostPort: {
|
||||
start: lastPort.hostPort,
|
||||
end: port.hostPort,
|
||||
},
|
||||
} satisfies Value,
|
||||
];
|
||||
}
|
||||
|
||||
if (
|
||||
lastPort.hostPort + 1 === port.hostPort &&
|
||||
lastPort.containerPort + 1 === port.containerPort
|
||||
) {
|
||||
return [
|
||||
...acc.slice(0, acc.length - 1),
|
||||
{
|
||||
...lastPort,
|
||||
hostPort: {
|
||||
start: lastPort.hostPort,
|
||||
end: port.hostPort,
|
||||
},
|
||||
containerPort: {
|
||||
start: lastPort.containerPort,
|
||||
end: port.containerPort,
|
||||
},
|
||||
} satisfies Value,
|
||||
];
|
||||
}
|
||||
|
||||
return [...acc, port];
|
||||
}
|
||||
|
||||
if (
|
||||
isRange(lastPort.hostPort) &&
|
||||
typeof lastPort.containerPort === 'number'
|
||||
) {
|
||||
if (
|
||||
lastPort.hostPort.end === port.hostPort &&
|
||||
lastPort.containerPort === port.containerPort
|
||||
) {
|
||||
return acc;
|
||||
}
|
||||
|
||||
if (
|
||||
lastPort.hostPort.end + 1 === port.hostPort &&
|
||||
lastPort.containerPort === port.containerPort
|
||||
) {
|
||||
return [
|
||||
...acc.slice(0, acc.length - 1),
|
||||
{
|
||||
...lastPort,
|
||||
hostPort: {
|
||||
...lastPort.hostPort,
|
||||
end: port.hostPort,
|
||||
},
|
||||
} satisfies Value,
|
||||
];
|
||||
}
|
||||
|
||||
if (
|
||||
lastPort.hostPort.end + 1 === port.hostPort &&
|
||||
lastPort.containerPort + 1 === port.containerPort
|
||||
) {
|
||||
return [
|
||||
...acc.slice(0, acc.length - 1),
|
||||
{
|
||||
...lastPort,
|
||||
hostPort: {
|
||||
...lastPort.hostPort,
|
||||
end: port.hostPort,
|
||||
},
|
||||
containerPort: {
|
||||
start: lastPort.containerPort,
|
||||
end: port.containerPort,
|
||||
},
|
||||
} satisfies Value,
|
||||
];
|
||||
}
|
||||
|
||||
return [...acc, port];
|
||||
}
|
||||
|
||||
if (isRange(lastPort.hostPort) && isRange(lastPort.containerPort)) {
|
||||
if (
|
||||
lastPort.hostPort.end === port.hostPort &&
|
||||
lastPort.containerPort.end === port.containerPort
|
||||
) {
|
||||
return acc;
|
||||
}
|
||||
|
||||
if (
|
||||
lastPort.hostPort.end + 1 === port.hostPort &&
|
||||
lastPort.containerPort.end === port.containerPort
|
||||
) {
|
||||
return [
|
||||
...acc.slice(0, acc.length - 1),
|
||||
{
|
||||
...lastPort,
|
||||
hostPort: {
|
||||
...lastPort.hostPort,
|
||||
end: port.hostPort,
|
||||
},
|
||||
} satisfies Value,
|
||||
];
|
||||
}
|
||||
|
||||
if (
|
||||
lastPort.hostPort.end + 1 === port.hostPort &&
|
||||
lastPort.containerPort.end + 1 === port.containerPort
|
||||
) {
|
||||
return [
|
||||
...acc.slice(0, acc.length - 1),
|
||||
{
|
||||
...lastPort,
|
||||
hostPort: {
|
||||
...lastPort.hostPort,
|
||||
end: port.hostPort,
|
||||
},
|
||||
containerPort: {
|
||||
...lastPort.containerPort,
|
||||
end: port.containerPort,
|
||||
},
|
||||
} satisfies Value,
|
||||
];
|
||||
}
|
||||
|
||||
return [...acc, port];
|
||||
}
|
||||
|
||||
return [...acc, port];
|
||||
}, [] as Array<Value>);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue