mirror of
https://github.com/balena-os/balena-supervisor.git
synced 2025-01-26 06:09:18 +00:00
Merge pull request #933 from balena-io/fix-network-joining
Fix network alias creation and comparison
This commit is contained in:
commit
b26a527ed0
@ -9,8 +9,6 @@ do ->
|
|||||||
return lookup(name, { verbatim: true }, opts)
|
return lookup(name, { verbatim: true }, opts)
|
||||||
return lookup(name, Object.assign({ verbatim: true }, opts), cb)
|
return lookup(name, Object.assign({ verbatim: true }, opts), cb)
|
||||||
|
|
||||||
require('log-timestamp')
|
|
||||||
|
|
||||||
Supervisor = require './supervisor'
|
Supervisor = require './supervisor'
|
||||||
|
|
||||||
supervisor = new Supervisor()
|
supervisor = new Supervisor()
|
||||||
|
@ -20,6 +20,7 @@ import {
|
|||||||
import * as LogTypes from '../lib/log-types';
|
import * as LogTypes from '../lib/log-types';
|
||||||
import { checkInt, isValidDeviceName } from '../lib/validation';
|
import { checkInt, isValidDeviceName } from '../lib/validation';
|
||||||
import { Service } from './service';
|
import { Service } from './service';
|
||||||
|
import { serviceNetworksToDockerNetworks } from './utils';
|
||||||
|
|
||||||
interface ServiceConstructOpts {
|
interface ServiceConstructOpts {
|
||||||
docker: Docker;
|
docker: Docker;
|
||||||
@ -266,7 +267,9 @@ export class ServiceManager extends (EventEmitter as {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const conf = service.toDockerContainer({ deviceName });
|
const conf = service.toDockerContainer({ deviceName });
|
||||||
const nets = service.extraNetworksToJoin();
|
const nets = serviceNetworksToDockerNetworks(
|
||||||
|
service.extraNetworksToJoin(),
|
||||||
|
);
|
||||||
|
|
||||||
this.logger.logSystemEvent(LogTypes.installService, { service });
|
this.logger.logSystemEvent(LogTypes.installService, { service });
|
||||||
this.reportNewStatus(mockContainerId, service, 'Installing');
|
this.reportNewStatus(mockContainerId, service, 'Installing');
|
||||||
@ -275,7 +278,7 @@ export class ServiceManager extends (EventEmitter as {
|
|||||||
service.containerId = container.id;
|
service.containerId = container.id;
|
||||||
|
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
_.map(nets, (endpointConfig, name) =>
|
_.map((nets || {}).EndpointsConfig, (endpointConfig, name) =>
|
||||||
this.docker.getNetwork(name).connect({
|
this.docker.getNetwork(name).connect({
|
||||||
Container: container.id,
|
Container: container.id,
|
||||||
EndpointConfig: endpointConfig,
|
EndpointConfig: endpointConfig,
|
||||||
|
@ -122,6 +122,17 @@ export class Service {
|
|||||||
}
|
}
|
||||||
// Prefix the network entries with the app id
|
// Prefix the network entries with the app id
|
||||||
networks = _.mapKeys(networks, (_v, k) => `${service.appId}_${k}`);
|
networks = _.mapKeys(networks, (_v, k) => `${service.appId}_${k}`);
|
||||||
|
// Ensure that we add an alias of the service name
|
||||||
|
networks = _.mapValues(networks, v => {
|
||||||
|
if (v.aliases == null) {
|
||||||
|
v.aliases = [];
|
||||||
|
}
|
||||||
|
const serviceName: string = service.serviceName || '';
|
||||||
|
if (!_.includes(v.aliases, serviceName)) {
|
||||||
|
v.aliases.push(serviceName);
|
||||||
|
}
|
||||||
|
return v;
|
||||||
|
});
|
||||||
delete config.networks;
|
delete config.networks;
|
||||||
|
|
||||||
// Check for unsupported networkMode entries
|
// Check for unsupported networkMode entries
|
||||||
@ -791,8 +802,8 @@ export class Service {
|
|||||||
appId: number,
|
appId: number,
|
||||||
serviceName: string,
|
serviceName: string,
|
||||||
): { [envVarName: string]: string } {
|
): { [envVarName: string]: string } {
|
||||||
let defaultEnv: { [envVarName: string]: string } = {};
|
const defaultEnv: { [envVarName: string]: string } = {};
|
||||||
for (let namespace of ['BALENA', 'RESIN']) {
|
for (const namespace of ['BALENA', 'RESIN']) {
|
||||||
_.assign(
|
_.assign(
|
||||||
defaultEnv,
|
defaultEnv,
|
||||||
_.mapKeys(
|
_.mapKeys(
|
||||||
@ -837,24 +848,11 @@ export class Service {
|
|||||||
const currentAliases = _.filter(current.aliases, (alias: string) => {
|
const currentAliases = _.filter(current.aliases, (alias: string) => {
|
||||||
return !_.startsWith(this.containerId!, alias);
|
return !_.startsWith(this.containerId!, alias);
|
||||||
});
|
});
|
||||||
const targetAliases = _.filter(current.aliases, (alias: string) => {
|
const targetAliases = target.aliases || [];
|
||||||
return !_.startsWith(this.containerId!, alias);
|
|
||||||
});
|
|
||||||
|
|
||||||
// Docker adds container ids to the alias list, directly after
|
sameNetwork = _.isEmpty(
|
||||||
// the service name, to detect this, check for both target having
|
_.xorWith(currentAliases, targetAliases, _.isEqual),
|
||||||
// exactly half of the amount of entries as the current, and check
|
);
|
||||||
// that every second entry (starting from 0) is equal
|
|
||||||
if (currentAliases.length === targetAliases.length * 2) {
|
|
||||||
sameNetwork = _(currentAliases)
|
|
||||||
.filter((_v, k) => k % 2 === 0)
|
|
||||||
.isEqual(targetAliases);
|
|
||||||
} else {
|
|
||||||
// Otherwise compare them literally
|
|
||||||
sameNetwork = _.isEmpty(
|
|
||||||
_.xorWith(currentAliases, targetAliases, _.isEqual),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (target.ipv4Address != null) {
|
if (target.ipv4Address != null) {
|
||||||
|
@ -348,7 +348,7 @@ export function addFeaturesFromLabels(
|
|||||||
}`;
|
}`;
|
||||||
}
|
}
|
||||||
// We keep balena.sock for backwards compatibility
|
// We keep balena.sock for backwards compatibility
|
||||||
if (constants.dockerSocket != '/var/run/balena.sock') {
|
if (constants.dockerSocket !== '/var/run/balena.sock') {
|
||||||
service.config.volumes.push(
|
service.config.volumes.push(
|
||||||
`${constants.dockerSocket}:/var/run/balena.sock`,
|
`${constants.dockerSocket}:/var/run/balena.sock`,
|
||||||
);
|
);
|
||||||
|
@ -311,7 +311,9 @@ describe 'compose/service', ->
|
|||||||
IPAMConfig: {
|
IPAMConfig: {
|
||||||
IPv4Address: '1.2.3.4'
|
IPv4Address: '1.2.3.4'
|
||||||
},
|
},
|
||||||
Aliases: []
|
Aliases: [
|
||||||
|
'test'
|
||||||
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
Loading…
x
Reference in New Issue
Block a user