Merge pull request #2217 from balena-os/v15-ignore-dc-expose

Ignore `expose` service compose configuration
This commit is contained in:
flowzone-app[bot] 2023-10-23 17:30:10 +00:00 committed by GitHub
commit 67dac79476
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 9 additions and 110 deletions

View File

@ -15,7 +15,6 @@ export interface PortBindings {
}
export interface DockerPortOptions {
exposedPorts: Dictionary<{}>;
portBindings: PortBindings;
}
@ -49,33 +48,19 @@ export class PortMap {
this.ports.externalEnd,
);
const exposedPorts: { [key: string]: {} } = {};
const portBindings: PortBindings = {};
_.zipWith(internalRange, externalRange, (internal, external) => {
exposedPorts[`${internal}/${this.ports.protocol}`] = {};
portBindings[`${internal}/${this.ports.protocol}`] = [
{ HostIp: this.ports.host, HostPort: external.toString() },
];
});
return {
exposedPorts,
portBindings,
};
}
public toExposedPortArray(): string[] {
const internalRange = this.generatePortRange(
this.ports.internalStart,
this.ports.internalEnd,
);
return _.map(internalRange, (internal) => {
return `${internal}/${this.ports.protocol}`;
});
}
/**
* fromDockerOpts
*

View File

@ -18,7 +18,6 @@ const supportedComposeFields = [
'tmpfs',
'entrypoint',
'environment',
'expose',
'extraHosts',
'groupAdd',
'healthcheck',

View File

@ -83,7 +83,6 @@ export class Service {
'dnsOpt',
'tmpfs',
'extraHosts',
'expose',
'ulimitsArray',
'groupAdd',
'securityOpt',
@ -336,25 +335,6 @@ export class Service {
}
delete config.ports;
// get the exposed ports, both from the image and the compose file
let expose: string[] = [];
if (config.expose != null) {
expose = _.map(config.expose, ComposeUtils.sanitiseExposeFromCompose);
}
const imageExposedPorts = _.get(
options.imageInfo,
'Config.ExposedPorts',
{},
);
expose = expose.concat(_.keys(imageExposedPorts));
// Also add any exposed ports which are implied from the portMaps
const exposedFromPortMappings = _.flatMap(portMaps, (port) =>
port.toExposedPortArray(),
);
expose = expose.concat(exposedFromPortMappings);
expose = _.uniq(expose);
delete config.expose;
let devices: DockerDevice[] = [];
if (config.devices != null) {
devices = _.map(config.devices, ComposeUtils.formatDevice);
@ -430,7 +410,6 @@ export class Service {
dnsOpt: [],
entrypoint: '',
extraHosts: [],
expose,
networks,
dns: [],
dnsSearch: [],
@ -526,17 +505,6 @@ export class Service {
});
const portMaps = PortMap.fromDockerOpts(container.HostConfig.PortBindings);
let expose = _.flatMap(
_.flatMap(portMaps, (p) => p.toDockerOpts().exposedPorts),
_.keys,
);
if (container.Config.ExposedPorts != null) {
expose = expose.concat(
_.map(container.Config.ExposedPorts, (_v, k) => k.toString()),
);
}
expose = _.uniq(expose);
const tmpfs: string[] = Object.keys(container.HostConfig?.Tmpfs || {});
const binds: string[] = _.uniq(
@ -573,7 +541,6 @@ export class Service {
networkMode: container.HostConfig.NetworkMode!,
portMaps,
expose,
hostname,
command: container.Config.Cmd || '',
entrypoint: container.Config.Entrypoint || '',
@ -684,7 +651,7 @@ export class Service {
containerIds: Dictionary<string>;
}): Dockerode.ContainerCreateOptions {
const { binds, mounts, volumes } = this.getBindsMountsAndVolumes();
const { exposedPorts, portBindings } = this.generateExposeAndPorts();
const { portBindings } = this.generatePortBindings();
const tmpFs: Dictionary<''> = this.config.tmpfs.reduce(
(dict, tmp) => ({ ...dict, [tmp]: '' }),
@ -722,7 +689,6 @@ export class Service {
this.config.environment,
),
),
ExposedPorts: exposedPorts,
Image: this.config.image,
Labels: this.config.labels,
NetworkingConfig:
@ -961,13 +927,11 @@ export class Service {
return { binds, mounts, volumes };
}
private generateExposeAndPorts(): DockerPortOptions {
const exposed: DockerPortOptions['exposedPorts'] = {};
private generatePortBindings(): DockerPortOptions {
const ports: DockerPortOptions['portBindings'] = {};
_.each(this.config.portMaps, (pmap) => {
const { exposedPorts, portBindings } = pmap.toDockerOpts();
_.merge(exposed, exposedPorts);
const { portBindings } = pmap.toDockerOpts();
_.mergeWith(ports, portBindings, (destVal, srcVal) => {
if (destVal == null) {
return srcVal;
@ -976,15 +940,7 @@ export class Service {
});
});
// We also want to merge the compose and image exposedPorts
// into the list of exposedPorts
const composeExposed: DockerPortOptions['exposedPorts'] = {};
_.each(this.config.expose, (port) => {
composeExposed[port] = {};
});
_.merge(exposed, composeExposed);
return { exposedPorts: exposed, portBindings: ports };
return { portBindings: ports };
}
private static extendEnvVars(

View File

@ -179,7 +179,6 @@ export interface ServiceComposeConfig {
tmpfs?: string | string[];
entrypoint?: string | string[];
environment?: { [envVarName: string]: string };
expose?: string[];
extraHosts?: string[];
groupAdd?: string[];
healthcheck?: ComposeHealthcheck;
@ -239,7 +238,6 @@ export interface ServiceConfig {
tmpfs: string[];
entrypoint: string | string[];
environment: { [envVarName: string]: string };
expose: string[];
extraHosts: string[];
groupAdd: string[];
healthcheck: ServiceHealthcheck;
@ -295,7 +293,6 @@ export type ServiceConfigArrayField =
| 'dns'
| 'dnsSearch'
| 'dnsOpt'
| 'expose'
| 'tmpfs'
| 'extraHosts'
| 'ulimitsArray'

View File

@ -288,13 +288,6 @@ export function getUser(
return user != null ? user : _.get(imageInfo, 'Config.User', '');
}
export function sanitiseExposeFromCompose(portStr: string): string {
if (/^[0-9]*$/.test(portStr)) {
return `${portStr}/tcp`;
}
return portStr;
}
export function formatDevice(deviceStr: string): DockerDevice {
const [pathOnHost, ...parts] = deviceStr.split(':');
let [pathInContainer, cgroup] = parts;

View File

@ -98,9 +98,6 @@ describe('compose/ports', function () {
describe('toDockerOpts', function () {
it('should correctly generate docker options', () =>
expect(new PortMapPublic('80').toDockerOpts()).to.deep.equal({
exposedPorts: {
'80/tcp': {},
},
portBindings: {
'80/tcp': [{ HostIp: '', HostPort: '80' }],
},
@ -108,14 +105,6 @@ describe('compose/ports', function () {
it('should correctly generate docker options for a port range', () =>
expect(new PortMapPublic('80-85').toDockerOpts()).to.deep.equal({
exposedPorts: {
'80/tcp': {},
'81/tcp': {},
'82/tcp': {},
'83/tcp': {},
'84/tcp': {},
'85/tcp': {},
},
portBindings: {
'80/tcp': [{ HostIp: '', HostPort: '80' }],
'81/tcp': [{ HostIp: '', HostPort: '81' }],

View File

@ -129,7 +129,7 @@ describe('compose/service: unit tests', () => {
} as any,
);
const ports = (s as any).generateExposeAndPorts();
const ports = (s as any).generatePortBindings();
expect(ports.portBindings).to.deep.equal({
'2344/tcp': [
{
@ -150,15 +150,6 @@ describe('compose/service: unit tests', () => {
},
],
});
expect(ports.exposedPorts).to.deep.equal({
'1000/tcp': {},
'243/udp': {},
'2344/tcp': {},
'2354/tcp': {},
'2367/udp': {},
'53/tcp': {},
'53/udp': {},
});
});
it('correctly handles port ranges', async () => {
@ -177,7 +168,7 @@ describe('compose/service: unit tests', () => {
{ appName: 'test' } as any,
);
const ports = (s as any).generateExposeAndPorts();
const ports = (s as any).generatePortBindings();
expect(ports.portBindings).to.deep.equal({
'2000/tcp': [
{
@ -204,15 +195,6 @@ describe('compose/service: unit tests', () => {
},
],
});
expect(ports.exposedPorts).to.deep.equal({
'1000/tcp': {},
'2000/tcp': {},
'2001/tcp': {},
'2002/tcp': {},
'2003/tcp': {},
'243/udp': {},
});
});
it('should correctly handle large port ranges', async function () {
@ -231,10 +213,10 @@ describe('compose/service: unit tests', () => {
{ appName: 'test' } as any,
);
expect((s as any).generateExposeAndPorts()).to.not.throw;
expect((s as any).generatePortBindings).to.not.throw;
});
it('should correctly report implied exposed ports from portMappings', async () => {
it('should not report implied exposed ports from portMappings', async () => {
const service = await Service.fromComposeObject(
{
appId: 123456,
@ -247,9 +229,7 @@ describe('compose/service: unit tests', () => {
{ appName: 'test' } as any,
);
expect(service.config)
.to.have.property('expose')
.that.deep.equals(['80/tcp', '100/tcp']);
expect(service.config).to.not.have.property('expose');
});
it('should correctly handle spaces in volume definitions', async () => {